chart_info_share.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 (?) AND share_type = ? ", m.TableName())
  73. err = tx.Exec(sql, chartInfoIdList, shareType).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. // GetAllEditSharedChartInfoIdListByReceivedUserId
  158. // @Description: 获取所有我收到的共享的、可以编辑的图表id列表
  159. // @author: Roc
  160. // @receiver m
  161. // @datetime 2024-12-03 09:56:41
  162. // @param sysUserId int
  163. // @return list []*ShareChartInfoQuery
  164. // @return err error
  165. func (m ChartInfoShare) GetAllEditSharedChartInfoIdListByReceivedUserId(sysUserId int, chartInfoIdList []int) (list []int, err error) {
  166. sql := `SELECT a."chart_info_id" FROM chart_info_share AS a
  167. WHERE 1=1 AND a.sys_user_id = ? AND share_type = ? `
  168. pars := []interface{}{sysUserId, utils.ShareTypeEdit}
  169. if len(chartInfoIdList) > 0 {
  170. sql += ` AND a.chart_info_id in (?) `
  171. pars = append(pars, chartInfoIdList)
  172. }
  173. sql += ` GROUP BY a.chart_info_id `
  174. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&list).Error
  175. return
  176. }
  177. // GetShareChartInfoListPageList
  178. // @Description: 获取分享的指标列表(分页)
  179. // @author: Roc
  180. // @receiver m
  181. // @datetime 2024-12-03 14:42:12
  182. // @param condition string
  183. // @param pars []interface{}
  184. // @param startSize int
  185. // @param pageSize int
  186. // @return total int32
  187. // @return list []*ChartInfoList
  188. // @return err error
  189. func (m ChartInfoShare) GetShareChartInfoListPageList(condition string, pars []interface{}, startSize, pageSize int) (total int32, list []*ChartInfoView, err error) {
  190. baseSql := `SELECT b."chart_info_id",
  191. MAX(a.share_type) AS share_type,
  192. MAX(a.create_time) as share_time,
  193. MAX(b."chart_name") "chart_name",
  194. MAX(b."source") "source",
  195. MAX(b."chart_type") "chart_type",
  196. MAX(b."chart_classify_id") "chart_classify_id",
  197. MAX(b."chart_public_classify_id") "chart_public_classify_id",
  198. MAX(b."unique_code") "unique_code",
  199. MAX(b."chart_image") "chart_image",
  200. MAX(b."sys_user_id") "sys_user_id",
  201. MAX(b."sys_user_real_name") "sys_user_real_name",
  202. MAX(b."create_time") "create_time",
  203. MAX(b."modify_time") "modify_time",
  204. MAX(b."sort") AS "sort"
  205. FROM chart_info_share AS a
  206. JOIN chart_info AS b ON a.chart_info_id = b.chart_info_id
  207. WHERE 1=1 `
  208. baseSql += condition
  209. baseSql += ` GROUP BY b.chart_info_id `
  210. totalSql := `SELECT COUNT(1) AS total FROM (` + baseSql + `) AS d`
  211. var totalNull sql2.NullInt32
  212. err = global.DmSQL["data"].Raw(totalSql, pars...).Scan(&totalNull).Error
  213. if err != nil {
  214. return
  215. }
  216. total = totalNull.Int32
  217. sql := baseSql + ` ORDER BY share_time DESC LIMIT ?,? `
  218. pars = append(pars, startSize, pageSize)
  219. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&list).Error
  220. if err != nil {
  221. return
  222. }
  223. for _, v := range list {
  224. v.ConvertToResp()
  225. }
  226. return
  227. }
  228. type UserShareChartClassifyItem struct {
  229. UserId int `description:"用户id"`
  230. UserName string `description:"用户名称"`
  231. ChartClassifyId int `description:"分类id"`
  232. ChartInfoId int `description:"指标id"`
  233. ChartClassifyName string
  234. ChartClassifyNameEn string
  235. ParentId int
  236. Level int `description:"层级"`
  237. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  238. UniqueCode string `description:"唯一编码"`
  239. Source int `description:"来源id"`
  240. SourceName string `description:"来源名称"`
  241. SysUserId int `description:"创建人id"`
  242. SysUserRealName string `description:"创建人姓名"`
  243. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"`
  244. StartDate string `description:"自定义开始日期"`
  245. EndDate string `description:"自定义结束日期"`
  246. ChartType int `description:"生成样式:1:曲线图,2:季节性图"`
  247. Calendar string `description:"公历/农历"`
  248. SeasonStartDate string `description:"季节性图开始日期"`
  249. SeasonEndDate string `description:"季节性图开始日期"`
  250. Children []*UserShareChartClassifyItem `gorm:"-"`
  251. Button ChartClassifyItemsButton `gorm:"-" description:"按钮权限"`
  252. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  253. HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
  254. ChartClassifyIdPath string `description:"分类的完整路径,格式为:父级ID,当前ID"`
  255. }
  256. type UserShareChartClassifyItemList []*UserShareChartClassifyItem
  257. func (m UserShareChartClassifyItemList) Len() int {
  258. return len(m)
  259. }
  260. func (m UserShareChartClassifyItemList) Less(i, j int) bool {
  261. return m[i].Sort < m[j].Sort
  262. }
  263. func (m UserShareChartClassifyItemList) Swap(i, j int) {
  264. m[i], m[j] = m[j], m[i]
  265. }