base_from_business_data.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package models
  2. import (
  3. "errors"
  4. "eta_gn/eta_index_lib/global"
  5. "eta_gn/eta_index_lib/utils"
  6. "fmt"
  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. sql := `SELECT * FROM base_from_business_data `
  65. if len(condition) > 0 {
  66. sql += ` WHERE ` + strings.Join(condition, " AND ")
  67. }
  68. if order != `` {
  69. sql += ` ORDER BY ` + order
  70. }
  71. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&result).Error
  72. return
  73. }
  74. // GetLimitDataList
  75. // @Description: 根据条件获取指定数量数据列表
  76. // @author: Roc
  77. // @receiver m
  78. // @datetime 2024-07-01 17:19:16
  79. // @param condition []string
  80. // @param pars []interface{}
  81. // @param order string
  82. // @param size int64
  83. // @return result []*BaseFromBusinessData
  84. // @return err error
  85. func (m *BaseFromBusinessData) GetLimitDataList(condition []string, pars []interface{}, order string, size int64) (result []*BaseFromBusinessData, err error) {
  86. sql := `SELECT * FROM base_from_business_data `
  87. if len(condition) > 0 {
  88. sql += ` WHERE ` + strings.Join(condition, " AND ")
  89. }
  90. if order != `` {
  91. sql += ` ORDER BY ` + order
  92. }
  93. sql += fmt.Sprintf(` LIMIT %d`, size)
  94. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&result).Error
  95. return
  96. }
  97. // GetPageDataList
  98. // @Description: 根据条件获取分页数据列表
  99. // @author: Roc
  100. // @receiver m
  101. // @datetime 2024-07-01 17:19:42
  102. // @param condition []string
  103. // @param pars []interface{}
  104. // @param order string
  105. // @param startSize int64
  106. // @param size int64
  107. // @return result []*BaseFromBusinessData
  108. // @return err error
  109. func (m *BaseFromBusinessData) GetPageDataList(condition []string, pars []interface{}, order string, startSize, size int64) (result []*BaseFromBusinessData, err error) {
  110. sql := `SELECT * FROM base_from_business_data `
  111. if len(condition) > 0 {
  112. sql += ` WHERE ` + strings.Join(condition, " AND ")
  113. }
  114. if order != `` {
  115. sql += ` ORDER BY ` + order
  116. }
  117. sql += fmt.Sprintf(` LIMIT %d,%d`, startSize, size)
  118. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&result).Error
  119. return
  120. }
  121. // GetCountDataList
  122. // @Description: 根据条件获取数据列表总数
  123. // @author: Roc
  124. // @receiver m
  125. // @datetime 2024-07-01 17:19:50
  126. // @param condition []string
  127. // @param pars []interface{}
  128. // @return count int64
  129. // @return err error
  130. func (m *BaseFromBusinessData) GetCountDataList(condition []string, pars []interface{}) (count int64, err error) {
  131. //o := orm.NewOrm()
  132. sql := `SELECT COUNT(1) as count FROM base_from_business_data `
  133. if len(condition) > 0 {
  134. sql += ` WHERE ` + strings.Join(condition, " AND ")
  135. }
  136. //err = o.Raw(sql, pars).QueryRow(&count)
  137. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&count).Error
  138. return
  139. }
  140. // InsertDataByColl
  141. // @Description: 写入单条数据(外部传入集合)
  142. // @author: Roc
  143. // @receiver m
  144. // @datetime 2024-04-26 14:22:18
  145. // @param addData interface{}
  146. // @return err error
  147. func (m *BaseFromBusinessData) InsertDataByColl(addData interface{}) (err error) {
  148. err = global.DEFAULT_DmSQL.Create(addData).Error
  149. return
  150. }
  151. // BatchInsertData
  152. // @Description: 批量写入数据
  153. // @author: Roc
  154. // @receiver m
  155. // @datetime 2024-04-26 14:22:18
  156. // @param bulk int 每次请求保存的数据量
  157. // @param dataList []*BaseFromBusinessData
  158. // @return err error
  159. func (m *BaseFromBusinessData) BatchInsertData(bulk int, dataList []*BaseFromBusinessData) (err error) {
  160. err = global.DEFAULT_DmSQL.CreateInBatches(dataList, bulk).Error
  161. return
  162. }
  163. // UpdateData
  164. // @Description: 单条数据修改
  165. // @author: Roc
  166. // @receiver m
  167. // @datetime 2024-04-26 15:01:51
  168. // @param whereParams interface{}
  169. // @param updateParams interface{}
  170. // @return err error
  171. func (m *BaseFromBusinessData) UpdateData(updateCols []string) (err error) {
  172. err = global.DEFAULT_DmSQL.Model(m).Select(updateCols).Updates(m).Error
  173. if err != nil {
  174. fmt.Println("UpdateDataByColl:Err:" + err.Error())
  175. return
  176. }
  177. return
  178. }
  179. // HandleData
  180. // @Description: 数据处理
  181. // @author: Roc
  182. // @receiver m
  183. // @datetime 2024-07-01 17:35:54
  184. // @param addDataList []*BaseFromBusinessData
  185. // @param updateDataList []*BaseFromBusinessData
  186. // @return err error
  187. func (m *BaseFromBusinessData) HandleData(addDataList, updateDataList []*BaseFromBusinessData) (err error) {
  188. to := global.DEFAULT_DmSQL.Begin()
  189. if err != nil {
  190. return
  191. }
  192. defer func() {
  193. if err != nil {
  194. fmt.Println("BaseFromBusinessData HandleData,Err:" + err.Error())
  195. _ = to.Rollback()
  196. } else {
  197. _ = to.Commit()
  198. }
  199. }()
  200. // 插入数据
  201. if len(addDataList) > 0 {
  202. err = to.CreateInBatches(addDataList, 500).Error
  203. if err != nil {
  204. return
  205. }
  206. }
  207. // 修改
  208. if len(updateDataList) > 0 {
  209. for _, v := range updateDataList {
  210. err = to.Model(v).Select("Value", "ModifyTime").Updates(v).Error
  211. if err != nil {
  212. fmt.Println("BaseFromBusinessData HandleData Update:Err:" + err.Error())
  213. return
  214. }
  215. }
  216. }
  217. return
  218. }
  219. // GetEdbInfoMaxAndMinInfo
  220. // @Description: 获取当前指标的最大最小值
  221. // @author: Roc
  222. // @receiver m
  223. // @datetime 2024-04-30 17:15:39
  224. // @param whereParams interface{}
  225. // @return result EdbInfoMaxAndMinInfo
  226. // @return err error
  227. func (m *BaseFromBusinessData) GetEdbInfoMaxAndMinInfo(indexCode string) (result *EdbInfoMaxAndMinInfo, err error) {
  228. sql := ``
  229. 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 = ? `
  230. err = global.DEFAULT_DmSQL.Raw(sql, indexCode).First(&result).Error
  231. if err != nil {
  232. return
  233. }
  234. var latestValue float64
  235. sql = ` SELECT value AS latest_value FROM base_from_business_data WHERE index_code = ? ORDER BY data_time DESC LIMIT 1 `
  236. err = global.DEFAULT_DmSQL.Raw(sql, indexCode).First(&latestValue).Error
  237. result.LatestValue = latestValue
  238. return
  239. }
  240. // DelDataByCond
  241. // @Description: 根据条件删除多条数据
  242. // @author: Roc
  243. // @receiver m
  244. // @datetime 2024-07-01 17:46:56
  245. // @param condition []string
  246. // @param pars []interface{}
  247. // @return err error
  248. func (m *BaseFromBusinessData) DelDataByCond(condition []string, pars []interface{}) (err error) {
  249. if len(condition) <= 0 {
  250. err = errors.New("条件不能为空")
  251. return
  252. }
  253. sql := `DELETE FROM base_from_business_data `
  254. sql += ` WHERE ` + strings.Join(condition, " AND ")
  255. err = global.DEFAULT_DmSQL.Exec(sql, pars...).Error
  256. return
  257. }