edb_classify.go 23 KB

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