collect_edb.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. // GetUserAllCollectEdbInfoIdList
  215. // @Description: 获取用户所有收藏的指标id列表
  216. // @author: Roc
  217. // @datetime 2024-11-28 15:27:52
  218. // @param userId int
  219. // @return list []int
  220. // @return err error
  221. func GetUserAllCollectEdbInfoIdList(userId int) (list []int, err error) {
  222. sql := `SELECT edb_info_id FROM edb_collect WHERE 1=1 AND sys_user_id = ? GROUP BY edb_info_id`
  223. err = global.DmSQL["data"].Raw(sql, userId).Scan(&list).Error
  224. return
  225. }
  226. type CollectEdbInfoQuery struct {
  227. EdbInfo
  228. CollectClassifyIdStr string `gorm:"column:collect_classify_id" description:"收藏分类ID"`
  229. CollectTime time.Time `gorm:"column:collect_time" description:"收藏时间"`
  230. }
  231. // CollectEdbInfoItem 收藏列表指标信息
  232. type CollectEdbInfoItem struct {
  233. EdbInfoId int `description:"指标ID"`
  234. EdbInfoType int `description:"指标类型:0-普通指标; 1-预测指标"`
  235. EdbType int `description:"指标类型:1-基础指标; 2-计算指标"`
  236. Source int `description:"来源ID"`
  237. SourceName string `description:"来源名称"`
  238. EdbCode string `description:"指标编码"`
  239. EdbName string `description:"指标名称"`
  240. Frequency string `description:"频率"`
  241. Unit string `description:"单位"`
  242. UniqueCode string `description:"唯一编码"`
  243. ChartImage string `description:"图表图片"`
  244. ClassifyId int `description:"指标分类ID"`
  245. CollectClassifyIdList []int `description:"收藏分类ID列表"`
  246. CollectTime string `description:"收藏时间"`
  247. CreateTime string `description:"创建时间"`
  248. Sort int `description:"排序"`
  249. }
  250. func FormatEdbInfo2CollectItem(origin *CollectEdbInfoQuery) (item *CollectEdbInfoItem) {
  251. item = new(CollectEdbInfoItem)
  252. item.EdbInfoId = origin.EdbInfoId
  253. item.EdbInfoType = origin.EdbInfoType
  254. item.EdbType = origin.EdbType
  255. item.Source = origin.Source
  256. item.SourceName = origin.SourceName
  257. item.EdbCode = origin.EdbCode
  258. item.EdbName = origin.EdbName
  259. item.Frequency = origin.Frequency
  260. item.Unit = origin.Unit
  261. item.UniqueCode = origin.UniqueCode
  262. item.ChartImage = origin.ChartImage
  263. item.ClassifyId = origin.ClassifyId
  264. collectClassifyIdList := make([]int, 0)
  265. if origin.CollectClassifyIdStr != `` {
  266. collectClassifyIdStrList := strings.Split(origin.CollectClassifyIdStr, ",")
  267. for _, v := range collectClassifyIdStrList {
  268. collectClassifyId, tmpErr := strconv.Atoi(v)
  269. if tmpErr == nil {
  270. collectClassifyIdList = append(collectClassifyIdList, collectClassifyId)
  271. }
  272. }
  273. }
  274. item.CollectClassifyIdList = collectClassifyIdList
  275. item.CollectTime = origin.CollectTime.Format(utils.FormatDateTime)
  276. item.CreateTime = origin.CreateTime.Format(utils.FormatDateTime)
  277. item.Sort = origin.Sort
  278. return
  279. }
  280. // CollectEdbInfoListResp 收藏指标分页列表相应
  281. type CollectEdbInfoListResp struct {
  282. Paging *paging.PagingItem
  283. List []*CollectEdbInfoItem
  284. }
  285. // EdbCollectMoveReq 移动收藏
  286. type EdbCollectMoveReq struct {
  287. EdbInfoId int `description:"收藏的指标ID(分类下指标ID唯一)"`
  288. PrevEdbInfoId int `description:"前一个收藏的指标ID"`
  289. NextEdbInfoId int `description:"后一个收藏的指标ID"`
  290. ClassifyId int `description:"当前分类ID"`
  291. }
  292. func GetEdbCollectSort(adminId, classifyId, sort int) (item *EdbCollect, err error) {
  293. sql := ` SELECT * FROM edb_collect WHERE 1=1 AND sys_user_id = ? AND edb_collect_classify_id = ? `
  294. if sort == 1 {
  295. sql += ` ORDER BY sort DESC, edb_collect_id ASC LIMIT 1 `
  296. } else {
  297. sql += ` ORDER BY sort ASC, edb_collect_id DESC LIMIT 1 `
  298. }
  299. err = global.DmSQL["data"].Raw(sql, adminId, classifyId).First(&item).Error
  300. return
  301. }
  302. func GetEdbCollectByEdbInfoId(adminId, edbInfoId, classifyId int) (item *EdbCollect, err error) {
  303. sql := `SELECT * FROM edb_collect WHERE sys_user_id = ? AND edb_info_id = ? AND edb_collect_classify_id=? `
  304. err = global.DmSQL["data"].Raw(sql, adminId, edbInfoId, classifyId).First(&item).Error
  305. return
  306. }
  307. func UpdateEdbCollectSortByClassifyId(classifyId, nowSort int, prevMyChartClassifyMappingId int, updateSort string) (err error) {
  308. sql := ` update edb_collect set sort = ` + updateSort + ` WHERE edb_collect_classify_id = ? `
  309. if prevMyChartClassifyMappingId > 0 {
  310. sql += ` AND ( sort > ? or ( edb_collect_id < ? and sort=? )) `
  311. }
  312. err = global.DmSQL["data"].Exec(sql, classifyId, nowSort, prevMyChartClassifyMappingId, nowSort).Error
  313. return
  314. }
  315. func UpdateEdbCollectMove(sort, adminId, edbInfoId, myChartClassifyId int) (err error) {
  316. sql := ` UPDATE edb_collect SET sort = ?,modify_time=NOW() WHERE sys_user_id=? AND edb_info_id=? AND edb_collect_classify_id=? `
  317. err = global.DmSQL["data"].Exec(sql, sort, adminId, edbInfoId, myChartClassifyId).Error
  318. return
  319. }
  320. // EdbCollectItem 指标收藏信息
  321. type EdbCollectItem struct {
  322. EdbCollectId int `gorm:"primaryKey;autoIncrement;column:edb_collect_id;type:int(10) unsigned;not null"`
  323. EdbCollectClassifyId int `gorm:"index:idx_classify_id;column:edb_collect_classify_id;type:int(10) unsigned;not null;default:0"` // 指标收藏分类ID
  324. EdbInfoId int `gorm:"column:edb_info_id;type:int(10) unsigned;not null;default:0"` // 指标ID
  325. EdbCode string `gorm:"column:edb_code;type:varchar(255);not null;default:''"` // 指标编码
  326. SysUserId int `gorm:"column:sys_user_id;type:int(10) unsigned;not null;default:0"` // 创建人ID
  327. SysRealName string `gorm:"column:sys_real_name;type:int(10) unsigned;not null;default:0"` // 创建人姓名
  328. ClassifyName string `gorm:"column:classify_name;type:varchar(255);not null;default:''"` // 分类名称
  329. Sort int `gorm:"column:sort;type:int(10);default:0"` // 排序
  330. CreateTime time.Time `gorm:"column:create_time;type:datetime"` // 创建时间
  331. ModifyTime time.Time `gorm:"column:modify_time;type:datetime"` // 更新时间
  332. }
  333. func (m *EdbCollect) GetItemsByUserIdAndEdbInfoIdList(userId int, edbInfoIdList []int) (items []*EdbCollectItem, err error) {
  334. if len(edbInfoIdList) <= 0 {
  335. return
  336. }
  337. sql := `SELECT a.*,b.classify_name FROM edb_collect AS a
  338. JOIN edb_collect_classify b on a.edb_collect_classify_id = b.edb_collect_classify_id
  339. WHERE a.sys_user_id = ? AND a.edb_info_id in (?) ORDER BY a.edb_collect_id DESC `
  340. err = global.DmSQL["data"].Raw(sql, userId, edbInfoIdList).Find(&items).Error
  341. return
  342. }