sandbox_classify.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package sandbox
  2. import (
  3. "eta_gn/eta_api/global"
  4. "fmt"
  5. "time"
  6. )
  7. type SandboxClassify struct {
  8. SandboxClassifyId int `gorm:"primaryKey;column:sandbox_classify_id;type:int(11);not null"`
  9. SandboxClassifyName string `gorm:"column:sandbox_classify_name;type:varchar(255);not null;default:''"` // 分类名称
  10. ParentId int `gorm:"column:parent_id;type:int(11);not null;default:0"` // 父级id
  11. HasData int `gorm:"column:has_data;type:tinyint(4);default:0"` // 是否存在指标数据:1有,2:无
  12. CreateTime time.Time `gorm:"column:create_time;type:datetime"` // 创建时间
  13. ModifyTime time.Time `gorm:"column:modify_time;type:datetime"` // 修改时间
  14. SysUserId int `gorm:"column:sys_user_id;type:int(11)"` // 创建人id
  15. SysUserRealName string `gorm:"column:sys_user_real_name;type:varchar(50)"` // 创建人姓名
  16. Level int `gorm:"column:level;type:int(11);default:0"` // 层级
  17. Sort int `gorm:"column:sort;type:int(9) unsigned;default:10"` // 排序字段,越小越靠前,默认值:10
  18. ChartPermissionId int `gorm:"column:chart_permission_id;type:int(9) unsigned;not null;default:0"` // 品种id
  19. ChartPermissionName string `gorm:"column:chart_permission_name;type:varchar(32);not null;default:''"` // 品种名称(冗余字段,避免列表页查询时再去关联表查询)
  20. }
  21. func AddSandboxClassify(item *SandboxClassify) (lastId int64, err error) {
  22. err = global.DmSQL["data"].Create(item).Error
  23. return
  24. }
  25. // GetSandboxClassifyByParentId
  26. func GetSandboxClassifyByParentId(parentId int) (items []*SandboxClassifyItems, err error) {
  27. sql := ` SELECT * FROM sandbox_classify WHERE parent_id=? order by sort asc,sandbox_classify_id asc`
  28. err = global.DmSQL["data"].Raw(sql, parentId).Find(&items).Error
  29. return
  30. }
  31. // GetSandboxClassifyAll
  32. func GetSandboxClassifyAll() (items []*SandboxClassifyItems, err error) {
  33. sql := ` SELECT * FROM sandbox_classify WHERE parent_id<>0 order by sort asc,sandbox_classify_id asc`
  34. err = global.DmSQL["data"].Raw(sql).Find(&items).Error
  35. return
  36. }
  37. type SandboxClassifyItems struct {
  38. SandboxClassifyId int `gorm:"primaryKey;column:sandbox_classify_id;type:int(11);not null"`
  39. SandboxClassifyName string `gorm:"column:sandbox_classify_name;type:varchar(255);not null;default:''"` // 分类名称
  40. ParentId int `gorm:"column:parent_id;type:int(11);not null;default:0"` // 父级id
  41. HasData int `gorm:"column:has_data;type:tinyint(4);default:0"` // 是否存在指标数据:1有,2:无
  42. CreateTime time.Time `gorm:"column:create_time;type:datetime"` // 创建时间
  43. ModifyTime time.Time `gorm:"column:modify_time;type:datetime"` // 修改时间
  44. SysUserId int `gorm:"column:sys_user_id;type:int(11)"` // 创建人id
  45. SysUserRealName string `gorm:"column:sys_user_real_name;type:varchar(50)"` // 创建人姓名
  46. Level int `gorm:"column:level;type:int(11);default:0"` // 层级
  47. Sort int `gorm:"column:sort;type:int(9) unsigned;default:10"` // 排序字段,越小越靠前,默认值:10
  48. ChartPermissionId int `gorm:"column:chart_permission_id;type:int(9) unsigned;not null;default:0"` // 品种id
  49. ChartPermissionName string `gorm:"column:chart_permission_name;type:varchar(32);not null;default:''"` // 品种名称(冗余字段,避免列表页查询时再去关联表查询)
  50. Children []*SandboxClassifyItems `gorm:"-"`
  51. SandboxId int `gorm:"column:sandbox_id" description:"沙盘id"`
  52. }
  53. type SandboxClassifyListResp struct {
  54. AllNodes []*SandboxClassifyItems
  55. }
  56. type AddSandboxClassifyReq struct {
  57. SandboxClassifyName string `description:"分类名称"`
  58. ParentId int `description:"父级id,第一级传0"`
  59. Level int `description:"层级,第一级传0,其余传上一级的层级"`
  60. ChartPermissionId int `description:"品种id"`
  61. ChartPermissionName string `description:"品种名称"`
  62. }
  63. func GetSandboxClassifyCount(sandboxClassifyName string, parentId int) (count int, err error) {
  64. sql := `SELECT COUNT(1) AS count FROM sandbox_classify WHERE parent_id=? AND sandbox_classify_name=? `
  65. err = global.DmSQL["data"].Raw(sql, parentId, sandboxClassifyName).Scan(&count).Error
  66. return
  67. }
  68. // GetSandboxClassifyMaxSort 获取沙盘分类下最大的排序数
  69. func GetSandboxClassifyMaxSort(parentId int) (sort int, err error) {
  70. sql := `SELECT COALESCE(MAX(sort), 0) AS sort FROM sandbox_classify WHERE parent_id=? `
  71. err = global.DmSQL["data"].Raw(sql, parentId).Scan(&sort).Error
  72. return
  73. }
  74. type EditSandboxClassifyReq struct {
  75. SandboxClassifyName string `description:"分类名称"`
  76. SandboxClassifyId int `description:"分类id"`
  77. ChartPermissionId int `description:"品种id"`
  78. ChartPermissionName string `description:"品种名称"`
  79. }
  80. func GetSandboxClassifyById(classifyId int) (item *SandboxClassify, err error) {
  81. sql := `SELECT * FROM sandbox_classify WHERE sandbox_classify_id=? `
  82. err = global.DmSQL["data"].Raw(sql, classifyId).First(&item).Error
  83. return
  84. }
  85. func EditSandboxClassify(classifyId, ChartPermissionId int, sandboxClassifyName, ChartPermissionName string) (err error) {
  86. sql := `UPDATE sandbox_classify SET sandbox_classify_name=?,chart_permission_id = ?, chart_permission_name = ?, modify_time=NOW() WHERE sandbox_classify_id=? `
  87. err = global.DmSQL["data"].Exec(sql, sandboxClassifyName, ChartPermissionId, ChartPermissionName, classifyId).Error
  88. return
  89. }
  90. type SandboxClassifyDeleteCheckReq struct {
  91. SandboxClassifyId int `description:"分类id"`
  92. }
  93. func GetSandboxInfoCountByClassifyId(classifyId int) (count int, err error) {
  94. sql := ` SELECT COUNT(1) AS count FROM sandbox AS a
  95. WHERE a.sandbox_classify_id IN(
  96. SELECT t.sandbox_classify_id FROM
  97. (
  98. SELECT rd.*
  99. FROM (SELECT * FROM sandbox_classify WHERE parent_id IS NOT NULL) rd,
  100. (SELECT @pid := ?) pd
  101. WHERE FIND_IN_SET(parent_id, @pid) > 0
  102. AND @pid := CONCAT(@pid, ',', sandbox_classify_id)
  103. UNION SELECT * FROM sandbox_classify WHERE sandbox_classify_id = @pid
  104. )AS t
  105. ) AND a.is_delete = 0 `
  106. err = global.DmSQL["data"].Raw(sql, classifyId).Scan(&count).Error
  107. return
  108. }
  109. type SandboxClassifyDeleteCheckResp struct {
  110. DeleteStatus int `description:"检测状态:0:默认值,如果为0,继续走其他校验,1:该分类下关联图表不可删除,2:确认删除当前目录及包含的子目录吗"`
  111. TipsMsg string `description:"提示信息"`
  112. }
  113. type DeleteSandboxClassifyReq struct {
  114. SandboxClassifyId int `description:"分类id"`
  115. SandboxId int `description:"指标id"`
  116. }
  117. func DeleteSandboxClassify(classifyId int) (err error) {
  118. sql := ` DELETE FROM sandbox_classify
  119. WHERE sandbox_classify_id IN(
  120. SELECT t.sandbox_classify_id FROM
  121. (
  122. SELECT rd.*
  123. FROM (SELECT * FROM sandbox_classify WHERE parent_id IS NOT NULL) rd,
  124. (SELECT @pid := ?) pd
  125. WHERE FIND_IN_SET(parent_id, @pid) > 0
  126. AND @pid := CONCAT(@pid, ',', sandbox_classify_id)
  127. UNION SELECT * FROM sandbox_classify WHERE sandbox_classify_id = @pid
  128. )AS t
  129. ) `
  130. err = global.DmSQL["data"].Exec(sql, classifyId).Error
  131. return
  132. }
  133. // MoveSandboxClassifyReq 移动沙盘分类请求参数
  134. type MoveSandboxClassifyReq struct {
  135. ClassifyId int `description:"分类id"`
  136. SandboxId int `description:"沙盘ID"`
  137. ParentClassifyId int `description:"父级分类id 移动沙盘时为目标分类id"`
  138. PrevId int `description:"上一个兄弟节点分类id"`
  139. NextId int `description:"下一个兄弟节点分类id"`
  140. PrevType int `description:"上一个兄弟节点类型 1分类 2沙盘 "`
  141. NextType int `description:"上一个兄弟节点类型 1分类 2沙盘 "`
  142. }
  143. // UpdateSandboxClassifySortByParentId 根据沙盘父类id更新排序
  144. func UpdateSandboxClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string) (err error) {
  145. sql := ` update sandbox_classify set sort = ` + updateSort + ` WHERE parent_id=? and sort > ? `
  146. if classifyId > 0 {
  147. sql += ` or ( sandbox_classify_id > ` + fmt.Sprint(classifyId) + ` and sort= ` + fmt.Sprint(nowSort) + `)`
  148. }
  149. err = global.DmSQL["data"].Exec(sql, parentId, nowSort).Error
  150. return
  151. }
  152. // GetFirstSandboxClassifyByParentId 获取当前父级沙盘分类下的排序第一条的数据
  153. func GetFirstSandboxClassifyByParentId(parentId int) (item *SandboxClassify, err error) {
  154. sql := ` SELECT * FROM sandbox_classify WHERE parent_id=? order by sort asc,sandbox_classify_id asc limit 1`
  155. err = global.DmSQL["data"].Raw(sql, parentId).First(&item).Error
  156. return
  157. }
  158. // Update 更新沙盘分类基础信息
  159. func (sandboxClassify *SandboxClassify) Update(cols []string) (err error) {
  160. err = global.DmSQL["data"].Select(cols).Updates(sandboxClassify).Error
  161. return
  162. }
  163. // GetSandboxClassifyAndInfoByParentId
  164. func GetSandboxClassifyAndInfoByParentId(parentId int) (items []*SandboxClassifyItems, err error) {
  165. sql := ` SELECT
  166. 0 AS sandbox_id,
  167. sandbox_classify_id,
  168. sandbox_classify_name,
  169. parent_id,
  170. create_time,
  171. modify_time,
  172. sys_user_id,
  173. sys_user_real_name AS sys_user_name,
  174. sort,
  175. level,
  176. chart_permission_id,
  177. chart_permission_name,
  178. 0 AS is_delete
  179. FROM
  180. sandbox_classify
  181. WHERE
  182. parent_id = ? UNION ALL
  183. SELECT
  184. sandbox_id,
  185. sandbox_classify_id,
  186. name AS sandbox_classify_name,
  187. 0 AS parent_id,
  188. create_time,
  189. modify_time,
  190. sys_user_id,
  191. sys_user_name,
  192. sort,
  193. 0 AS level,
  194. chart_permission_id,
  195. chart_permission_name,
  196. is_delete
  197. FROM
  198. sandbox
  199. WHERE
  200. sandbox_classify_id = ? AND is_delete = 0
  201. ORDER BY
  202. sort ASC,
  203. sandbox_classify_id ASC`
  204. err = global.DmSQL["data"].Raw(sql, parentId, parentId).Find(&items).Error
  205. return
  206. }
  207. type SandboxLinkCheckReq struct {
  208. EdbInfoIdList []int `description:"指标id列表"`
  209. ChartInfoIdList []int `description:"图库id列表"`
  210. ReportIdList []int `description:"报告id列表"`
  211. }
  212. type SandboxLinkCheckItem struct {
  213. Id int `description:"id"`
  214. Name string `description:"名称"`
  215. UniqueCode string `description:"唯一编码"`
  216. ClassifyId int `description:"分类id"`
  217. }
  218. type SandboxLinkCheckResp struct {
  219. EdbInfoIdList []*SandboxLinkCheckItem `description:"指标id列表"`
  220. ChartInfoIdList []*SandboxLinkCheckItem `description:"图库id列表"`
  221. ReportIdList []*SandboxLinkCheckItem `description:"报告id列表"`
  222. }
  223. // GetSandboxClassifySubcategories 获取所有子级分类id
  224. func GetSandboxClassifySubcategories(classifyId int) (Ids string, err error) {
  225. sql := `WITH RECURSIVE Subcategories (sandbox_classify_id) AS (
  226. SELECT ? AS sandbox_classify_id
  227. FROM DUAL
  228. UNION ALL
  229. SELECT sc.sandbox_classify_id
  230. FROM sandbox_classify sc
  231. JOIN Subcategories s
  232. ON sc.parent_id = s.sandbox_classify_id
  233. )
  234. SELECT LISTAGG(sandbox_classify_id, ',') WITHIN GROUP (ORDER BY sandbox_classify_id) AS ids
  235. FROM Subcategories;`
  236. err = global.DmSQL["data"].Raw(sql, classifyId).First(&Ids).Error
  237. return
  238. }
  239. // UpdateSandboxClassifyChartPermissionById 根据沙盘id更新品种
  240. func UpdateSandboxClassifyChartPermissionById(ChartPermissionId int, ChartPermissionName, Ids string) (err error) {
  241. sql := ` UPDATE sandbox_classify SET chart_permission_id = ?, chart_permission_name = ? WHERE sandbox_classify_id IN ( ` + Ids + ` ) `
  242. err = global.DmSQL["data"].Exec(sql, ChartPermissionId, ChartPermissionName).Error
  243. return
  244. }
  245. // GetAllSandboxClassify
  246. func GetAllSandboxClassify() (items []*SandboxClassifyItems, err error) {
  247. sql := ` SELECT * FROM sandbox_classify `
  248. err = global.DmSQL["data"].Raw(sql).Find(&items).Error
  249. return
  250. }