edb_info_share.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. // EdbInfoShare
  10. // @Description: 指标分享表
  11. type EdbInfoShare struct {
  12. EdbInfoShareId int `gorm:"primaryKey" `
  13. EdbInfoId 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 EdbInfoShare) TableName() string {
  25. return `edb_info_share`
  26. }
  27. // GetListByEdbInfoId
  28. // @Description: 根据指标id获取分享用户列表
  29. // @author: Roc
  30. // @receiver m
  31. // @datetime 2024-11-27 15:39:17
  32. // @param edbInfoId int
  33. // @return items []*EdbInfoShare
  34. // @return err error
  35. func (m EdbInfoShare) GetListByEdbInfoId(edbInfoId int) (items []*EdbInfoShare, err error) {
  36. sql := ` SELECT * FROM edb_info_share WHERE edb_info_id = ? ORDER BY edb_info_share_id ASC `
  37. err = global.DmSQL["data"].Raw(sql, edbInfoId).Find(&items).Error
  38. return
  39. }
  40. // SaveEdbInfoShare
  41. // @Description: 保存配置
  42. // @author: Roc
  43. // @receiver m
  44. // @datetime 2024-11-27 15:54:07
  45. // @param edbInfoIdList []int
  46. // @param userIdList []int
  47. // @param shareType int8
  48. // @return err error
  49. func (m EdbInfoShare) SaveEdbInfoShare(edbInfoIdList, userIdList []int, shareType int8) (err error) {
  50. // TODO 保存之前,得先做关联校验
  51. addList := make([]*EdbInfoShare, 0)
  52. for _, edbInfoId := range edbInfoIdList {
  53. for _, userId := range userIdList {
  54. addList = append(addList, &EdbInfoShare{
  55. EdbInfoShareId: 0,
  56. EdbInfoId: edbInfoId,
  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 edb_info_id IN (?) AND share_type = ? ", m.TableName())
  73. err = tx.Exec(sql, edbInfoIdList, 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. // ShareEdbInfoQuery
  87. // @Description: 共享指标
  88. type ShareEdbInfoQuery struct {
  89. EdbInfo
  90. ShareType string `gorm:"column:share_type" description:"共享类型"`
  91. ShareTime time.Time `gorm:"column:share_time" description:"设置共享时间"`
  92. }
  93. // GetAllSharedEdbInfoListByFromUserId
  94. // @Description: 获取所有我共享出去的指标列表
  95. // @author: Roc
  96. // @receiver m
  97. // @datetime 2024-12-03 09:56:41
  98. // @param sysUserId int
  99. // @return list []*ShareEdbInfoQuery
  100. // @return err error
  101. func (m EdbInfoShare) GetAllSharedEdbInfoListByFromUserId(sysUserId int) (list []*ShareEdbInfoQuery, err error) {
  102. sql := `SELECT b."edb_info_id",
  103. MAX(a.share_type) AS share_type,
  104. MAX(a.create_time) as share_time,
  105. MAX(b."edb_code") AS "edb_code",
  106. MAX(b."edb_name") "edb_name",
  107. MAX(b."edb_info_type") "edb_info_type",
  108. MAX(b."edb_type") "edb_type",
  109. MAX(b."source") "source",
  110. MAX(b."source_name") "source_name",
  111. MAX(b."frequency") "frequency",
  112. MAX(b."unit") "unit",
  113. MAX(b."classify_id") "classify_id",
  114. MAX(b."create_time") "create_time",
  115. MAX(b."unique_code") "unique_code",
  116. MAX(b."chart_image") "chart_image",
  117. MAX(b."modify_time") "modify_time",
  118. MAX(b."start_date") "start_date",
  119. MAX(b."sort") AS "sort"
  120. FROM edb_info_share AS a
  121. JOIN edb_info AS b ON a.edb_info_id = b.edb_info_id
  122. WHERE 1=1 AND b.sys_user_id = ?
  123. GROUP BY b.edb_info_id
  124. ORDER BY share_time DESC`
  125. err = global.DmSQL["data"].Raw(sql, sysUserId).Scan(&list).Error
  126. return
  127. }
  128. // GetAllSharedEdbInfoListByReceivedUserId
  129. // @Description: 获取所有我收到的共享的指标列表
  130. // @author: Roc
  131. // @receiver m
  132. // @datetime 2024-12-03 09:56:41
  133. // @param sysUserId int
  134. // @return list []*ShareEdbInfoQuery
  135. // @return err error
  136. func (m EdbInfoShare) GetAllSharedEdbInfoListByReceivedUserId(sysUserId int) (list []*ShareEdbInfoQuery, err error) {
  137. sql := `SELECT b."edb_info_id",
  138. MAX(a.share_type) AS share_type,
  139. MAX(a.create_time) as share_time,
  140. MAX(b."edb_code") AS "edb_code",
  141. MAX(b."edb_name") "edb_name",
  142. MAX(b."edb_info_type") "edb_info_type",
  143. MAX(b."edb_type") "edb_type",
  144. MAX(b."source") "source",
  145. MAX(b."source_name") "source_name",
  146. MAX(b."latest_date") "latest_date",
  147. MAX(b."latest_value") "latest_value",
  148. MAX(b."sys_user_id") "sys_user_id",
  149. MAX(b."sys_user_real_name") "sys_user_real_name",
  150. MAX(b."frequency") "frequency",
  151. MAX(b."unit") "unit",
  152. MAX(b."classify_id") "classify_id",
  153. MAX(b."create_time") "create_time",
  154. MAX(b."unique_code") "unique_code",
  155. MAX(b."chart_image") "chart_image",
  156. MAX(b."modify_time") "modify_time",
  157. MAX(b."start_date") "start_date",
  158. MAX(b."sort") AS "sort"
  159. FROM edb_info_share AS a
  160. JOIN edb_info AS b ON a.edb_info_id = b.edb_info_id
  161. WHERE 1=1 AND a.sys_user_id = ? AND b.sys_user_id != ?
  162. GROUP BY b.edb_info_id
  163. ORDER BY share_time DESC`
  164. err = global.DmSQL["data"].Raw(sql, sysUserId, sysUserId).Scan(&list).Error
  165. return
  166. }
  167. // GetAllEditSharedEdbInfoIdListByReceivedUserId
  168. // @Description: 获取所有我收到的共享的、可以编辑的指标id列表
  169. // @author: Roc
  170. // @receiver m
  171. // @datetime 2024-12-03 09:56:41
  172. // @param sysUserId int
  173. // @return list []*ShareEdbInfoQuery
  174. // @return err error
  175. func (m EdbInfoShare) GetAllEditSharedEdbInfoIdListByReceivedUserId(sysUserId int, edbInfoIdList []int) (list []int, err error) {
  176. sql := `SELECT a."edb_info_id" FROM edb_info_share AS a
  177. WHERE 1=1 AND a.sys_user_id = ? AND share_type = ? `
  178. pars := []interface{}{sysUserId, utils.ShareTypeEdit}
  179. if len(edbInfoIdList) > 0 {
  180. sql += ` AND a.edb_info_id in (?) `
  181. pars = append(pars, edbInfoIdList)
  182. }
  183. sql += ` GROUP BY a.edb_info_id `
  184. err = global.DmSQL["data"].Raw(sql, sysUserId, utils.ShareTypeEdit).Scan(&list).Error
  185. return
  186. }
  187. // GetShareEdbInfoListPageList
  188. // @Description: 获取分享的指标列表(分页)
  189. // @author: Roc
  190. // @receiver m
  191. // @datetime 2024-12-03 14:42:12
  192. // @param condition string
  193. // @param pars []interface{}
  194. // @param startSize int
  195. // @param pageSize int
  196. // @return total int32
  197. // @return list []*EdbInfoList
  198. // @return err error
  199. func (m EdbInfoShare) GetShareEdbInfoListPageList(condition string, pars []interface{}, startSize, pageSize int) (total int32, list []*EdbInfoList, err error) {
  200. baseSql := `SELECT b."edb_info_id",
  201. MAX(a.share_type) AS share_type,
  202. MAX(a.create_time) as share_time,
  203. MAX(b."edb_code") AS "edb_code",
  204. MAX(b."edb_name") "edb_name",
  205. MAX(b."edb_info_type") "edb_info_type",
  206. MAX(b."edb_type") "edb_type",
  207. MAX(b."source") "source",
  208. MAX(b."source_name") "source_name",
  209. MAX(b."end_date") "end_date",
  210. MAX(b."end_value") "end_value",
  211. MAX(b."latest_date") "latest_date",
  212. MAX(b."latest_value") "latest_value",
  213. MAX(b."sys_user_id") "sys_user_id",
  214. MAX(b."sys_user_real_name") "sys_user_real_name",
  215. MAX(b."frequency") "frequency",
  216. MAX(b."unit") "unit",
  217. MAX(b."classify_id") "classify_id",
  218. MAX(b."create_time") "create_time",
  219. MAX(b."unique_code") "unique_code",
  220. MAX(b."chart_image") "chart_image",
  221. MAX(b."modify_time") "modify_time",
  222. MAX(b."start_date") "start_date",
  223. MAX(b."sort") AS "sort"
  224. FROM edb_info_share AS a
  225. JOIN edb_info AS b ON a.edb_info_id = b.edb_info_id
  226. WHERE 1=1 `
  227. baseSql += condition
  228. baseSql += ` GROUP BY b.edb_info_id `
  229. totalSql := `SELECT COUNT(1) AS total FROM (` + baseSql + `) AS d`
  230. var totalNull sql2.NullInt32
  231. err = global.DmSQL["data"].Raw(totalSql, pars...).Scan(&totalNull).Error
  232. if err != nil {
  233. return
  234. }
  235. total = totalNull.Int32
  236. sql := baseSql + ` ORDER BY share_time DESC LIMIT ?,? `
  237. pars = append(pars, startSize, pageSize)
  238. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&list).Error
  239. if err != nil {
  240. return
  241. }
  242. for _, v := range list {
  243. v.ConvertToResp()
  244. }
  245. return
  246. }
  247. // UserShareEdbClassifyItem
  248. // @Description: 用户分享的指标分类列表
  249. type UserShareEdbClassifyItem struct {
  250. UserId int `description:"用户id"`
  251. UserName string `description:"用户名称"`
  252. ClassifyId int `description:"分类id"`
  253. EdbInfoId int `description:"指标id"`
  254. ClassifyType uint8 `description:"分类类型,0:普通指标分类,1:预测指标分类"`
  255. ClassifyName string
  256. ClassifyNameEn string
  257. ParentId int
  258. RootId int `description:"顶级id"`
  259. Level int `description:"层级"`
  260. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  261. UniqueCode string `description:"唯一编码"`
  262. Source int `description:"来源id"`
  263. SourceName string `description:"来源名称"`
  264. SysUserId int `description:"创建人id"`
  265. SysUserRealName string `description:"创建人姓名"`
  266. StartDate string
  267. EdbCode string
  268. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  269. Children []*UserShareEdbClassifyItem `gorm:"-"`
  270. Button EdbClassifyItemsButton `gorm:"-" description:"操作权限"`
  271. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  272. HaveOperaAuth bool `description:"是否有数据权限"`
  273. ClassifyIdPath string `description:"分类的完整路径,格式为:父级ID,当前ID"`
  274. }
  275. type UserShareEdbClassifyItemList []*UserShareEdbClassifyItem
  276. func (m UserShareEdbClassifyItemList) Len() int {
  277. return len(m)
  278. }
  279. func (m UserShareEdbClassifyItemList) Less(i, j int) bool {
  280. return m[i].Sort < m[j].Sort
  281. }
  282. func (m UserShareEdbClassifyItemList) Swap(i, j int) {
  283. m[i], m[j] = m[j], m[i]
  284. }