edb_data_quarter.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "hongze/hongze_ETA_mobile_api/utils"
  7. "sort"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/nosixtools/solarlunar"
  12. )
  13. func GetEdbDataQuarterCount(edbInfoId int) (count int, err error) {
  14. o := orm.NewOrmUsingDB("data")
  15. hsql := `SELECT COUNT(1) AS count FROM edb_data_quarter WHERE edb_info_id=? `
  16. err = o.Raw(hsql, edbInfoId).QueryRow(&count)
  17. return
  18. }
  19. //指标季度数据计算(公历转农历)
  20. func AddCalculateQuarterV2(edbInfoId, source int, edbCode string) (err error) {
  21. o := orm.NewOrmUsingDB("data")
  22. to, err := o.Begin()
  23. if err != nil {
  24. return
  25. }
  26. defer func() {
  27. if err != nil {
  28. fmt.Println("Err:", err)
  29. _ = to.Rollback()
  30. } else {
  31. _ = to.Commit()
  32. }
  33. }()
  34. edbInfoIdStr := strconv.Itoa(edbInfoId)
  35. fmt.Println(edbInfoIdStr)
  36. //计算数据
  37. var condition string
  38. var pars []interface{}
  39. condition += " AND edb_info_id=? "
  40. pars = append(pars, edbInfoId)
  41. dataList, err := GetEdbDataListAll(condition, pars, source, 0)
  42. if err != nil {
  43. return err
  44. }
  45. var yearArr []int
  46. yearMap := make(map[int]int)
  47. dataMap := make(map[string]*EdbInfoSearchData)
  48. for _, v := range dataList {
  49. //日其中获取年
  50. itemDate, err := time.Parse(utils.FormatDate, v.DataTime)
  51. if err != nil {
  52. return err
  53. }
  54. year := itemDate.Year()
  55. if _, ok := yearMap[year]; !ok {
  56. yearArr = append(yearArr, year)
  57. yearMap[year] = year
  58. }
  59. dataMap[v.DataTime] = v
  60. }
  61. sort.Sort(sort.Reverse(sort.IntSlice(yearArr)))
  62. addSql := ` INSERT INTO edb_data_quarter(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  63. var isAdd bool
  64. //yearLen := len(yearArr)
  65. fmt.Println(yearArr)
  66. for _, yv := range yearArr {
  67. fmt.Println(yv)
  68. }
  69. fmt.Println("isAdd", isAdd)
  70. if isAdd {
  71. addSql = strings.TrimRight(addSql, ",")
  72. utils.FileLog.Info(addSql)
  73. _, err = to.Raw(addSql).Exec()
  74. fmt.Println("err:", err)
  75. if err != nil {
  76. return err
  77. }
  78. }
  79. return
  80. }
  81. //指标季度数据计算(公历转农历)
  82. func AddCalculateQuarterV3(edbInfoId, source int, edbCode string, list []*EdbDataList) (err error) {
  83. var errMsg string
  84. defer func() {
  85. if errMsg != "" {
  86. fmt.Println("errMsg:", errMsg)
  87. }
  88. }()
  89. lunarDate := "2017-01-01" //农历
  90. fmt.Println(solarlunar.LunarToSolar(lunarDate, false))
  91. dataList, err := GetHzTestEdbdata()
  92. var yearArr []int
  93. yearMap := make(map[int]int)
  94. var cureentDate time.Time
  95. for k, v := range dataList {
  96. if k == 0 {
  97. cureentDate = v.Dt
  98. }
  99. year := v.Dt.Year()
  100. if _, ok := yearMap[year]; !ok {
  101. yearArr = append(yearArr, year)
  102. }
  103. yearMap[year] = year
  104. }
  105. //排序
  106. fmt.Println(yearArr)
  107. thisYear := cureentDate.Year()
  108. thisMonth := int(cureentDate.Month())
  109. sort.Ints(yearArr)
  110. fmt.Println("thisYear:", thisYear)
  111. result := new(HzTestEdbdataResult)
  112. for ky, vy := range yearArr {
  113. if thisMonth < 11 {
  114. currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
  115. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
  116. currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
  117. if err != nil {
  118. errMsg = "生成当前春节失败,Err:" + err.Error()
  119. return err
  120. }
  121. fmt.Println(ky, vy)
  122. preYear := vy
  123. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  124. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  125. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  126. if err != nil {
  127. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  128. return err
  129. }
  130. day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  131. fmt.Println("day:", day)
  132. items := new(HzTestEdbdataItems)
  133. dataLen := len(dataList)
  134. for i := dataLen - 1; i >= 0; i-- {
  135. v := dataList[i]
  136. newDate := v.Dt.AddDate(0, 0, int(day))
  137. selectDateStr := strconv.Itoa(thisYear) + "-11" + "-30"
  138. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  139. if newDate.Before(selectDate) {
  140. item := new(HzTestEdbdata)
  141. item.TtradeCode = v.TtradeCode
  142. item.Dt = newDate
  143. item.Close = v.Close
  144. timestamp := item.Dt.UnixNano() / 1e6
  145. item.DataTimestamp = timestamp
  146. items.Items = append(items.Items, item)
  147. }
  148. }
  149. result.List = append(result.List, items)
  150. } else {
  151. fmt.Println(ky, vy)
  152. nextYear := thisYear + 1
  153. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  154. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  155. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  156. if err != nil {
  157. errMsg = "生成当前春节失败,Err:" + err.Error()
  158. return err
  159. }
  160. preYear := vy
  161. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  162. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  163. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  164. if err != nil {
  165. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  166. return err
  167. }
  168. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  169. fmt.Println("day:", day)
  170. items := new(HzTestEdbdataItems)
  171. dataLen := len(dataList)
  172. for i := dataLen - 1; i >= 0; i-- {
  173. v := dataList[i]
  174. newDate := v.Dt.AddDate(0, 0, int(day))
  175. selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
  176. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  177. if newDate.Before(selectDate) {
  178. item := new(HzTestEdbdata)
  179. item.TtradeCode = v.TtradeCode
  180. item.Dt = newDate
  181. item.Close = v.Close
  182. timestamp := item.Dt.UnixNano() / 1e6
  183. item.DataTimestamp = timestamp
  184. items.Items = append(items.Items, item)
  185. }
  186. }
  187. result.List = append(result.List, items)
  188. }
  189. }
  190. resultJson, err := json.Marshal(result)
  191. utils.FileLog.Info(string(resultJson))
  192. return
  193. }
  194. //指标季度数据计算(公历转农历)
  195. func AddCalculateQuarterV4(dataList []*EdbDataList) (result *EdbDataResult, err error) {
  196. var errMsg string
  197. defer func() {
  198. if errMsg != "" {
  199. fmt.Println("errMsg:", errMsg)
  200. }
  201. }()
  202. endDate := dataList[len(dataList)-1].DataTime
  203. endDateForm, err := time.Parse(utils.FormatDate, endDate)
  204. if err != nil {
  205. return result, err
  206. }
  207. thisMonth := int(endDateForm.Month())
  208. result = new(EdbDataResult)
  209. var yearArr []int
  210. yearMap := make(map[int]int)
  211. var cureentDate time.Time
  212. if thisMonth < 11 {
  213. for k, v := range dataList {
  214. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  215. if err != nil {
  216. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  217. return result, err
  218. }
  219. if k == len(dataList)-1 {
  220. cureentDate = dateTime
  221. }
  222. year := dateTime.Year()
  223. if _, ok := yearMap[year]; !ok {
  224. yearArr = append(yearArr, year)
  225. }
  226. yearMap[year] = year
  227. }
  228. } else {
  229. for k, v := range dataList {
  230. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  231. if err != nil {
  232. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  233. return result, err
  234. }
  235. if k == len(dataList)-1 {
  236. cureentDate = dateTime
  237. }
  238. year := dateTime.Year() + 1
  239. if _, ok := yearMap[year]; !ok {
  240. yearArr = append(yearArr, year)
  241. }
  242. yearMap[year] = year
  243. }
  244. }
  245. //排序
  246. fmt.Println("yearArr:", yearArr)
  247. thisYear := cureentDate.Year()
  248. //thisMonth := int(cureentDate.Month())
  249. fmt.Println("thisMonth:", thisMonth)
  250. for ky, vy := range yearArr {
  251. fmt.Println("line 432:", ky, vy, thisYear, thisMonth)
  252. if thisMonth < 11 {
  253. currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
  254. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
  255. currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
  256. if err != nil {
  257. errMsg = "生成当前春节失败,Err:" + err.Error()
  258. return result, err
  259. }
  260. preYear := vy
  261. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  262. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  263. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  264. if err != nil {
  265. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  266. return result, err
  267. }
  268. day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  269. items := new(EdbDataItems)
  270. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  271. items.Year = preYear
  272. for _, v := range dataList {
  273. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  274. if err != nil {
  275. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  276. return result, err
  277. }
  278. newDate := dateTime.AddDate(0, 0, int(day))
  279. selectDateStr := strconv.Itoa(thisYear) + "-11" + "-30"
  280. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  281. if newDate.Before(selectDate) || newDate == selectDate {
  282. timestamp := newDate.UnixNano() / 1e6
  283. item := new(EdbDataList)
  284. item.DataTime = newDate.Format(utils.FormatDate)
  285. item.EdbInfoId = v.EdbInfoId
  286. item.Value = v.Value
  287. item.EdbDataId = v.EdbDataId
  288. item.DataTimestamp = timestamp
  289. items.Items = append(items.Items, item)
  290. }
  291. }
  292. result.List = append(result.List, items)
  293. } else {
  294. nextYear := thisYear + 1
  295. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  296. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  297. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  298. if err != nil {
  299. errMsg = "生成当前春节失败,Err:" + err.Error()
  300. return result, err
  301. }
  302. preYear := vy
  303. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  304. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  305. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  306. if err != nil {
  307. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  308. return result, err
  309. }
  310. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  311. fmt.Println("day:", day, nextYearCjglDate, preYearCjglDate)
  312. items := new(EdbDataItems)
  313. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  314. items.Year = preYear - 1
  315. fmt.Println("preYear:", preYear, "ky:", ky, "yearArrLen:", len(yearArr))
  316. if ky+1 < len(yearArr) {
  317. for _, v := range dataList {
  318. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  319. if err != nil {
  320. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  321. return result, err
  322. }
  323. newDate := dateTime.AddDate(0, 0, int(day))
  324. selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
  325. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  326. if newDate.Before(selectDate) || newDate == selectDate {
  327. timestamp := newDate.UnixNano() / 1e6
  328. item := new(EdbDataList)
  329. item.DataTime = newDate.Format(utils.FormatDate)
  330. item.EdbInfoId = v.EdbInfoId
  331. item.Value = v.Value
  332. item.EdbDataId = v.EdbDataId
  333. item.DataTimestamp = timestamp
  334. items.Items = append(items.Items, item)
  335. }
  336. }
  337. result.List = append(result.List, items)
  338. } else {
  339. for _, v := range dataList {
  340. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  341. if err != nil {
  342. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  343. return result, err
  344. }
  345. timestamp := dateTime.UnixNano() / 1e6
  346. item := new(EdbDataList)
  347. item.DataTime = dateTime.Format(utils.FormatDate)
  348. item.EdbInfoId = v.EdbInfoId
  349. item.Value = v.Value
  350. item.EdbDataId = v.EdbDataId
  351. item.DataTimestamp = timestamp
  352. items.Items = append(items.Items, item)
  353. }
  354. result.List = append(result.List, items)
  355. }
  356. }
  357. }
  358. return
  359. }
  360. // AddCalculateQuarterV4ByUniqueCode 指标季度数据计算(公历转农历)
  361. func AddCalculateQuarterV4ByUniqueCode(dataList []*EdbDataListByUniqueCode) (result *EdbDataResult, err error) {
  362. var errMsg string
  363. defer func() {
  364. if errMsg != "" {
  365. fmt.Println("errMsg:", errMsg)
  366. }
  367. }()
  368. endDate := dataList[len(dataList)-1].DataTime
  369. endDateForm, err := time.Parse(utils.FormatDate, endDate)
  370. if err != nil {
  371. return result, err
  372. }
  373. thisMonth := int(endDateForm.Month())
  374. result = new(EdbDataResult)
  375. var yearArr []int
  376. yearMap := make(map[int]int)
  377. var cureentDate time.Time
  378. if thisMonth < 11 {
  379. for k, v := range dataList {
  380. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  381. if err != nil {
  382. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  383. return result, err
  384. }
  385. if k == len(dataList)-1 {
  386. cureentDate = dateTime
  387. }
  388. year := dateTime.Year()
  389. if _, ok := yearMap[year]; !ok {
  390. yearArr = append(yearArr, year)
  391. }
  392. yearMap[year] = year
  393. }
  394. } else {
  395. for k, v := range dataList {
  396. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  397. if err != nil {
  398. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  399. return result, err
  400. }
  401. if k == len(dataList)-1 {
  402. cureentDate = dateTime
  403. }
  404. year := dateTime.Year() + 1
  405. if _, ok := yearMap[year]; !ok {
  406. yearArr = append(yearArr, year)
  407. }
  408. yearMap[year] = year
  409. }
  410. }
  411. //排序
  412. fmt.Println("yearArr:", yearArr)
  413. thisYear := cureentDate.Year()
  414. //thisMonth := int(cureentDate.Month())
  415. fmt.Println("thisMonth:", thisMonth)
  416. for ky, vy := range yearArr {
  417. fmt.Println("line 432:", ky, vy, thisYear, thisMonth)
  418. if thisMonth < 11 {
  419. currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
  420. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
  421. currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
  422. if err != nil {
  423. errMsg = "生成当前春节失败,Err:" + err.Error()
  424. return result, err
  425. }
  426. preYear := vy
  427. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  428. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  429. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  430. if err != nil {
  431. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  432. return result, err
  433. }
  434. day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  435. items := new(EdbDataItems)
  436. items.Year = preYear
  437. for _, v := range dataList {
  438. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  439. if err != nil {
  440. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  441. return result, err
  442. }
  443. newDate := dateTime.AddDate(0, 0, int(day))
  444. selectDateStr := strconv.Itoa(thisYear) + "-11" + "-30"
  445. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  446. if newDate.Before(selectDate) || newDate == selectDate {
  447. timestamp := newDate.UnixNano() / 1e6
  448. item := new(EdbDataList)
  449. item.DataTime = newDate.Format(utils.FormatDate)
  450. item.EdbInfoId = v.EdbInfoId
  451. item.Value = v.Value
  452. item.EdbDataId = v.EdbDataId
  453. item.DataTimestamp = timestamp
  454. items.Items = append(items.Items, item)
  455. }
  456. }
  457. result.List = append(result.List, items)
  458. } else {
  459. nextYear := thisYear + 1
  460. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  461. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  462. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  463. if err != nil {
  464. errMsg = "生成当前春节失败,Err:" + err.Error()
  465. return result, err
  466. }
  467. preYear := vy
  468. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  469. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  470. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  471. if err != nil {
  472. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  473. return result, err
  474. }
  475. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  476. fmt.Println("day:", day, nextYearCjglDate, preYearCjglDate)
  477. items := new(EdbDataItems)
  478. items.Year = preYear - 1
  479. fmt.Println("preYear:", preYear, "ky:", ky, "yearArrLen:", len(yearArr))
  480. if ky+1 < len(yearArr) {
  481. for _, v := range dataList {
  482. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  483. if err != nil {
  484. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  485. return result, err
  486. }
  487. newDate := dateTime.AddDate(0, 0, int(day))
  488. selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
  489. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  490. if newDate.Before(selectDate) || newDate == selectDate {
  491. timestamp := newDate.UnixNano() / 1e6
  492. item := new(EdbDataList)
  493. item.DataTime = newDate.Format(utils.FormatDate)
  494. item.EdbInfoId = v.EdbInfoId
  495. item.Value = v.Value
  496. item.EdbDataId = v.EdbDataId
  497. item.DataTimestamp = timestamp
  498. items.Items = append(items.Items, item)
  499. }
  500. }
  501. result.List = append(result.List, items)
  502. } else {
  503. for _, v := range dataList {
  504. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  505. if err != nil {
  506. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  507. return result, err
  508. }
  509. timestamp := dateTime.UnixNano() / 1e6
  510. item := new(EdbDataList)
  511. item.DataTime = dateTime.Format(utils.FormatDate)
  512. item.EdbInfoId = v.EdbInfoId
  513. item.Value = v.Value
  514. item.EdbDataId = v.EdbDataId
  515. item.DataTimestamp = timestamp
  516. items.Items = append(items.Items, item)
  517. }
  518. result.List = append(result.List, items)
  519. }
  520. }
  521. }
  522. return
  523. }
  524. type HzTestEdbdataResult struct {
  525. List []*HzTestEdbdataItems
  526. }
  527. type HzTestEdbdataItems struct {
  528. Items []*HzTestEdbdata
  529. }
  530. type HzTestEdbdata struct {
  531. TtradeCode string `orm:"column(TRADE_CODE)"`
  532. Dt time.Time `orm:"column(DT)"`
  533. Close float64 `orm:"column(CLOSE)"`
  534. ModifyTime time.Time `orm:"column(modify_time)"`
  535. DataTimestamp int64
  536. }
  537. func GetHzTestEdbdata() (list []*HzTestEdbdata, err error) {
  538. o := orm.NewOrm()
  539. sql := ` SELECT * FROM hz_test_edbdata WHERE TRADE_CODE=? ORDER BY DT DESC `
  540. _, err = o.Raw(sql, "ST_0000577845").QueryRows(&list)
  541. return
  542. }
  543. type EdbDataItems struct {
  544. Items []*EdbDataList
  545. Year int
  546. BetweenDay int `json:"-" description:"公历与农历之间相差的天数"`
  547. CuttingDataTimestamp int64 `description:"切割的时间戳"`
  548. }
  549. type EdbDataResult struct {
  550. List []*EdbDataItems
  551. }
  552. func AddCalculateQuarterV5(dataList []*EdbDataList) (result *EdbDataResult, err error) {
  553. var errMsg string
  554. defer func() {
  555. if errMsg != "" {
  556. fmt.Println("errMsg:", errMsg)
  557. }
  558. }()
  559. result = new(EdbDataResult)
  560. thisYear := time.Now().Year()
  561. endDate := dataList[len(dataList)-1].DataTime
  562. endDateForm, err := time.Parse(utils.FormatDate, endDate)
  563. if err != nil {
  564. return result, err
  565. }
  566. thisMonth := int(endDateForm.Month())
  567. fmt.Println(thisMonth)
  568. for i := 4; i > 0; i-- {
  569. if thisMonth < 11 {
  570. nextYear := thisYear
  571. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  572. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  573. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  574. if err != nil {
  575. errMsg = "生成当前春节失败,Err:" + err.Error()
  576. return result, err
  577. }
  578. preYearCjnl := strconv.Itoa(nextYear-i) + "-01-01" //之前年份春节农历
  579. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  580. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  581. if err != nil {
  582. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  583. return result, err
  584. }
  585. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  586. items := new(EdbDataItems)
  587. for _, v := range dataList {
  588. dataDate := v.DataTime
  589. dataDateForm, err := time.Parse(utils.FormatDate, dataDate)
  590. if err != nil {
  591. return result, err
  592. }
  593. newDate := dataDateForm.AddDate(0, 0, int(day))
  594. selectDateStr := strconv.Itoa(nextYear) + "-11" + "-30"
  595. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  596. if newDate.Before(selectDate) {
  597. timestamp := newDate.UnixNano() / 1e6
  598. item := new(EdbDataList)
  599. item.DataTime = newDate.Format(utils.FormatDate)
  600. item.EdbInfoId = v.EdbInfoId
  601. item.Value = v.Value
  602. item.EdbDataId = v.EdbDataId
  603. item.DataTimestamp = timestamp
  604. items.Items = append(items.Items, item)
  605. }
  606. }
  607. fmt.Println(nextYear, "年春节阳历", nextYearCjgl)
  608. fmt.Println(i, "年前春节", preYearCjnl)
  609. fmt.Println(i, "年前春节阳历", preYearCjgl)
  610. fmt.Println("与", nextYear, "年春节相差天数", day, "____________")
  611. result.List = append(result.List, items)
  612. } else {
  613. nextYear := thisYear + 1
  614. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  615. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  616. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  617. if err != nil {
  618. errMsg = "生成当前春节失败,Err:" + err.Error()
  619. return result, err
  620. }
  621. preYearCjnl := strconv.Itoa(nextYear-i) + "-01-01" //之前年份春节农历
  622. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  623. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  624. if err != nil {
  625. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  626. return result, err
  627. }
  628. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  629. items := new(EdbDataItems)
  630. for _, v := range dataList {
  631. dataDate := v.DataTime
  632. dataDateForm, err := time.Parse(utils.FormatDate, dataDate)
  633. if err != nil {
  634. return result, err
  635. }
  636. newDate := dataDateForm.AddDate(0, 0, int(day))
  637. selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
  638. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  639. if newDate.Before(selectDate) {
  640. timestamp := newDate.UnixNano() / 1e6
  641. item := new(EdbDataList)
  642. item.DataTime = newDate.Format(utils.FormatDate)
  643. item.EdbInfoId = v.EdbInfoId
  644. item.Value = v.Value
  645. item.EdbDataId = v.EdbDataId
  646. item.DataTimestamp = timestamp
  647. items.Items = append(items.Items, item)
  648. }
  649. }
  650. fmt.Println(nextYear, "年春节阳历", nextYearCjgl)
  651. fmt.Println(i, "年前春节", preYearCjnl)
  652. fmt.Println(i, "年前春节阳历", preYearCjgl)
  653. fmt.Println("与", nextYear, "年春节相差天数", day, "____________")
  654. result.List = append(result.List, items)
  655. }
  656. }
  657. //合并第五组数据
  658. items := new(EdbDataItems)
  659. for _, v := range dataList {
  660. dataDate := v.DataTime
  661. dataDateForm, err := time.Parse(utils.FormatDate, dataDate)
  662. if err != nil {
  663. return result, err
  664. }
  665. timestamp := dataDateForm.UnixNano() / 1e6
  666. item := new(EdbDataList)
  667. item.DataTime = v.DataTime
  668. item.EdbInfoId = v.EdbInfoId
  669. item.Value = v.Value
  670. item.EdbDataId = v.EdbDataId
  671. item.DataTimestamp = timestamp
  672. items.Items = append(items.Items, item)
  673. }
  674. result.List = append(result.List, items)
  675. return
  676. }