edb_classify.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. package data_manage
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "time"
  7. )
  8. type EdbClassify struct {
  9. ClassifyId int `gorm:"primaryKey" `
  10. ClassifyType uint8 `description:"分类类型,0:普通指标分类,1:预测指标分类"`
  11. ClassifyName string `description:"分类名称"`
  12. ParentId int `description:"父级id"`
  13. RootId int `description:"顶级id"`
  14. HasData int `description:"是否含有指标数据"`
  15. CreateTime time.Time `description:"创建时间"`
  16. ModifyTime time.Time `description:"修改时间"`
  17. SysUserId int `description:"创建人id"`
  18. SysUserRealName string `description:"创建人姓名"`
  19. Level int `description:"层级"`
  20. UniqueCode string `description:"唯一编码"`
  21. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  22. LastModifyUserId int `description:"最后修改人ID"`
  23. LastModifyUserRealName string `description:"最后修改人姓名"`
  24. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  25. ClassifyNameEn string `description:"英文分类名称"`
  26. ClassifyNamePath string `description:"分类名称的完整路径,格式为:父级分类名称|当前分类名称"`
  27. ClassifyIdPath string `description:"分类的完整路径,格式为:父级ID,当前ID"`
  28. }
  29. func AddEdbClassify(item *EdbClassify) (err error) {
  30. err = global.DmSQL["data"].Create(item).Error
  31. return
  32. }
  33. type AddEdbClassifyReq struct {
  34. ClassifyName string `description:"分类名称"`
  35. ParentId int `description:"父级id,第一级传0"`
  36. Level int `description:"层级,第一级传0,其余传上一级的层级"`
  37. ClassifyType uint8 `description:"分类类型,0:普通指标分类,1:预测指标分类"`
  38. }
  39. // GetEdbClassifyCount
  40. // @Description: 根据父级id获取同级中文名称的数量
  41. // @author: Roc
  42. // @datetime 2024-04-17 14:12:22
  43. // @param classifyName string
  44. // @param parentId int
  45. // @param classifyType uint8
  46. // @return count int
  47. // @return err error
  48. func GetEdbClassifyCount(classifyName string, parentId, sysUserId int, classifyType uint8) (count int, err error) {
  49. sql := `SELECT COUNT(1) AS count FROM edb_classify WHERE parent_id=? AND classify_name=? AND classify_type = ? AND sys_user_id = ? `
  50. err = global.DmSQL["data"].Raw(sql, parentId, classifyName, classifyType, sysUserId).Scan(&count).Error
  51. return
  52. }
  53. // GetEdbClassifyEnCount
  54. // @Description: 根据父级id获取同级英文名称的数量
  55. // @author: Roc
  56. // @datetime 2024-04-17 14:12:09
  57. // @param classifyNameEn string
  58. // @param parentId int
  59. // @param classifyType uint8
  60. // @return count int
  61. // @return err error
  62. func GetEdbClassifyEnCount(classifyNameEn string, parentId, sysUserId int, classifyType uint8) (count int, err error) {
  63. sql := `SELECT COUNT(1) AS count FROM edb_classify WHERE parent_id=? AND classify_name_en = ? AND classify_type = ? AND sys_user_id = ? `
  64. err = global.DmSQL["data"].Raw(sql, parentId, classifyNameEn, classifyType, sysUserId).Scan(&count).Error
  65. return
  66. }
  67. type EditEdbClassifyReq struct {
  68. ClassifyName string `description:"分类名称"`
  69. ClassifyId int `description:"分类名称"`
  70. }
  71. func GetEdbClassifyById(classifyId int) (item *EdbClassify, err error) {
  72. sql := `SELECT * FROM edb_classify WHERE classify_id=? `
  73. err = global.DmSQL["data"].Raw(sql, classifyId).First(&item).Error
  74. return
  75. }
  76. func GetEdbClassifyCountById(classifyId int) (count int, err error) {
  77. sql := `SELECT count(1) AS count FROM edb_classify WHERE classify_id=? `
  78. err = global.DmSQL["data"].Raw(sql, classifyId).Scan(&count).Error
  79. return
  80. }
  81. // EditEdbClassify
  82. // @Description: 修改指标分类的中文名称
  83. // @author: Roc
  84. // @datetime 2024-04-17 14:14:48
  85. // @param classifyId int
  86. // @param classifyName string
  87. // @param userId int
  88. // @param userRealName string
  89. // @return err error
  90. func EditEdbClassify(classifyId int, classifyName string, userId int, userRealName string) (err error) {
  91. sql := `UPDATE edb_classify SET classify_name=?,modify_time=NOW(), last_modify_user_id = ?, last_modify_user_real_name = ? WHERE classify_id=? `
  92. err = global.DmSQL["data"].Exec(sql, classifyName, userId, userRealName, classifyId).Error
  93. return
  94. }
  95. type DeleteEdbClassifyReq struct {
  96. ClassifyId int `description:"分类id"`
  97. EdbInfoId int `description:"指标id"`
  98. }
  99. // TODO: sql优化,后续解决
  100. func GetEdbInfoCountByClassifyId(classifyId int) (count int, err error) {
  101. sql := `WITH RECURSIVE classify_cte(classify_id, parent_id) AS (
  102. SELECT classify_id, parent_id
  103. FROM edb_classify
  104. WHERE parent_id = ?
  105. UNION ALL
  106. SELECT e.classify_id, e.parent_id
  107. FROM edb_classify e
  108. INNER JOIN classify_cte c ON e.parent_id = c.classify_id
  109. )
  110. SELECT COUNT(1) AS COUNT
  111. FROM edb_info AS a
  112. WHERE a.classify_id IN (SELECT classify_id FROM classify_cte);`
  113. err = global.DmSQL["data"].Raw(sql, classifyId).Scan(&count).Error
  114. return
  115. }
  116. func DeleteEdbClassify(classifyId int) (err error) {
  117. sql := `WITH RECURSIVE ClassifyHierarchy ( classify_id, parent_id ) AS
  118. (
  119. SELECT classify_id,
  120. parent_id
  121. FROM edb_classify
  122. WHERE parent_id = ? OR classify_id=?
  123. UNION ALL
  124. SELECT e.classify_id,
  125. e.parent_id
  126. FROM edb_classify e
  127. INNER JOIN ClassifyHierarchy ch
  128. ON e.parent_id = ch.classify_id
  129. )
  130. DELETE
  131. FROM edb_classify
  132. WHERE classify_id IN ( SELECT classify_id
  133. FROM ClassifyHierarchy
  134. );`
  135. err = global.DmSQL["data"].Exec(sql, classifyId, classifyId).Error
  136. return
  137. }
  138. func GetEdbClassifyByParentId(parentId int, classifyType int8, sysUserId int) (items []*EdbClassifyItems, err error) {
  139. o := global.DmSQL["data"]
  140. sql := ` SELECT * FROM edb_classify WHERE parent_id=? AND classify_type = ? `
  141. if sysUserId > 0 {
  142. sql += fmt.Sprintf(` AND sys_user_id = %d `, sysUserId)
  143. }
  144. sql += ` order by sort asc,classify_id asc `
  145. err = o.Raw(sql, parentId, classifyType).Scan(&items).Error
  146. return
  147. }
  148. func GetEdbClassifyByRootIdLevel(rootId int, classifyType uint8, orderStr string) (items []*EdbClassifyItems, err error) {
  149. o := global.DmSQL["data"]
  150. sql := ` SELECT * FROM edb_classify WHERE root_id=? AND classify_type = ? `
  151. if orderStr != "" {
  152. sql += orderStr
  153. } else {
  154. sql += ` order by level desc, sort asc, classify_id asc`
  155. }
  156. err = o.Raw(sql, rootId, classifyType).Scan(&items).Error
  157. return
  158. }
  159. // GetAllEdbClassify 获取所有的普通指标的分类列表(包含第一级)
  160. func GetAllEdbClassify() (items []*EdbClassifyItems, err error) {
  161. o := global.DmSQL["data"]
  162. sql := ` SELECT * FROM edb_classify WHERE classify_type = 0 order by sort asc,classify_id asc`
  163. err = o.Raw(sql).Scan(&items).Error
  164. return
  165. }
  166. // GetPredictEdbClassifyAll 获取预测指标的分类列表
  167. func GetPredictEdbClassifyAll(sysUserId int) (items []*EdbClassifyItems, err error) {
  168. o := global.DmSQL["data"]
  169. sql := ` SELECT * FROM edb_classify WHERE classify_type = 1 order by sort asc,classify_id asc`
  170. err = o.Raw(sql).Scan(&items).Error
  171. return
  172. }
  173. // GetAllEdbClassifyByType 根据类型获取分类列表
  174. func GetAllEdbClassifyByType(classifyType, sysUserId int) (items []*EdbClassifyItems, err error) {
  175. o := global.DmSQL["data"]
  176. sql := ` SELECT * FROM edb_classify WHERE classify_type = ?`
  177. if sysUserId > 0 {
  178. sql += fmt.Sprintf(` AND sys_user_id = %d `, sysUserId)
  179. }
  180. sql += ` order by sort ASC,classify_id ASC `
  181. err = o.Raw(sql, classifyType).Scan(&items).Error
  182. return
  183. }
  184. type EdbClassifyItems struct {
  185. ClassifyId int `description:"分类id"`
  186. EdbInfoId int `description:"指标id"`
  187. ClassifyType uint8 `description:"分类类型,0:普通指标分类,1:预测指标分类"`
  188. ClassifyName string
  189. ClassifyNameEn string
  190. ParentId int
  191. RootId int `description:"顶级id"`
  192. Level int `description:"层级"`
  193. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  194. UniqueCode string `description:"唯一编码"`
  195. Source int `description:"来源id"`
  196. SourceName string `description:"来源名称"`
  197. SysUserId int `description:"创建人id"`
  198. SysUserRealName string `description:"创建人姓名"`
  199. StartDate string
  200. EdbCode string
  201. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  202. Children []*EdbClassifyItems `gorm:"-"`
  203. Button EdbClassifyItemsButton `gorm:"-" description:"操作权限"`
  204. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  205. HaveOperaAuth bool `description:"是否有数据权限"`
  206. ClassifyIdPath string `description:"分类的完整路径,格式为:父级ID,当前ID"`
  207. }
  208. type EdbClassifyIdItems struct {
  209. ClassifyId int `description:"分类id"`
  210. ClassifyName string
  211. ClassifyNameEn string
  212. UniqueCode string `description:"唯一编码"`
  213. ParentId int `description:"父级分类id"`
  214. Level int `description:"层级"`
  215. RootId int `description:"顶级分类id"`
  216. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0" json:"-"`
  217. HaveOperaAuth bool `description:"是否有该数据权限,默认:false"`
  218. }
  219. type EdbClassifyItemList []*EdbClassifyItems
  220. func (m EdbClassifyItemList) Len() int {
  221. return len(m)
  222. }
  223. func (m EdbClassifyItemList) Less(i, j int) bool {
  224. return m[i].Sort < m[j].Sort
  225. }
  226. func (m EdbClassifyItemList) Swap(i, j int) {
  227. m[i], m[j] = m[j], m[i]
  228. }
  229. // EdbClassifyItemsButton 操作按钮
  230. type EdbClassifyItemsButton struct {
  231. AddButton bool `description:"是否可添加"`
  232. OpButton bool `description:"是否可编辑"`
  233. DeleteButton bool `description:"是否可删除"`
  234. MoveButton bool `description:"是否可移动"`
  235. ShowEdbRelation bool `description:"是否展示关联指标"`
  236. ShowChartRelation bool `description:"是否展示关联图表"`
  237. InsertNewDataButton bool `description:"是否可插入最新值"`
  238. }
  239. type EdbClassifyListResp struct {
  240. AllNodes EdbClassifyItemList
  241. CanOpClassify bool `description:"是否允许操作分类"`
  242. Language string `description:"指标的展示语言,CN:中文,EN:英文"`
  243. }
  244. type ClassifyDeleteCheckResp struct {
  245. DeleteStatus int `description:"检测状态:0:默认值,如果为0,继续走其他校验,1:若目录关联指标不可删除,2:确认删除当前目录及包含的子目录吗,3:当前指标已用作画图,不可删除"`
  246. TipsMsg string `description:"提示信息"`
  247. TableList []*ExcelBaseInfo `description:"关联的表格"`
  248. }
  249. type ExcelBaseInfo struct {
  250. ExcelInfoId int `orm:"column(excel_info_id);pk" gorm:"primaryKey" `
  251. Source int `description:"表格来源,1:excel插件的表格,2:自定义表格,3:混合表格,4:自定义分析,默认:1"`
  252. ExcelType int `description:"表格类型,1:指标列,2:日期列,默认:1"`
  253. ExcelName string `description:"表格名称"`
  254. UniqueCode string `description:"表格唯一编码"`
  255. ExcelClassifyId int `description:"表格分类id"`
  256. //SysUserId int `description:"操作人id"`
  257. //SysUserRealName string `description:"操作人真实姓名"`
  258. }
  259. type ClassifyDeleteCheckReq struct {
  260. ClassifyId int `description:"分类id"`
  261. EdbInfoId int `description:"指标id"`
  262. }
  263. func GetClassifyCountByClassifyId(classifyId int) (count int, err error) {
  264. o := global.DmSQL["data"]
  265. sql := `WITH RECURSIVE classify_cte(classify_id, parent_id) AS (
  266. SELECT classify_id, parent_id
  267. FROM edb_classify
  268. WHERE parent_id = ?
  269. UNION ALL
  270. SELECT e.classify_id, e.parent_id
  271. FROM edb_classify e
  272. INNER JOIN classify_cte c ON e.parent_id = c.classify_id
  273. )
  274. SELECT COUNT(1) AS COUNT
  275. FROM classify_cte
  276. WHERE classify_id <> ? ;`
  277. err = o.Raw(sql, classifyId, classifyId).Scan(&count).Error
  278. return
  279. }
  280. type EdbClassifySimplify struct {
  281. ClassifyId int `description:"分类id"`
  282. ClassifyName string `description:"分类名称"`
  283. ParentId int
  284. }
  285. // 递归获取所有目录
  286. func GetClassifyAllByClassifyId(classifyId int) (resultList []*EdbClassifySimplify, err error) {
  287. for {
  288. sql := ` SELECT * FROM edb_classify AS a
  289. WHERE a.classify_id=? order by sort asc,classify_id asc`
  290. item := new(EdbClassifySimplify)
  291. err = global.DmSQL["data"].Raw(sql, classifyId).Scan(&item).Error
  292. if err != nil {
  293. break
  294. }
  295. if item.ClassifyId > 0 {
  296. resultList = append(resultList, item)
  297. classifyId = item.ParentId
  298. } else {
  299. break
  300. }
  301. }
  302. return resultList, err
  303. }
  304. // MoveEdbClassifyReq 移动分类请求参数
  305. type MoveEdbClassifyReq struct {
  306. ClassifyId int `description:"分类id"`
  307. ParentClassifyId int `description:"父级分类id"`
  308. PrevClassifyId int `description:"上一个兄弟节点分类id"`
  309. NextClassifyId int `description:"下一个兄弟节点分类id"`
  310. EdbInfoId int `description:"指标ID, 如果指标ID有值,则移动对象为指标,否则认为移动对象为分类"`
  311. PrevEdbInfoId int `description:"上一个指标ID"`
  312. NextEdbInfoId int `description:"下一个指标ID"`
  313. ClassifyType int `description:"分类类型:0-指标库;2-计算指标"`
  314. }
  315. // GetFirstEdbClassifyByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据
  316. func GetFirstEdbClassifyByParentId(parentId int) (item *EdbClassify, err error) {
  317. o := global.DmSQL["data"]
  318. sql := ` SELECT * FROM edb_classify WHERE parent_id=? order by sort asc,classify_id asc limit 1`
  319. err = o.Raw(sql, parentId).First(&item).Error
  320. return
  321. }
  322. // UpdateEdbClassifySortByParentId 根据父类id更新排序
  323. func UpdateEdbClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string, classifyType uint8) (err error) {
  324. sql := ` update edb_classify set sort = ` + updateSort + ` WHERE parent_id=? AND sort > ? AND classify_type = ? `
  325. if classifyId > 0 {
  326. sql += ` or ( classify_id > ` + fmt.Sprint(classifyId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  327. }
  328. err = global.DmSQL["data"].Exec(sql, parentId, nowSort, classifyType).Error
  329. return
  330. }
  331. // Update 更新分类基础信息
  332. func (edbClassify *EdbClassify) Update(cols []string) (err error) {
  333. err = global.DmSQL["data"].Select(cols).Updates(edbClassify).Error
  334. return
  335. }
  336. // GetEdbClassifyMaxSort 获取分类下最大的排序数
  337. func GetEdbClassifyMaxSort(parentId int, classifyType uint8) (sort int, err error) {
  338. sql := `SELECT COALESCE(Max(sort), 0) AS sort FROM edb_classify WHERE parent_id=? AND classify_type=? `
  339. err = global.DmSQL["data"].Raw(sql, parentId, classifyType).Scan(&sort).Error
  340. return
  341. }
  342. // GetEdbInfoMaxSortByClassifyId 获取分类下指标的最大的排序数
  343. func GetEdbInfoMaxSortByClassifyId(classifyId int) (sort int, err error) {
  344. sql := `SELECT COALESCE(Max(sort), 0) AS sort FROM edb_info WHERE classify_id=? `
  345. err = global.DmSQL["data"].Raw(sql, classifyId).Scan(&sort).Error
  346. return
  347. }
  348. // GetEdbInfoMaxPublicSortByClassifyId 获取分类下公共指标的最大的排序数
  349. func GetEdbInfoMaxPublicSortByClassifyId(classifyId int) (sort int, err error) {
  350. sql := `SELECT COALESCE(Max(public_sort), 0) AS sort FROM edb_info WHERE public_status = 3 AND edb_public_classify_id=? `
  351. err = global.DmSQL["data"].Raw(sql, classifyId).Scan(&sort).Error
  352. return
  353. }
  354. type ClassifyEdbInfoListResp struct {
  355. EdbInfoList []*EdbClassifyItems
  356. }
  357. // GetEdbClassifyAllV2 获取普通指标的分类列表
  358. func GetEdbClassifyAllV2(classifyType int8) (items []*EdbClassifyItems, err error) {
  359. sql := ` SELECT * FROM edb_classify WHERE parent_id <>0 AND classify_type = ? order by sort asc,classify_id asc`
  360. err = global.DmSQL["data"].Raw(sql, classifyType).Scan(&items).Error
  361. return
  362. }
  363. func GetEdbClassifyByParentIdTwo(classifyType int8) (items []*EdbClassifyItems, err error) {
  364. sql := ` SELECT * FROM edb_classify WHERE level=2 AND classify_type = ? order by sort asc,classify_id asc`
  365. err = global.DmSQL["data"].Raw(sql, classifyType).Scan(&items).Error
  366. return
  367. }
  368. type AddEdbClassifyReqV2 struct {
  369. ClassifyName string `description:"分类名称"`
  370. ParentId int `description:"父级id,第一级传0"`
  371. Level int `description:"层级,第一级传0,其余传上一级的层级"`
  372. ClassifyType uint8 `description:"分类类型,0:普通指标分类,1:预测指标分类"`
  373. }
  374. // GetEdbClassifyMaxSortV2 获取分类下最大的排序数
  375. func GetEdbClassifyMaxSortV2(parentId int, classifyType uint8) (sort int, err error) {
  376. sql := `SELECT COALESCE(MAX(sort), 0) AS sort FROM edb_classify WHERE parent_id=? AND parent_id_two=? AND classify_type=? `
  377. err = global.DmSQL["data"].Raw(sql, parentId, classifyType).Scan(&sort).Error
  378. return
  379. }
  380. func GetEdbClassifyByLevel(level, source int) (items []*EdbClassifyItems, err error) {
  381. sql := ` SELECT * FROM edb_classify WHERE level=? AND classify_type = ? `
  382. err = global.DmSQL["data"].Raw(sql, level, source).Scan(&items).Error
  383. return
  384. }
  385. func FIXEdbClassifyId(newId, oldId int) (err error) {
  386. sql := ` UPDATE edb_info SET classify_id=? WHERE classify_id=? `
  387. err = global.DmSQL["data"].Exec(sql, newId, oldId).Error
  388. return
  389. }
  390. func EditEdbClassifySysUser(classifyId, sysUserId int, chartClassifyName string) (err error) {
  391. sql := `UPDATE edb_classify SET sys_user_id=?,sys_user_real_name=?,modify_time=NOW() WHERE parent_id=? AND level=3 `
  392. err = global.DmSQL["data"].Exec(sql, sysUserId, chartClassifyName, classifyId).Error
  393. return
  394. }
  395. func UpdateEdbClassifyChildByParentClassifyId(classifyIds []int, rootId int, levelStep int) (err error) {
  396. var pars []interface{}
  397. pars = append(pars, rootId, levelStep)
  398. pars = append(pars, classifyIds)
  399. // 更新相关联的二级分类的parentId,和classify_name_second
  400. sql := `update edb_classify
  401. SET root_id = ?, level = level+?
  402. where classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `)`
  403. err = global.DmSQL["data"].Exec(sql, pars...).Error
  404. if err != nil {
  405. return
  406. }
  407. return
  408. }
  409. // GetEdbClassifyByParentIdAndName 根据父级ID和名称获取分类
  410. func GetEdbClassifyByParentIdAndName(parentId int, classifyName string, classifyId int, classifyType uint8) (item *EdbClassify, err error) {
  411. sql := `SELECT * FROM edb_classify WHERE parent_id = ? AND classify_name = ? AND classify_id <> ? and classify_type=? LIMIT 1`
  412. err = global.DmSQL["data"].Raw(sql, parentId, classifyName, classifyId, classifyType).First(&item).Error
  413. return
  414. }
  415. // GetEdbClassifyByIdList
  416. // @Description: 根据分类id列表获取分类列表
  417. // @author: Roc
  418. // @datetime 2024-03-29 10:30:21
  419. // @param classifyId int
  420. // @return item *EdbClassify
  421. // @return err error
  422. func GetEdbClassifyByIdList(classifyIdList []int) (item []*EdbClassify, err error) {
  423. num := len(classifyIdList)
  424. if num <= 0 {
  425. return
  426. }
  427. sql := `SELECT * FROM edb_classify WHERE classify_id in (` + utils.GetOrmInReplace(num) + `) `
  428. err = global.DmSQL["data"].Raw(sql, classifyIdList).Scan(&item).Error
  429. return
  430. }
  431. // GetEdbClassifyByClassifyTypeAndIsJoinPermission
  432. // @Description: 根据分类类型和是否涉密获取分类列表
  433. // @author: Roc
  434. // @datetime 2024-03-29 10:30:21
  435. // @param classifyType int
  436. // @param isJoinPermission int
  437. // @return items []*EdbClassify
  438. // @return err error
  439. func GetEdbClassifyByClassifyTypeAndIsJoinPermission(classifyType, isJoinPermission int) (items []*EdbClassify, err error) {
  440. sql := `SELECT * FROM edb_classify WHERE classify_type = ? AND is_join_permission = ? `
  441. err = global.DmSQL["data"].Raw(sql, classifyType, isJoinPermission).Scan(&items).Error
  442. return
  443. }
  444. // GetEdbClassifyRootIdsByClassifyIds 获取普通指标的顶级分类列表
  445. func GetEdbClassifyRootIdsByClassifyIds(classifyIds []int) (items []int, err error) {
  446. o := global.DmSQL["data"]
  447. sql := `SELECT distinct root_id FROM edb_classify WHERE classify_type=0 and classify_id in (` + utils.GetOrmInReplace(len(classifyIds)) + `)`
  448. err = o.Raw(sql, classifyIds).Scan(&items).Error
  449. return
  450. }
  451. // GetChildEdbClassifyByClassifyType 根据类型获取非顶级分类
  452. func GetChildEdbClassifyByClassifyType(classifyType, sysUserId int) (items []*EdbClassifyItems, err error) {
  453. o := global.DmSQL["data"]
  454. sql := `SELECT * FROM edb_classify WHERE parent_id <> 0 AND classify_type = ?`
  455. if sysUserId > 0 {
  456. sql += fmt.Sprintf(` AND sys_user_id = %d `, sysUserId)
  457. }
  458. sql += ` ORDER BY sort ASC,classify_id ASC `
  459. err = o.Raw(sql, classifyType).Find(&items).Error
  460. return
  461. }
  462. // GetIsJoinPermissionEdbClassifyIdList
  463. // @Description: 获取已经加入指标管控的指标分类id列表
  464. // @param classifyType
  465. // @return list
  466. // @return err
  467. func GetIsJoinPermissionEdbClassifyIdList(classifyType int) (list []int, err error) {
  468. pars := make([]interface{}, 0)
  469. sql := ` SELECT classify_id FROM edb_classify WHERE 1=1 AND is_join_permission = 1 `
  470. if classifyType >= 0 {
  471. sql += ` AND classify_type = ? `
  472. pars = append(pars, classifyType)
  473. }
  474. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&list).Error
  475. return
  476. }
  477. // GetEdbClassifyByClassifyTypes 根据类型IDs获取分类
  478. func GetEdbClassifyByClassifyTypes(classifyTypes []int, sysUserId int) (items []*EdbClassifyItems, err error) {
  479. if len(classifyTypes) == 0 {
  480. return
  481. }
  482. o := global.DmSQL["data"]
  483. sql := `SELECT * FROM edb_classify WHERE classify_type IN (?) `
  484. // 指定人
  485. if sysUserId > 0 {
  486. sql += ` AND sys_user_id = ? `
  487. }
  488. sql += ` ORDER BY classify_type ASC, sort ASC, classify_id ASC `
  489. err = o.Raw(sql, classifyTypes).Scan(&items).Error
  490. return
  491. }
  492. // UpdateEdbClassifyNameAndNamePath
  493. // @Description: 更新分类名称和子分类的全路径
  494. // @author: Roc
  495. // @receiver edbClassify
  496. // @datetime 2024-11-26 14:39:09
  497. // @param cols []string
  498. // @param oldClassifyNamePath string
  499. // @param newClassifyNamePath string
  500. // @return err error
  501. func (edbClassify *EdbClassify) UpdateEdbClassifyNameAndNamePath(cols []string, oldClassifyNamePath, newClassifyNamePath string) (err error) {
  502. tx := global.DmSQL["data"].Begin()
  503. defer func() {
  504. if err != nil {
  505. _ = tx.Rollback()
  506. return
  507. }
  508. _ = tx.Commit()
  509. }()
  510. // 变更分类信息
  511. err = tx.Select(cols).Updates(edbClassify).Error
  512. if err != nil {
  513. return
  514. }
  515. // 更改子分类的完整的路径
  516. if oldClassifyNamePath != `` && newClassifyNamePath != `` {
  517. sql := `UPDATE edb_classify SET classify_name_path = REPLACE(classify_name_path,?,?) WHERE classify_id_path LIKE ?`
  518. err = tx.Exec(sql, oldClassifyNamePath, newClassifyNamePath, fmt.Sprint(edbClassify.ClassifyIdPath+`,%`)).Error
  519. if err != nil {
  520. return
  521. }
  522. }
  523. return
  524. }
  525. // UpdateEdbClassifyNameAndNamePathByOldClassifyIdPath
  526. // @Description: 根据源分类id全路径更新分类名称和子分类的全路径
  527. // @author: Roc
  528. // @receiver edbClassify
  529. // @datetime 2024-11-26 14:39:09
  530. // @param cols []string
  531. // @param oldClassifyNamePath string
  532. // @param newClassifyNamePath string
  533. // @return err error
  534. func UpdateEdbClassifyNameAndNamePathByOldClassifyIdPath(oldClassifyIdPath, newClassifyIdPath, oldClassifyNamePath, newClassifyNamePath string) (err error) {
  535. // 更改子分类的完整的路径
  536. if oldClassifyIdPath != `` && newClassifyNamePath != `` && newClassifyIdPath != `` {
  537. sql := `UPDATE edb_classify SET classify_id_path = REPLACE(classify_id_path,?,?) ,classify_name_path = REPLACE(classify_name_path,?,?) WHERE classify_id_path LIKE ?`
  538. err = global.DmSQL["data"].Exec(sql, oldClassifyIdPath, newClassifyIdPath, oldClassifyNamePath, newClassifyNamePath, fmt.Sprint(oldClassifyIdPath+`,%`)).Error
  539. if err != nil {
  540. return
  541. }
  542. }
  543. return
  544. }
  545. // UpdateEdbClassify 更新分类基础信息
  546. func UpdateEdbClassify(classifyId int, classifyNamePath, classifyIdPath string) (err error) {
  547. sql := `UPDATE edb_classify SET classify_name_path = ?,classify_id_path = ? WHERE classify_id = ?`
  548. err = global.DmSQL["data"].Exec(sql, classifyNamePath, classifyIdPath, classifyId).Error
  549. return
  550. }
  551. // DelEdbClassifyByIdList
  552. // @Description: 根据id删除分类
  553. // @author: Roc
  554. // @datetime 2024-12-02 09:27:26
  555. // @param idList []int
  556. // @return err error
  557. func DelEdbClassifyByIdList(idList []int) (err error) {
  558. num := len(idList)
  559. if num <= 0 {
  560. return
  561. }
  562. sql := `DELETE FROM edb_classify WHERE classify_id IN (?) `
  563. err = global.DmSQL["data"].Exec(sql, idList).Error
  564. return
  565. }
  566. func GetChildEdbClassifyListByParentId(classifyIdPath, orderStr string) (items []*EdbClassifyItems, err error) {
  567. o := global.DmSQL["data"]
  568. sql := ` SELECT * FROM edb_classify WHERE classify_id_path like ? `
  569. if orderStr != "" {
  570. sql += orderStr
  571. } else {
  572. sql += ` order by level desc, sort asc, classify_id asc`
  573. }
  574. err = o.Raw(sql, classifyIdPath).Scan(&items).Error
  575. return
  576. }
  577. // GetEdbClassifyItemsByIdList
  578. // @Description: 根据分类id列表获取分类列表
  579. // @author: Roc
  580. // @datetime 2024-03-29 10:30:21
  581. // @param classifyId int
  582. // @return item *EdbClassify
  583. // @return err error
  584. func GetEdbClassifyItemsByIdList(classifyIdList []int) (item []*EdbClassifyItems, err error) {
  585. num := len(classifyIdList)
  586. if num <= 0 {
  587. return
  588. }
  589. sql := `SELECT * FROM edb_classify WHERE classify_id in (` + utils.GetOrmInReplace(num) + `) `
  590. err = global.DmSQL["data"].Raw(sql, classifyIdList).Scan(&item).Error
  591. return
  592. }