base_from_business.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. func (m BaseFromBusinessIndex) GetEdbInfoMaxAndMinInfo(edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  192. mogDataObj := new(mgo.BaseFromBusinessData)
  193. pipeline := []bson.M{
  194. {"$match": bson.M{"index_code": edbCode}},
  195. {"$group": bson.M{
  196. "_id": nil,
  197. "min_date": bson.M{"$min": "$data_time"},
  198. "max_date": bson.M{"$max": "$data_time"},
  199. "min_value": bson.M{"$min": "$value"},
  200. "max_value": bson.M{"$max": "$value"},
  201. }},
  202. {"$project": bson.M{"_id": 0}}, // 可选,如果不需要_id字段
  203. }
  204. result, err := mogDataObj.GetEdbInfoMaxAndMinInfo(pipeline)
  205. if err != nil {
  206. fmt.Println("BaseFromBusinessIndex GetEdbInfoMaxAndMinInfo Err:" + err.Error())
  207. return
  208. }
  209. if !result.MaxDate.IsZero() {
  210. whereQuery := bson.M{"index_code": edbCode, "data_time": result.MaxDate}
  211. selectParam := bson.D{{"value", 1}, {"_id", 0}}
  212. latestValue, tmpErr := mogDataObj.GetLatestValue(whereQuery, selectParam)
  213. if tmpErr != nil {
  214. err = tmpErr
  215. return
  216. }
  217. result.LatestValue = latestValue.Value
  218. result.EndValue = latestValue.Value
  219. }
  220. item = &EdbInfoMaxAndMinInfo{
  221. MinDate: result.MinDate.Format(utils.FormatDate),
  222. MaxDate: result.MaxDate.Format(utils.FormatDate),
  223. MinValue: result.MinValue,
  224. MaxValue: result.MaxValue,
  225. LatestValue: result.LatestValue,
  226. LatestDate: result.LatestDate.Format(utils.FormatDate),
  227. EndValue: result.EndValue,
  228. }
  229. return
  230. }
  231. // ModifyIndexMaxAndMinInfo
  232. // @Description: 修改最大值和最小值信息
  233. // @author: Roc
  234. // @receiver m
  235. // @datetime 2024-05-06 14:07:46
  236. // @param indexCode string
  237. // @param item *EdbInfoMaxAndMinInfo
  238. // @param isIndexUpdateOrAdd bool
  239. // @return err error
  240. func (m *BaseFromBusinessIndex) ModifyIndexMaxAndMinInfo(indexCode string, item *EdbInfoMaxAndMinInfo, isIndexUpdateOrAdd bool) (err error) {
  241. o := orm.NewOrm()
  242. sql := ` UPDATE base_from_business_index SET start_date=?,end_date=?,modify_time=NOW() `
  243. if isIndexUpdateOrAdd {
  244. sql += `,data_update_time=NOW() `
  245. }
  246. sql += ` WHERE index_code=?`
  247. _, err = o.Raw(sql, item.MinDate, item.MaxDate, indexCode).Exec()
  248. return
  249. }