base_from_business.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. // EdbBusinessSource
  29. // @Description: 自有数据(商家)指标来源
  30. type EdbBusinessSource struct {
  31. EdbBusinessSourceId int64 `orm:"column(edb_business_source_id);pk"`
  32. SourceName string `description:"来源名称"` // 来源名称
  33. CreateTime time.Time `description:"创建时间"` // 创建时间
  34. }
  35. // AddBusinessIndexReq
  36. // @Description: 添加外部指标(商家)请求
  37. type AddBusinessIndexReq struct {
  38. IndexCode string `description:"指标编码"`
  39. IndexName string `description:"指标名称"`
  40. Unit string `description:"单位"`
  41. Frequency string `description:"频度"`
  42. SourceName string `description:"数据来源名称"`
  43. Remark string `description:"备注字段"`
  44. DataList []AddBusinessDataReq `description:"指标数据"`
  45. }
  46. // AddBusinessDataReq
  47. // @Description: 外部指标(商家系统)数据
  48. type AddBusinessDataReq struct {
  49. Value float64 `description:"值"`
  50. Date string `description:"日期"`
  51. }
  52. func (m *BaseFromBusinessIndex) GetIndexItem(indexCode string) (item *BaseFromBusinessIndex, err error) {
  53. o := orm.NewOrm()
  54. sql := `SELECT * FROM base_from_business_index WHERE index_code = ? `
  55. err = o.Raw(sql, indexCode).QueryRow(&item)
  56. return
  57. }
  58. func (m *BaseFromBusinessIndex) GetIndexCreate(terminalCode string) (items []*BaseFromBusinessIndex, err error) {
  59. o := orm.NewOrm()
  60. endTime := time.Now().Add(-2 * time.Minute).Format(utils.FormatDateTime)
  61. sql := `SELECT * FROM base_from_business_index WHERE index_name = '' AND create_time <= ? AND terminal_code = ? `
  62. _, err = o.Raw(sql, endTime, terminalCode).QueryRows(&items)
  63. return
  64. }
  65. // Add 新增
  66. func (m *BaseFromBusinessIndex) Add() (err error) {
  67. o := orm.NewOrm()
  68. lastId, err := o.Insert(m)
  69. if err != nil {
  70. return
  71. }
  72. m.BaseFromBusinessIndexId = lastId
  73. return
  74. }
  75. func (m *BaseFromBusinessIndex) Update(cols []string) (err error) {
  76. o := orm.NewOrm()
  77. _, err = o.Update(m, cols...)
  78. return
  79. }
  80. func (m *BaseFromBusinessIndex) UpdateIndex(item *BaseFromBusinessIndex, updateCols []string) (err error) {
  81. if item == nil {
  82. return
  83. }
  84. if len(updateCols) == 0 {
  85. return
  86. }
  87. o := orm.NewOrm()
  88. _, err = o.Update(item, updateCols...)
  89. return
  90. }
  91. // GetEdbBusinessSourceItem
  92. // @Description: 根据来源名称获取来源信息
  93. // @author: Roc
  94. // @receiver m
  95. // @datetime 2024-04-25 18:09:03
  96. // @param sourceName string
  97. // @return item *EdbBusinessSource
  98. // @return err error
  99. func (m *EdbBusinessSource) GetEdbBusinessSourceItem(sourceName string) (item *EdbBusinessSource, err error) {
  100. o := orm.NewOrm()
  101. sql := `SELECT * FROM edb_business_source WHERE source_name = ? `
  102. err = o.Raw(sql, sourceName).QueryRow(&item)
  103. return
  104. }
  105. // Add 新增
  106. func (m *EdbBusinessSource) Add() (err error) {
  107. o := orm.NewOrm()
  108. lastId, err := o.Insert(m)
  109. if err != nil {
  110. return
  111. }
  112. m.EdbBusinessSourceId = lastId
  113. return
  114. }
  115. // GetEdbInfoMaxAndMinInfo 获取指标的最新数据记录信息
  116. func (m BaseFromBusinessIndex) GetEdbInfoMaxAndMinInfo(edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  117. mogDataObj := new(mgo.BaseFromBusinessData)
  118. pipeline := []bson.M{
  119. {"$match": bson.M{"index_code": edbCode}},
  120. {"$group": bson.M{
  121. "_id": nil,
  122. "min_date": bson.M{"$min": "$data_time"},
  123. "max_date": bson.M{"$max": "$data_time"},
  124. "min_value": bson.M{"$min": "$value"},
  125. "max_value": bson.M{"$max": "$value"},
  126. }},
  127. {"$project": bson.M{"_id": 0}}, // 可选,如果不需要_id字段
  128. }
  129. result, err := mogDataObj.GetEdbInfoMaxAndMinInfo(pipeline)
  130. if err != nil {
  131. fmt.Println("BaseFromBusinessIndex GetEdbInfoMaxAndMinInfo Err:" + err.Error())
  132. return
  133. }
  134. if !result.MaxDate.IsZero() {
  135. whereQuery := bson.M{"index_code": edbCode, "data_time": result.MaxDate}
  136. selectParam := bson.D{{"value", 1}, {"_id", 0}}
  137. latestValue, tmpErr := mogDataObj.GetLatestValue(whereQuery, selectParam)
  138. if tmpErr != nil {
  139. err = tmpErr
  140. return
  141. }
  142. result.LatestValue = latestValue.Value
  143. result.EndValue = latestValue.Value
  144. }
  145. item = &EdbInfoMaxAndMinInfo{
  146. MinDate: result.MinDate.Format(utils.FormatDate),
  147. MaxDate: result.MaxDate.Format(utils.FormatDate),
  148. MinValue: result.MinValue,
  149. MaxValue: result.MaxValue,
  150. LatestValue: result.LatestValue,
  151. LatestDate: result.LatestDate.Format(utils.FormatDate),
  152. EndValue: result.EndValue,
  153. }
  154. return
  155. }
  156. // ModifyIndexMaxAndMinInfo
  157. // @Description: 修改最大值和最小值信息
  158. // @author: Roc
  159. // @receiver m
  160. // @datetime 2024-05-06 14:07:46
  161. // @param indexCode string
  162. // @param item *EdbInfoMaxAndMinInfo
  163. // @param isIndexUpdateOrAdd bool
  164. // @return err error
  165. func (m *BaseFromBusinessIndex) ModifyIndexMaxAndMinInfo(indexCode string, item *EdbInfoMaxAndMinInfo, isIndexUpdateOrAdd bool) (err error) {
  166. o := orm.NewOrm()
  167. sql := ` UPDATE base_from_business_index SET start_date=?,end_date=?,modify_time=NOW() `
  168. if isIndexUpdateOrAdd {
  169. sql += `,data_update_time=NOW() `
  170. }
  171. sql += ` WHERE index_code=?`
  172. _, err = o.Raw(sql, item.MinDate, item.MaxDate, indexCode).Exec()
  173. return
  174. }