collect_chart.go 15 KB

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