edb_data_quarter.go 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  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, utils.DATA_SUB_SOURCE_EDB, 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, utils.DATA_SUB_SOURCE_EDB, 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. fmt.Println("day:", day, currentYearCjglDate, preYearCjglDate)
  446. items := new(EdbDataItems)
  447. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  448. items.Year = preYear
  449. for _, v := range dataList {
  450. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  451. if err != nil {
  452. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  453. return result, err
  454. }
  455. newDate := dateTime.AddDate(0, 0, int(day))
  456. selectDateStr := strconv.Itoa(thisYear) + "-11" + "-30"
  457. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  458. if newDate.Before(selectDate) || newDate == selectDate {
  459. timestamp := newDate.UnixNano() / 1e6
  460. item := new(EdbDataList)
  461. item.DataTime = newDate.Format(utils.FormatDate)
  462. item.EdbInfoId = v.EdbInfoId
  463. item.Value = v.Value
  464. item.EdbDataId = v.EdbDataId
  465. item.DataTimestamp = timestamp
  466. items.Items = append(items.Items, item)
  467. }
  468. }
  469. result.List = append(result.List, items)
  470. } else {
  471. nextYear := thisYear + 1
  472. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  473. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  474. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  475. if err != nil {
  476. errMsg = "生成当前春节失败,Err:" + err.Error()
  477. return result, err
  478. }
  479. preYear := vy
  480. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  481. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  482. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  483. if err != nil {
  484. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  485. return result, err
  486. }
  487. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  488. fmt.Println("day:", day, nextYearCjglDate, preYearCjglDate)
  489. items := new(EdbDataItems)
  490. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  491. items.Year = preYear - 1
  492. fmt.Println("preYear:", preYear, "ky:", ky, "yearArrLen:", len(yearArr))
  493. if ky+1 < len(yearArr) {
  494. for _, v := range dataList {
  495. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  496. if err != nil {
  497. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  498. return result, err
  499. }
  500. newDate := dateTime.AddDate(0, 0, int(day))
  501. selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
  502. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  503. if newDate.Before(selectDate) || newDate == selectDate {
  504. timestamp := newDate.UnixNano() / 1e6
  505. item := new(EdbDataList)
  506. item.DataTime = newDate.Format(utils.FormatDate)
  507. item.EdbInfoId = v.EdbInfoId
  508. item.Value = v.Value
  509. item.EdbDataId = v.EdbDataId
  510. item.DataTimestamp = timestamp
  511. items.Items = append(items.Items, item)
  512. }
  513. }
  514. result.List = append(result.List, items)
  515. } else {
  516. for _, v := range dataList {
  517. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  518. if err != nil {
  519. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  520. return result, err
  521. }
  522. timestamp := dateTime.UnixNano() / 1e6
  523. item := new(EdbDataList)
  524. item.DataTime = dateTime.Format(utils.FormatDate)
  525. item.EdbInfoId = v.EdbInfoId
  526. item.Value = v.Value
  527. item.EdbDataId = v.EdbDataId
  528. item.DataTimestamp = timestamp
  529. items.Items = append(items.Items, item)
  530. }
  531. result.List = append(result.List, items)
  532. }
  533. }
  534. }
  535. return
  536. }
  537. // AddCalculateQuarterV6 指标季度数据计算(季节性图表)
  538. func AddCalculateQuarterV6(dataList []*EdbDataList) (result *EdbDataResult, err error) {
  539. var errMsg string
  540. defer func() {
  541. if errMsg != "" {
  542. fmt.Println("errMsg:", errMsg)
  543. }
  544. }()
  545. endDate := dataList[len(dataList)-1].DataTime
  546. endDateForm, err := time.Parse(utils.FormatDate, endDate)
  547. if err != nil {
  548. return result, err
  549. }
  550. thisMonth := int(endDateForm.Month())
  551. result = new(EdbDataResult)
  552. var yearArr []int
  553. yearMap := make(map[int]int)
  554. var cureentDate time.Time
  555. if thisMonth < 11 {
  556. for k, v := range dataList {
  557. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  558. if err != nil {
  559. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  560. return result, err
  561. }
  562. if k == len(dataList)-1 {
  563. cureentDate = dateTime
  564. }
  565. year := dateTime.Year()
  566. if _, ok := yearMap[year]; !ok {
  567. yearArr = append(yearArr, year)
  568. }
  569. yearMap[year] = year
  570. }
  571. } else {
  572. for k, v := range dataList {
  573. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  574. if err != nil {
  575. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  576. return result, err
  577. }
  578. if k == len(dataList)-1 {
  579. cureentDate = dateTime
  580. }
  581. year := dateTime.Year() + 1
  582. if _, ok := yearMap[year]; !ok {
  583. yearArr = append(yearArr, year)
  584. }
  585. yearMap[year] = year
  586. }
  587. }
  588. //排序
  589. fmt.Println("yearArr:", yearArr)
  590. thisYear := cureentDate.Year()
  591. //thisMonth := int(cureentDate.Month())
  592. fmt.Println("thisMonth:", thisMonth)
  593. for ky, vy := range yearArr {
  594. fmt.Printf("line 432:ky:%d, vy:%d, thisYear:%d, thisMonth:%d", ky, vy, thisYear, thisMonth)
  595. fmt.Println("")
  596. if thisMonth < 11 {
  597. currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
  598. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
  599. currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
  600. if err != nil {
  601. errMsg = "生成当前春节失败,Err:" + err.Error()
  602. return result, err
  603. }
  604. preYear := vy
  605. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  606. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  607. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  608. if err != nil {
  609. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  610. return result, err
  611. }
  612. day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  613. fmt.Println("day:", day, currentYearCjglDate, preYearCjglDate)
  614. items := new(EdbDataItems)
  615. items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  616. items.Year = preYear
  617. for _, v := range dataList {
  618. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  619. if err != nil {
  620. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  621. return result, err
  622. }
  623. newDate := dateTime.AddDate(0, 0, int(day))
  624. timestamp := newDate.UnixNano() / 1e6
  625. item := new(EdbDataList)
  626. item.DataTime = newDate.Format(utils.FormatDate)
  627. item.EdbInfoId = v.EdbInfoId
  628. item.Value = v.Value
  629. item.EdbDataId = v.EdbDataId
  630. item.DataTimestamp = timestamp
  631. items.Items = append(items.Items, item)
  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.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
  655. items.Year = preYear
  656. fmt.Println("preYear:", preYear, "ky:", ky, "yearArrLen:", len(yearArr))
  657. //if ky+1 < len(yearArr) {
  658. for _, v := range dataList {
  659. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  660. if err != nil {
  661. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  662. return result, err
  663. }
  664. newDate := dateTime.AddDate(0, 0, int(day))
  665. timestamp := newDate.UnixNano() / 1e6
  666. item := new(EdbDataList)
  667. item.DataTime = newDate.Format(utils.FormatDate)
  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. /*} else {
  676. for _, v := range dataList {
  677. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  678. if err != nil {
  679. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  680. return result, err
  681. }
  682. timestamp := dateTime.UnixNano() / 1e6
  683. item := new(EdbDataList)
  684. item.DataTime = dateTime.Format(utils.FormatDate)
  685. item.EdbInfoId = v.EdbInfoId
  686. item.Value = v.Value
  687. item.EdbDataId = v.EdbDataId
  688. item.DataTimestamp = timestamp
  689. items.Items = append(items.Items, item)
  690. }
  691. result.List = append(result.List, items)
  692. }*/
  693. }
  694. }
  695. return
  696. }
  697. // AddCalculateQuarterV4ByUniqueCode 指标季度数据计算(公历转农历)
  698. func AddCalculateQuarterV4ByUniqueCode(dataList []*EdbDataListByUniqueCode) (result *EdbDataResult, err error) {
  699. var errMsg string
  700. defer func() {
  701. if errMsg != "" {
  702. fmt.Println("errMsg:", errMsg)
  703. }
  704. }()
  705. endDate := dataList[len(dataList)-1].DataTime
  706. endDateForm, err := time.Parse(utils.FormatDate, endDate)
  707. if err != nil {
  708. return result, err
  709. }
  710. thisMonth := int(endDateForm.Month())
  711. result = new(EdbDataResult)
  712. var yearArr []int
  713. yearMap := make(map[int]int)
  714. var cureentDate time.Time
  715. if thisMonth < 11 {
  716. for k, v := range dataList {
  717. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  718. if err != nil {
  719. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  720. return result, err
  721. }
  722. if k == len(dataList)-1 {
  723. cureentDate = dateTime
  724. }
  725. year := dateTime.Year()
  726. if _, ok := yearMap[year]; !ok {
  727. yearArr = append(yearArr, year)
  728. }
  729. yearMap[year] = year
  730. }
  731. } else {
  732. for k, v := range dataList {
  733. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  734. if err != nil {
  735. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  736. return result, err
  737. }
  738. if k == len(dataList)-1 {
  739. cureentDate = dateTime
  740. }
  741. year := dateTime.Year() + 1
  742. if _, ok := yearMap[year]; !ok {
  743. yearArr = append(yearArr, year)
  744. }
  745. yearMap[year] = year
  746. }
  747. }
  748. //排序
  749. fmt.Println("yearArr:", yearArr)
  750. thisYear := cureentDate.Year()
  751. //thisMonth := int(cureentDate.Month())
  752. fmt.Println("thisMonth:", thisMonth)
  753. for ky, vy := range yearArr {
  754. fmt.Println("line 432:", ky, vy, thisYear, thisMonth)
  755. if thisMonth < 11 {
  756. currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
  757. currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
  758. currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
  759. if err != nil {
  760. errMsg = "生成当前春节失败,Err:" + err.Error()
  761. return result, err
  762. }
  763. preYear := vy
  764. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  765. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  766. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  767. if err != nil {
  768. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  769. return result, err
  770. }
  771. day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  772. items := new(EdbDataItems)
  773. items.Year = preYear
  774. for _, v := range dataList {
  775. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  776. if err != nil {
  777. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  778. return result, err
  779. }
  780. newDate := dateTime.AddDate(0, 0, int(day))
  781. selectDateStr := strconv.Itoa(thisYear) + "-11" + "-30"
  782. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  783. if newDate.Before(selectDate) || newDate == selectDate {
  784. timestamp := newDate.UnixNano() / 1e6
  785. item := new(EdbDataList)
  786. item.DataTime = newDate.Format(utils.FormatDate)
  787. item.EdbInfoId = v.EdbInfoId
  788. item.Value = v.Value
  789. item.EdbDataId = v.EdbDataId
  790. item.DataTimestamp = timestamp
  791. items.Items = append(items.Items, item)
  792. }
  793. }
  794. result.List = append(result.List, items)
  795. } else {
  796. nextYear := thisYear + 1
  797. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  798. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  799. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  800. if err != nil {
  801. errMsg = "生成当前春节失败,Err:" + err.Error()
  802. return result, err
  803. }
  804. preYear := vy
  805. preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
  806. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  807. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  808. if err != nil {
  809. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  810. return result, err
  811. }
  812. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  813. fmt.Println("day:", day, nextYearCjglDate, preYearCjglDate)
  814. items := new(EdbDataItems)
  815. items.Year = preYear - 1
  816. fmt.Println("preYear:", preYear, "ky:", ky, "yearArrLen:", len(yearArr))
  817. if ky+1 < len(yearArr) {
  818. for _, v := range dataList {
  819. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  820. if err != nil {
  821. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  822. return result, err
  823. }
  824. newDate := dateTime.AddDate(0, 0, int(day))
  825. selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
  826. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  827. if newDate.Before(selectDate) || newDate == selectDate {
  828. timestamp := newDate.UnixNano() / 1e6
  829. item := new(EdbDataList)
  830. item.DataTime = newDate.Format(utils.FormatDate)
  831. item.EdbInfoId = v.EdbInfoId
  832. item.Value = v.Value
  833. item.EdbDataId = v.EdbDataId
  834. item.DataTimestamp = timestamp
  835. items.Items = append(items.Items, item)
  836. }
  837. }
  838. result.List = append(result.List, items)
  839. } else {
  840. for _, v := range dataList {
  841. dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
  842. if err != nil {
  843. errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
  844. return result, err
  845. }
  846. timestamp := dateTime.UnixNano() / 1e6
  847. item := new(EdbDataList)
  848. item.DataTime = dateTime.Format(utils.FormatDate)
  849. item.EdbInfoId = v.EdbInfoId
  850. item.Value = v.Value
  851. item.EdbDataId = v.EdbDataId
  852. item.DataTimestamp = timestamp
  853. items.Items = append(items.Items, item)
  854. }
  855. result.List = append(result.List, items)
  856. }
  857. }
  858. }
  859. return
  860. }
  861. type HzTestEdbdataResult struct {
  862. List []*HzTestEdbdataItems
  863. }
  864. type HzTestEdbdataItems struct {
  865. Items []*HzTestEdbdata
  866. }
  867. type HzTestEdbdata struct {
  868. TtradeCode string `orm:"column(TRADE_CODE)"`
  869. Dt time.Time `orm:"column(DT)"`
  870. Close float64 `orm:"column(CLOSE)"`
  871. ModifyTime time.Time `orm:"column(modify_time)"`
  872. DataTimestamp int64
  873. }
  874. func GetHzTestEdbdata() (list []*HzTestEdbdata, err error) {
  875. o := orm.NewOrm()
  876. sql := ` SELECT * FROM hz_test_edbdata WHERE TRADE_CODE=? ORDER BY DT DESC `
  877. _, err = o.Raw(sql, "ST_0000577845").QueryRows(&list)
  878. return
  879. }
  880. type EdbDataItems struct {
  881. Items []*EdbDataList
  882. Year int
  883. BetweenDay int `json:"-" description:"公历与农历之间相差的天数"`
  884. CuttingDataTimestamp int64 `description:"切割的时间戳"`
  885. }
  886. type EdbDataResult struct {
  887. List []*EdbDataItems
  888. }
  889. func AddCalculateQuarterV5(dataList []*EdbDataList) (result *EdbDataResult, err error) {
  890. var errMsg string
  891. defer func() {
  892. if errMsg != "" {
  893. fmt.Println("errMsg:", errMsg)
  894. }
  895. }()
  896. result = new(EdbDataResult)
  897. thisYear := time.Now().Year()
  898. endDate := dataList[len(dataList)-1].DataTime
  899. endDateForm, err := time.Parse(utils.FormatDate, endDate)
  900. if err != nil {
  901. return result, err
  902. }
  903. thisMonth := int(endDateForm.Month())
  904. fmt.Println(thisMonth)
  905. for i := 4; i > 0; i-- {
  906. if thisMonth < 11 {
  907. nextYear := thisYear
  908. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  909. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  910. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  911. if err != nil {
  912. errMsg = "生成当前春节失败,Err:" + err.Error()
  913. return result, err
  914. }
  915. preYearCjnl := strconv.Itoa(nextYear-i) + "-01-01" //之前年份春节农历
  916. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  917. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  918. if err != nil {
  919. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  920. return result, err
  921. }
  922. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  923. items := new(EdbDataItems)
  924. for _, v := range dataList {
  925. dataDate := v.DataTime
  926. dataDateForm, err := time.Parse(utils.FormatDate, dataDate)
  927. if err != nil {
  928. return result, err
  929. }
  930. newDate := dataDateForm.AddDate(0, 0, int(day))
  931. selectDateStr := strconv.Itoa(nextYear) + "-11" + "-30"
  932. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  933. if newDate.Before(selectDate) {
  934. timestamp := newDate.UnixNano() / 1e6
  935. item := new(EdbDataList)
  936. item.DataTime = newDate.Format(utils.FormatDate)
  937. item.EdbInfoId = v.EdbInfoId
  938. item.Value = v.Value
  939. item.EdbDataId = v.EdbDataId
  940. item.DataTimestamp = timestamp
  941. items.Items = append(items.Items, item)
  942. }
  943. }
  944. fmt.Println(nextYear, "年春节阳历", nextYearCjgl)
  945. fmt.Println(i, "年前春节", preYearCjnl)
  946. fmt.Println(i, "年前春节阳历", preYearCjgl)
  947. fmt.Println("与", nextYear, "年春节相差天数", day, "____________")
  948. result.List = append(result.List, items)
  949. } else {
  950. nextYear := thisYear + 1
  951. nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
  952. nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
  953. nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
  954. if err != nil {
  955. errMsg = "生成当前春节失败,Err:" + err.Error()
  956. return result, err
  957. }
  958. preYearCjnl := strconv.Itoa(nextYear-i) + "-01-01" //之前年份春节农历
  959. preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
  960. preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
  961. if err != nil {
  962. errMsg = "生成历史年份春节失败,Err:" + err.Error()
  963. return result, err
  964. }
  965. day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
  966. items := new(EdbDataItems)
  967. for _, v := range dataList {
  968. dataDate := v.DataTime
  969. dataDateForm, err := time.Parse(utils.FormatDate, dataDate)
  970. if err != nil {
  971. return result, err
  972. }
  973. newDate := dataDateForm.AddDate(0, 0, int(day))
  974. selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
  975. selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
  976. if newDate.Before(selectDate) {
  977. timestamp := newDate.UnixNano() / 1e6
  978. item := new(EdbDataList)
  979. item.DataTime = newDate.Format(utils.FormatDate)
  980. item.EdbInfoId = v.EdbInfoId
  981. item.Value = v.Value
  982. item.EdbDataId = v.EdbDataId
  983. item.DataTimestamp = timestamp
  984. items.Items = append(items.Items, item)
  985. }
  986. }
  987. fmt.Println(nextYear, "年春节阳历", nextYearCjgl)
  988. fmt.Println(i, "年前春节", preYearCjnl)
  989. fmt.Println(i, "年前春节阳历", preYearCjgl)
  990. fmt.Println("与", nextYear, "年春节相差天数", day, "____________")
  991. result.List = append(result.List, items)
  992. }
  993. }
  994. //合并第五组数据
  995. items := new(EdbDataItems)
  996. for _, v := range dataList {
  997. dataDate := v.DataTime
  998. dataDateForm, err := time.Parse(utils.FormatDate, dataDate)
  999. if err != nil {
  1000. return result, err
  1001. }
  1002. timestamp := dataDateForm.UnixNano() / 1e6
  1003. item := new(EdbDataList)
  1004. item.DataTime = v.DataTime
  1005. item.EdbInfoId = v.EdbInfoId
  1006. item.Value = v.Value
  1007. item.EdbDataId = v.EdbDataId
  1008. item.DataTimestamp = timestamp
  1009. items.Items = append(items.Items, item)
  1010. }
  1011. result.List = append(result.List, items)
  1012. return
  1013. }