edb_refresh_config.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package edb_refresh
  2. import (
  3. "errors"
  4. "eta/eta_api/utils"
  5. "github.com/beego/beego/v2/client/orm"
  6. "time"
  7. )
  8. // EdbRefreshConfig
  9. // @Description: 指标的刷新时间配置表
  10. type EdbRefreshConfig struct {
  11. EdbRefreshConfigId int `orm:"column(edb_refresh_config_id);pk"`
  12. RefreshFrequency string `description:"刷新频率"`
  13. RefreshFrequencyDay int `description:"具体刷新的日期"`
  14. RefreshTime string `description:"刷新时间"`
  15. RefreshAllData int `description:"是否刷新所有数据,0:否,1:刷新所有数据"`
  16. RefreshDataNum int `description:"刷新单元格数"`
  17. ModifyTime time.Time `description:"最晚一次的更新时间"`
  18. CreateTime time.Time `description:"添加时间"`
  19. }
  20. // Add
  21. // @Description: 添加
  22. // @author: Roc
  23. // @receiver m
  24. // @datetime 2023-12-14 16:11:10
  25. // @param cols []string
  26. // @return err error
  27. func (m *EdbRefreshConfig) Add() (err error) {
  28. if m.EdbRefreshConfigId > 0 {
  29. err = errors.New("该配置已存在")
  30. return
  31. }
  32. o := orm.NewOrmUsingDB("data")
  33. lastId, err := o.Insert(m)
  34. if err != nil {
  35. return
  36. }
  37. m.EdbRefreshConfigId = int(lastId)
  38. return
  39. }
  40. // Update
  41. // @Description: 更新
  42. // @author: Roc
  43. // @receiver m
  44. // @datetime 2023-12-14 16:11:10
  45. // @param cols []string
  46. // @return err error
  47. func (m *EdbRefreshConfig) Update(cols []string) (err error) {
  48. o := orm.NewOrmUsingDB("data")
  49. _, err = o.Update(m, cols...)
  50. return
  51. }
  52. // Delete
  53. // @Description: 删除
  54. // @author: Roc
  55. // @receiver m
  56. // @datetime 2023-12-14 16:11:10
  57. // @return err error
  58. func (m *EdbRefreshConfig) Delete() (err error) {
  59. o := orm.NewOrmUsingDB("data")
  60. _, err = o.Delete(m)
  61. return
  62. }
  63. // GetEdbRefreshConfigListByCondition
  64. // @Description: 根据条件获取刷新配置列表
  65. // @author: Roc
  66. // @datetime 2024-01-08 14:07:10
  67. // @param refreshFrequency string
  68. // @param refreshTime string
  69. // @param refreshFrequencyDay int
  70. // @param refreshAllData int
  71. // @param refreshDataNum int
  72. // @return item *EdbRefreshConfig
  73. // @return err error
  74. func GetEdbRefreshConfigListByCondition(refreshFrequency, refreshTime string, refreshFrequencyDay, refreshAllData, refreshDataNum int) (item *EdbRefreshConfig, err error) {
  75. o := orm.NewOrmUsingDB("data")
  76. sql := `SELECT * FROM edb_refresh_config
  77. WHERE refresh_frequency = ? AND refresh_frequency_day = ? AND refresh_time = ? AND refresh_all_data = ? AND refresh_data_num = ? ORDER BY edb_refresh_config_id ASC `
  78. err = o.Raw(sql, refreshFrequency, refreshFrequencyDay, refreshTime, refreshAllData, refreshDataNum).QueryRow(&item)
  79. return
  80. }
  81. // SaveEdbRefreshConfig
  82. // @Description: 保存刷新配置
  83. // @author: Roc
  84. // @datetime 2024-01-08 15:13:23
  85. // @param source int
  86. // @param subSource int
  87. // @param sysUserId int
  88. // @param sysUserRealName string
  89. // @param newConfigList []*EdbRefreshConfig
  90. // @param addMapping []*EdbRefreshMapping
  91. // @param edbIdList []int
  92. // @return err error
  93. func SaveEdbRefreshConfig(source, subSource, sysUserId int, sysUserRealName string, newConfigList []*EdbRefreshConfig, addMapping []*EdbRefreshMapping, edbIdList []int) (err error) {
  94. num := len(edbIdList)
  95. if num <= 0 {
  96. return
  97. }
  98. o, err := orm.NewOrmUsingDB("data").Begin()
  99. if err != nil {
  100. return
  101. }
  102. defer func() {
  103. if err != nil {
  104. _ = o.Rollback()
  105. return
  106. }
  107. _ = o.Commit()
  108. }()
  109. for _, v := range newConfigList {
  110. lastId, tmpErr := o.Insert(v)
  111. if tmpErr != nil {
  112. err = tmpErr
  113. return
  114. }
  115. v.EdbRefreshConfigId = int(lastId)
  116. for _, edbId := range edbIdList {
  117. addMapping = append(addMapping, &EdbRefreshMapping{
  118. EdbRefreshMappingId: 0,
  119. Source: source,
  120. SubSource: subSource,
  121. EdbInfoId: edbId,
  122. EdbRefreshConfigId: v.EdbRefreshConfigId,
  123. SysUserId: sysUserId,
  124. SysUserRealName: sysUserRealName,
  125. ModifyTime: time.Now(),
  126. CreateTime: time.Now(),
  127. })
  128. }
  129. }
  130. // 先删除
  131. sql := `DELETE FROM edb_refresh_mapping WHERE source =? AND sub_source =? AND edb_info_id in (` + utils.GetOrmInReplace(num) + `)`
  132. _, err = o.Raw(sql, source, subSource, edbIdList).Exec()
  133. // 再写入
  134. if len(addMapping) > 0 {
  135. tmpAddDataList := make([]*EdbRefreshMapping, 0)
  136. i := 0
  137. for _, v := range addMapping {
  138. tmpAddDataList = append(tmpAddDataList, v)
  139. i++
  140. if i >= 500 {
  141. _, err = o.InsertMulti(len(tmpAddDataList), tmpAddDataList)
  142. if err != nil {
  143. return
  144. }
  145. i = 0
  146. tmpAddDataList = make([]*EdbRefreshMapping, 0)
  147. }
  148. }
  149. if len(tmpAddDataList) > 0 {
  150. _, err = o.InsertMulti(len(tmpAddDataList), tmpAddDataList)
  151. if err != nil {
  152. return
  153. }
  154. }
  155. }
  156. return
  157. }
  158. // EdbRefreshConfigItem
  159. // @Description: 指标的刷新时间配置项
  160. type EdbRefreshConfigItem struct {
  161. RefreshFrequency string `description:"刷新频率"`
  162. RefreshFrequencyDay int `description:"具体刷新的日期"`
  163. RefreshTime string `description:"刷新时间"`
  164. RefreshAllData int `description:"是否刷新所有数据,0:否,1:刷新所有数据"`
  165. RefreshDataNum int `description:"刷新单元格数"`
  166. }
  167. // GetEdbRefreshConfigListBySourceAndeEdbInfoId
  168. // @Description: 根据来源和指标获取其配置列表
  169. // @author: Roc
  170. // @datetime 2024-01-10 14:18:21
  171. // @param source int
  172. // @param subSource int
  173. // @param edbInfoId int
  174. // @return list []*EdbRefreshDefaultConfig
  175. // @return err error
  176. func GetEdbRefreshConfigListBySourceAndeEdbInfoId(source, subSource, edbInfoId int) (list []*EdbRefreshConfigItem, err error) {
  177. o := orm.NewOrmUsingDB("data")
  178. sql := `SELECT a.* FROM edb_refresh_config a
  179. JOIN edb_refresh_mapping b ON a.edb_refresh_config_id = b.edb_refresh_config_id
  180. WHERE b.source = ? AND b.sub_source = ? AND b.edb_info_id = ? ORDER BY a.edb_refresh_config_id ASC `
  181. _, err = o.Raw(sql, source, subSource, edbInfoId).QueryRows(&list)
  182. return
  183. }
  184. // EdbRefreshConfigAndEdbItem
  185. // @Description: 指标的刷新时间配置项
  186. type EdbRefreshConfigAndEdbItem struct {
  187. RefreshFrequency string `description:"刷新频率"`
  188. RefreshFrequencyDay int `description:"具体刷新的日期"`
  189. RefreshTime string `description:"刷新时间"`
  190. RefreshAllData int `description:"是否刷新所有数据,0:否,1:刷新所有数据"`
  191. RefreshDataNum int `description:"刷新单元格数"`
  192. EdbInfoId int `description:"指标id,如果是数据源(钢联、有色金属)的,那么就是数据源里面的id"`
  193. Source int `description:"来源"`
  194. SubSource int `description:"子来源"`
  195. }
  196. // GetEdbRefreshConfigAndEdbListBySourceAndeEdbInfoId
  197. // @Description: 根据来源和指标获取其配置列表
  198. // @author: Roc
  199. // @datetime 2024-01-10 14:18:21
  200. // @param source int
  201. // @param subSource int
  202. // @param edbInfoId int
  203. // @return list []*EdbRefreshDefaultConfig
  204. // @return err error
  205. func GetEdbRefreshConfigAndEdbListBySourceAndeEdbInfoId(source, subSource int, edbInfoIdList []int) (list []*EdbRefreshConfigAndEdbItem, err error) {
  206. num := len(edbInfoIdList)
  207. if num <= 0 {
  208. return
  209. }
  210. o := orm.NewOrmUsingDB("data")
  211. sql := `SELECT a.*,b.source,b.sub_source,b.edb_info_id FROM edb_refresh_config a
  212. JOIN edb_refresh_mapping b ON a.edb_refresh_config_id = b.edb_refresh_config_id
  213. WHERE b.source = ? AND b.sub_source = ? AND b.edb_info_id in (` + utils.GetOrmInReplace(num) + `) ORDER BY a.edb_refresh_config_id ASC `
  214. _, err = o.Raw(sql, source, subSource, edbInfoIdList).QueryRows(&list)
  215. return
  216. }