edb_data_quarter.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  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 AddCalculateQuarter(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. currentYear := yv
  68. hsql := `SELECT * FROM holiday WHERE year=? `
  69. currentHolidayObj := new(Holiday)
  70. err = to.Raw(hsql, currentYear).QueryRow(&currentHolidayObj)
  71. if err != nil {
  72. fmt.Println("get holiday err:" + err.Error())
  73. return err
  74. }
  75. cjDate := currentHolidayObj.HolidayDate
  76. fmt.Println("year:", yv, "cjDate:", cjDate)
  77. //utils.FileLog.Info("cjDate:", cjDate)
  78. existMap := make(map[string]string)
  79. for i := 1; i <= 70; i++ {
  80. preDate := cjDate.Format(utils.FormatDate)
  81. fmt.Println("year:", yv, "preDate:", preDate, "i:", i)
  82. if findItem, ok := dataMap[preDate]; ok {
  83. timestamp := currentHolidayObj.HolidayDate.UnixNano() / 1e6
  84. timestampStr := fmt.Sprintf("%d", timestamp)
  85. if _, exist := existMap[preDate]; !exist {
  86. valStr := utils.SubFloatToString(findItem.Value, 4)
  87. addSql += GetAddSql(edbInfoIdStr, edbCode, preDate, timestampStr, valStr)
  88. isAdd = true
  89. }
  90. }
  91. cjDate = cjDate.AddDate(0, 0, -1)
  92. }
  93. cjDate = currentHolidayObj.HolidayDate
  94. for i := 1; i <= 70; i++ {
  95. nextDate := cjDate.Format(utils.FormatDate)
  96. fmt.Println("year:", yv, "nextDate:", nextDate, "i:", i)
  97. if findItem, ok := dataMap[nextDate]; ok {
  98. timestamp := currentHolidayObj.HolidayDate.UnixNano() / 1e6
  99. timestampStr := fmt.Sprintf("%d", timestamp)
  100. if _, exist := existMap[nextDate]; !exist {
  101. valStr := utils.SubFloatToString(findItem.Value, 4)
  102. addSql += GetAddSql(edbInfoIdStr, edbCode, nextDate, timestampStr, valStr)
  103. isAdd = true
  104. }
  105. }
  106. cjDate = cjDate.AddDate(0, 0, +1)
  107. }
  108. //nextIndex := yk + 1
  109. //if nextIndex < yearLen {
  110. // nextYear := yearArr[nextIndex]
  111. // fmt.Println("year:", currentYear, nextYear)
  112. // nextHolidayObj := new(Holiday)
  113. // hsql := `SELECT * FROM holiday WHERE year=? `
  114. // err = to.Raw(hsql, nextYear).QueryRow(&nextHolidayObj)
  115. // if err != nil {
  116. // return err
  117. // }
  118. // nextHolidayStr := nextHolidayObj.HolidayDate.Format(utils.FormatDate)
  119. // currentHolidayStr := currentHolidayObj.HolidayDate.Format(utils.FormatDate)
  120. //
  121. // if findItem, ok := dataMap[nextHolidayStr]; ok {
  122. // timestamp := currentHolidayObj.HolidayDate.UnixNano() / 1e6
  123. // timestampStr := fmt.Sprintf("%d", timestamp)
  124. // addSql += GetAddSql(edbInfoIdStr, edbCode, currentHolidayStr, timestampStr, findItem.Value)
  125. // isAdd = true
  126. // }
  127. //
  128. // nextMinDate := strconv.Itoa(nextYear) + "-01-01"
  129. // nextMaxDate := strconv.Itoa(nextYear) + "-12-31"
  130. // currentMinDate := strconv.Itoa(currentYear) + "-01-01"
  131. // currentMaxDate := strconv.Itoa(currentYear) + "-12-31"
  132. //
  133. // i := 0
  134. // for {
  135. // i--
  136. // nextHolidayFormat := nextHolidayObj.HolidayDate.AddDate(0, 0, i)
  137. // currentHolidayFormat := currentHolidayObj.HolidayDate.AddDate(0, 0, i)
  138. //
  139. // nextHolidayStr := nextHolidayFormat.Format(utils.FormatDate)
  140. // currentHolidayStr := currentHolidayFormat.Format(utils.FormatDate)
  141. //
  142. // fmt.Println("currentHolidayStr:", currentHolidayStr, nextHolidayStr)
  143. // if findItem, ok := dataMap[nextHolidayStr]; ok {
  144. // timestamp := currentHolidayFormat.UnixNano() / 1e6
  145. // timestampStr := fmt.Sprintf("%d", timestamp)
  146. // addSql += GetAddSql(edbInfoIdStr, edbCode, currentHolidayStr, timestampStr, findItem.Value)
  147. // isAdd = true
  148. // }
  149. // //fmt.Println("nextHolidayStr:", nextHolidayStr,nextMinDate)
  150. // if nextHolidayStr == nextMinDate || currentHolidayStr == currentMinDate {
  151. // fmt.Println("min跳出循环:" + nextMinDate)
  152. // break
  153. // }
  154. // }
  155. //
  156. // n := 0
  157. // for {
  158. // n++
  159. // nextHolidayFormat := nextHolidayObj.HolidayDate.AddDate(0, 0, n)
  160. // currentHolidayFormat := currentHolidayObj.HolidayDate.AddDate(0, 0, n)
  161. //
  162. // nextHolidayStr := nextHolidayFormat.Format(utils.FormatDate)
  163. // currentHolidayStr := currentHolidayFormat.Format(utils.FormatDate)
  164. //
  165. // fmt.Println("currentHolidayStr:", currentHolidayStr, nextHolidayStr)
  166. //
  167. // if findItem, ok := dataMap[nextHolidayStr]; ok {
  168. // timestamp := currentHolidayFormat.UnixNano() / 1e6
  169. // timestampStr := fmt.Sprintf("%d", timestamp)
  170. //
  171. // addSql += GetAddSql(edbInfoIdStr, edbCode, currentHolidayStr, timestampStr, findItem.Value)
  172. // isAdd = true
  173. // }
  174. //
  175. // //fmt.Println("nextHolidayStr:", nextHolidayStr,nextMaxDate)
  176. // if nextHolidayStr == nextMaxDate || currentHolidayStr == currentMaxDate {
  177. // fmt.Println("max跳出循环:" + nextMaxDate)
  178. // break
  179. // }
  180. // }
  181. //}
  182. }
  183. fmt.Println("isAdd", isAdd)
  184. if isAdd {
  185. addSql = strings.TrimRight(addSql, ",")
  186. utils.FileLog.Info(addSql)
  187. _, err = to.Raw(addSql).Exec()
  188. fmt.Println("err:", err)
  189. if err != nil {
  190. return err
  191. }
  192. }
  193. return
  194. }
  195. // 指标季度数据计算(公历转农历)
  196. func AddCalculateQuarterV2(edbInfoId, source int, edbCode string) (err error) {
  197. o := orm.NewOrmUsingDB("data")
  198. to, err := o.Begin()
  199. if err != nil {
  200. return
  201. }
  202. defer func() {
  203. if err != nil {
  204. fmt.Println("Err:", err)
  205. _ = to.Rollback()
  206. } else {
  207. _ = to.Commit()
  208. }
  209. }()
  210. edbInfoIdStr := strconv.Itoa(edbInfoId)
  211. fmt.Println(edbInfoIdStr)
  212. //计算数据
  213. var condition string
  214. var pars []interface{}
  215. condition += " AND edb_info_id=? "
  216. pars = append(pars, edbInfoId)
  217. dataList, err := GetEdbDataListAll(condition, pars, source, 0)
  218. if err != nil {
  219. return err
  220. }
  221. var yearArr []int
  222. yearMap := make(map[int]int)
  223. dataMap := make(map[string]*EdbInfoSearchData)
  224. for _, v := range dataList {
  225. //日其中获取年
  226. itemDate, err := time.Parse(utils.FormatDate, v.DataTime)
  227. if err != nil {
  228. return err
  229. }
  230. year := itemDate.Year()
  231. if _, ok := yearMap[year]; !ok {
  232. yearArr = append(yearArr, year)
  233. yearMap[year] = year
  234. }
  235. dataMap[v.DataTime] = v
  236. }
  237. sort.Sort(sort.Reverse(sort.IntSlice(yearArr)))
  238. addSql := ` INSERT INTO edb_data_quarter(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  239. var isAdd bool
  240. //yearLen := len(yearArr)
  241. fmt.Println(yearArr)
  242. for _, yv := range yearArr {
  243. fmt.Println(yv)
  244. }
  245. fmt.Println("isAdd", isAdd)
  246. if isAdd {
  247. addSql = strings.TrimRight(addSql, ",")
  248. utils.FileLog.Info(addSql)
  249. _, err = to.Raw(addSql).Exec()
  250. fmt.Println("err:", err)
  251. if err != nil {
  252. return err
  253. }
  254. }
  255. return
  256. }
  257. // 指标季度数据计算(公历转农历)
  258. func AddCalculateQuarterV3(edbInfoId, source int, edbCode string, list []*EdbDataList) (err error) {
  259. var errMsg string
  260. defer func() {
  261. if errMsg != "" {
  262. fmt.Println("errMsg:", errMsg)
  263. }
  264. }()
  265. lunarDate := "2017-01-01" //农历
  266. fmt.Println(solarlunar.LunarToSolar(lunarDate, false))
  267. dataList, err := GetHzTestEdbdata()
  268. var yearArr []int
  269. yearMap := make(map[int]int)
  270. var cureentDate time.Time
  271. for k, v := range dataList {
  272. if k == 0 {
  273. cureentDate = v.Dt
  274. }
  275. year := v.Dt.Year()
  276. if _, ok := yearMap[year]; !ok {
  277. yearArr = append(yearArr, year)
  278. }
  279. yearMap[year] = year
  280. }
  281. //排序
  282. fmt.Println(yearArr)
  283. thisYear := cureentDate.Year()
  284. thisMonth := int(cureentDate.Month())
  285. sort.Ints(yearArr)
  286. fmt.Println("thisYear:", thisYear)
  287. result := new(HzTestEdbdataResult)
  288. for ky, vy := range yearArr {
  289. if thisMonth < 11 {
  290. currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
  291. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
  292. currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
  293. if err != nil {
  294. errMsg = "生成当前春节失败,Err:" + err.Error()
  295. return err
  296. }
  297. fmt.Println(ky, vy)
  298. preYear := vy
  299. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  300. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  301. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  302. if err != nil {
  303. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  304. return err
  305. }
  306. day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  307. fmt.Println("day:", day)
  308. items := new(HzTestEdbdataItems)
  309. dataLen := len(dataList)
  310. for i := dataLen - 1; i >= 0; i-- {
  311. v := dataList[i]
  312. newDate := v.Dt.AddDate(0, 0, int(day))
  313. selectDateStr := strconv.Itoa(thisYear) + "-11" + "-30"
  314. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  315. if newDate.Before(selectDate) {
  316. item := new(HzTestEdbdata)
  317. item.TtradeCode = v.TtradeCode
  318. item.Dt = newDate
  319. item.Close = v.Close
  320. timestamp := item.Dt.UnixNano() / 1e6
  321. item.DataTimestamp = timestamp
  322. items.Items = append(items.Items, item)
  323. }
  324. }
  325. result.List = append(result.List, items)
  326. } else {
  327. fmt.Println(ky, vy)
  328. nextYear := thisYear + 1
  329. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  330. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  331. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  332. if err != nil {
  333. errMsg = "生成当前春节失败,Err:" + err.Error()
  334. return err
  335. }
  336. preYear := vy
  337. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  338. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  339. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  340. if err != nil {
  341. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  342. return err
  343. }
  344. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  345. fmt.Println("day:", day)
  346. items := new(HzTestEdbdataItems)
  347. dataLen := len(dataList)
  348. for i := dataLen - 1; i >= 0; i-- {
  349. v := dataList[i]
  350. newDate := v.Dt.AddDate(0, 0, int(day))
  351. selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
  352. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  353. if newDate.Before(selectDate) {
  354. item := new(HzTestEdbdata)
  355. item.TtradeCode = v.TtradeCode
  356. item.Dt = newDate
  357. item.Close = v.Close
  358. timestamp := item.Dt.UnixNano() / 1e6
  359. item.DataTimestamp = timestamp
  360. items.Items = append(items.Items, item)
  361. }
  362. }
  363. result.List = append(result.List, items)
  364. }
  365. }
  366. resultJson, err := json.Marshal(result)
  367. utils.FileLog.Info(string(resultJson))
  368. return
  369. }
  370. // 指标季度数据计算(公历转农历)
  371. func AddCalculateQuarterV4(dataList []*EdbDataList) (result *EdbDataResult, err error) {
  372. var errMsg string
  373. defer func() {
  374. if errMsg != "" {
  375. fmt.Println("errMsg:", errMsg)
  376. }
  377. }()
  378. endDate := dataList[len(dataList)-1].DataTime
  379. endDateForm, err := time.Parse(utils.FormatDate, endDate)
  380. if err != nil {
  381. return result, err
  382. }
  383. thisMonth := int(endDateForm.Month())
  384. result = new(EdbDataResult)
  385. var yearArr []int
  386. yearMap := make(map[int]int)
  387. var cureentDate time.Time
  388. if thisMonth < 11 {
  389. for k, v := range dataList {
  390. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  391. if err != nil {
  392. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  393. return result, err
  394. }
  395. if k == len(dataList)-1 {
  396. cureentDate = dateTime
  397. }
  398. year := dateTime.Year()
  399. if _, ok := yearMap[year]; !ok {
  400. yearArr = append(yearArr, year)
  401. }
  402. yearMap[year] = year
  403. }
  404. } else {
  405. for k, v := range dataList {
  406. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  407. if err != nil {
  408. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  409. return result, err
  410. }
  411. if k == len(dataList)-1 {
  412. cureentDate = dateTime
  413. }
  414. year := dateTime.Year() + 1
  415. if _, ok := yearMap[year]; !ok {
  416. yearArr = append(yearArr, year)
  417. }
  418. yearMap[year] = year
  419. }
  420. }
  421. //排序
  422. fmt.Println("yearArr:", yearArr)
  423. thisYear := cureentDate.Year()
  424. //thisMonth := int(cureentDate.Month())
  425. fmt.Println("thisMonth:", thisMonth)
  426. for ky, vy := range yearArr {
  427. fmt.Println("line 432:", ky, vy, thisYear, thisMonth)
  428. if thisMonth < 11 {
  429. currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
  430. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
  431. currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
  432. if err != nil {
  433. errMsg = "生成当前春节失败,Err:" + err.Error()
  434. return result, err
  435. }
  436. preYear := vy
  437. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  438. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  439. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  440. if err != nil {
  441. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  442. return result, err
  443. }
  444. day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  445. items := new(EdbDataItems)
  446. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  447. items.Year = preYear
  448. for _, v := range dataList {
  449. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  450. if err != nil {
  451. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  452. return result, err
  453. }
  454. newDate := dateTime.AddDate(0, 0, int(day))
  455. selectDateStr := strconv.Itoa(thisYear) + "-11" + "-30"
  456. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  457. if newDate.Before(selectDate) || newDate == selectDate {
  458. timestamp := newDate.UnixNano() / 1e6
  459. item := new(EdbDataList)
  460. item.DataTime = newDate.Format(utils.FormatDate)
  461. item.EdbInfoId = v.EdbInfoId
  462. item.Value = v.Value
  463. item.EdbDataId = v.EdbDataId
  464. item.DataTimestamp = timestamp
  465. items.Items = append(items.Items, item)
  466. }
  467. }
  468. result.List = append(result.List, items)
  469. } else {
  470. nextYear := thisYear + 1
  471. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  472. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  473. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  474. if err != nil {
  475. errMsg = "生成当前春节失败,Err:" + err.Error()
  476. return result, err
  477. }
  478. preYear := vy
  479. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  480. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  481. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  482. if err != nil {
  483. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  484. return result, err
  485. }
  486. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  487. fmt.Println("day:", day, nextYearCjglDate, preYearCjglDate)
  488. items := new(EdbDataItems)
  489. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  490. items.Year = preYear - 1
  491. fmt.Println("preYear:", preYear, "ky:", ky, "yearArrLen:", len(yearArr))
  492. if ky+1 < len(yearArr) {
  493. for _, v := range dataList {
  494. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  495. if err != nil {
  496. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  497. return result, err
  498. }
  499. newDate := dateTime.AddDate(0, 0, int(day))
  500. selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
  501. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  502. if newDate.Before(selectDate) || newDate == selectDate {
  503. timestamp := newDate.UnixNano() / 1e6
  504. item := new(EdbDataList)
  505. item.DataTime = newDate.Format(utils.FormatDate)
  506. item.EdbInfoId = v.EdbInfoId
  507. item.Value = v.Value
  508. item.EdbDataId = v.EdbDataId
  509. item.DataTimestamp = timestamp
  510. items.Items = append(items.Items, item)
  511. }
  512. }
  513. result.List = append(result.List, items)
  514. } else {
  515. for _, v := range dataList {
  516. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  517. if err != nil {
  518. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  519. return result, err
  520. }
  521. timestamp := dateTime.UnixNano() / 1e6
  522. item := new(EdbDataList)
  523. item.DataTime = dateTime.Format(utils.FormatDate)
  524. item.EdbInfoId = v.EdbInfoId
  525. item.Value = v.Value
  526. item.EdbDataId = v.EdbDataId
  527. item.DataTimestamp = timestamp
  528. items.Items = append(items.Items, item)
  529. }
  530. result.List = append(result.List, items)
  531. }
  532. }
  533. }
  534. return
  535. }
  536. // AddCalculateQuarterV4ByUniqueCode 指标季度数据计算(公历转农历)
  537. func AddCalculateQuarterV4ByUniqueCode(dataList []*EdbDataListByUniqueCode) (result *EdbDataResult, err error) {
  538. var errMsg string
  539. defer func() {
  540. if errMsg != "" {
  541. fmt.Println("errMsg:", errMsg)
  542. }
  543. }()
  544. endDate := dataList[len(dataList)-1].DataTime
  545. endDateForm, err := time.Parse(utils.FormatDate, endDate)
  546. if err != nil {
  547. return result, err
  548. }
  549. thisMonth := int(endDateForm.Month())
  550. result = new(EdbDataResult)
  551. var yearArr []int
  552. yearMap := make(map[int]int)
  553. var cureentDate time.Time
  554. if thisMonth < 11 {
  555. for k, v := range dataList {
  556. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  557. if err != nil {
  558. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  559. return result, err
  560. }
  561. if k == len(dataList)-1 {
  562. cureentDate = dateTime
  563. }
  564. year := dateTime.Year()
  565. if _, ok := yearMap[year]; !ok {
  566. yearArr = append(yearArr, year)
  567. }
  568. yearMap[year] = year
  569. }
  570. } else {
  571. for k, v := range dataList {
  572. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  573. if err != nil {
  574. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  575. return result, err
  576. }
  577. if k == len(dataList)-1 {
  578. cureentDate = dateTime
  579. }
  580. year := dateTime.Year() + 1
  581. if _, ok := yearMap[year]; !ok {
  582. yearArr = append(yearArr, year)
  583. }
  584. yearMap[year] = year
  585. }
  586. }
  587. //排序
  588. fmt.Println("yearArr:", yearArr)
  589. thisYear := cureentDate.Year()
  590. //thisMonth := int(cureentDate.Month())
  591. fmt.Println("thisMonth:", thisMonth)
  592. for ky, vy := range yearArr {
  593. fmt.Println("line 432:", ky, vy, thisYear, thisMonth)
  594. if thisMonth < 11 {
  595. currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
  596. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
  597. currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
  598. if err != nil {
  599. errMsg = "生成当前春节失败,Err:" + err.Error()
  600. return result, err
  601. }
  602. preYear := vy
  603. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  604. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  605. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  606. if err != nil {
  607. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  608. return result, err
  609. }
  610. day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  611. items := new(EdbDataItems)
  612. items.Year = preYear
  613. for _, v := range dataList {
  614. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  615. if err != nil {
  616. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  617. return result, err
  618. }
  619. newDate := dateTime.AddDate(0, 0, int(day))
  620. selectDateStr := strconv.Itoa(thisYear) + "-11" + "-30"
  621. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  622. if newDate.Before(selectDate) || newDate == selectDate {
  623. timestamp := newDate.UnixNano() / 1e6
  624. item := new(EdbDataList)
  625. item.DataTime = newDate.Format(utils.FormatDate)
  626. item.EdbInfoId = v.EdbInfoId
  627. item.Value = v.Value
  628. item.EdbDataId = v.EdbDataId
  629. item.DataTimestamp = timestamp
  630. items.Items = append(items.Items, item)
  631. }
  632. }
  633. result.List = append(result.List, items)
  634. } else {
  635. nextYear := thisYear + 1
  636. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  637. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  638. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  639. if err != nil {
  640. errMsg = "生成当前春节失败,Err:" + err.Error()
  641. return result, err
  642. }
  643. preYear := vy
  644. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  645. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  646. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  647. if err != nil {
  648. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  649. return result, err
  650. }
  651. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  652. fmt.Println("day:", day, nextYearCjglDate, preYearCjglDate)
  653. items := new(EdbDataItems)
  654. items.Year = preYear - 1
  655. fmt.Println("preYear:", preYear, "ky:", ky, "yearArrLen:", len(yearArr))
  656. if ky+1 < len(yearArr) {
  657. for _, v := range dataList {
  658. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  659. if err != nil {
  660. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  661. return result, err
  662. }
  663. newDate := dateTime.AddDate(0, 0, int(day))
  664. selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
  665. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  666. if newDate.Before(selectDate) || newDate == selectDate {
  667. timestamp := newDate.UnixNano() / 1e6
  668. item := new(EdbDataList)
  669. item.DataTime = newDate.Format(utils.FormatDate)
  670. item.EdbInfoId = v.EdbInfoId
  671. item.Value = v.Value
  672. item.EdbDataId = v.EdbDataId
  673. item.DataTimestamp = timestamp
  674. items.Items = append(items.Items, item)
  675. }
  676. }
  677. result.List = append(result.List, items)
  678. } else {
  679. for _, v := range dataList {
  680. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  681. if err != nil {
  682. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  683. return result, err
  684. }
  685. timestamp := dateTime.UnixNano() / 1e6
  686. item := new(EdbDataList)
  687. item.DataTime = dateTime.Format(utils.FormatDate)
  688. item.EdbInfoId = v.EdbInfoId
  689. item.Value = v.Value
  690. item.EdbDataId = v.EdbDataId
  691. item.DataTimestamp = timestamp
  692. items.Items = append(items.Items, item)
  693. }
  694. result.List = append(result.List, items)
  695. }
  696. }
  697. }
  698. return
  699. }
  700. type HzTestEdbdataResult struct {
  701. List []*HzTestEdbdataItems
  702. }
  703. type HzTestEdbdataItems struct {
  704. Items []*HzTestEdbdata
  705. }
  706. type HzTestEdbdata struct {
  707. TtradeCode string `orm:"column(TRADE_CODE)"`
  708. Dt time.Time `orm:"column(DT)"`
  709. Close float64 `orm:"column(CLOSE)"`
  710. ModifyTime time.Time `orm:"column(modify_time)"`
  711. DataTimestamp int64
  712. }
  713. func GetHzTestEdbdata() (list []*HzTestEdbdata, err error) {
  714. o := orm.NewOrm()
  715. sql := ` SELECT * FROM hz_test_edbdata WHERE TRADE_CODE=? ORDER BY DT DESC `
  716. _, err = o.Raw(sql, "ST_0000577845").QueryRows(&list)
  717. return
  718. }
  719. type EdbDataItems struct {
  720. Items []*EdbDataList
  721. Year int
  722. BetweenDay int `json:"-" description:"公历与农历之间相差的天数"`
  723. CuttingDataTimestamp int64 `description:"切割的时间戳"`
  724. }
  725. type EdbDataResult struct {
  726. List []*EdbDataItems
  727. }
  728. func AddCalculateQuarterV5(dataList []*EdbDataList) (result *EdbDataResult, err error) {
  729. var errMsg string
  730. defer func() {
  731. if errMsg != "" {
  732. fmt.Println("errMsg:", errMsg)
  733. }
  734. }()
  735. result = new(EdbDataResult)
  736. thisYear := time.Now().Year()
  737. endDate := dataList[len(dataList)-1].DataTime
  738. endDateForm, err := time.Parse(utils.FormatDate, endDate)
  739. if err != nil {
  740. return result, err
  741. }
  742. thisMonth := int(endDateForm.Month())
  743. fmt.Println(thisMonth)
  744. for i := 4; i > 0; i-- {
  745. if thisMonth < 11 {
  746. nextYear := thisYear
  747. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  748. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  749. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  750. if err != nil {
  751. errMsg = "生成当前春节失败,Err:" + err.Error()
  752. return result, err
  753. }
  754. preYearCjnl := strconv.Itoa(nextYear-i) + "-01-01" //之前年份春节农历
  755. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  756. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  757. if err != nil {
  758. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  759. return result, err
  760. }
  761. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  762. items := new(EdbDataItems)
  763. for _, v := range dataList {
  764. dataDate := v.DataTime
  765. dataDateForm, err := time.Parse(utils.FormatDate, dataDate)
  766. if err != nil {
  767. return result, err
  768. }
  769. newDate := dataDateForm.AddDate(0, 0, int(day))
  770. selectDateStr := strconv.Itoa(nextYear) + "-11" + "-30"
  771. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  772. if newDate.Before(selectDate) {
  773. timestamp := newDate.UnixNano() / 1e6
  774. item := new(EdbDataList)
  775. item.DataTime = newDate.Format(utils.FormatDate)
  776. item.EdbInfoId = v.EdbInfoId
  777. item.Value = v.Value
  778. item.EdbDataId = v.EdbDataId
  779. item.DataTimestamp = timestamp
  780. items.Items = append(items.Items, item)
  781. }
  782. }
  783. fmt.Println(nextYear, "年春节阳历", nextYearCjgl)
  784. fmt.Println(i, "年前春节", preYearCjnl)
  785. fmt.Println(i, "年前春节阳历", preYearCjgl)
  786. fmt.Println("与", nextYear, "年春节相差天数", day, "____________")
  787. result.List = append(result.List, items)
  788. } else {
  789. nextYear := thisYear + 1
  790. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  791. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  792. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  793. if err != nil {
  794. errMsg = "生成当前春节失败,Err:" + err.Error()
  795. return result, err
  796. }
  797. preYearCjnl := strconv.Itoa(nextYear-i) + "-01-01" //之前年份春节农历
  798. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  799. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  800. if err != nil {
  801. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  802. return result, err
  803. }
  804. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  805. items := new(EdbDataItems)
  806. for _, v := range dataList {
  807. dataDate := v.DataTime
  808. dataDateForm, err := time.Parse(utils.FormatDate, dataDate)
  809. if err != nil {
  810. return result, err
  811. }
  812. newDate := dataDateForm.AddDate(0, 0, int(day))
  813. selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
  814. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  815. if newDate.Before(selectDate) {
  816. timestamp := newDate.UnixNano() / 1e6
  817. item := new(EdbDataList)
  818. item.DataTime = newDate.Format(utils.FormatDate)
  819. item.EdbInfoId = v.EdbInfoId
  820. item.Value = v.Value
  821. item.EdbDataId = v.EdbDataId
  822. item.DataTimestamp = timestamp
  823. items.Items = append(items.Items, item)
  824. }
  825. }
  826. fmt.Println(nextYear, "年春节阳历", nextYearCjgl)
  827. fmt.Println(i, "年前春节", preYearCjnl)
  828. fmt.Println(i, "年前春节阳历", preYearCjgl)
  829. fmt.Println("与", nextYear, "年春节相差天数", day, "____________")
  830. result.List = append(result.List, items)
  831. }
  832. }
  833. //合并第五组数据
  834. items := new(EdbDataItems)
  835. for _, v := range dataList {
  836. dataDate := v.DataTime
  837. dataDateForm, err := time.Parse(utils.FormatDate, dataDate)
  838. if err != nil {
  839. return result, err
  840. }
  841. timestamp := dataDateForm.UnixNano() / 1e6
  842. item := new(EdbDataList)
  843. item.DataTime = v.DataTime
  844. item.EdbInfoId = v.EdbInfoId
  845. item.Value = v.Value
  846. item.EdbDataId = v.EdbDataId
  847. item.DataTimestamp = timestamp
  848. items.Items = append(items.Items, item)
  849. }
  850. result.List = append(result.List, items)
  851. return
  852. }