base_from_business.go 12 KB

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