edb_data_quarter.go 29 KB

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