edb_data_business.go 20 KB

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