chart_info_share.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package data_manage
  2. import (
  3. sql2 "database/sql"
  4. "eta_gn/eta_api/global"
  5. "eta_gn/eta_api/utils"
  6. "fmt"
  7. "time"
  8. )
  9. // ChartInfoShare
  10. // @Description: 指标分享表
  11. type ChartInfoShare struct {
  12. ChartInfoShareId int `gorm:"primaryKey" `
  13. ChartInfoId int `description:"图表id"`
  14. SysUserId int `description:"sys_user_id"`
  15. ShareType int8 `description:"分享类型,1:仅查看;2:可编辑"`
  16. CreateTime time.Time `description:"创建时间"`
  17. }
  18. // TableName
  19. // @Description: 表名
  20. // @author: Roc
  21. // @receiver m
  22. // @datetime 2024-11-27 14:04:14
  23. // @return string
  24. func (m ChartInfoShare) TableName() string {
  25. return `chart_info_share`
  26. }
  27. // GetListByChartInfoId
  28. // @Description: 根据指标id获取分享用户列表
  29. // @author: Roc
  30. // @receiver m
  31. // @datetime 2024-11-27 15:39:17
  32. // @param chartInfoId int
  33. // @return items []*ChartInfoShare
  34. // @return err error
  35. func (m ChartInfoShare) GetListByChartInfoId(chartInfoId int) (items []*ChartInfoShare, err error) {
  36. sql := ` SELECT * FROM chart_info_share WHERE chart_info_id = ? ORDER BY chart_info_share_id ASC `
  37. err = global.DmSQL["data"].Raw(sql, chartInfoId).Find(&items).Error
  38. return
  39. }
  40. // SaveChartInfoShare
  41. // @Description: 保存配置
  42. // @author: Roc
  43. // @receiver m
  44. // @datetime 2024-11-27 15:54:07
  45. // @param chartInfoIdList []int
  46. // @param userIdList []int
  47. // @param shareType int8
  48. // @return err error
  49. func (m ChartInfoShare) SaveChartInfoShare(chartInfoIdList, userIdList []int, shareType int8) (err error) {
  50. // TODO 保存之前,得先做关联校验
  51. addList := make([]*ChartInfoShare, 0)
  52. for _, chartInfoId := range chartInfoIdList {
  53. for _, userId := range userIdList {
  54. addList = append(addList, &ChartInfoShare{
  55. ChartInfoShareId: 0,
  56. ChartInfoId: chartInfoId,
  57. SysUserId: userId,
  58. ShareType: shareType,
  59. CreateTime: time.Now(),
  60. })
  61. }
  62. }
  63. tx := global.DmSQL["data"].Begin()
  64. defer func() {
  65. if err != nil {
  66. _ = tx.Rollback()
  67. return
  68. }
  69. _ = tx.Commit()
  70. }()
  71. // 先删除原来的配置,新增最新的配置
  72. sql := fmt.Sprintf("DELETE FROM %s WHERE chart_info_id IN (?) ", m.TableName())
  73. err = tx.Exec(sql, chartInfoIdList).Error
  74. if err != nil {
  75. return
  76. }
  77. // 批量添加新的配置
  78. if len(addList) > 0 {
  79. err = tx.CreateInBatches(addList, utils.MultiAddNum).Error
  80. if err != nil {
  81. return
  82. }
  83. }
  84. return
  85. }
  86. // ShareChartInfoQuery
  87. // @Description: 共享指标
  88. type ShareChartInfoQuery struct {
  89. ChartInfo
  90. ShareType string `gorm:"column:share_type" description:"共享类型"`
  91. ShareTime time.Time `gorm:"column:share_time" description:"设置共享时间"`
  92. }
  93. // GetAllSharedChartInfoListByFromUserId
  94. // @Description: 获取所有我共享出去的指标列表
  95. // @author: Roc
  96. // @receiver m
  97. // @datetime 2024-12-03 09:56:41
  98. // @param sysUserId int
  99. // @return list []*ShareChartInfoQuery
  100. // @return err error
  101. func (m ChartInfoShare) GetAllSharedChartInfoListByFromUserId(sysUserId int) (list []*ShareChartInfoQuery, err error) {
  102. sql := `SELECT b."chart_info_id",
  103. MAX(a.share_type) AS share_type,
  104. MAX(a.create_time) as share_time,
  105. MAX(b."chart_name") "chart_name",
  106. MAX(b."source") "source",
  107. MAX(b."chart_type") "chart_type",
  108. MAX(b."chart_classify_id") "chart_classify_id",
  109. MAX(b."chart_public_classify_id") "chart_public_classify_id",
  110. MAX(b."unique_code") "unique_code",
  111. MAX(b."chart_image") "chart_image",
  112. MAX(b."sys_user_id") "sys_user_id",
  113. MAX(b."sys_user_real_name") "sys_user_real_name",
  114. MAX(b."create_time") "create_time",
  115. MAX(b."modify_time") "modify_time",
  116. MAX(b."sort") AS "sort"
  117. FROM chart_info_share AS a
  118. JOIN chart_info AS b ON a.chart_info_id = b.chart_info_id
  119. WHERE 1=1 AND b.sys_user_id = ?
  120. GROUP BY b.chart_info_id
  121. ORDER BY share_time DESC`
  122. err = global.DmSQL["data"].Raw(sql, sysUserId).Scan(&list).Error
  123. return
  124. }
  125. // GetAllSharedChartInfoListByReceivedUserId
  126. // @Description: 获取所有我收到的共享的指标列表
  127. // @author: Roc
  128. // @receiver m
  129. // @datetime 2024-12-03 09:56:41
  130. // @param sysUserId int
  131. // @return list []*ShareChartInfoQuery
  132. // @return err error
  133. func (m ChartInfoShare) GetAllSharedChartInfoListByReceivedUserId(sysUserId int) (list []*ShareChartInfoQuery, err error) {
  134. sql := `SELECT b."chart_info_id",
  135. MAX(a.share_type) AS share_type,
  136. MAX(a.create_time) as share_time,
  137. MAX(b."chart_name") "chart_name",
  138. MAX(b."source") "source",
  139. MAX(b."chart_type") "chart_type",
  140. MAX(b."chart_classify_id") "chart_classify_id",
  141. MAX(b."chart_public_classify_id") "chart_public_classify_id",
  142. MAX(b."unique_code") "unique_code",
  143. MAX(b."chart_image") "chart_image",
  144. MAX(b."sys_user_id") "sys_user_id",
  145. MAX(b."sys_user_real_name") "sys_user_real_name",
  146. MAX(b."create_time") "create_time",
  147. MAX(b."modify_time") "modify_time",
  148. MAX(b."sort") AS "sort"
  149. FROM chart_info_share AS a
  150. JOIN chart_info AS b ON a.chart_info_id = b.chart_info_id
  151. WHERE 1=1 AND a.sys_user_id = ? AND b.sys_user_id != ?
  152. GROUP BY b.chart_info_id
  153. ORDER BY share_time DESC`
  154. err = global.DmSQL["data"].Raw(sql, sysUserId, sysUserId).Scan(&list).Error
  155. return
  156. }
  157. // GetShareChartInfoListPageList
  158. // @Description: 获取分享的指标列表(分页)
  159. // @author: Roc
  160. // @receiver m
  161. // @datetime 2024-12-03 14:42:12
  162. // @param condition string
  163. // @param pars []interface{}
  164. // @param startSize int
  165. // @param pageSize int
  166. // @return total int32
  167. // @return list []*ChartInfoList
  168. // @return err error
  169. func (m ChartInfoShare) GetShareChartInfoListPageList(condition string, pars []interface{}, startSize, pageSize int) (total int32, list []*ChartInfoView, err error) {
  170. baseSql := `SELECT b."chart_info_id",
  171. MAX(a.share_type) AS share_type,
  172. MAX(a.create_time) as share_time,
  173. MAX(b."chart_name") "chart_name",
  174. MAX(b."source") "source",
  175. MAX(b."chart_type") "chart_type",
  176. MAX(b."chart_classify_id") "chart_classify_id",
  177. MAX(b."chart_public_classify_id") "chart_public_classify_id",
  178. MAX(b."unique_code") "unique_code",
  179. MAX(b."chart_image") "chart_image",
  180. MAX(b."sys_user_id") "sys_user_id",
  181. MAX(b."sys_user_real_name") "sys_user_real_name",
  182. MAX(b."create_time") "create_time",
  183. MAX(b."modify_time") "modify_time",
  184. MAX(b."sort") AS "sort"
  185. FROM chart_info_share AS a
  186. JOIN chart_info AS b ON a.chart_info_id = b.chart_info_id
  187. WHERE 1=1 `
  188. baseSql += condition
  189. baseSql += ` GROUP BY b.chart_info_id `
  190. totalSql := `SELECT COUNT(1) AS total FROM (` + baseSql + `) AS d`
  191. var totalNull sql2.NullInt32
  192. err = global.DmSQL["data"].Raw(totalSql, pars...).Scan(&totalNull).Error
  193. if err != nil {
  194. return
  195. }
  196. total = totalNull.Int32
  197. sql := baseSql + ` ORDER BY share_time DESC LIMIT ?,? `
  198. pars = append(pars, startSize, pageSize)
  199. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&list).Error
  200. if err != nil {
  201. return
  202. }
  203. for _, v := range list {
  204. v.ConvertToResp()
  205. }
  206. return
  207. }