chart_info_share.go 11 KB

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