edb_info_share.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 (?) ", m.TableName())
  73. err = tx.Exec(sql, edbInfoIdList).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. // GetShareEdbInfoListPageList
  168. // @Description: 获取分享的指标列表(分页)
  169. // @author: Roc
  170. // @receiver m
  171. // @datetime 2024-12-03 14:42:12
  172. // @param condition string
  173. // @param pars []interface{}
  174. // @param startSize int
  175. // @param pageSize int
  176. // @return total int32
  177. // @return list []*EdbInfoList
  178. // @return err error
  179. func (m EdbInfoShare) GetShareEdbInfoListPageList(condition string, pars []interface{}, startSize, pageSize int) (total int32, list []*EdbInfoList, err error) {
  180. baseSql := `SELECT b."edb_info_id",
  181. MAX(a.share_type) AS share_type,
  182. MAX(a.create_time) as share_time,
  183. MAX(b."edb_code") AS "edb_code",
  184. MAX(b."edb_name") "edb_name",
  185. MAX(b."edb_info_type") "edb_info_type",
  186. MAX(b."edb_type") "edb_type",
  187. MAX(b."source") "source",
  188. MAX(b."source_name") "source_name",
  189. MAX(b."end_date") "end_date",
  190. MAX(b."end_value") "end_value",
  191. MAX(b."latest_date") "latest_date",
  192. MAX(b."latest_value") "latest_value",
  193. MAX(b."sys_user_id") "sys_user_id",
  194. MAX(b."sys_user_real_name") "sys_user_real_name",
  195. MAX(b."frequency") "frequency",
  196. MAX(b."unit") "unit",
  197. MAX(b."classify_id") "classify_id",
  198. MAX(b."create_time") "create_time",
  199. MAX(b."unique_code") "unique_code",
  200. MAX(b."chart_image") "chart_image",
  201. MAX(b."modify_time") "modify_time",
  202. MAX(b."start_date") "start_date",
  203. MAX(b."sort") AS "sort"
  204. FROM edb_info_share AS a
  205. JOIN edb_info AS b ON a.edb_info_id = b.edb_info_id
  206. WHERE 1=1 `
  207. baseSql += condition
  208. baseSql += ` GROUP BY b.edb_info_id `
  209. totalSql := `SELECT COUNT(1) AS total FROM (` + baseSql + `) AS d`
  210. var totalNull sql2.NullInt32
  211. err = global.DmSQL["data"].Raw(totalSql, pars...).Scan(&totalNull).Error
  212. if err != nil {
  213. return
  214. }
  215. total = totalNull.Int32
  216. sql := baseSql + ` ORDER BY share_time DESC LIMIT ?,? `
  217. pars = append(pars, startSize, pageSize)
  218. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&list).Error
  219. if err != nil {
  220. return
  221. }
  222. for _, v := range list {
  223. v.ConvertToResp()
  224. }
  225. return
  226. }