edb_data_quarter.go 14 KB

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