edb_classify.go 27 KB

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