base_from_business_data.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package data_manage
  2. import (
  3. "eta/eta_mobile/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strings"
  7. "time"
  8. )
  9. // BaseFromBusinessData
  10. // @Description: 外部指标(商家系统)原始数据表
  11. type BaseFromBusinessData struct {
  12. BusinessDataId int `orm:"column(business_data_id);pk" json:"business_data_id"`
  13. BaseFromBusinessIndexId int `json:"base_from_business_index_id"` // 指标id
  14. IndexCode string `json:"index_code"` // 指标编码
  15. DataTime time.Time `json:"data_time"` // 数据日期
  16. Value float64 `json:"value"` // 数据值
  17. CreateTime time.Time `json:"create_time"` // 创建时间
  18. ModifyTime time.Time `json:"modify_time"` // 修改时间
  19. }
  20. // TableName
  21. // @Description: 获取表名
  22. // @author: Roc
  23. // @receiver m
  24. // @datetime 2024-04-26 13:41:36
  25. // @return string
  26. func (m *BaseFromBusinessData) TableName() string {
  27. return "base_from_business_data"
  28. }
  29. // CollectionName
  30. // @Description: 获取集合名称
  31. // @author: Roc
  32. // @receiver m
  33. // @datetime 2024-04-26 13:41:36
  34. // @return string
  35. func (m *BaseFromBusinessData) CollectionName() string {
  36. return "base_from_business_data"
  37. }
  38. // DataBaseName
  39. // @Description: 获取数据库名称
  40. // @author: Roc
  41. // @receiver m
  42. // @datetime 2024-04-26 13:41:33
  43. // @return string
  44. func (m *BaseFromBusinessData) DataBaseName() string {
  45. return utils.MgoDataDbName
  46. }
  47. type WhereParams struct {
  48. Condition string
  49. Pars []interface{}
  50. Order string `description:"排序字段"`
  51. }
  52. // GetAllDataList
  53. // @Description: 根据条件获取所有数据
  54. // @author: Roc
  55. // @receiver m
  56. // @datetime 2024-04-26 13:42:19
  57. // @param sort []string
  58. // @param whereParams interface{}
  59. // @return result []BaseFromBusinessData
  60. // @return err error
  61. func (m *BaseFromBusinessData) GetAllDataList(condition string, pars []interface{}, order string) (result []*BaseFromBusinessData, err error) {
  62. o := orm.NewOrmUsingDB("data")
  63. sql := `SELECT * FROM base_from_business_data WHERE 1=1 `
  64. if condition != `` {
  65. sql += ` ` + condition
  66. }
  67. if order != `` {
  68. sql += ` ORDER BY ` + order
  69. }
  70. _, err = o.Raw(sql, pars).QueryRows(&result)
  71. return
  72. }
  73. // GetLimitDataList
  74. // @Description: 根据条件获取指定数量数据列表
  75. // @author: Roc
  76. // @receiver m
  77. // @datetime 2024-05-06 17:08:32
  78. // @param whereParams interface{}
  79. // @param size int64
  80. // @return result []*BaseFromBusinessData
  81. // @return err error
  82. func (m *BaseFromBusinessData) GetLimitDataList(condition string, pars []interface{}, order string, size int64) (result []*BaseFromBusinessData, err error) {
  83. o := orm.NewOrmUsingDB("data")
  84. sql := `SELECT * FROM base_from_business_data WHERE 1=1 `
  85. if condition != `` {
  86. sql += ` ` + condition
  87. }
  88. if order != `` {
  89. sql += ` ORDER BY ` + order
  90. }
  91. sql += fmt.Sprintf(` LIMIT %d`, size)
  92. _, err = o.Raw(sql, pars).QueryRows(&result)
  93. return
  94. }
  95. // GetPageDataList
  96. // @Description: 根据条件获取分页数据列表
  97. // @author: Roc
  98. // @receiver m
  99. // @datetime 2024-05-07 10:21:07
  100. // @param whereParams interface{}
  101. // @param startSize int64
  102. // @param size int64
  103. // @param sort []string
  104. // @return result []*BaseFromBusinessData
  105. // @return err error
  106. func (m *BaseFromBusinessData) GetPageDataList(condition []string, pars []interface{}, order string, startSize, size int64) (result []*BaseFromBusinessData, err error) {
  107. o := orm.NewOrmUsingDB("data")
  108. sql := `SELECT * FROM base_from_business_data `
  109. if len(condition) > 0 {
  110. sql += ` WHERE ` + strings.Join(condition, " AND ")
  111. }
  112. if order != `` {
  113. sql += ` ORDER BY ` + order
  114. }
  115. sql += fmt.Sprintf(` LIMIT %d,%d`, startSize, size)
  116. _, err = o.Raw(sql, pars).QueryRows(&result)
  117. return
  118. }
  119. // GetCountDataList
  120. // @Description: 根据条件获取数据列表总数
  121. // @author: Roc
  122. // @receiver m
  123. // @datetime 2024-05-07 10:29:00
  124. // @param whereParams interface{}
  125. // @return count int64
  126. // @return err error
  127. func (m *BaseFromBusinessData) GetCountDataList(condition []string, pars []interface{}) (count int64, err error) {
  128. o := orm.NewOrmUsingDB("data")
  129. sql := `SELECT COUNT(1) FROM base_from_business_data `
  130. if len(condition) > 0 {
  131. sql += ` WHERE ` + strings.Join(condition, " AND ")
  132. }
  133. err = o.Raw(sql, pars).QueryRow(&count)
  134. return
  135. }
  136. // InsertDataByColl
  137. // @Description: 写入单条数据(外部传入集合)
  138. // @author: Roc
  139. // @receiver m
  140. // @datetime 2024-04-26 14:22:18
  141. // @param addData interface{}
  142. // @return err error
  143. func (m *BaseFromBusinessData) InsertDataByColl(addData interface{}) (err error) {
  144. o := orm.NewOrmUsingDB("data")
  145. _, err = o.Insert(addData)
  146. return
  147. }
  148. // BatchInsertData
  149. // @Description: 批量写入数据
  150. // @author: Roc
  151. // @receiver m
  152. // @datetime 2024-04-26 14:22:18
  153. // @param bulk int 每次请求保存的数据量
  154. // @param dataList []interface{}
  155. // @return err error
  156. func (m *BaseFromBusinessData) BatchInsertData(bulk int, dataList []interface{}) (err error) {
  157. o := orm.NewOrmUsingDB("data")
  158. _, err = o.InsertMulti(bulk, dataList)
  159. return
  160. }
  161. // UpdateData
  162. // @Description: 单条数据修改
  163. // @author: Roc
  164. // @receiver m
  165. // @datetime 2024-04-26 15:01:51
  166. // @param whereParams interface{}
  167. // @param updateParams interface{}
  168. // @return err error
  169. func (m *BaseFromBusinessData) UpdateData(updateCols []string) (err error) {
  170. o := orm.NewOrmUsingDB("data")
  171. _, err = o.Update(m, updateCols...)
  172. if err != nil {
  173. fmt.Println("UpdateDataByColl:Err:" + err.Error())
  174. return
  175. }
  176. return
  177. }
  178. // GetEdbInfoMaxAndMinInfo
  179. // @Description: 获取当前指标的最大最小值
  180. // @author: Roc
  181. // @receiver m
  182. // @datetime 2024-04-30 17:15:39
  183. // @param whereParams interface{}
  184. // @return result EdbInfoMaxAndMinInfo
  185. // @return err error
  186. func (m *BaseFromBusinessData) GetEdbInfoMaxAndMinInfo(indexCode string) (result EdbInfoMaxAndMinInfo, err error) {
  187. o := orm.NewOrmUsingDB("data")
  188. sql := ``
  189. 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 = ? `
  190. err = o.Raw(sql, indexCode).QueryRow(&result)
  191. if err != nil {
  192. return
  193. }
  194. var latestValue float64
  195. sql = ` SELECT value AS latest_value FROM base_from_business_data WHERE index_code = ? ORDER BY data_time DESC LIMIT 1 `
  196. err = o.Raw(sql, indexCode).QueryRow(&latestValue)
  197. result.LatestValue = latestValue
  198. return
  199. }