edb_data_business.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. package models
  2. import (
  3. "eta_gn/eta_index_lib/global"
  4. "eta_gn/eta_index_lib/models/mgo"
  5. "eta_gn/eta_index_lib/utils"
  6. "fmt"
  7. "github.com/shopspring/decimal"
  8. "go.mongodb.org/mongo-driver/bson"
  9. "gorm.io/gorm"
  10. "strings"
  11. "time"
  12. )
  13. // Business 自有数据
  14. type Business struct {
  15. }
  16. // AddBaseParams
  17. // @Description: 基础指标的添加参数
  18. type AddBaseParams struct {
  19. EdbCode string `description:"指标编码"`
  20. EdbName string `description:"指标名称"`
  21. Unit string `description:"单位"`
  22. ClassifyId int `description:"所属分类"`
  23. SysUserId int `description:"用户id"`
  24. SysUserRealName string `description:"用户真实名称"`
  25. UniqueCode string `description:"编码"`
  26. }
  27. // EditBaseParams
  28. // @Description: 基础指标的修改参数
  29. type EditBaseParams struct {
  30. EdbCode string `description:"指标编码"`
  31. EdbName string `description:"指标名称"`
  32. EdbNameEn string `description:"指标名称(英文)"`
  33. Unit string `description:"单位"`
  34. UnitEn string `description:"单位(英文)"`
  35. ClassifyId int `description:"所属分类"`
  36. SysUserId int `description:"用户id"`
  37. SysUserRealName string `description:"用户真实名称"`
  38. UniqueCode string `description:"编码"`
  39. Lang string `description:"语言版本"`
  40. EdbInfo *EdbInfo `description:"指标信息"`
  41. }
  42. type RefreshBaseParams struct {
  43. EdbInfo *EdbInfo
  44. StartDate string
  45. EndDate string
  46. }
  47. // Add
  48. // @Description: 添加指标
  49. // @author: Roc
  50. // @receiver obj
  51. // @datetime 2024-04-30 17:35:14
  52. // @param params AddBaseParams
  53. // @param businessIndexItem *BaseFromBusinessIndex
  54. // @return edbInfo *EdbInfo
  55. // @return err error
  56. // @return errMsg string
  57. func (obj Business) Add(params AddBaseParams, businessIndexItem *BaseFromBusinessIndex) (edbInfo *EdbInfo, err error, errMsg string) {
  58. to := global.DEFAULT_DmSQL.Begin()
  59. defer func() {
  60. if err != nil {
  61. to.Rollback()
  62. } else {
  63. to.Commit()
  64. }
  65. }()
  66. edbInfo = new(EdbInfo)
  67. edbInfo.Source = obj.GetSource()
  68. edbInfo.SourceName = obj.GetSourceName()
  69. edbInfo.EdbCode = params.EdbCode
  70. edbInfo.EdbName = params.EdbName
  71. edbInfo.EdbNameSource = params.EdbName
  72. edbInfo.Frequency = businessIndexItem.Frequency
  73. edbInfo.Unit = params.Unit
  74. edbInfo.ClassifyId = params.ClassifyId
  75. edbInfo.SysUserId = params.SysUserId
  76. edbInfo.SysUserRealName = params.SysUserRealName
  77. edbInfo.CreateTime = time.Now()
  78. edbInfo.ModifyTime = time.Now()
  79. edbInfo.UniqueCode = params.UniqueCode
  80. edbInfo.CalculateFormula = ``
  81. edbInfo.EdbNameEn = params.EdbName
  82. edbInfo.UnitEn = params.Unit
  83. edbInfo.EdbType = obj.GetEdbType()
  84. edbInfo.SubSource = businessIndexItem.Source
  85. edbInfo.SubSourceName = businessIndexItem.SourceName
  86. edbInfo.Sort = GetAddEdbMaxSortByClassifyId(params.ClassifyId, utils.EDB_INFO_TYPE)
  87. tmpErr := to.Create(edbInfo).Error
  88. if tmpErr != nil {
  89. err = tmpErr
  90. return
  91. }
  92. // 更新数据
  93. err = obj.refresh(to, edbInfo, "")
  94. return
  95. }
  96. // Edit
  97. // @Description: 编辑指标
  98. // @author: Roc
  99. // @receiver obj
  100. // @datetime 2024-04-30 17:35:05
  101. // @param params EditBaseParams
  102. // @param businessIndexItem *BaseFromBusinessIndex
  103. // @return err error
  104. // @return errMsg string
  105. func (obj Business) Edit(params EditBaseParams, businessIndexItem *BaseFromBusinessIndex) (err error, errMsg string) {
  106. edbInfo := params.EdbInfo
  107. to := global.DEFAULT_DmSQL.Begin()
  108. defer func() {
  109. if err != nil {
  110. to.Rollback()
  111. } else {
  112. to.Commit()
  113. }
  114. }()
  115. //oldEdbInfo := *edbInfo
  116. //修改指标信息
  117. edbInfo.EdbName = params.EdbName
  118. edbInfo.EdbNameSource = params.EdbName
  119. edbInfo.Frequency = businessIndexItem.Frequency
  120. edbInfo.Unit = params.Unit
  121. edbInfo.ClassifyId = params.ClassifyId
  122. edbInfo.EdbNameEn = params.EdbNameEn
  123. edbInfo.UnitEn = params.UnitEn
  124. edbInfo.SubSource = businessIndexItem.Source
  125. edbInfo.SubSourceName = businessIndexItem.SourceName
  126. edbInfo.ModifyTime = time.Now()
  127. err = to.Model(edbInfo).Select([]string{"EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "CalculateFormula", "ModifyTime", "EdbNameEn", "UnitEn", "SubSource", "SubSourceName"}).Updates(edbInfo).Error
  128. if err != nil {
  129. return
  130. }
  131. //计算数据
  132. err = obj.refresh(to, edbInfo, "")
  133. return
  134. }
  135. // Refresh 刷新
  136. func (obj Business) Refresh(params RefreshBaseParams) (err error, errMsg string) {
  137. to := global.DEFAULT_DmSQL.Begin()
  138. defer func() {
  139. if err != nil {
  140. to.Rollback()
  141. } else {
  142. to.Commit()
  143. }
  144. }()
  145. // 计算数据
  146. err = obj.refresh(to, params.EdbInfo, params.StartDate)
  147. return
  148. }
  149. // GetSource 获取来源编码id
  150. func (obj Business) GetSource() int {
  151. return utils.DATA_SOURCE_BUSINESS
  152. }
  153. // GetSourceName 获取来源名称
  154. func (obj Business) GetSourceName() string {
  155. return utils.DATA_SOURCE_NAME_BUSINESS
  156. }
  157. // GetEdbType 获取指标类型
  158. func (obj Business) GetEdbType() int {
  159. return utils.DEFAULT_EDB_TYPE
  160. }
  161. func (obj Business) refresh(to *gorm.DB, edbInfo *EdbInfo, startDate string) (err error) {
  162. if utils.UseMongo {
  163. return obj.refreshByMongo(edbInfo, startDate)
  164. } else {
  165. return obj.refreshByMysql(to, edbInfo, startDate)
  166. }
  167. return
  168. }
  169. func (obj Business) refreshByMongo(edbInfo *EdbInfo, startDate string) (err error) {
  170. // 真实数据的最大日期 , 插入规则配置的日期
  171. var realDataMaxDate, edbDataInsertConfigDate time.Time
  172. var edbDataInsertConfig *EdbDataInsertConfig
  173. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  174. {
  175. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfo.EdbInfoId)
  176. if err != nil && err.Error() != utils.ErrNoRow() {
  177. return
  178. }
  179. if edbDataInsertConfig != nil {
  180. edbDataInsertConfigDate = edbDataInsertConfig.Date
  181. }
  182. }
  183. //获取已存在的所有数据
  184. baseDataList, err := obj.getBaseBusinessDataByMongo(edbInfo, startDate)
  185. //获取指标所有数据
  186. existDataList := make([]*mgo.EdbDataBusiness, 0)
  187. mogDataObj := new(mgo.EdbDataBusiness)
  188. {
  189. // 构建查询条件
  190. queryConditions := bson.M{
  191. "edb_code": edbInfo.EdbCode,
  192. }
  193. if startDate != `` {
  194. //获取已存在的所有数据
  195. startDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  196. if tmpErr != nil {
  197. err = tmpErr
  198. return
  199. }
  200. queryConditions["data_time"] = bson.M{"$gte": startDateTime}
  201. }
  202. existDataList, err = mogDataObj.GetAllDataList(queryConditions, []string{"data_time"})
  203. if err != nil {
  204. fmt.Println(obj.GetSourceName() + ",refresh err;getEdbDataBusinessList Err:" + err.Error())
  205. return
  206. }
  207. }
  208. existDataMap := make(map[string]*mgo.EdbDataBusiness)
  209. removeDataTimeMap := make(map[string]bool) //需要移除的日期数据
  210. for _, v := range existDataList {
  211. tmpDate := v.DataTime.Format(utils.FormatDate)
  212. existDataMap[tmpDate] = v
  213. removeDataTimeMap[tmpDate] = true
  214. }
  215. needAddDateMap := make(map[time.Time]int)
  216. // 待添加的数据集
  217. addDataList := make([]interface{}, 0)
  218. // 待更新的数据集
  219. updateDataList := make([]mgo.EdbDataBusiness, 0)
  220. for _, tmpData := range baseDataList {
  221. currDate := tmpData.DataTime
  222. currDateStr := currDate.Format(utils.FormatDate)
  223. // 当前的实际值
  224. saveValue := decimal.NewFromFloat(tmpData.Value).Round(4).String()
  225. // 下面代码主要目的是处理掉手动插入的数据判断
  226. {
  227. if realDataMaxDate.IsZero() || currDate.After(realDataMaxDate) {
  228. realDataMaxDate = currDate
  229. }
  230. if edbDataInsertConfigDate.IsZero() || currDate.Equal(edbDataInsertConfigDate) {
  231. isFindConfigDateRealData = true
  232. }
  233. }
  234. existData, ok := existDataMap[currDateStr]
  235. // 如果库中已经存在该数据的话,那么就进行值的变更操作
  236. if ok {
  237. // 已经入到指标库的值
  238. existValStr := decimal.NewFromFloat(existData.Value).Round(4).String()
  239. //校验待删除日期数据里面是否存在该元素,如果存在的话,那么移除该日期
  240. delete(removeDataTimeMap, currDateStr)
  241. if existValStr != saveValue {
  242. existData.Value = tmpData.Value
  243. updateDataList = append(updateDataList, *existData)
  244. }
  245. continue
  246. }
  247. // 库中不存在该日期的数据
  248. timestamp := currDate.UnixNano() / 1e6
  249. addDataList = append(addDataList, mgo.EdbDataBusiness{
  250. EdbInfoId: edbInfo.EdbInfoId,
  251. EdbCode: edbInfo.EdbCode,
  252. DataTime: currDate,
  253. Value: tmpData.Value,
  254. CreateTime: time.Now(),
  255. ModifyTime: time.Now(),
  256. DataTimestamp: timestamp,
  257. })
  258. needAddDateMap[currDate] = 1
  259. }
  260. // 入库
  261. {
  262. coll := mogDataObj.GetCollection()
  263. //删除已经不存在的指标数据(由于该指标当日的数据删除了)
  264. {
  265. removeDateList := make([]time.Time, 0)
  266. for dateTime := range removeDataTimeMap {
  267. //获取已存在的所有数据
  268. tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dateTime, time.Local)
  269. if tmpErr != nil {
  270. err = tmpErr
  271. return
  272. }
  273. removeDateList = append(removeDateList, tmpDateTime)
  274. }
  275. removeNum := len(removeDateList)
  276. if removeNum > 0 {
  277. err = mogDataObj.RemoveManyByColl(coll, bson.M{"edb_code": edbInfo.EdbCode, "data_time": bson.M{"$in": removeDateList}})
  278. if err != nil {
  279. fmt.Println("mogDataObj.RemoveMany() Err:" + err.Error())
  280. return
  281. }
  282. }
  283. }
  284. // 插入新数据
  285. if len(addDataList) > 0 {
  286. err = mogDataObj.BatchInsertDataByColl(coll, 500, addDataList)
  287. if err != nil {
  288. fmt.Println("mogDataObj.BatchInsertData() Err:" + err.Error())
  289. return
  290. }
  291. }
  292. // 修改历史数据
  293. if len(updateDataList) > 0 {
  294. for _, v := range updateDataList {
  295. err = mogDataObj.UpdateDataByColl(coll, bson.M{"_id": v.ID}, bson.M{"$set": bson.M{"value": v.Value, "modify_time": v.ModifyTime}})
  296. if err != nil {
  297. fmt.Println("mogDataObj.UpdateDataByColl:Err:" + err.Error())
  298. return
  299. }
  300. }
  301. }
  302. }
  303. // 处理手工数据补充的配置
  304. HandleConfigInsertEdbDataByMongo(realDataMaxDate, edbDataInsertConfig, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, existDataMap, isFindConfigDateRealData)
  305. return
  306. }
  307. func (obj Business) refreshByMysql(to *gorm.DB, edbInfo *EdbInfo, startDate string) (err error) {
  308. dataTableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
  309. edbInfoIdStr := fmt.Sprint(edbInfo.EdbInfoId)
  310. // 真实数据的最大日期 , 插入规则配置的日期
  311. var realDataMaxDate, edbDataInsertConfigDate time.Time
  312. var edbDataInsertConfig *EdbDataInsertConfig
  313. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  314. {
  315. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfo.EdbInfoId)
  316. if err != nil && err.Error() != utils.ErrNoRow() {
  317. return
  318. }
  319. if edbDataInsertConfig != nil {
  320. edbDataInsertConfigDate = edbDataInsertConfig.Date
  321. }
  322. }
  323. //获取已存在的所有数据
  324. baseDataList, err := obj.getBaseBusinessDataByMysql(edbInfo, startDate)
  325. //获取指标所有数据
  326. var existCondition string
  327. var existPars []interface{}
  328. existCondition += " AND edb_info_id=? "
  329. existPars = append(existPars, edbInfo.EdbInfoId)
  330. if startDate != "" {
  331. existCondition += " AND data_time>=? "
  332. existPars = append(existPars, startDate)
  333. }
  334. existList, err := GetEdbDataByCondition(edbInfo.Source, edbInfo.SubSource, existCondition, existPars)
  335. if err != nil {
  336. fmt.Println(obj.GetSourceName() + ",refreshByMysql err;getEdbDataBusinessList Err:" + err.Error())
  337. return err
  338. }
  339. existDataMap := make(map[string]*EdbInfoSearchData)
  340. removeDataTimeMap := make(map[string]bool) //需要移除的日期数据
  341. for _, v := range existList {
  342. existDataMap[v.DataTime] = v
  343. removeDataTimeMap[v.DataTime] = true
  344. }
  345. needAddDateMap := make(map[time.Time]int)
  346. // 待添加的数据集
  347. addSql := ` INSERT INTO ` + dataTableName + ` (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  348. var isAdd bool
  349. // 待更新的数据集
  350. updateDataList := make([]*EdbInfoSearchData, 0)
  351. for _, tmpData := range baseDataList {
  352. currDate := tmpData.DataTime
  353. currDateStr := currDate.Format(utils.FormatDate)
  354. // 当前的实际值
  355. saveValue := decimal.NewFromFloat(tmpData.Value).Round(4).String()
  356. // 下面代码主要目的是处理掉手动插入的数据判断
  357. {
  358. if realDataMaxDate.IsZero() || currDate.After(realDataMaxDate) {
  359. realDataMaxDate = currDate
  360. }
  361. if edbDataInsertConfigDate.IsZero() || currDate.Equal(edbDataInsertConfigDate) {
  362. isFindConfigDateRealData = true
  363. }
  364. }
  365. existData, ok := existDataMap[currDateStr]
  366. // 如果库中已经存在该数据的话,那么就进行值的变更操作
  367. if ok {
  368. // 已经入到指标库的值
  369. existValStr := decimal.NewFromFloat(existData.Value).Round(4).String()
  370. //校验待删除日期数据里面是否存在该元素,如果存在的话,那么移除该日期
  371. delete(removeDataTimeMap, currDateStr)
  372. if existValStr != saveValue {
  373. existData.Value = tmpData.Value
  374. updateDataList = append(updateDataList, existData)
  375. }
  376. continue
  377. }
  378. // 库中不存在该日期的数据
  379. timestamp := currDate.UnixNano() / 1e6
  380. needAddDateMap[currDate] = 1
  381. addSql += GetAddSql(edbInfoIdStr, edbInfo.EdbCode, currDateStr, fmt.Sprint(timestamp), saveValue)
  382. isAdd = true
  383. }
  384. //删除已经不存在的指标数据(由于该指标当日的数据删除了)
  385. {
  386. removeDateList := make([]string, 0)
  387. for dateTime := range removeDataTimeMap {
  388. removeDateList = append(removeDateList, dateTime)
  389. }
  390. removeNum := len(removeDateList)
  391. if removeNum > 0 {
  392. //如果拼接指标变更了,那么需要删除所有的指标数据
  393. sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (`+utils.GetOrmInReplace(removeNum)+`) `, dataTableName)
  394. err = to.Exec(sql, edbInfo.EdbInfoId, removeDateList).Error
  395. if err != nil {
  396. err = fmt.Errorf("删除自有数据的明细数据失败,Err:" + err.Error())
  397. return
  398. }
  399. }
  400. }
  401. if isAdd {
  402. addSql = strings.TrimRight(addSql, ",")
  403. err = to.Exec(addSql).Error
  404. if err != nil {
  405. fmt.Println("RefreshAllCalculate add Err", err.Error())
  406. return
  407. }
  408. }
  409. // 修改历史数据
  410. if len(updateDataList) > 0 {
  411. for _, v := range updateDataList {
  412. err = ModifyEdbDataById(edbInfo.Source, edbInfo.SubSource, v.EdbDataId, fmt.Sprint(v.Value))
  413. if err != nil {
  414. fmt.Println(obj.GetSourceName() + ",refreshByMysql:Err:" + err.Error())
  415. return err
  416. }
  417. }
  418. }
  419. // 处理手工数据补充的配置
  420. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, existDataMap, isFindConfigDateRealData)
  421. return
  422. }
  423. // GetEdbInfoMaxAndMinInfo 获取指标的最新数据记录信息
  424. func (obj Business) getEdbInfoMaxAndMinInfoByMongo(edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  425. mogDataObj := new(mgo.EdbDataBusiness)
  426. pipeline := []bson.M{
  427. {"$match": bson.M{"edb_code": edbCode}},
  428. {"$group": bson.M{
  429. "_id": nil,
  430. "min_date": bson.M{"$min": "$data_time"},
  431. "max_date": bson.M{"$max": "$data_time"},
  432. "min_value": bson.M{"$min": "$value"},
  433. "max_value": bson.M{"$max": "$value"},
  434. }},
  435. {"$project": bson.M{"_id": 0}}, // 可选,如果不需要_id字段
  436. }
  437. result, err := mogDataObj.GetEdbInfoMaxAndMinInfo(pipeline)
  438. if err != nil {
  439. fmt.Println("EdbDataBusiness getEdbDataBusinessList Err:" + err.Error())
  440. return
  441. }
  442. if !result.MaxDate.IsZero() {
  443. whereQuery := bson.M{"edb_code": edbCode, "data_time": result.MaxDate}
  444. selectParam := bson.D{{"value", 1}, {"_id", 0}}
  445. latestValue, tmpErr := mogDataObj.GetLatestValue(whereQuery, selectParam)
  446. if tmpErr != nil {
  447. err = tmpErr
  448. return
  449. }
  450. result.LatestValue = latestValue.Value
  451. result.EndValue = latestValue.Value
  452. }
  453. item = &EdbInfoMaxAndMinInfo{
  454. MinDate: result.MinDate.Format(utils.FormatDate),
  455. MaxDate: result.MaxDate.Format(utils.FormatDate),
  456. MinValue: result.MinValue,
  457. MaxValue: result.MaxValue,
  458. LatestValue: result.LatestValue,
  459. LatestDate: result.LatestDate.Format(utils.FormatDate),
  460. EndValue: result.EndValue,
  461. }
  462. return
  463. }
  464. // UnifiedModifyEdbInfoMaxAndMinInfo
  465. // @Description: 修改指标的最大最小值和最新值
  466. // @author: Roc
  467. // @receiver obj
  468. // @datetime 2024-04-30 17:07:35
  469. // @param edbInfo *EdbInfo
  470. // @return err error
  471. func (obj Business) UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo *EdbInfo) (err error) {
  472. if utils.UseMongo {
  473. edbInfoMaxAndMinInfo, tmpErr := obj.getEdbInfoMaxAndMinInfoByMongo(edbInfo.EdbCode)
  474. // 如果正常获取到了,那就去修改指标的最大最小值
  475. if tmpErr == nil && edbInfoMaxAndMinInfo != nil {
  476. err = ModifyEdbInfoMaxAndMinInfo(edbInfo.EdbInfoId, edbInfoMaxAndMinInfo)
  477. } else {
  478. // 清空的目的是为了避免异常返回
  479. err = nil
  480. }
  481. } else {
  482. err, _ = UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  483. }
  484. return
  485. }
  486. // EdbInfoMgoData
  487. // @Description: mgo里面的数据
  488. type EdbInfoMgoData struct {
  489. //EdbDataId primitive.ObjectID `description:"数据ID"`
  490. DataTime time.Time `description:"数据日期"`
  491. Value float64 `description:"数据"`
  492. EdbCode string `description:"指标编码"`
  493. }
  494. // getBaseBusinessData
  495. // @Description: 获取基础数据
  496. // @author: Roc
  497. // @receiver obj
  498. // @datetime 2024-04-30 11:10:46
  499. // @param edbInfo *EdbInfo
  500. // @param startDate string
  501. // @return newDataList []EdbInfoSearchData
  502. // @return err error
  503. func (obj Business) getBaseBusinessData(edbInfo *EdbInfo, startDate string) (newDataList []EdbInfoMgoData, err error) {
  504. return obj.getBaseBusinessDataByMongo(edbInfo, startDate)
  505. }
  506. // getBaseBusinessDataByMongo
  507. // @Description: 从mongo中获取基础的明细数据
  508. // @author: Roc
  509. // @receiver obj
  510. // @datetime 2024-07-02 10:12:02
  511. // @param edbInfo *EdbInfo
  512. // @param startDate string
  513. // @return newDataList []EdbInfoMgoData
  514. // @return err error
  515. func (obj Business) getBaseBusinessDataByMongo(edbInfo *EdbInfo, startDate string) (newDataList []EdbInfoMgoData, err error) {
  516. newDataList = make([]EdbInfoMgoData, 0)
  517. // 获取数据源的指标数据
  518. mogDataObj := new(mgo.BaseFromBusinessData)
  519. // 构建查询条件
  520. queryConditions := bson.M{
  521. "index_code": edbInfo.EdbCode,
  522. }
  523. if startDate != `` {
  524. //获取已存在的所有数据
  525. startDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  526. if tmpErr != nil {
  527. err = tmpErr
  528. return
  529. }
  530. queryConditions["data_time"] = bson.M{"$gte": startDateTime}
  531. }
  532. baseDataList, err := mogDataObj.GetAllDataList(queryConditions, []string{"data_time"})
  533. if err != nil {
  534. fmt.Println("getBaseBusinessData Err:" + err.Error())
  535. return
  536. }
  537. for _, v := range baseDataList {
  538. newDataList = append(newDataList, EdbInfoMgoData{
  539. //EdbDataId: v.ID,
  540. DataTime: v.DataTime,
  541. Value: v.Value,
  542. EdbCode: v.IndexCode,
  543. })
  544. }
  545. return
  546. }
  547. // getBaseBusinessDataByMysql
  548. // @Description: 从mysql中获取基础的明细数据
  549. // @author: Roc
  550. // @receiver obj
  551. // @datetime 2024-07-02 10:12:16
  552. // @param edbInfo *EdbInfo
  553. // @param startDate string
  554. // @return newDataList []EdbInfoMgoData
  555. // @return err error
  556. func (obj Business) getBaseBusinessDataByMysql(edbInfo *EdbInfo, startDate string) (newDataList []EdbInfoMgoData, err error) {
  557. newDataList = make([]EdbInfoMgoData, 0)
  558. // 获取数据源的指标数据
  559. baseBusinessDataObj := new(BaseFromBusinessData)
  560. // 构建查询条件
  561. var condition []string
  562. var pars []interface{}
  563. condition = append(condition, "index_code = ? ")
  564. pars = append(pars, edbInfo.EdbCode)
  565. if startDate != `` {
  566. condition = append(condition, " data_time >= ? ")
  567. pars = append(pars, startDate)
  568. }
  569. baseDataList, err := baseBusinessDataObj.GetAllDataList(condition, pars, " data_time ASC ")
  570. if err != nil {
  571. fmt.Println("getBaseBusinessData Err:" + err.Error())
  572. return
  573. }
  574. for _, v := range baseDataList {
  575. newDataList = append(newDataList, EdbInfoMgoData{
  576. DataTime: v.DataTime,
  577. Value: v.Value,
  578. EdbCode: v.IndexCode,
  579. })
  580. }
  581. return
  582. }