edb_data.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. package data
  2. import (
  3. "errors"
  4. "eta/eta_api/models"
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/models/mgo"
  7. "eta/eta_api/utils"
  8. "fmt"
  9. "github.com/shopspring/decimal"
  10. "go.mongodb.org/mongo-driver/bson"
  11. "time"
  12. )
  13. // EdbDataEdit 指标数据修改
  14. type EdbDataEdit struct {
  15. OldCreateDate string `description:"旧的录入日期"`
  16. CreateDate string `description:"新的录入日期"`
  17. TradeCode string `description:"指标唯一编码"`
  18. Close string `description:"录入值"`
  19. }
  20. // BatchEdbData 批量修改指标数据
  21. func BatchEdbData(oldCreateDate, createDate string, list []EdbDataEdit, opUserId int) (failEdbDataList []EdbDataEdit, err error) {
  22. if oldCreateDate == createDate { //修改
  23. err = errors.New("开始日期与结束日期一致!")
  24. return
  25. }
  26. for _, newEdbData := range list {
  27. edbdata := new(models.Edbdata)
  28. edbdata.TradeCode = newEdbData.TradeCode
  29. edbdata.Dt = newEdbData.CreateDate
  30. edbdata.Close = newEdbData.Close
  31. edbdata.ModifyTime = time.Now()
  32. item, err := models.GetDataInfo(newEdbData.TradeCode, createDate)
  33. if err != nil && err.Error() != utils.ErrNoRow() {
  34. failEdbDataList = append(failEdbDataList, newEdbData)
  35. continue
  36. }
  37. if item == nil {
  38. _, err = models.AddEdbdata(edbdata)
  39. if err != nil {
  40. failEdbDataList = append(failEdbDataList, newEdbData)
  41. continue
  42. }
  43. } else {
  44. err = models.EditEdbdata(edbdata)
  45. if err != nil {
  46. failEdbDataList = append(failEdbDataList, newEdbData)
  47. continue
  48. }
  49. }
  50. oldItem, err := models.GetDataInfo(newEdbData.TradeCode, oldCreateDate)
  51. if err != nil && err.Error() != utils.ErrNoRow() {
  52. failEdbDataList = append(failEdbDataList, newEdbData)
  53. continue
  54. }
  55. if oldItem != nil {
  56. record := new(models.EdbdataDeleteRecord)
  57. record.TradeCode = oldItem.TradeCode
  58. record.Dt = oldItem.Dt
  59. record.Close = oldItem.Close
  60. record.ModifyTime = oldItem.ModifyTime
  61. record.CreateTime = time.Now()
  62. record.SysUserId = opUserId
  63. _, err = models.AddEdbdataDeleteRecord(record)
  64. if err != nil {
  65. failEdbDataList = append(failEdbDataList, newEdbData)
  66. continue
  67. }
  68. //删除数据
  69. models.DeleteEdbData(newEdbData.TradeCode, newEdbData.OldCreateDate)
  70. }
  71. }
  72. return
  73. }
  74. // GetEdbDataTbz 获取指标的同比值数据
  75. func GetEdbDataTbz(edbInfo *data_manage.EdbInfo, tmpDataList []*data_manage.EdbDataList, startDateTime time.Time) (dataList []*data_manage.EdbDataList, minValue, maxValue float64, err error) {
  76. dataList = make([]*data_manage.EdbDataList, 0)
  77. // 数据处理
  78. var dateArr []string
  79. dataMap := make(map[string]*data_manage.EdbDataList)
  80. for _, v := range tmpDataList {
  81. dateArr = append(dateArr, v.DataTime)
  82. dataMap[v.DataTime] = v
  83. }
  84. for _, av := range dateArr {
  85. currentItem, ok := dataMap[av]
  86. // 如果找不到当前日期的数据,那么终止当前循环,进入下一循环
  87. if !ok {
  88. continue
  89. }
  90. tmpItem := *currentItem
  91. var isOk bool //是否计算出来结果
  92. //当前日期
  93. currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, av, time.Local)
  94. if tmpErr != nil {
  95. err = tmpErr
  96. return
  97. }
  98. // 如果存在开始日期,同时,当前日期早于开始日期,那么终止当前循环,进入下一循环
  99. if !startDateTime.IsZero() && currentDate.Before(startDateTime) {
  100. continue
  101. }
  102. //上一年的日期
  103. preDate := currentDate.AddDate(-1, 0, 0)
  104. preDateStr := preDate.Format(utils.FormatDate)
  105. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期找到
  106. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  107. isOk = true
  108. } else {
  109. if edbInfo.Frequency == "月度" { //向上和向下,各找一个月
  110. for i := 0; i <= 35; i++ {
  111. nextDateDay := preDate.AddDate(0, 0, i)
  112. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  113. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  114. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  115. isOk = true
  116. break
  117. } else {
  118. preDateDay := preDate.AddDate(0, 0, -i)
  119. preDateDayStr := preDateDay.Format(utils.FormatDate)
  120. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  121. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  122. isOk = true
  123. break
  124. }
  125. }
  126. }
  127. } else if edbInfo.Frequency == "季度" || edbInfo.Frequency == "年度" {
  128. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期->下一个月找到
  129. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  130. isOk = true
  131. break
  132. }
  133. } else {
  134. nextDateDay := preDate.AddDate(0, 0, 1)
  135. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  136. preDateDay := preDate.AddDate(0, 0, -1)
  137. preDateDayStr := preDateDay.Format(utils.FormatDate)
  138. for i := 0; i < 35; i++ {
  139. if i >= 1 {
  140. nextDateDay = nextDateDay.AddDate(0, 0, i)
  141. nextDateDayStr = nextDateDay.Format(utils.FormatDate)
  142. }
  143. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  144. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  145. isOk = true
  146. break
  147. } else {
  148. if i >= 1 {
  149. preDateDay = preDate.AddDate(0, 0, -i)
  150. preDateDayStr = nextDateDay.Format(utils.FormatDate)
  151. }
  152. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  153. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  154. isOk = true
  155. break
  156. }
  157. }
  158. }
  159. }
  160. }
  161. if isOk {
  162. if tmpItem.Value > maxValue {
  163. maxValue = tmpItem.Value
  164. }
  165. if tmpItem.Value < minValue {
  166. minValue = tmpItem.Value
  167. }
  168. dataList = append(dataList, &tmpItem)
  169. }
  170. }
  171. return
  172. }
  173. // GetChartEdbSeasonalData 获取指标的季节性数据
  174. func GetChartEdbSeasonalData(calendar, startDate, endDate string, edbInfo *data_manage.EdbInfo) (resultData interface{}, err error) {
  175. var startDateReal string
  176. startDateReal = startDate
  177. calendarPreYear := 0
  178. if calendar == "农历" {
  179. newStartDateReal, tmpErr := time.Parse(utils.FormatDate, startDateReal)
  180. if tmpErr != nil {
  181. err = tmpErr
  182. fmt.Println("time.Parse:" + err.Error())
  183. return
  184. }
  185. calendarPreYear = newStartDateReal.Year() - 1
  186. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  187. startDateReal = newStartDateReal.Format(utils.FormatDate)
  188. }
  189. dataList := make([]*data_manage.EdbDataList, 0)
  190. switch edbInfo.EdbInfoType {
  191. case 0:
  192. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, startDateReal, endDate)
  193. case 1:
  194. _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, startDateReal, endDate, false)
  195. default:
  196. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  197. }
  198. if err != nil {
  199. return
  200. }
  201. latestDateStr := edbInfo.LatestDate //实际数据的截止日期
  202. latestDate, tmpErr := time.Parse(utils.FormatDate, edbInfo.LatestDate)
  203. if tmpErr != nil {
  204. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + edbInfo.LatestDate))
  205. return
  206. }
  207. latestDateYear := latestDate.Year() //实际数据截止年份
  208. if calendar == "农历" {
  209. if len(dataList) <= 0 {
  210. resultData = new(data_manage.EdbDataResult)
  211. } else {
  212. result, tmpErr := data_manage.AddCalculateQuarterV4(dataList)
  213. if tmpErr != nil {
  214. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  215. return
  216. }
  217. // 处理季节图的截止日期
  218. for k, edbDataItems := range result.List {
  219. var cuttingDataTimestamp int64
  220. // 切割的日期时间字符串
  221. cuttingDataTimeStr := latestDate.AddDate(0, 0, edbDataItems.BetweenDay).Format(utils.FormatDate)
  222. //如果等于最后的实际日期,那么遍历找到该日期对应的时间戳,并将其赋值为 切割时间戳
  223. if edbDataItems.Year >= latestDateYear {
  224. for _, tmpData := range edbDataItems.Items {
  225. if tmpData.DataTime == cuttingDataTimeStr {
  226. cuttingDataTimestamp = tmpData.DataTimestamp
  227. break
  228. }
  229. }
  230. }
  231. edbDataItems.CuttingDataTimestamp = cuttingDataTimestamp
  232. result.List[k] = edbDataItems
  233. }
  234. if result.List[0].Year != calendarPreYear {
  235. itemList := make([]*data_manage.EdbDataList, 0)
  236. items := new(data_manage.EdbDataItems)
  237. //items.Year = calendarPreYear
  238. items.Items = itemList
  239. newResult := new(data_manage.EdbDataResult)
  240. newResult.List = append(newResult.List, items)
  241. newResult.List = append(newResult.List, result.List...)
  242. resultData = newResult
  243. } else {
  244. resultData = result
  245. }
  246. }
  247. } else {
  248. currentYear := time.Now().Year()
  249. quarterDataList := make([]*data_manage.QuarterData, 0)
  250. quarterMap := make(map[int][]*data_manage.EdbDataList)
  251. var quarterArr []int
  252. for _, v := range dataList {
  253. itemDate, tmpErr := time.Parse(utils.FormatDate, v.DataTime)
  254. if tmpErr != nil {
  255. err = errors.New("季度指标日期转换,Err:" + tmpErr.Error() + ";DataTime:" + v.DataTime)
  256. return
  257. }
  258. year := itemDate.Year()
  259. newItemDate := itemDate.AddDate(currentYear-year, 0, 0)
  260. timestamp := newItemDate.UnixNano() / 1e6
  261. v.DataTimestamp = timestamp
  262. if findVal, ok := quarterMap[year]; !ok {
  263. quarterArr = append(quarterArr, year)
  264. findVal = append(findVal, v)
  265. quarterMap[year] = findVal
  266. } else {
  267. findVal = append(findVal, v)
  268. quarterMap[year] = findVal
  269. }
  270. }
  271. for _, v := range quarterArr {
  272. itemList := quarterMap[v]
  273. quarterItem := new(data_manage.QuarterData)
  274. quarterItem.Year = v
  275. quarterItem.DataList = itemList
  276. //如果等于最后的实际日期,那么将切割时间戳记录
  277. if v == latestDateYear {
  278. var cuttingDataTimestamp int64
  279. for _, tmpData := range itemList {
  280. if tmpData.DataTime == latestDateStr {
  281. cuttingDataTimestamp = tmpData.DataTimestamp
  282. break
  283. }
  284. }
  285. quarterItem.CuttingDataTimestamp = cuttingDataTimestamp
  286. } else if v > latestDateYear {
  287. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  288. if len(itemList) > 0 {
  289. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  290. }
  291. }
  292. quarterDataList = append(quarterDataList, quarterItem)
  293. }
  294. resultData = quarterDataList
  295. }
  296. return
  297. }
  298. // TbzDiv 同比值计算
  299. func TbzDiv(a, b float64) float64 {
  300. var valFloat float64
  301. if b != 0 {
  302. af := decimal.NewFromFloat(a)
  303. bf := decimal.NewFromFloat(b)
  304. val, _ := af.Div(bf).Float64()
  305. val = val - 1
  306. valFloat, _ = decimal.NewFromFloat(val).Round(4).Float64()
  307. } else {
  308. valFloat = 0
  309. }
  310. return valFloat
  311. }
  312. // GetDateData 获取指标的单个日期的值
  313. func GetDateData(edbInfo *data_manage.EdbInfo, startDate, endDate string) (item *data_manage.EdbDataList, err error) {
  314. var dataList []*data_manage.EdbDataList
  315. switch edbInfo.EdbInfoType {
  316. case 0:
  317. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, startDate, endDate)
  318. case 1:
  319. _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, startDate, endDate, false)
  320. default:
  321. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  322. }
  323. if err != nil {
  324. return
  325. }
  326. if len(dataList) <= 0 {
  327. return
  328. }
  329. item = dataList[0]
  330. return
  331. }
  332. // GetEdbBeforeAndAfterDateData 获取指标的单个日期的值
  333. func GetEdbBeforeAndAfterDateData(targetDate string, dataList []*data_manage.EdbDataList) (list []*data_manage.EdbDataList, err error) {
  334. lenDataList := len(dataList)
  335. if lenDataList <= 0 {
  336. return
  337. }
  338. // 数组按照日期从小到大排序
  339. maxIndex := lenDataList - 1
  340. startIndex := 0
  341. endIndex := 0
  342. for index, v := range dataList {
  343. if v.DataTime == targetDate {
  344. if index+2 > maxIndex {
  345. endIndex = maxIndex
  346. } else if index+1 > maxIndex {
  347. endIndex = maxIndex
  348. } else {
  349. endIndex = index + 2
  350. }
  351. startIndex = endIndex - 4
  352. if startIndex < 0 {
  353. startIndex = 0
  354. }
  355. if endIndex == maxIndex {
  356. list = dataList[startIndex:]
  357. } else {
  358. list = dataList[startIndex : endIndex+1]
  359. }
  360. break
  361. }
  362. }
  363. return
  364. }
  365. // GetPageData
  366. // @Description: 获取指标的分页数据
  367. // @author: Roc
  368. // @datetime 2024-05-07 10:33:02
  369. // @param edbInfoId int
  370. // @param source int
  371. // @param subSource int
  372. // @param startSize int
  373. // @param pageSize int
  374. // @return dataCount int
  375. // @return dataList []*data_manage.EdbData
  376. // @return err error
  377. func GetPageData(edbInfoId, source, subSource int, endDataTime string, startSize, pageSize int) (dataCount int, dataList []*data_manage.EdbData, err error) {
  378. dataList = make([]*data_manage.EdbData, 0)
  379. // 自有数据需要额外处理(从mongo获取)
  380. if source == utils.DATA_SOURCE_BUSINESS && utils.UseMongo {
  381. return getPageDataByMongo(edbInfoId, source, subSource, endDataTime, startSize, pageSize)
  382. }
  383. if source == utils.DATA_SOURCE_THS && subSource == utils.DATA_SUB_SOURCE_HIGH_FREQUENCY && utils.UseMongo {
  384. return getThsHfPageDataByMongo(edbInfoId, source, subSource, endDataTime, startSize, pageSize)
  385. }
  386. // 默认走mysql
  387. return getPageDataByMysql(edbInfoId, source, subSource, endDataTime, startSize, pageSize)
  388. }
  389. // getPageDataByMysql
  390. // @Description: 从Mysql获取数据
  391. // @author: Roc
  392. // @datetime 2024-05-07 15:08:41
  393. // @param edbInfoId int
  394. // @param source int
  395. // @param subSource int
  396. // @param endDataTime string
  397. // @param startSize int
  398. // @param pageSize int
  399. // @return dataCount int
  400. // @return dataList []*data_manage.EdbData
  401. // @return err error
  402. func getPageDataByMysql(edbInfoId, source, subSource int, endDataTime string, startSize, pageSize int) (dataCount int, dataList []*data_manage.EdbData, err error) {
  403. dataList = make([]*data_manage.EdbData, 0)
  404. var dataCondition string
  405. var dataPars []interface{}
  406. dataCondition += ` AND edb_info_id=? `
  407. dataPars = append(dataPars, edbInfoId)
  408. // 结束日期
  409. if endDataTime != "" {
  410. dataCondition += ` AND data_time <= ? `
  411. dataPars = append(dataPars, endDataTime)
  412. }
  413. // 获取数据总量
  414. dataCount, err = data_manage.GetEdbDataCountByCondition(dataCondition, dataPars, source, subSource)
  415. if err != nil && err.Error() != utils.ErrNoRow() {
  416. return
  417. }
  418. // 获取列表数据
  419. dataList, err = data_manage.GetEdbDataListByCondition(dataCondition, dataPars, source, subSource, pageSize, startSize)
  420. if err != nil {
  421. return
  422. }
  423. return
  424. }
  425. // getPageDataByMongo
  426. // @Description: 从MongoDB获取数据
  427. // @author: Roc
  428. // @datetime 2024-05-07 15:06:42
  429. // @param edbInfoId int
  430. // @param source int
  431. // @param subSource int
  432. // @param endDataTime string
  433. // @param startSize int
  434. // @param pageSize int
  435. // @return dataCount int
  436. // @return dataList []*data_manage.EdbData
  437. // @return err error
  438. func getPageDataByMongo(edbInfoId, source, subSource int, endDataTime string, startSize, pageSize int) (dataCount int, dataList []*data_manage.EdbData, err error) {
  439. dataList = make([]*data_manage.EdbData, 0)
  440. mogDataObj := mgo.EdbDataBusiness{}
  441. // 构建查询条件
  442. queryConditions := bson.M{
  443. "edb_info_id": edbInfoId,
  444. }
  445. // 结束日期
  446. dateCondition, err := mgo.BuildDateCondition("", endDataTime)
  447. if err != nil {
  448. return
  449. }
  450. if len(dateCondition) > 0 {
  451. queryConditions["data_time"] = dateCondition
  452. }
  453. // 获取数据总量
  454. tmpCount, tmpErr := mogDataObj.GetCountDataList(queryConditions)
  455. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  456. err = tmpErr
  457. return
  458. }
  459. dataCount = int(tmpCount)
  460. // 获取列表数据
  461. tmpDataList, tmpErr := mogDataObj.GetPageDataList(queryConditions, int64(startSize), int64(pageSize), []string{"-data_time"})
  462. if tmpErr != nil {
  463. err = tmpErr
  464. return
  465. }
  466. for k, v := range tmpDataList {
  467. dataList = append(dataList, &data_manage.EdbData{
  468. EdbDataId: k + 1,
  469. EdbInfoId: v.EdbInfoId,
  470. DataTime: v.DataTime.Format(utils.FormatDate),
  471. Value: v.Value,
  472. })
  473. }
  474. return
  475. }
  476. // GetEdbDataTbzForSeason 获取指标的同比值数据
  477. func GetEdbDataTbzForSeason(frequency string, tmpDataList []*data_manage.EdbDataList, startDateTime time.Time) (dataList []*data_manage.EdbDataList, minValue, maxValue float64, err error) {
  478. dataList = make([]*data_manage.EdbDataList, 0)
  479. // 数据处理
  480. var dateArr []string
  481. dataMap := make(map[string]*data_manage.EdbDataList)
  482. for _, v := range tmpDataList {
  483. dateArr = append(dateArr, v.DataTime)
  484. dataMap[v.DataTime] = v
  485. }
  486. for _, av := range dateArr {
  487. currentItem, ok := dataMap[av]
  488. // 如果找不到当前日期的数据,那么终止当前循环,进入下一循环
  489. if !ok {
  490. continue
  491. }
  492. tmpItem := *currentItem
  493. var isOk bool //是否计算出来结果
  494. //当前日期
  495. currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, av, time.Local)
  496. if tmpErr != nil {
  497. err = tmpErr
  498. return
  499. }
  500. // 如果存在开始日期,同时,当前日期早于开始日期,那么终止当前循环,进入下一循环
  501. if !startDateTime.IsZero() && currentDate.Before(startDateTime) {
  502. continue
  503. }
  504. //上一年的日期
  505. preDate := currentDate.AddDate(-1, 0, 0)
  506. preDateStr := preDate.Format(utils.FormatDate)
  507. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期找到
  508. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  509. isOk = true
  510. } else {
  511. if frequency == "月度" { //向上和向下,各找一个月
  512. for i := 0; i <= 35; i++ {
  513. nextDateDay := preDate.AddDate(0, 0, i)
  514. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  515. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  516. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  517. isOk = true
  518. break
  519. } else {
  520. preDateDay := preDate.AddDate(0, 0, -i)
  521. preDateDayStr := preDateDay.Format(utils.FormatDate)
  522. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  523. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  524. isOk = true
  525. break
  526. }
  527. }
  528. }
  529. } else if frequency == "季度" || frequency == "年度" {
  530. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期->下一个月找到
  531. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  532. isOk = true
  533. break
  534. }
  535. } else {
  536. nextDateDay := preDate.AddDate(0, 0, 1)
  537. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  538. preDateDay := preDate.AddDate(0, 0, -1)
  539. preDateDayStr := preDateDay.Format(utils.FormatDate)
  540. for i := 0; i < 35; i++ {
  541. if i >= 1 {
  542. nextDateDay = nextDateDay.AddDate(0, 0, i)
  543. nextDateDayStr = nextDateDay.Format(utils.FormatDate)
  544. }
  545. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  546. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  547. isOk = true
  548. break
  549. } else {
  550. if i >= 1 {
  551. preDateDay = preDate.AddDate(0, 0, -i)
  552. preDateDayStr = nextDateDay.Format(utils.FormatDate)
  553. }
  554. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  555. tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
  556. isOk = true
  557. break
  558. }
  559. }
  560. }
  561. }
  562. }
  563. if isOk {
  564. if tmpItem.Value > maxValue {
  565. maxValue = tmpItem.Value
  566. }
  567. if tmpItem.Value < minValue {
  568. minValue = tmpItem.Value
  569. }
  570. dataList = append(dataList, &tmpItem)
  571. }
  572. }
  573. return
  574. }
  575. func getThsHfPageDataByMongo(edbInfoId, source, subSource int, endDataTime string, startSize, pageSize int) (dataCount int, dataList []*data_manage.EdbData, err error) {
  576. dataList = make([]*data_manage.EdbData, 0)
  577. mogDataObj := mgo.EdbDataThsHf{}
  578. // 构建查询条件
  579. queryConditions := bson.M{
  580. "edb_info_id": edbInfoId,
  581. }
  582. // 结束日期
  583. dateCondition, err := mgo.BuildDateCondition("", endDataTime)
  584. if err != nil {
  585. return
  586. }
  587. if len(dateCondition) > 0 {
  588. queryConditions["data_time"] = dateCondition
  589. }
  590. // 获取数据总量
  591. tmpCount, tmpErr := mogDataObj.GetCountDataList(queryConditions)
  592. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  593. err = tmpErr
  594. return
  595. }
  596. dataCount = int(tmpCount)
  597. // 获取列表数据
  598. tmpDataList, tmpErr := mogDataObj.GetPageDataList(queryConditions, int64(startSize), int64(pageSize), []string{"-data_time"})
  599. if tmpErr != nil {
  600. err = tmpErr
  601. return
  602. }
  603. for k, v := range tmpDataList {
  604. dataList = append(dataList, &data_manage.EdbData{
  605. EdbDataId: k + 1,
  606. EdbInfoId: v.EdbInfoId,
  607. DataTime: v.DataTime.Format(utils.FormatDate),
  608. Value: v.Value,
  609. })
  610. }
  611. return
  612. }