edb_data_quarter.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. package data_manage
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "sort"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/nosixtools/solarlunar"
  11. )
  12. func GetEdbDataQuarterCount(edbInfoId int) (count int, err error) {
  13. hsql := `SELECT COUNT(1) AS count FROM edb_data_quarter WHERE edb_info_id=? `
  14. err = global.DmSQL["data"].Raw(hsql, edbInfoId).Scan(&count).Error
  15. return
  16. }
  17. // 指标季度数据计算
  18. func AddCalculateQuarter(edbInfoId, source int, edbCode string) (err error) {
  19. to := global.DmSQL["data"].Begin()
  20. defer func() {
  21. if err != nil {
  22. fmt.Println("Err:", err)
  23. _ = to.Rollback()
  24. } else {
  25. _ = to.Commit()
  26. }
  27. }()
  28. edbInfoIdStr := strconv.Itoa(edbInfoId)
  29. fmt.Println(edbInfoIdStr)
  30. //计算数据
  31. var condition string
  32. var pars []interface{}
  33. condition += " AND edb_info_id=? "
  34. pars = append(pars, edbInfoId)
  35. dataList, err := GetAllEdbDataListData(edbInfoId, source, utils.DATA_SUB_SOURCE_EDB, "")
  36. if err != nil {
  37. return err
  38. }
  39. var yearArr []int
  40. yearMap := make(map[int]int)
  41. dataMap := make(map[string]*EdbData)
  42. for _, v := range dataList {
  43. //日其中获取年
  44. itemDate, err := time.Parse(utils.FormatDate, v.DataTime)
  45. if err != nil {
  46. return err
  47. }
  48. year := itemDate.Year()
  49. if _, ok := yearMap[year]; !ok {
  50. yearArr = append(yearArr, year)
  51. yearMap[year] = year
  52. }
  53. dataMap[v.DataTime] = v
  54. }
  55. sort.Sort(sort.Reverse(sort.IntSlice(yearArr)))
  56. addSql := ` INSERT INTO edb_data_quarter(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  57. var isAdd bool
  58. fmt.Println(yearArr)
  59. for _, yv := range yearArr {
  60. currentYear := yv
  61. hsql := `SELECT * FROM holiday WHERE year=? `
  62. currentHolidayObj := new(Holiday)
  63. err = to.Raw(hsql, currentYear).Scan(&currentHolidayObj).Error
  64. if err != nil {
  65. fmt.Println("get holiday err:" + err.Error())
  66. return err
  67. }
  68. cjDate := currentHolidayObj.HolidayDate
  69. fmt.Println("year:", yv, "cjDate:", cjDate)
  70. existMap := make(map[string]string)
  71. for i := 1; i <= 70; i++ {
  72. preDate := cjDate.Format(utils.FormatDate)
  73. fmt.Println("year:", yv, "preDate:", preDate, "i:", i)
  74. if findItem, ok := dataMap[preDate]; ok {
  75. timestamp := currentHolidayObj.HolidayDate.UnixNano() / 1e6
  76. timestampStr := fmt.Sprintf("%d", timestamp)
  77. if _, exist := existMap[preDate]; !exist {
  78. valStr := utils.SubFloatToString(findItem.Value, 4)
  79. addSql += GetAddSql(edbInfoIdStr, edbCode, preDate, timestampStr, valStr)
  80. isAdd = true
  81. }
  82. }
  83. cjDate = cjDate.AddDate(0, 0, -1)
  84. }
  85. cjDate = currentHolidayObj.HolidayDate
  86. for i := 1; i <= 70; i++ {
  87. nextDate := cjDate.Format(utils.FormatDate)
  88. fmt.Println("year:", yv, "nextDate:", nextDate, "i:", i)
  89. if findItem, ok := dataMap[nextDate]; ok {
  90. timestamp := currentHolidayObj.HolidayDate.UnixNano() / 1e6
  91. timestampStr := fmt.Sprintf("%d", timestamp)
  92. if _, exist := existMap[nextDate]; !exist {
  93. valStr := utils.SubFloatToString(findItem.Value, 4)
  94. addSql += GetAddSql(edbInfoIdStr, edbCode, nextDate, timestampStr, valStr)
  95. isAdd = true
  96. }
  97. }
  98. cjDate = cjDate.AddDate(0, 0, +1)
  99. }
  100. }
  101. fmt.Println("isAdd", isAdd)
  102. if isAdd {
  103. addSql = strings.TrimRight(addSql, ",")
  104. utils.FileLog.Info(addSql)
  105. err = to.Exec(addSql).Error
  106. fmt.Println("err:", err)
  107. if err != nil {
  108. return err
  109. }
  110. }
  111. return
  112. }
  113. // 指标季度数据计算(公历转农历)
  114. func AddCalculateQuarterV4(dataList []*EdbDataList) (result *EdbDataResult, err error) {
  115. var errMsg string
  116. defer func() {
  117. if errMsg != "" {
  118. fmt.Println("errMsg:", errMsg)
  119. }
  120. }()
  121. endDate := dataList[len(dataList)-1].DataTime
  122. endDateForm, err := time.Parse(utils.FormatDate, endDate)
  123. if err != nil {
  124. return result, err
  125. }
  126. thisMonth := int(endDateForm.Month())
  127. result = new(EdbDataResult)
  128. var yearArr []int
  129. yearMap := make(map[int]int)
  130. var cureentDate time.Time
  131. if thisMonth < 11 {
  132. for k, v := range dataList {
  133. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  134. if err != nil {
  135. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  136. return result, err
  137. }
  138. if k == len(dataList)-1 {
  139. cureentDate = dateTime
  140. }
  141. year := dateTime.Year()
  142. if _, ok := yearMap[year]; !ok {
  143. yearArr = append(yearArr, year)
  144. }
  145. yearMap[year] = year
  146. }
  147. } else {
  148. for k, v := range dataList {
  149. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  150. if err != nil {
  151. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  152. return result, err
  153. }
  154. if k == len(dataList)-1 {
  155. cureentDate = dateTime
  156. }
  157. year := dateTime.Year() + 1
  158. if _, ok := yearMap[year]; !ok {
  159. yearArr = append(yearArr, year)
  160. }
  161. yearMap[year] = year
  162. }
  163. }
  164. //排序
  165. fmt.Println("yearArr:", yearArr)
  166. thisYear := cureentDate.Year()
  167. //thisMonth := int(cureentDate.Month())
  168. fmt.Println("thisMonth:", thisMonth)
  169. for ky, vy := range yearArr {
  170. fmt.Println("line 432:", ky, vy, thisYear, thisMonth)
  171. if thisMonth < 11 {
  172. currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
  173. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
  174. currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
  175. if err != nil {
  176. errMsg = "生成当前春节失败,Err:" + err.Error()
  177. return result, err
  178. }
  179. preYear := vy
  180. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  181. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  182. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  183. if err != nil {
  184. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  185. return result, err
  186. }
  187. day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  188. fmt.Println("day:", day, currentYearCjglDate, preYearCjglDate)
  189. items := new(EdbDataItems)
  190. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  191. items.Year = preYear
  192. for _, v := range dataList {
  193. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  194. if err != nil {
  195. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  196. return result, err
  197. }
  198. newDate := dateTime.AddDate(0, 0, int(day))
  199. selectDateStr := strconv.Itoa(thisYear) + "-11" + "-30"
  200. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  201. if newDate.Before(selectDate) || newDate == selectDate {
  202. timestamp := newDate.UnixNano() / 1e6
  203. item := new(EdbDataList)
  204. item.DataTime = newDate.Format(utils.FormatDate)
  205. item.EdbInfoId = v.EdbInfoId
  206. item.Value = v.Value
  207. item.EdbDataId = v.EdbDataId
  208. item.DataTimestamp = timestamp
  209. items.Items = append(items.Items, item)
  210. }
  211. }
  212. result.List = append(result.List, items)
  213. } else {
  214. nextYear := thisYear + 1
  215. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  216. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  217. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  218. if err != nil {
  219. errMsg = "生成当前春节失败,Err:" + err.Error()
  220. return result, err
  221. }
  222. preYear := vy
  223. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  224. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  225. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  226. if err != nil {
  227. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  228. return result, err
  229. }
  230. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  231. fmt.Println("day:", day, nextYearCjglDate, preYearCjglDate)
  232. items := new(EdbDataItems)
  233. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  234. items.Year = preYear - 1
  235. fmt.Println("preYear:", preYear, "ky:", ky, "yearArrLen:", len(yearArr))
  236. if ky+1 < len(yearArr) {
  237. for _, v := range dataList {
  238. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  239. if err != nil {
  240. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  241. return result, err
  242. }
  243. newDate := dateTime.AddDate(0, 0, int(day))
  244. selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
  245. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  246. if newDate.Before(selectDate) || newDate == selectDate {
  247. timestamp := newDate.UnixNano() / 1e6
  248. item := new(EdbDataList)
  249. item.DataTime = newDate.Format(utils.FormatDate)
  250. item.EdbInfoId = v.EdbInfoId
  251. item.Value = v.Value
  252. item.EdbDataId = v.EdbDataId
  253. item.DataTimestamp = timestamp
  254. items.Items = append(items.Items, item)
  255. }
  256. }
  257. result.List = append(result.List, items)
  258. } else {
  259. for _, v := range dataList {
  260. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  261. if err != nil {
  262. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  263. return result, err
  264. }
  265. timestamp := dateTime.UnixNano() / 1e6
  266. item := new(EdbDataList)
  267. item.DataTime = dateTime.Format(utils.FormatDate)
  268. item.EdbInfoId = v.EdbInfoId
  269. item.Value = v.Value
  270. item.EdbDataId = v.EdbDataId
  271. item.DataTimestamp = timestamp
  272. items.Items = append(items.Items, item)
  273. }
  274. result.List = append(result.List, items)
  275. }
  276. }
  277. }
  278. return
  279. }
  280. // AddCalculateQuarterV6 指标季度数据计算(季节性图表)
  281. func AddCalculateQuarterV6(dataList []*EdbDataList) (result *EdbDataResult, err error) {
  282. var errMsg string
  283. defer func() {
  284. if errMsg != "" {
  285. fmt.Println("errMsg:", errMsg)
  286. }
  287. }()
  288. endDate := dataList[len(dataList)-1].DataTime
  289. endDateForm, err := time.Parse(utils.FormatDate, endDate)
  290. if err != nil {
  291. return result, err
  292. }
  293. thisMonth := int(endDateForm.Month())
  294. result = new(EdbDataResult)
  295. var yearArr []int
  296. yearMap := make(map[int]int)
  297. var cureentDate time.Time
  298. if thisMonth < 11 {
  299. for k, v := range dataList {
  300. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  301. if err != nil {
  302. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  303. return result, err
  304. }
  305. if k == len(dataList)-1 {
  306. cureentDate = dateTime
  307. }
  308. year := dateTime.Year()
  309. if _, ok := yearMap[year]; !ok {
  310. yearArr = append(yearArr, year)
  311. }
  312. yearMap[year] = year
  313. }
  314. } else {
  315. for k, v := range dataList {
  316. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  317. if err != nil {
  318. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  319. return result, err
  320. }
  321. if k == len(dataList)-1 {
  322. cureentDate = dateTime
  323. }
  324. year := dateTime.Year() + 1
  325. if _, ok := yearMap[year]; !ok {
  326. yearArr = append(yearArr, year)
  327. }
  328. yearMap[year] = year
  329. }
  330. }
  331. //排序
  332. thisYear := cureentDate.Year()
  333. for _, vy := range yearArr {
  334. if thisMonth < 11 {
  335. currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
  336. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
  337. currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
  338. if err != nil {
  339. errMsg = "生成当前春节失败,Err:" + err.Error()
  340. return result, err
  341. }
  342. preYear := vy
  343. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  344. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  345. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  346. if err != nil {
  347. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  348. return result, err
  349. }
  350. day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  351. fmt.Println("day:", day, currentYearCjglDate, preYearCjglDate)
  352. items := new(EdbDataItems)
  353. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  354. items.Year = preYear
  355. for _, v := range dataList {
  356. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  357. if err != nil {
  358. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  359. return result, err
  360. }
  361. newDate := dateTime.AddDate(0, 0, int(day))
  362. timestamp := newDate.UnixNano() / 1e6
  363. item := new(EdbDataList)
  364. item.DataTime = newDate.Format(utils.FormatDate)
  365. item.EdbInfoId = v.EdbInfoId
  366. item.Value = v.Value
  367. item.EdbDataId = v.EdbDataId
  368. item.DataTimestamp = timestamp
  369. items.Items = append(items.Items, item)
  370. }
  371. result.List = append(result.List, items)
  372. } else {
  373. nextYear := thisYear + 1
  374. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  375. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  376. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  377. if err != nil {
  378. errMsg = "生成当前春节失败,Err:" + err.Error()
  379. return result, err
  380. }
  381. preYear := vy
  382. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  383. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  384. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  385. if err != nil {
  386. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  387. return result, err
  388. }
  389. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  390. items := new(EdbDataItems)
  391. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  392. items.Year = preYear
  393. for _, v := range dataList {
  394. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  395. if err != nil {
  396. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  397. return result, err
  398. }
  399. newDate := dateTime.AddDate(0, 0, int(day))
  400. timestamp := newDate.UnixNano() / 1e6
  401. item := new(EdbDataList)
  402. item.DataTime = newDate.Format(utils.FormatDate)
  403. item.EdbInfoId = v.EdbInfoId
  404. item.Value = v.Value
  405. item.EdbDataId = v.EdbDataId
  406. item.DataTimestamp = timestamp
  407. items.Items = append(items.Items, item)
  408. }
  409. result.List = append(result.List, items)
  410. }
  411. }
  412. return
  413. }
  414. type EdbDataItems struct {
  415. Items []*EdbDataList
  416. Year int
  417. BetweenDay int `json:"-" description:"公历与农历之间相差的天数"`
  418. CuttingDataTimestamp int64 `description:"切割的时间戳"`
  419. }
  420. type EdbDataResult struct {
  421. List []*EdbDataItems
  422. }