edb_collect.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. package data_manage
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. // EdbCollect 指标收藏
  12. type EdbCollect struct {
  13. EdbCollectId int `gorm:"primaryKey;autoIncrement;column:edb_collect_id;type:int(10) unsigned;not null"`
  14. EdbCollectClassifyId int `gorm:"index:idx_classify_id;column:edb_collect_classify_id;type:int(10) unsigned;not null;default:0"` // 指标收藏分类ID
  15. EdbInfoId int `gorm:"column:edb_info_id;type:int(10) unsigned;not null;default:0"` // 指标ID
  16. EdbCode string `gorm:"column:edb_code;type:varchar(255);not null;default:''"` // 指标编码
  17. SysUserId int `gorm:"column:sys_user_id;type:int(10) unsigned;not null;default:0"` // 创建人ID
  18. SysRealName string `gorm:"column:sys_real_name;type:int(10) unsigned;not null;default:0"` // 创建人姓名
  19. Sort int `gorm:"column:sort;type:int(10);default:0"` // 排序
  20. CreateTime time.Time `gorm:"column:create_time;type:datetime"` // 创建时间
  21. ModifyTime time.Time `gorm:"column:modify_time;type:datetime"` // 更新时间
  22. }
  23. func (m *EdbCollect) TableName() string {
  24. return "edb_collect"
  25. }
  26. type EdbCollectCols struct {
  27. PrimaryId string
  28. EdbCollectClassifyId string
  29. EdbInfoId string
  30. EdbCode string
  31. SysUserId string
  32. SysRealName string
  33. Sort string
  34. CreateTime string
  35. ModifyTime string
  36. }
  37. func (m *EdbCollect) Cols() EdbCollectCols {
  38. return EdbCollectCols{
  39. PrimaryId: "edb_collect_id",
  40. EdbCollectClassifyId: "edb_collect_classify_id",
  41. EdbInfoId: "edb_info_id",
  42. EdbCode: "edb_code",
  43. SysUserId: "sys_user_id",
  44. SysRealName: "sys_real_name",
  45. Sort: "sort",
  46. CreateTime: "create_time",
  47. ModifyTime: "modify_time",
  48. }
  49. }
  50. func (m *EdbCollect) Create() (err error) {
  51. err = global.DmSQL["data"].Create(m).Error
  52. return
  53. }
  54. func (m *EdbCollect) CreateMulti(items []*EdbCollect) (err error) {
  55. if len(items) == 0 {
  56. return
  57. }
  58. err = global.DmSQL["data"].CreateInBatches(items, utils.MultiAddNum).Error
  59. return
  60. }
  61. func (m *EdbCollect) Update(cols []string) (err error) {
  62. err = global.DmSQL["data"].Select(cols).Updates(m).Error
  63. return
  64. }
  65. func (m *EdbCollect) Remove() (err error) {
  66. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  67. err = global.DmSQL["data"].Exec(sql, m.EdbCollectId).Error
  68. return
  69. }
  70. func (m *EdbCollect) MultiRemove(ids []int) (err error) {
  71. if len(ids) == 0 {
  72. return
  73. }
  74. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  75. err = global.DmSQL["data"].Exec(sql, ids).Error
  76. return
  77. }
  78. func (m *EdbCollect) RemoveByCondition(condition string, pars []interface{}) (err error) {
  79. if condition == "" {
  80. return
  81. }
  82. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
  83. err = global.DmSQL["data"].Exec(sql, pars...).Error
  84. return
  85. }
  86. // RemoveAndCreateMulti
  87. // @Description: 清除原有配置并新增收藏
  88. // @receiver m
  89. // @param delCondition
  90. // @param delPars
  91. // @param items
  92. // @return err
  93. func (m *EdbCollect) RemoveAndCreateMulti(delCondition string, delPars []interface{}, items []*EdbCollect) (err error) {
  94. to := global.DmSQL["data"].Begin()
  95. defer func() {
  96. if err != nil {
  97. _ = to.Rollback()
  98. } else {
  99. _ = to.Commit()
  100. }
  101. }()
  102. // 先删除
  103. {
  104. if delCondition == "" {
  105. return
  106. }
  107. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), delCondition)
  108. err = to.Exec(sql, delPars...).Error
  109. if err != nil {
  110. return
  111. }
  112. }
  113. if len(items) == 0 {
  114. return
  115. }
  116. err = to.CreateInBatches(items, utils.MultiAddNum).Error
  117. return
  118. }
  119. func (m *EdbCollect) GetItemById(id int) (item *EdbCollect, err error) {
  120. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  121. err = global.DmSQL["data"].Raw(sql, id).First(&item).Error
  122. return
  123. }
  124. func (m *EdbCollect) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *EdbCollect, err error) {
  125. order := ``
  126. if orderRule != "" {
  127. order = ` ORDER BY ` + orderRule
  128. }
  129. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  130. err = global.DmSQL["data"].Raw(sql, pars...).First(&item).Error
  131. return
  132. }
  133. func (m *EdbCollect) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  134. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  135. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&count).Error
  136. return
  137. }
  138. func (m *EdbCollect) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EdbCollect, err error) {
  139. fields := strings.Join(fieldArr, ",")
  140. if len(fieldArr) == 0 {
  141. fields = `*`
  142. }
  143. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  144. if orderRule != "" {
  145. order = ` ORDER BY ` + orderRule
  146. }
  147. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  148. err = global.DmSQL["data"].Raw(sql, pars...).Find(&items).Error
  149. return
  150. }
  151. func (m *EdbCollect) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*EdbCollect, err error) {
  152. fields := strings.Join(fieldArr, ",")
  153. if len(fieldArr) == 0 {
  154. fields = `*`
  155. }
  156. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  157. if orderRule != "" {
  158. order = ` ORDER BY ` + orderRule
  159. }
  160. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  161. pars = append(pars, startSize, pageSize)
  162. err = global.DmSQL["data"].Raw(sql, pars...).Find(&items).Error
  163. return
  164. }
  165. // GetCollectEdbInfoByClassifyId 获取分类下收藏的指标信息
  166. func GetCollectEdbInfoByClassifyId(classifyId int) (items []*EdbInfo, err error) {
  167. sql := `SELECT b.* FROM edb_collect AS a JOIN edb_info AS b ON a.edb_info_id = b.edb_info_id WHERE a.edb_collect_classify_id = ? ORDER BY a.sort ASC`
  168. err = global.DmSQL["data"].Raw(sql, classifyId).Find(&items).Error
  169. return
  170. }
  171. // EdbCollectReq 加入/取消收藏
  172. type EdbCollectReq struct {
  173. ClassifyId int `description:"分类ID"`
  174. ClassifyIdList []int `description:"分类ID列表"`
  175. EdbInfoId int `description:"指标ID"`
  176. }
  177. // GetCollectEdbInfoCount 获取收藏的指标信息总数
  178. func GetCollectEdbInfoCount(condition string, pars []interface{}) (total int64, err error) {
  179. sql := fmt.Sprintf(`SELECT COUNT(1) AS ct FROM (
  180. SELECT b.edb_info_id FROM edb_collect AS a JOIN edb_info AS b ON a.edb_info_id = b.edb_info_id
  181. WHERE 1=1 %s
  182. GROUP BY b.edb_info_id
  183. ) AS sub`, condition)
  184. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&total).Error
  185. return
  186. }
  187. // GetCollectEdbInfoPageList 获取收藏的指标信息列表-分页
  188. func GetCollectEdbInfoPageList(condition string, pars []interface{}, startSize, pageSize int) (list []*CollectEdbInfoQuery, err error) {
  189. sql := fmt.Sprintf(`SELECT b."edb_info_id",
  190. WM_CONCAT(DISTINCT a."edb_collect_classify_id") AS "collect_classify_id",
  191. MAX(a.create_time) as collect_time,
  192. MAX(b."edb_code") AS "edb_code",
  193. MAX(b."edb_name") "edb_name",
  194. MAX(b."edb_info_type") "edb_info_type",
  195. MAX(b."edb_type") "edb_type",
  196. MAX(b."source") "source",
  197. MAX(b."source_name") "source_name",
  198. MAX(b."frequency") "frequency",
  199. MAX(b."unit") "unit",
  200. MAX(b."classify_id") "classify_id",
  201. MAX(b."create_time") "create_time",
  202. MAX(b."unique_code") "unique_code",
  203. MAX(b."chart_image") "chart_image",
  204. MAX(b."modify_time") "modify_time",
  205. MAX(a."sort") AS "sort"
  206. FROM edb_collect AS a JOIN edb_info AS b ON a.edb_info_id = b.edb_info_id
  207. WHERE 1=1 %s
  208. GROUP BY b.edb_info_id
  209. ORDER BY collect_time DESC LIMIT ?,?`, condition)
  210. pars = append(pars, startSize, pageSize)
  211. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&list).Error
  212. return
  213. }
  214. type CollectEdbInfoQuery struct {
  215. EdbInfo
  216. CollectClassifyIdStr string `gorm:"column:collect_classify_id" description:"收藏分类ID"`
  217. CollectTime time.Time `gorm:"column:collect_time" description:"收藏时间"`
  218. }
  219. // CollectEdbInfoItem 收藏列表指标信息
  220. type CollectEdbInfoItem struct {
  221. EdbInfoId int `description:"指标ID"`
  222. EdbInfoType int `description:"指标类型:0-普通指标; 1-预测指标"`
  223. EdbType int `description:"指标类型:1-基础指标; 2-计算指标"`
  224. Source int `description:"来源ID"`
  225. SourceName string `description:"来源名称"`
  226. EdbCode string `description:"指标编码"`
  227. EdbName string `description:"指标名称"`
  228. Frequency string `description:"频率"`
  229. Unit string `description:"单位"`
  230. UniqueCode string `description:"唯一编码"`
  231. ChartImage string `description:"图表图片"`
  232. ClassifyId int `description:"指标分类ID"`
  233. CollectClassifyIdList []int `description:"收藏分类ID列表"`
  234. CollectTime string `description:"收藏时间"`
  235. CreateTime string `description:"创建时间"`
  236. Sort int `description:"排序"`
  237. }
  238. func FormatEdbInfo2CollectItem(origin *CollectEdbInfoQuery) (item *CollectEdbInfoItem) {
  239. item = new(CollectEdbInfoItem)
  240. item.EdbInfoId = origin.EdbInfoId
  241. item.EdbInfoType = origin.EdbInfoType
  242. item.EdbType = origin.EdbType
  243. item.Source = origin.Source
  244. item.SourceName = origin.SourceName
  245. item.EdbCode = origin.EdbCode
  246. item.EdbName = origin.EdbName
  247. item.Frequency = origin.Frequency
  248. item.Unit = origin.Unit
  249. item.UniqueCode = origin.UniqueCode
  250. item.ChartImage = origin.ChartImage
  251. item.ClassifyId = origin.ClassifyId
  252. collectClassifyIdList := make([]int, 0)
  253. if origin.CollectClassifyIdStr != `` {
  254. collectClassifyIdStrList := strings.Split(origin.CollectClassifyIdStr, ",")
  255. for _, v := range collectClassifyIdStrList {
  256. collectClassifyId, tmpErr := strconv.Atoi(v)
  257. if tmpErr == nil {
  258. collectClassifyIdList = append(collectClassifyIdList, collectClassifyId)
  259. }
  260. }
  261. }
  262. item.CollectClassifyIdList = collectClassifyIdList
  263. item.CollectTime = origin.CollectTime.Format(utils.FormatDateTime)
  264. item.CreateTime = origin.CreateTime.Format(utils.FormatDateTime)
  265. item.Sort = origin.Sort
  266. return
  267. }
  268. // CollectEdbInfoListResp 收藏指标分页列表相应
  269. type CollectEdbInfoListResp struct {
  270. Paging *paging.PagingItem
  271. List []*CollectEdbInfoItem
  272. }
  273. // EdbCollectMoveReq 移动收藏
  274. type EdbCollectMoveReq struct {
  275. EdbInfoId int `description:"收藏的指标ID(分类下指标ID唯一)"`
  276. PrevEdbInfoId int `description:"前一个收藏的指标ID"`
  277. NextEdbInfoId int `description:"后一个收藏的指标ID"`
  278. ClassifyId int `description:"当前分类ID"`
  279. }
  280. func GetEdbCollectSort(adminId, classifyId, sort int) (item *EdbCollect, err error) {
  281. sql := ` SELECT * FROM edb_collect WHERE 1=1 AND sys_user_id = ? AND edb_collect_classify_id = ? `
  282. if sort == 1 {
  283. sql += ` ORDER BY sort DESC, edb_collect_id ASC LIMIT 1 `
  284. } else {
  285. sql += ` ORDER BY sort ASC, edb_collect_id DESC LIMIT 1 `
  286. }
  287. err = global.DmSQL["data"].Raw(sql, adminId, classifyId).First(&item).Error
  288. return
  289. }
  290. func GetEdbCollectByEdbInfoId(adminId, edbInfoId, classifyId int) (item *EdbCollect, err error) {
  291. sql := `SELECT * FROM edb_collect WHERE sys_user_id = ? AND edb_info_id = ? AND edb_collect_classify_id=? `
  292. err = global.DmSQL["data"].Raw(sql, adminId, edbInfoId, classifyId).First(&item).Error
  293. return
  294. }
  295. func UpdateEdbCollectSortByClassifyId(classifyId, nowSort int, prevMyChartClassifyMappingId int, updateSort string) (err error) {
  296. sql := ` update edb_collect set sort = ` + updateSort + ` WHERE edb_collect_classify_id = ? `
  297. if prevMyChartClassifyMappingId > 0 {
  298. sql += ` AND ( sort > ? or ( edb_collect_id < ? and sort=? )) `
  299. }
  300. err = global.DmSQL["data"].Exec(sql, classifyId, nowSort, prevMyChartClassifyMappingId, nowSort).Error
  301. return
  302. }
  303. func UpdateEdbCollectMove(sort, adminId, edbInfoId, myChartClassifyId int) (err error) {
  304. sql := ` UPDATE edb_collect SET sort = ?,modify_time=NOW() WHERE sys_user_id=? AND edb_info_id=? AND edb_collect_classify_id=? `
  305. err = global.DmSQL["data"].Exec(sql, sort, adminId, edbInfoId, myChartClassifyId).Error
  306. return
  307. }