edb_classify.go 23 KB

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