base_from_business_data.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. package models
  2. import (
  3. "errors"
  4. "eta/eta_index_lib/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "strings"
  8. "time"
  9. )
  10. // BaseFromBusinessData
  11. // @Description: 外部指标(商家系统)原始数据表
  12. type BaseFromBusinessData struct {
  13. BusinessDataId int `orm:"column(business_data_id);pk" json:"business_data_id"`
  14. BaseFromBusinessIndexId int `json:"base_from_business_index_id"` // 指标id
  15. IndexCode string `json:"index_code"` // 指标编码
  16. DataTime time.Time `json:"data_time"` // 数据日期
  17. Value float64 `json:"value"` // 数据值
  18. CreateTime time.Time `json:"create_time"` // 创建时间
  19. ModifyTime time.Time `json:"modify_time"` // 修改时间
  20. }
  21. // TableName
  22. // @Description: 获取表名
  23. // @author: Roc
  24. // @receiver m
  25. // @datetime 2024-04-26 13:41:36
  26. // @return string
  27. func (m *BaseFromBusinessData) TableName() string {
  28. return "base_from_business_data"
  29. }
  30. // CollectionName
  31. // @Description: 获取集合名称
  32. // @author: Roc
  33. // @receiver m
  34. // @datetime 2024-04-26 13:41:36
  35. // @return string
  36. func (m *BaseFromBusinessData) CollectionName() string {
  37. return "base_from_business_data"
  38. }
  39. // DataBaseName
  40. // @Description: 获取数据库名称
  41. // @author: Roc
  42. // @receiver m
  43. // @datetime 2024-04-26 13:41:33
  44. // @return string
  45. func (m *BaseFromBusinessData) DataBaseName() string {
  46. return utils.MgoDataDbName
  47. }
  48. type WhereParams struct {
  49. Condition string
  50. Pars []interface{}
  51. Order string `description:"排序字段"`
  52. }
  53. // GetAllDataList
  54. // @Description: 根据条件获取所有数据
  55. // @author: Roc
  56. // @receiver m
  57. // @datetime 2024-07-01 17:18:57
  58. // @param condition []string
  59. // @param pars []interface{}
  60. // @param order string
  61. // @return result []*BaseFromBusinessData
  62. // @return err error
  63. func (m *BaseFromBusinessData) GetAllDataList(condition []string, pars []interface{}, order string) (result []*BaseFromBusinessData, err error) {
  64. o := orm.NewOrm()
  65. sql := `SELECT * FROM base_from_business_data `
  66. if len(condition) > 0 {
  67. sql += ` WHERE ` + strings.Join(condition, " AND ")
  68. }
  69. if order != `` {
  70. sql += ` ORDER BY ` + order
  71. }
  72. _, err = o.Raw(sql, pars).QueryRows(&result)
  73. return
  74. }
  75. // GetLimitDataList
  76. // @Description: 根据条件获取指定数量数据列表
  77. // @author: Roc
  78. // @receiver m
  79. // @datetime 2024-07-01 17:19:16
  80. // @param condition []string
  81. // @param pars []interface{}
  82. // @param order string
  83. // @param size int64
  84. // @return result []*BaseFromBusinessData
  85. // @return err error
  86. func (m *BaseFromBusinessData) GetLimitDataList(condition []string, pars []interface{}, order string, size int64) (result []*BaseFromBusinessData, err error) {
  87. o := orm.NewOrm()
  88. sql := `SELECT * FROM base_from_business_data `
  89. if len(condition) > 0 {
  90. sql += ` WHERE ` + strings.Join(condition, " AND ")
  91. }
  92. if order != `` {
  93. sql += ` ORDER BY ` + order
  94. }
  95. sql += fmt.Sprintf(` LIMIT %d`, size)
  96. _, err = o.Raw(sql, pars).QueryRows(&result)
  97. return
  98. }
  99. // GetPageDataList
  100. // @Description: 根据条件获取分页数据列表
  101. // @author: Roc
  102. // @receiver m
  103. // @datetime 2024-07-01 17:19:42
  104. // @param condition []string
  105. // @param pars []interface{}
  106. // @param order string
  107. // @param startSize int64
  108. // @param size int64
  109. // @return result []*BaseFromBusinessData
  110. // @return err error
  111. func (m *BaseFromBusinessData) GetPageDataList(condition []string, pars []interface{}, order string, startSize, size int64) (result []*BaseFromBusinessData, err error) {
  112. o := orm.NewOrm()
  113. sql := `SELECT * FROM base_from_business_data `
  114. if len(condition) > 0 {
  115. sql += ` WHERE ` + strings.Join(condition, " AND ")
  116. }
  117. if order != `` {
  118. sql += ` ORDER BY ` + order
  119. }
  120. sql += fmt.Sprintf(` LIMIT %d,%d`, startSize, size)
  121. _, err = o.Raw(sql, pars).QueryRows(&result)
  122. return
  123. }
  124. // GetCountDataList
  125. // @Description: 根据条件获取数据列表总数
  126. // @author: Roc
  127. // @receiver m
  128. // @datetime 2024-07-01 17:19:50
  129. // @param condition []string
  130. // @param pars []interface{}
  131. // @return count int64
  132. // @return err error
  133. func (m *BaseFromBusinessData) GetCountDataList(condition []string, pars []interface{}) (count int64, err error) {
  134. o := orm.NewOrm()
  135. sql := `SELECT COUNT(1) FROM base_from_business_data `
  136. if len(condition) > 0 {
  137. sql += ` WHERE ` + strings.Join(condition, " AND ")
  138. }
  139. err = o.Raw(sql, pars).QueryRow(&count)
  140. return
  141. }
  142. // InsertDataByColl
  143. // @Description: 写入单条数据(外部传入集合)
  144. // @author: Roc
  145. // @receiver m
  146. // @datetime 2024-04-26 14:22:18
  147. // @param addData interface{}
  148. // @return err error
  149. func (m *BaseFromBusinessData) InsertDataByColl(addData interface{}) (err error) {
  150. o := orm.NewOrm()
  151. _, err = o.Insert(addData)
  152. return
  153. }
  154. // BatchInsertData
  155. // @Description: 批量写入数据
  156. // @author: Roc
  157. // @receiver m
  158. // @datetime 2024-04-26 14:22:18
  159. // @param bulk int 每次请求保存的数据量
  160. // @param dataList []*BaseFromBusinessData
  161. // @return err error
  162. func (m *BaseFromBusinessData) BatchInsertData(bulk int, dataList []*BaseFromBusinessData) (err error) {
  163. o := orm.NewOrm()
  164. _, err = o.InsertMulti(bulk, dataList)
  165. return
  166. }
  167. // UpdateData
  168. // @Description: 单条数据修改
  169. // @author: Roc
  170. // @receiver m
  171. // @datetime 2024-04-26 15:01:51
  172. // @param whereParams interface{}
  173. // @param updateParams interface{}
  174. // @return err error
  175. func (m *BaseFromBusinessData) UpdateData(updateCols []string) (err error) {
  176. o := orm.NewOrm()
  177. _, err = o.Update(m, updateCols...)
  178. if err != nil {
  179. fmt.Println("UpdateDataByColl:Err:" + err.Error())
  180. return
  181. }
  182. return
  183. }
  184. // HandleData
  185. // @Description: 数据处理
  186. // @author: Roc
  187. // @receiver m
  188. // @datetime 2024-07-01 17:35:54
  189. // @param addDataList []*BaseFromBusinessData
  190. // @param updateDataList []*BaseFromBusinessData
  191. // @return err error
  192. func (m *BaseFromBusinessData) HandleData(addDataList, updateDataList []*BaseFromBusinessData) (err error) {
  193. o := orm.NewOrm()
  194. to, err := o.Begin()
  195. if err != nil {
  196. return
  197. }
  198. defer func() {
  199. if err != nil {
  200. fmt.Println("BaseFromBusinessData HandleData,Err:" + err.Error())
  201. _ = to.Rollback()
  202. } else {
  203. _ = to.Commit()
  204. }
  205. }()
  206. // 插入数据
  207. if len(addDataList) > 0 {
  208. _, err = to.InsertMulti(500, addDataList)
  209. if err != nil {
  210. return
  211. }
  212. }
  213. // 修改
  214. if len(updateDataList) > 0 {
  215. for _, v := range updateDataList {
  216. _, err = to.Update(v, "Value", "ModifyTime")
  217. if err != nil {
  218. fmt.Println("BaseFromBusinessData HandleData Update:Err:" + err.Error())
  219. return
  220. }
  221. }
  222. }
  223. return
  224. }
  225. // GetEdbInfoMaxAndMinInfo
  226. // @Description: 获取当前指标的最大最小值
  227. // @author: Roc
  228. // @receiver m
  229. // @datetime 2024-04-30 17:15:39
  230. // @param whereParams interface{}
  231. // @return result EdbInfoMaxAndMinInfo
  232. // @return err error
  233. func (m *BaseFromBusinessData) GetEdbInfoMaxAndMinInfo(indexCode string) (result EdbInfoMaxAndMinInfo, err error) {
  234. o := orm.NewOrm()
  235. sql := ``
  236. sql = ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date,MIN(value) AS min_value,MAX(value) AS max_value FROM base_from_business_data WHERE index_code = ? `
  237. err = o.Raw(sql, indexCode).QueryRow(&result)
  238. if err != nil {
  239. return
  240. }
  241. var latestValue float64
  242. sql = ` SELECT value AS latest_value FROM base_from_business_data WHERE index_code = ? ORDER BY data_time DESC LIMIT 1 `
  243. err = o.Raw(sql, indexCode).QueryRow(&latestValue)
  244. result.LatestValue = latestValue
  245. return
  246. }
  247. // DelDataByCond
  248. // @Description: 根据条件删除多条数据
  249. // @author: Roc
  250. // @receiver m
  251. // @datetime 2024-07-01 17:46:56
  252. // @param condition []string
  253. // @param pars []interface{}
  254. // @return err error
  255. func (m *BaseFromBusinessData) DelDataByCond(condition []string, pars []interface{}) (err error) {
  256. if len(condition) <= 0 {
  257. err = errors.New("条件不能为空")
  258. return
  259. }
  260. o := orm.NewOrm()
  261. sql := `DELETE FROM base_from_business_data `
  262. sql += ` WHERE ` + strings.Join(condition, " AND ")
  263. _, err = o.Raw(sql, pars).Exec()
  264. return
  265. }