edb_data.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. package edb_data
  2. import (
  3. "fmt"
  4. "github.com/nosixtools/solarlunar"
  5. edbDataModel "hongze/hongze_yb/models/tables/edb_data"
  6. "hongze/hongze_yb/utils"
  7. "strconv"
  8. "time"
  9. )
  10. // AddCalculateQuarter 指标季度数据计算(公历转农历)
  11. func AddCalculateQuarter(dataList []*edbDataModel.EdbDataList) (result *edbDataModel.EdbDataResult, err error) {
  12. var errMsg string
  13. defer func() {
  14. if errMsg != "" {
  15. fmt.Println("errMsg:", errMsg)
  16. }
  17. }()
  18. endDate := dataList[len(dataList)-1].DataTime
  19. endDateForm, err := time.Parse(utils.FormatDate, endDate)
  20. if err != nil {
  21. return result, err
  22. }
  23. thisMonth := int(endDateForm.Month())
  24. result = new(edbDataModel.EdbDataResult)
  25. var yearArr []int
  26. yearMap := make(map[int]int)
  27. var cureentDate time.Time
  28. if thisMonth < 11 {
  29. for k, v := range dataList {
  30. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  31. if err != nil {
  32. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  33. return result, err
  34. }
  35. if k == len(dataList)-1 {
  36. cureentDate = dateTime
  37. }
  38. year := dateTime.Year()
  39. if _, ok := yearMap[year]; !ok {
  40. yearArr = append(yearArr, year)
  41. }
  42. yearMap[year] = year
  43. }
  44. } else {
  45. for k, v := range dataList {
  46. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  47. if err != nil {
  48. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  49. return result, err
  50. }
  51. if k == len(dataList)-1 {
  52. cureentDate = dateTime
  53. }
  54. year := dateTime.Year() + 1
  55. if _, ok := yearMap[year]; !ok {
  56. yearArr = append(yearArr, year)
  57. }
  58. yearMap[year] = year
  59. }
  60. }
  61. //排序
  62. //fmt.Println("yearArr:", yearArr)
  63. thisYear := cureentDate.Year()
  64. //thisMonth := int(cureentDate.Month())
  65. //fmt.Println("thisMonth:", thisMonth)
  66. for ky, vy := range yearArr {
  67. //fmt.Println("line 432:", ky, vy, thisYear, thisMonth)
  68. if thisMonth < 11 {
  69. currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
  70. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
  71. currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
  72. if err != nil {
  73. errMsg = "生成当前春节失败,Err:" + err.Error()
  74. return result, err
  75. }
  76. preYear := vy
  77. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  78. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  79. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  80. if err != nil {
  81. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  82. return result, err
  83. }
  84. day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  85. items := new(edbDataModel.EdbDataItems)
  86. items.Year = preYear
  87. for _, v := range dataList {
  88. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  89. if err != nil {
  90. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  91. return result, err
  92. }
  93. newDate := dateTime.AddDate(0, 0, int(day))
  94. selectDateStr := strconv.Itoa(thisYear) + "-11" + "-30"
  95. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  96. if newDate.Before(selectDate) || newDate == selectDate {
  97. timestamp := newDate.UnixNano() / 1e6
  98. item := new(edbDataModel.EdbDataList)
  99. item.DataTime = newDate.Format(utils.FormatDate)
  100. item.EdbInfoId = v.EdbInfoId
  101. item.Value = v.Value
  102. item.EdbDataId = v.EdbDataId
  103. item.DataTimestamp = timestamp
  104. items.Items = append(items.Items, item)
  105. }
  106. }
  107. result.List = append(result.List, items)
  108. } else {
  109. nextYear := thisYear + 1
  110. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  111. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  112. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  113. if err != nil {
  114. errMsg = "生成当前春节失败,Err:" + err.Error()
  115. return result, err
  116. }
  117. preYear := vy
  118. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  119. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  120. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  121. if err != nil {
  122. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  123. return result, err
  124. }
  125. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  126. //fmt.Println("day:", day, nextYearCjglDate, preYearCjglDate)
  127. items := new(edbDataModel.EdbDataItems)
  128. items.Year = preYear - 1
  129. //fmt.Println("preYear:", preYear, "ky:", ky, "yearArrLen:", len(yearArr))
  130. if ky+1 < len(yearArr) {
  131. for _, v := range dataList {
  132. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  133. if err != nil {
  134. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  135. return result, err
  136. }
  137. newDate := dateTime.AddDate(0, 0, int(day))
  138. selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
  139. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  140. if newDate.Before(selectDate) || newDate == selectDate {
  141. timestamp := newDate.UnixNano() / 1e6
  142. item := new(edbDataModel.EdbDataList)
  143. item.DataTime = newDate.Format(utils.FormatDate)
  144. item.EdbInfoId = v.EdbInfoId
  145. item.Value = v.Value
  146. item.EdbDataId = v.EdbDataId
  147. item.DataTimestamp = timestamp
  148. items.Items = append(items.Items, item)
  149. }
  150. }
  151. result.List = append(result.List, items)
  152. } else {
  153. for _, v := range dataList {
  154. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  155. if err != nil {
  156. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  157. return result, err
  158. }
  159. timestamp := dateTime.UnixNano() / 1e6
  160. item := new(edbDataModel.EdbDataList)
  161. item.DataTime = dateTime.Format(utils.FormatDate)
  162. item.EdbInfoId = v.EdbInfoId
  163. item.Value = v.Value
  164. item.EdbDataId = v.EdbDataId
  165. item.DataTimestamp = timestamp
  166. items.Items = append(items.Items, item)
  167. }
  168. result.List = append(result.List, items)
  169. }
  170. }
  171. }
  172. return
  173. }
  174. // AddCalculateQuarterV6 指标季度数据计算(季节性图表)
  175. func AddCalculateQuarterV6(dataList []*edbDataModel.EdbDataList) (result *edbDataModel.EdbDataResult, err error) {
  176. var errMsg string
  177. defer func() {
  178. if errMsg != "" {
  179. fmt.Println("errMsg:", errMsg)
  180. }
  181. }()
  182. endDate := dataList[len(dataList)-1].DataTime
  183. endDateForm, err := time.Parse(utils.FormatDate, endDate)
  184. if err != nil {
  185. return result, err
  186. }
  187. thisMonth := int(endDateForm.Month())
  188. result = new(edbDataModel.EdbDataResult)
  189. var yearArr []int
  190. yearMap := make(map[int]int)
  191. var cureentDate time.Time
  192. if thisMonth < 11 {
  193. for k, v := range dataList {
  194. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  195. if err != nil {
  196. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  197. return result, err
  198. }
  199. if k == len(dataList)-1 {
  200. cureentDate = dateTime
  201. }
  202. year := dateTime.Year()
  203. if _, ok := yearMap[year]; !ok {
  204. yearArr = append(yearArr, year)
  205. }
  206. yearMap[year] = year
  207. }
  208. } else {
  209. for k, v := range dataList {
  210. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  211. if err != nil {
  212. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  213. return result, err
  214. }
  215. if k == len(dataList)-1 {
  216. cureentDate = dateTime
  217. }
  218. year := dateTime.Year() + 1
  219. if _, ok := yearMap[year]; !ok {
  220. yearArr = append(yearArr, year)
  221. }
  222. yearMap[year] = year
  223. }
  224. }
  225. //排序
  226. fmt.Println("yearArr:", yearArr)
  227. thisYear := cureentDate.Year()
  228. //thisMonth := int(cureentDate.Month())
  229. fmt.Println("thisMonth:", thisMonth)
  230. for ky, vy := range yearArr {
  231. fmt.Printf("line 432:ky:%d, vy:%d, thisYear:%d, thisMonth:%d", ky, vy, thisYear, thisMonth)
  232. fmt.Println("")
  233. if thisMonth < 11 {
  234. currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
  235. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
  236. currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
  237. if err != nil {
  238. errMsg = "生成当前春节失败,Err:" + err.Error()
  239. return result, err
  240. }
  241. preYear := vy
  242. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  243. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  244. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  245. if err != nil {
  246. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  247. return result, err
  248. }
  249. day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  250. fmt.Println("day:", day, currentYearCjglDate, preYearCjglDate)
  251. items := new(edbDataModel.EdbDataItems)
  252. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  253. items.Year = preYear
  254. for _, v := range dataList {
  255. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  256. if err != nil {
  257. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  258. return result, err
  259. }
  260. newDate := dateTime.AddDate(0, 0, int(day))
  261. timestamp := newDate.UnixNano() / 1e6
  262. item := new(edbDataModel.EdbDataList)
  263. item.DataTime = newDate.Format(utils.FormatDate)
  264. item.EdbInfoId = v.EdbInfoId
  265. item.Value = v.Value
  266. item.EdbDataId = v.EdbDataId
  267. item.DataTimestamp = timestamp
  268. items.Items = append(items.Items, item)
  269. }
  270. result.List = append(result.List, items)
  271. } else {
  272. nextYear := thisYear + 1
  273. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  274. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  275. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  276. if err != nil {
  277. errMsg = "生成当前春节失败,Err:" + err.Error()
  278. return result, err
  279. }
  280. preYear := vy
  281. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  282. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  283. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  284. if err != nil {
  285. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  286. return result, err
  287. }
  288. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  289. fmt.Println("day:", day, nextYearCjglDate, preYearCjglDate)
  290. items := new(edbDataModel.EdbDataItems)
  291. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  292. items.Year = preYear
  293. fmt.Println("preYear:", preYear, "ky:", ky, "yearArrLen:", len(yearArr))
  294. //if ky+1 < len(yearArr) {
  295. for _, v := range dataList {
  296. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  297. if err != nil {
  298. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  299. return result, err
  300. }
  301. newDate := dateTime.AddDate(0, 0, int(day))
  302. timestamp := newDate.UnixNano() / 1e6
  303. item := new(edbDataModel.EdbDataList)
  304. item.DataTime = newDate.Format(utils.FormatDate)
  305. item.EdbInfoId = v.EdbInfoId
  306. item.Value = v.Value
  307. item.EdbDataId = v.EdbDataId
  308. item.DataTimestamp = timestamp
  309. items.Items = append(items.Items, item)
  310. }
  311. result.List = append(result.List, items)
  312. /*} else {
  313. for _, v := range dataList {
  314. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  315. if err != nil {
  316. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  317. return result, err
  318. }
  319. timestamp := dateTime.UnixNano() / 1e6
  320. item := new(EdbDataList)
  321. item.DataTime = dateTime.Format(utils.FormatDate)
  322. item.EdbInfoId = v.EdbInfoId
  323. item.Value = v.Value
  324. item.EdbDataId = v.EdbDataId
  325. item.DataTimestamp = timestamp
  326. items.Items = append(items.Items, item)
  327. }
  328. result.List = append(result.List, items)
  329. }*/
  330. }
  331. }
  332. return
  333. }