base_from_business_data.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package data_manage
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "strings"
  7. "time"
  8. )
  9. // BaseFromBusinessData
  10. // @Description: 外部指标(商家系统)原始数据表
  11. type BaseFromBusinessData struct {
  12. BusinessDataId int `orm:"column(business_data_id);pk" gorm:"primaryKey" 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. sql := `SELECT * FROM base_from_business_data WHERE 1=1 `
  63. if condition != `` {
  64. sql += ` ` + condition
  65. }
  66. if order != `` {
  67. sql += ` ORDER BY ` + order
  68. }
  69. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&result).Error
  70. return
  71. }
  72. // GetLimitDataList
  73. // @Description: 根据条件获取指定数量数据列表
  74. // @author: Roc
  75. // @receiver m
  76. // @datetime 2024-05-06 17:08:32
  77. // @param whereParams interface{}
  78. // @param size int64
  79. // @return result []*BaseFromBusinessData
  80. // @return err error
  81. func (m *BaseFromBusinessData) GetLimitDataList(condition string, pars []interface{}, order string, size int64) (result []*BaseFromBusinessData, err error) {
  82. o := global.DmSQL["data"]
  83. sql := `SELECT * FROM base_from_business_data WHERE 1=1 `
  84. if condition != `` {
  85. sql += ` ` + condition
  86. }
  87. if order != `` {
  88. sql += ` ORDER BY ` + order
  89. }
  90. sql += fmt.Sprintf(` LIMIT %d`, size)
  91. err = o.Raw(sql, pars...).Scan(&result).Error
  92. return
  93. }
  94. // GetPageDataList
  95. // @Description: 根据条件获取分页数据列表
  96. // @author: Roc
  97. // @receiver m
  98. // @datetime 2024-05-07 10:21:07
  99. // @param whereParams interface{}
  100. // @param startSize int64
  101. // @param size int64
  102. // @param sort []string
  103. // @return result []*BaseFromBusinessData
  104. // @return err error
  105. func (m *BaseFromBusinessData) GetPageDataList(condition []string, pars []interface{}, order string, startSize, size int64) (result []*BaseFromBusinessData, err error) {
  106. sql := `SELECT * FROM base_from_business_data `
  107. if len(condition) > 0 {
  108. sql += ` WHERE ` + strings.Join(condition, " AND ")
  109. }
  110. if order != `` {
  111. sql += ` ORDER BY ` + order
  112. }
  113. sql += fmt.Sprintf(` LIMIT %d,%d`, startSize, size)
  114. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&result).Error
  115. return
  116. }
  117. // GetCountDataList
  118. // @Description: 根据条件获取数据列表总数
  119. // @author: Roc
  120. // @receiver m
  121. // @datetime 2024-05-07 10:29:00
  122. // @param whereParams interface{}
  123. // @return count int64
  124. // @return err error
  125. func (m *BaseFromBusinessData) GetCountDataList(condition []string, pars []interface{}) (count int64, err error) {
  126. sql := `SELECT COUNT(1) FROM base_from_business_data `
  127. if len(condition) > 0 {
  128. sql += ` WHERE ` + strings.Join(condition, " AND ")
  129. }
  130. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&count).Error
  131. return
  132. }
  133. // InsertDataByColl
  134. // @Description: 写入单条数据(外部传入集合)
  135. // @author: Roc
  136. // @receiver m
  137. // @datetime 2024-04-26 14:22:18
  138. // @param addData interface{}
  139. // @return err error
  140. func (m *BaseFromBusinessData) InsertDataByColl(addData interface{}) (err error) {
  141. err = global.DmSQL["data"].Create(addData).Error
  142. return
  143. }
  144. // BatchInsertData
  145. // @Description: 批量写入数据
  146. // @author: Roc
  147. // @receiver m
  148. // @datetime 2024-04-26 14:22:18
  149. // @param bulk int 每次请求保存的数据量
  150. // @param dataList []interface{}
  151. // @return err error
  152. func (m *BaseFromBusinessData) BatchInsertData(bulk int, dataList []interface{}) (err error) {
  153. err = global.DmSQL["data"].CreateInBatches(dataList, bulk).Error
  154. return
  155. }
  156. // UpdateData
  157. // @Description: 单条数据修改
  158. // @author: Roc
  159. // @receiver m
  160. // @datetime 2024-04-26 15:01:51
  161. // @param whereParams interface{}
  162. // @param updateParams interface{}
  163. // @return err error
  164. func (m *BaseFromBusinessData) UpdateData(updateCols []string) (err error) {
  165. err = global.DmSQL["data"].Select(updateCols).Updates(m).Error
  166. if err != nil {
  167. fmt.Println("UpdateDataByColl:Err:" + err.Error())
  168. return
  169. }
  170. return
  171. }
  172. // GetEdbInfoMaxAndMinInfo
  173. // @Description: 获取当前指标的最大最小值
  174. // @author: Roc
  175. // @receiver m
  176. // @datetime 2024-04-30 17:15:39
  177. // @param whereParams interface{}
  178. // @return result EdbInfoMaxAndMinInfo
  179. // @return err error
  180. func (m *BaseFromBusinessData) GetEdbInfoMaxAndMinInfo(indexCode string) (result EdbInfoMaxAndMinInfo, err error) {
  181. o := global.DmSQL["data"]
  182. sql := ``
  183. 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 = ? `
  184. err = o.Raw(sql, indexCode).Scan(&result).Error
  185. if err != nil {
  186. return
  187. }
  188. var latestValue float64
  189. sql = ` SELECT value AS latest_value FROM base_from_business_data WHERE index_code = ? ORDER BY data_time DESC LIMIT 1 `
  190. err = o.Raw(sql, indexCode).Scan(&latestValue).Error
  191. result.LatestValue = latestValue
  192. return
  193. }