collect_edb.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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."end_date") "end_date",
  201. MAX(b."end_value") "end_value",
  202. MAX(b."latest_date") "latest_date",
  203. MAX(b."latest_value") "latest_value",
  204. MAX(b."sys_user_id") "sys_user_id",
  205. MAX(b."sys_user_real_name") "sys_user_real_name",
  206. MAX(b."classify_id") "classify_id",
  207. MAX(b."create_time") "create_time",
  208. MAX(b."unique_code") "unique_code",
  209. MAX(b."chart_image") "chart_image",
  210. MAX(b."modify_time") "modify_time",
  211. MAX(a."sort") AS "sort"
  212. FROM edb_collect AS a JOIN edb_info AS b ON a.edb_info_id = b.edb_info_id
  213. WHERE 1=1 %s
  214. GROUP BY b.edb_info_id
  215. ORDER BY collect_time DESC LIMIT ?,?`, condition)
  216. pars = append(pars, startSize, pageSize)
  217. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&list).Error
  218. return
  219. }
  220. // GetUserAllCollectEdbInfoIdList
  221. // @Description: 获取用户所有收藏的指标id列表
  222. // @author: Roc
  223. // @datetime 2024-11-28 15:27:52
  224. // @param userId int
  225. // @return list []int
  226. // @return err error
  227. func GetUserAllCollectEdbInfoIdList(userId int) (list []int, err error) {
  228. sql := `SELECT edb_info_id FROM edb_collect WHERE 1=1 AND sys_user_id = ? GROUP BY edb_info_id`
  229. err = global.DmSQL["data"].Raw(sql, userId).Scan(&list).Error
  230. return
  231. }
  232. // GetEdbAllCollectUserIdList
  233. // @Description: 根据指标ID获取所有收藏的用户id列表
  234. // @author: Roc
  235. // @receiver m
  236. // @datetime 2024-12-27 17:03:03
  237. // @param edbInfoId int
  238. // @return list []int
  239. // @return err error
  240. func (m *EdbCollect) GetEdbAllCollectUserIdList(edbInfoId int) (list []int, err error) {
  241. sql := `SELECT sys_user_id FROM edb_collect WHERE 1=1 AND edb_info_id = ? GROUP BY sys_user_id`
  242. err = global.DmSQL["data"].Raw(sql, edbInfoId).Scan(&list).Error
  243. return
  244. }
  245. // DeleteByEdbInfoIdAndUserIdList
  246. // @Description: 根据指标ID和用户id列表删除对应关系
  247. // @author: Roc
  248. // @receiver m
  249. // @datetime 2024-12-27 17:12:03
  250. // @param edbInfoId int
  251. // @param userIdList []int
  252. // @return err error
  253. func (m *EdbCollect) DeleteByEdbInfoIdAndUserIdList(edbInfoId int, userIdList []int) (err error) {
  254. // 先删除
  255. sql := fmt.Sprintf(`DELETE FROM %s WHERE edb_info_id = ? AND sys_user_id in (?) `, m.TableName())
  256. err = global.DmSQL["data"].Exec(sql, edbInfoId, userIdList).Error
  257. return
  258. }
  259. type CollectEdbInfoQuery struct {
  260. EdbInfo
  261. CollectClassifyIdStr string `gorm:"column:collect_classify_id" description:"收藏分类ID"`
  262. CollectTime time.Time `gorm:"column:collect_time" description:"收藏时间"`
  263. }
  264. // CollectEdbInfoItem 收藏列表指标信息
  265. type CollectEdbInfoItem struct {
  266. EdbInfoId int `description:"指标ID"`
  267. EdbInfoType int `description:"指标类型:0-普通指标; 1-预测指标"`
  268. EdbType int `description:"指标类型:1-基础指标; 2-计算指标"`
  269. Source int `description:"来源ID"`
  270. SourceName string `description:"来源名称"`
  271. EdbCode string `description:"指标编码"`
  272. EdbName string `description:"指标名称"`
  273. Frequency string `description:"频率"`
  274. Unit string `description:"单位"`
  275. UniqueCode string `description:"唯一编码"`
  276. ChartImage string `description:"图表图片"`
  277. ClassifyId int `description:"指标分类ID"`
  278. CollectClassifyIdList []int `description:"收藏分类ID列表"`
  279. CollectTime string `description:"收藏时间"`
  280. CreateTime string `description:"创建时间"`
  281. Sort int `description:"排序"`
  282. LatestDate string `description:"数据最新日期(实际日期)"`
  283. LatestValue float64 `description:"数据最新值(实际值)"`
  284. EndValue float64 `description:"数据的最新值(预测日期的最新值)"`
  285. EndDate string `description:"终止日期"`
  286. SysUserId int
  287. SysUserRealName string
  288. }
  289. func FormatEdbInfo2CollectItem(origin *CollectEdbInfoQuery) (item *CollectEdbInfoItem) {
  290. item = new(CollectEdbInfoItem)
  291. item.EdbInfoId = origin.EdbInfoId
  292. item.EdbInfoType = origin.EdbInfoType
  293. item.EdbType = origin.EdbType
  294. item.Source = origin.Source
  295. item.SourceName = origin.SourceName
  296. item.EdbCode = origin.EdbCode
  297. item.EdbName = origin.EdbName
  298. item.Frequency = origin.Frequency
  299. item.Unit = origin.Unit
  300. item.UniqueCode = origin.UniqueCode
  301. item.ChartImage = origin.ChartImage
  302. item.ClassifyId = origin.ClassifyId
  303. item.EndDate = utils.TimeToFormatDate(origin.EndDate)
  304. item.EndValue = origin.EndValue
  305. item.LatestDate = origin.LatestDate
  306. item.LatestValue = origin.LatestValue
  307. item.SysUserId = origin.SysUserId
  308. item.SysUserRealName = origin.SysUserRealName
  309. collectClassifyIdList := make([]int, 0)
  310. if origin.CollectClassifyIdStr != `` {
  311. collectClassifyIdStrList := strings.Split(origin.CollectClassifyIdStr, ",")
  312. for _, v := range collectClassifyIdStrList {
  313. collectClassifyId, tmpErr := strconv.Atoi(v)
  314. if tmpErr == nil {
  315. collectClassifyIdList = append(collectClassifyIdList, collectClassifyId)
  316. }
  317. }
  318. }
  319. item.CollectClassifyIdList = collectClassifyIdList
  320. item.CollectTime = origin.CollectTime.Format(utils.FormatDateTime)
  321. item.CreateTime = origin.CreateTime.Format(utils.FormatDateTime)
  322. item.Sort = origin.Sort
  323. return
  324. }
  325. // CollectEdbInfoListResp 收藏指标分页列表相应
  326. type CollectEdbInfoListResp struct {
  327. Paging *paging.PagingItem
  328. List []*CollectEdbInfoItem
  329. }
  330. // EdbCollectMoveReq 移动收藏
  331. type EdbCollectMoveReq struct {
  332. EdbInfoId int `description:"收藏的指标ID(分类下指标ID唯一)"`
  333. PrevEdbInfoId int `description:"前一个收藏的指标ID"`
  334. NextEdbInfoId int `description:"后一个收藏的指标ID"`
  335. ClassifyId int `description:"当前分类ID"`
  336. }
  337. func GetEdbCollectSort(adminId, classifyId, sort int) (item *EdbCollect, err error) {
  338. sql := ` SELECT * FROM edb_collect WHERE 1=1 AND sys_user_id = ? AND edb_collect_classify_id = ? `
  339. if sort == 1 {
  340. sql += ` ORDER BY sort DESC, edb_collect_id ASC LIMIT 1 `
  341. } else {
  342. sql += ` ORDER BY sort ASC, edb_collect_id DESC LIMIT 1 `
  343. }
  344. err = global.DmSQL["data"].Raw(sql, adminId, classifyId).First(&item).Error
  345. return
  346. }
  347. func GetEdbCollectByEdbInfoId(adminId, edbInfoId, classifyId int) (item *EdbCollect, err error) {
  348. sql := `SELECT * FROM edb_collect WHERE sys_user_id = ? AND edb_info_id = ? AND edb_collect_classify_id=? `
  349. err = global.DmSQL["data"].Raw(sql, adminId, edbInfoId, classifyId).First(&item).Error
  350. return
  351. }
  352. func UpdateEdbCollectSortByClassifyId(classifyId, nowSort int, prevMyChartClassifyMappingId int, updateSort string) (err error) {
  353. sql := ` update edb_collect set sort = ` + updateSort + ` WHERE edb_collect_classify_id = ? `
  354. if prevMyChartClassifyMappingId > 0 {
  355. sql += ` AND ( sort > ? or ( edb_collect_id < ? and sort=? )) `
  356. }
  357. err = global.DmSQL["data"].Exec(sql, classifyId, nowSort, prevMyChartClassifyMappingId, nowSort).Error
  358. return
  359. }
  360. func UpdateEdbCollectMove(sort, adminId, edbInfoId, myChartClassifyId int) (err error) {
  361. sql := ` UPDATE edb_collect SET sort = ?,modify_time=NOW() WHERE sys_user_id=? AND edb_info_id=? AND edb_collect_classify_id=? `
  362. err = global.DmSQL["data"].Exec(sql, sort, adminId, edbInfoId, myChartClassifyId).Error
  363. return
  364. }
  365. // EdbCollectItem 指标收藏信息
  366. type EdbCollectItem struct {
  367. EdbCollectId int `gorm:"primaryKey;autoIncrement;column:edb_collect_id;type:int(10) unsigned;not null"`
  368. EdbCollectClassifyId int `gorm:"index:idx_classify_id;column:edb_collect_classify_id;type:int(10) unsigned;not null;default:0"` // 指标收藏分类ID
  369. EdbInfoId int `gorm:"column:edb_info_id;type:int(10) unsigned;not null;default:0"` // 指标ID
  370. EdbCode string `gorm:"column:edb_code;type:varchar(255);not null;default:''"` // 指标编码
  371. SysUserId int `gorm:"column:sys_user_id;type:int(10) unsigned;not null;default:0"` // 创建人ID
  372. SysRealName string `gorm:"column:sys_real_name;type:int(10) unsigned;not null;default:0"` // 创建人姓名
  373. ClassifyName string `gorm:"column:classify_name;type:varchar(255);not null;default:''"` // 分类名称
  374. Sort int `gorm:"column:sort;type:int(10);default:0"` // 排序
  375. CreateTime time.Time `gorm:"column:create_time;type:datetime"` // 创建时间
  376. ModifyTime time.Time `gorm:"column:modify_time;type:datetime"` // 更新时间
  377. }
  378. func (m *EdbCollect) GetItemsByUserIdAndEdbInfoIdList(userId int, edbInfoIdList []int) (items []*EdbCollectItem, err error) {
  379. if len(edbInfoIdList) <= 0 {
  380. return
  381. }
  382. sql := `SELECT a.*,b.classify_name FROM edb_collect AS a
  383. JOIN edb_collect_classify b on a.edb_collect_classify_id = b.edb_collect_classify_id
  384. WHERE a.sys_user_id = ? AND a.edb_info_id in (?) ORDER BY a.edb_collect_id DESC `
  385. err = global.DmSQL["data"].Raw(sql, userId, edbInfoIdList).Find(&items).Error
  386. return
  387. }
  388. // DataCollectHandleItem
  389. // @Description: 处理收藏的结构体
  390. type DataCollectHandleItem struct {
  391. DataId int
  392. DataType int `description:"数据类型,1:指标,2:图表"`
  393. }