base_from_business.go 10 KB

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