base_from_business.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. package models
  2. import (
  3. "eta/eta_index_lib/models/mgo"
  4. "eta/eta_index_lib/utils"
  5. "fmt"
  6. "go.mongodb.org/mongo-driver/bson"
  7. "time"
  8. "github.com/beego/beego/v2/client/orm"
  9. )
  10. // BaseFromBusinessIndex
  11. // @Description: 外部指标(商家系统)表
  12. type BaseFromBusinessIndex struct {
  13. BaseFromBusinessIndexId int64 `orm:"column(base_from_business_index_id);pk"`
  14. IndexCode string `description:"指标编码"`
  15. IndexName string `description:"指标名称"`
  16. Unit string `description:"单位"`
  17. Frequency string `description:"频度"`
  18. Source int `description:"数据来源"`
  19. SourceName string `description:"数据来源名称"`
  20. StartDate time.Time `description:"开始日期"`
  21. EndDate time.Time `description:"结束日期"`
  22. Remark string `description:"备注字段"`
  23. BaseModifyTime time.Time `description:"基础信息(名称,单位,频度)变更时间"`
  24. DataUpdateTime time.Time `description:"最近一次数据发生变化的时间"`
  25. CreateTime time.Time `description:"创建时间"`
  26. ModifyTime time.Time `description:"修改时间"`
  27. }
  28. // BaseFromBusinessIndexResp
  29. // @Description: 外部指标(商家系统)表
  30. type BaseFromBusinessIndexResp struct {
  31. IndexCode string `description:"指标编码"`
  32. IndexName string `description:"指标名称"`
  33. Unit string `description:"单位"`
  34. Frequency string `description:"频度"`
  35. SourceName string `description:"数据来源名称"`
  36. }
  37. // EdbBusinessSource
  38. // @Description: 自有数据(商家)指标来源
  39. type EdbBusinessSource struct {
  40. EdbBusinessSourceId int64 `orm:"column(edb_business_source_id);pk"`
  41. SourceName string `description:"来源名称"` // 来源名称
  42. CreateTime time.Time `description:"创建时间"` // 创建时间
  43. }
  44. // AddBusinessIndexReq
  45. // @Description: 添加外部指标(商家)请求
  46. type AddBusinessIndexReq struct {
  47. IndexCode string `description:"指标编码"`
  48. IndexName string `description:"指标名称"`
  49. Unit string `description:"单位"`
  50. Frequency string `description:"频度"`
  51. SourceName string `description:"数据来源名称"`
  52. Remark string `description:"备注字段"`
  53. DataList []AddBusinessDataReq `description:"指标数据"`
  54. }
  55. // AddBusinessDataReq
  56. // @Description: 外部指标(商家系统)数据
  57. type AddBusinessDataReq struct {
  58. Value float64 `description:"值"`
  59. Date string `description:"日期"`
  60. }
  61. // DelBusinessIndexReq
  62. // @Description: 删除外部指标(商家)请求
  63. type DelBusinessIndexReq struct {
  64. IndexCodeList []string `description:"指标编码"`
  65. }
  66. // DelBusinessIndexDataReq
  67. // @Description: 删除外部指标(商家)明细数据请求
  68. type DelBusinessIndexDataReq struct {
  69. IndexCode string `description:"指标编码"`
  70. StartDate string `description:"开始日期"`
  71. EndDate string `description:"结束日期"`
  72. }
  73. // GetIndexItem
  74. // @Description: 根据指标编码获取自有数据指标
  75. // @author: Roc
  76. // @receiver m
  77. // @datetime 2024-05-31 16:29:30
  78. // @param indexCode string
  79. // @return item *BaseFromBusinessIndex
  80. // @return err error
  81. func (m *BaseFromBusinessIndex) GetIndexItem(indexCode string) (item *BaseFromBusinessIndex, err error) {
  82. o := orm.NewOrm()
  83. sql := `SELECT * FROM base_from_business_index WHERE index_code = ? `
  84. err = o.Raw(sql, indexCode).QueryRow(&item)
  85. return
  86. }
  87. // GetIndexItemList
  88. // @Description: 根据指标编码列表获取自有数据指标列表
  89. // @author: Roc
  90. // @receiver m
  91. // @datetime 2024-05-31 16:29:12
  92. // @param indexCodeList []string
  93. // @return items []*BaseFromBusinessIndex
  94. // @return err error
  95. func (m *BaseFromBusinessIndex) GetIndexItemList(indexCodeList []string) (items []*BaseFromBusinessIndex, err error) {
  96. num := len(indexCodeList)
  97. if num <= 0 {
  98. return
  99. }
  100. o := orm.NewOrm()
  101. sql := `SELECT * FROM base_from_business_index WHERE index_code in (` + utils.GetOrmInReplace(num) + `) `
  102. _, err = o.Raw(sql, indexCodeList).QueryRows(&items)
  103. return
  104. }
  105. // DelIndexItemList
  106. // @Description: 根据指标编码列表删除自有数据指标
  107. // @author: Roc
  108. // @receiver m
  109. // @datetime 2024-05-31 16:36:52
  110. // @param indexCodeList []string
  111. // @return err error
  112. func (m *BaseFromBusinessIndex) DelIndexItemList(indexCodeList []string) (err error) {
  113. num := len(indexCodeList)
  114. if num <= 0 {
  115. return
  116. }
  117. o := orm.NewOrm()
  118. sql := `DELETE FROM base_from_business_index WHERE index_code in (` + utils.GetOrmInReplace(num) + `) `
  119. _, err = o.Raw(sql, indexCodeList).Exec()
  120. return
  121. }
  122. // GetMaxId
  123. // @Description: 获取自有数据库中的最大id
  124. // @author: Roc
  125. // @receiver m
  126. // @datetime 2024-05-31 13:11:34
  127. // @return maxId int
  128. // @return err error
  129. func (m *BaseFromBusinessIndex) GetMaxId() (maxId int, err error) {
  130. o := orm.NewOrm()
  131. sql := `SELECT max(base_from_business_index_id) id FROM base_from_business_index limit 1`
  132. err = o.Raw(sql).QueryRow(&maxId)
  133. return
  134. }
  135. // Add 新增
  136. func (m *BaseFromBusinessIndex) Add() (err error) {
  137. o := orm.NewOrm()
  138. lastId, err := o.Insert(m)
  139. if err != nil {
  140. return
  141. }
  142. m.BaseFromBusinessIndexId = lastId
  143. return
  144. }
  145. func (m *BaseFromBusinessIndex) Update(cols []string) (err error) {
  146. o := orm.NewOrm()
  147. _, err = o.Update(m, cols...)
  148. return
  149. }
  150. func (m *BaseFromBusinessIndex) Del() (err error) {
  151. o := orm.NewOrm()
  152. _, err = o.Delete(m)
  153. return
  154. }
  155. func (m *BaseFromBusinessIndex) UpdateIndex(item *BaseFromBusinessIndex, updateCols []string) (err error) {
  156. if item == nil {
  157. return
  158. }
  159. if len(updateCols) == 0 {
  160. return
  161. }
  162. o := orm.NewOrm()
  163. _, err = o.Update(item, updateCols...)
  164. return
  165. }
  166. // GetEdbBusinessSourceItem
  167. // @Description: 根据来源名称获取来源信息
  168. // @author: Roc
  169. // @receiver m
  170. // @datetime 2024-04-25 18:09:03
  171. // @param sourceName string
  172. // @return item *EdbBusinessSource
  173. // @return err error
  174. func (m *EdbBusinessSource) GetEdbBusinessSourceItem(sourceName string) (item *EdbBusinessSource, err error) {
  175. o := orm.NewOrm()
  176. sql := `SELECT * FROM edb_business_source WHERE source_name = ? `
  177. err = o.Raw(sql, sourceName).QueryRow(&item)
  178. return
  179. }
  180. // Add 新增
  181. func (m *EdbBusinessSource) Add() (err error) {
  182. o := orm.NewOrm()
  183. lastId, err := o.Insert(m)
  184. if err != nil {
  185. return
  186. }
  187. m.EdbBusinessSourceId = lastId
  188. return
  189. }
  190. // GetEdbInfoMaxAndMinInfo
  191. // @Description: 获取指标的最新数据记录信息
  192. // @author: Roc
  193. // @receiver m
  194. // @datetime 2024-07-02 14:50:50
  195. // @param edbCode string
  196. // @return item *EdbInfoMaxAndMinInfo
  197. // @return err error
  198. func (m BaseFromBusinessIndex) GetEdbInfoMaxAndMinInfo(edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  199. if utils.UseMongo {
  200. return m.getEdbInfoMaxAndMinInfoByMongo(edbCode)
  201. } else {
  202. return m.getEdbInfoMaxAndMinInfoByMysql(edbCode)
  203. }
  204. return
  205. }
  206. // getEdbInfoMaxAndMinInfoByMongo
  207. // @Description: 获取指标的最新数据记录信息(从mongo中获取)
  208. // @author: Roc
  209. // @receiver m
  210. // @datetime 2024-07-02 14:41:20
  211. // @param edbCode string
  212. // @return item *EdbInfoMaxAndMinInfo
  213. // @return err error
  214. func (m BaseFromBusinessIndex) getEdbInfoMaxAndMinInfoByMongo(edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  215. mogDataObj := new(mgo.BaseFromBusinessData)
  216. pipeline := []bson.M{
  217. {"$match": bson.M{"index_code": edbCode}},
  218. {"$group": bson.M{
  219. "_id": nil,
  220. "min_date": bson.M{"$min": "$data_time"},
  221. "max_date": bson.M{"$max": "$data_time"},
  222. "min_value": bson.M{"$min": "$value"},
  223. "max_value": bson.M{"$max": "$value"},
  224. }},
  225. {"$project": bson.M{"_id": 0}}, // 可选,如果不需要_id字段
  226. }
  227. result, err := mogDataObj.GetEdbInfoMaxAndMinInfo(pipeline)
  228. if err != nil {
  229. fmt.Println("BaseFromBusinessIndex GetEdbInfoMaxAndMinInfo Err:" + err.Error())
  230. return
  231. }
  232. if !result.MaxDate.IsZero() {
  233. whereQuery := bson.M{"index_code": edbCode, "data_time": result.MaxDate}
  234. selectParam := bson.D{{"value", 1}, {"_id", 0}}
  235. latestValue, tmpErr := mogDataObj.GetLatestValue(whereQuery, selectParam)
  236. if tmpErr != nil {
  237. err = tmpErr
  238. return
  239. }
  240. result.LatestValue = latestValue.Value
  241. result.EndValue = latestValue.Value
  242. }
  243. item = &EdbInfoMaxAndMinInfo{
  244. MinDate: result.MinDate.Format(utils.FormatDate),
  245. MaxDate: result.MaxDate.Format(utils.FormatDate),
  246. MinValue: result.MinValue,
  247. MaxValue: result.MaxValue,
  248. LatestValue: result.LatestValue,
  249. LatestDate: result.LatestDate.Format(utils.FormatDate),
  250. EndValue: result.EndValue,
  251. }
  252. return
  253. }
  254. // getEdbInfoMaxAndMinInfoByMysql
  255. // @Description: 获取指标的最新数据记录信息(从mysql中获取)
  256. // @author: Roc
  257. // @receiver m
  258. // @datetime 2024-07-02 14:49:58
  259. // @param edbCode string
  260. // @return item *EdbInfoMaxAndMinInfo
  261. // @return err error
  262. func (m BaseFromBusinessIndex) getEdbInfoMaxAndMinInfoByMysql(edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  263. dataObj := BaseFromBusinessData{}
  264. result, err := dataObj.GetEdbInfoMaxAndMinInfo(edbCode)
  265. if err != nil {
  266. return
  267. }
  268. item = &EdbInfoMaxAndMinInfo{
  269. MinDate: result.MinDate,
  270. MaxDate: result.MaxDate,
  271. MinValue: result.MinValue,
  272. MaxValue: result.MaxValue,
  273. LatestValue: result.LatestValue,
  274. LatestDate: result.LatestDate,
  275. EndValue: result.EndValue,
  276. }
  277. return
  278. }
  279. // ModifyIndexMaxAndMinInfo
  280. // @Description: 修改最大值和最小值信息
  281. // @author: Roc
  282. // @receiver m
  283. // @datetime 2024-05-06 14:07:46
  284. // @param indexCode string
  285. // @param item *EdbInfoMaxAndMinInfo
  286. // @param isIndexUpdateOrAdd bool
  287. // @return err error
  288. func (m *BaseFromBusinessIndex) ModifyIndexMaxAndMinInfo(indexCode string, item *EdbInfoMaxAndMinInfo, isIndexUpdateOrAdd bool) (err error) {
  289. o := orm.NewOrm()
  290. sql := ` UPDATE base_from_business_index SET start_date=?,end_date=?,latest_value=?,modify_time=NOW() `
  291. if isIndexUpdateOrAdd {
  292. sql += `,data_update_time=NOW() `
  293. }
  294. sql += ` WHERE index_code=?`
  295. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.LatestValue, indexCode).Exec()
  296. return
  297. }