edb_data_business.go 20 KB

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