edb_classify.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. package data_manage
  2. import (
  3. "eta_gn/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. ClassifyNameEn string
  237. UniqueCode string `description:"唯一编码"`
  238. ParentId int `description:"父级分类id"`
  239. Level int `description:"层级"`
  240. RootId int `description:"顶级分类id"`
  241. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0" json:"-"`
  242. HaveOperaAuth bool `description:"是否有该数据权限,默认:false"`
  243. }
  244. type EdbClassifyItemList []*EdbClassifyItems
  245. func (m EdbClassifyItemList) Len() int {
  246. return len(m)
  247. }
  248. func (m EdbClassifyItemList) Less(i, j int) bool {
  249. return m[i].Sort < m[j].Sort
  250. }
  251. func (m EdbClassifyItemList) Swap(i, j int) {
  252. m[i], m[j] = m[j], m[i]
  253. }
  254. // EdbClassifyItemsButton 操作按钮
  255. type EdbClassifyItemsButton struct {
  256. AddButton bool `description:"是否可添加"`
  257. OpButton bool `description:"是否可编辑"`
  258. DeleteButton bool `description:"是否可删除"`
  259. MoveButton bool `description:"是否可移动"`
  260. ShowEdbRelation bool `description:"是否展示关联指标"`
  261. ShowChartRelation bool `description:"是否展示关联图表"`
  262. InsertNewDataButton bool `description:"是否可插入最新值"`
  263. }
  264. type EdbClassifyListResp struct {
  265. AllNodes EdbClassifyItemList
  266. CanOpClassify bool `description:"是否允许操作分类"`
  267. Language string `description:"指标的展示语言,CN:中文,EN:英文"`
  268. }
  269. type ClassifyDeleteCheckResp struct {
  270. DeleteStatus int `description:"检测状态:0:默认值,如果为0,继续走其他校验,1:若目录关联指标不可删除,2:确认删除当前目录及包含的子目录吗,3:当前指标已用作画图,不可删除"`
  271. TipsMsg string `description:"提示信息"`
  272. TableList []*ExcelBaseInfo `description:"关联的表格"`
  273. }
  274. type ExcelBaseInfo struct {
  275. ExcelInfoId int `orm:"column(excel_info_id);pk"`
  276. Source int `description:"表格来源,1:excel插件的表格,2:自定义表格,3:混合表格,4:自定义分析,默认:1"`
  277. ExcelType int `description:"表格类型,1:指标列,2:日期列,默认:1"`
  278. ExcelName string `description:"表格名称"`
  279. UniqueCode string `description:"表格唯一编码"`
  280. ExcelClassifyId int `description:"表格分类id"`
  281. //SysUserId int `description:"操作人id"`
  282. //SysUserRealName string `description:"操作人真实姓名"`
  283. }
  284. type ClassifyDeleteCheckReq struct {
  285. ClassifyId int `description:"分类id"`
  286. EdbInfoId int `description:"指标id"`
  287. }
  288. func GetClassifyCountByClassifyId(classifyId int) (count int, err error) {
  289. o := orm.NewOrmUsingDB("data")
  290. sql := ` SELECT COUNT(1) AS count FROM (
  291. SELECT rd.*
  292. FROM (SELECT * FROM edb_classify WHERE parent_id IS NOT NULL) rd,
  293. (SELECT @pid := ?) pd
  294. WHERE FIND_IN_SET(parent_id, @pid) > 0
  295. AND @pid := CONCAT(@pid, ',', classify_id)
  296. UNION SELECT * FROM edb_classify WHERE classify_id = @pid
  297. )AS t
  298. WHERE t.classify_id<>? `
  299. err = o.Raw(sql, classifyId, classifyId).QueryRow(&count)
  300. return
  301. }
  302. type EdbClassifySimplify struct {
  303. ClassifyId int `description:"分类id"`
  304. ClassifyName string `description:"分类名称"`
  305. ParentId int
  306. }
  307. // 递归获取所有目录
  308. func GetClassifyAllByClassifyId(classifyId int) (resultList []*EdbClassifySimplify, err error) {
  309. fmt.Println("185")
  310. o := orm.NewOrmUsingDB("data")
  311. for {
  312. sql := ` SELECT * FROM edb_classify AS a
  313. WHERE a.classify_id=? order by sort asc,classify_id asc`
  314. item := new(EdbClassifySimplify)
  315. err = o.Raw(sql, classifyId).QueryRow(&item)
  316. if err != nil {
  317. break
  318. }
  319. if item.ClassifyId > 0 {
  320. resultList = append(resultList, item)
  321. classifyId = item.ParentId
  322. } else {
  323. break
  324. }
  325. }
  326. return resultList, err
  327. }
  328. // MoveEdbClassifyReq 移动分类请求参数
  329. type MoveEdbClassifyReq struct {
  330. ClassifyId int `description:"分类id"`
  331. ParentClassifyId int `description:"父级分类id"`
  332. PrevClassifyId int `description:"上一个兄弟节点分类id"`
  333. NextClassifyId int `description:"下一个兄弟节点分类id"`
  334. EdbInfoId int `description:"指标ID, 如果指标ID有值,则移动对象为指标,否则认为移动对象为分类"`
  335. PrevEdbInfoId int `description:"上一个指标ID"`
  336. NextEdbInfoId int `description:"下一个指标ID"`
  337. }
  338. // GetFirstEdbClassifyByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据
  339. func GetFirstEdbClassifyByParentId(parentId int) (item *EdbClassify, err error) {
  340. o := orm.NewOrmUsingDB("data")
  341. sql := ` SELECT * FROM edb_classify WHERE parent_id=? order by sort asc,classify_id asc limit 1`
  342. err = o.Raw(sql, parentId).QueryRow(&item)
  343. return
  344. }
  345. // UpdateEdbClassifySortByParentId 根据父类id更新排序
  346. func UpdateEdbClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string, classifyType uint8) (err error) {
  347. o := orm.NewOrmUsingDB("data")
  348. sql := ` update edb_classify set sort = ` + updateSort + ` WHERE parent_id=? AND sort > ? AND classify_type = ? `
  349. if classifyId > 0 {
  350. sql += ` or ( classify_id > ` + fmt.Sprint(classifyId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  351. }
  352. _, err = o.Raw(sql, parentId, nowSort, classifyType).Exec()
  353. return
  354. }
  355. // Update 更新分类基础信息
  356. func (edbClassify *EdbClassify) Update(cols []string) (err error) {
  357. o := orm.NewOrmUsingDB("data")
  358. _, err = o.Update(edbClassify, cols...)
  359. return
  360. }
  361. // GetEdbClassifyMaxSort 获取分类下最大的排序数
  362. func GetEdbClassifyMaxSort(parentId int, classifyType uint8) (sort int, err error) {
  363. o := orm.NewOrmUsingDB("data")
  364. sql := `SELECT Max(sort) AS sort FROM edb_classify WHERE parent_id=? AND classify_type=? `
  365. err = o.Raw(sql, parentId, classifyType).QueryRow(&sort)
  366. return
  367. }
  368. // GetEdbInfoMaxSortByClassifyId 获取分类下指标的最大的排序数
  369. func GetEdbInfoMaxSortByClassifyId(classifyId int) (sort int, err error) {
  370. o := orm.NewOrmUsingDB("data")
  371. sql := `SELECT Max(sort) AS sort FROM edb_info WHERE classify_id=? `
  372. err = o.Raw(sql, classifyId).QueryRow(&sort)
  373. return
  374. }
  375. type ClassifyEdbInfoListResp struct {
  376. EdbInfoList []*EdbClassifyItems
  377. }
  378. // GetEdbClassifyAllV2 获取普通指标的分类列表
  379. func GetEdbClassifyAllV2(classifyType int8) (items []*EdbClassifyItems, err error) {
  380. o := orm.NewOrmUsingDB("data")
  381. sql := ` SELECT * FROM edb_classify WHERE parent_id <>0 AND classify_type = ? order by sort asc,classify_id asc`
  382. _, err = o.Raw(sql, classifyType).QueryRows(&items)
  383. return
  384. }
  385. func GetEdbClassifyByParentIdTwo(classifyType int8) (items []*EdbClassifyItems, err error) {
  386. o := orm.NewOrmUsingDB("data")
  387. sql := ` SELECT * FROM edb_classify WHERE level=2 AND classify_type = ? order by sort asc,classify_id asc`
  388. _, err = o.Raw(sql, classifyType).QueryRows(&items)
  389. return
  390. }
  391. type AddEdbClassifyReqV2 struct {
  392. ClassifyName string `description:"分类名称"`
  393. ParentId int `description:"父级id,第一级传0"`
  394. Level int `description:"层级,第一级传0,其余传上一级的层级"`
  395. ClassifyType uint8 `description:"分类类型,0:普通指标分类,1:预测指标分类"`
  396. }
  397. // GetEdbClassifyMaxSortV2 获取分类下最大的排序数
  398. func GetEdbClassifyMaxSortV2(parentId int, classifyType uint8) (sort int, err error) {
  399. o := orm.NewOrmUsingDB("data")
  400. sql := `SELECT Max(sort) AS sort FROM edb_classify WHERE parent_id=? AND parent_id_two=? AND classify_type=? `
  401. err = o.Raw(sql, parentId, classifyType).QueryRow(&sort)
  402. return
  403. }
  404. func GetEdbClassifyByLevel(level, source int) (items []*EdbClassifyItems, err error) {
  405. o := orm.NewOrmUsingDB("data")
  406. sql := ` SELECT * FROM edb_classify WHERE level=? AND classify_type = ? `
  407. _, err = o.Raw(sql, level, source).QueryRows(&items)
  408. return
  409. }
  410. func FIXEdbClassifyId(newId, oldId int) (err error) {
  411. o := orm.NewOrmUsingDB("data")
  412. sql := ` UPDATE edb_info SET classify_id=? WHERE classify_id=? `
  413. _, err = o.Raw(sql, newId, oldId).Exec()
  414. return
  415. }
  416. func FixEdbClassify() {
  417. //新增三级分类
  418. list, err := GetEdbClassifyByLevel(2, 0)
  419. if err != nil {
  420. return
  421. }
  422. for _, v := range list {
  423. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  424. classifyInfo := &EdbClassify{
  425. //ClassifyId: 0,
  426. ClassifyType: 0,
  427. ClassifyName: v.ClassifyName,
  428. ParentId: v.ClassifyId,
  429. HasData: 0,
  430. CreateTime: time.Now(),
  431. ModifyTime: time.Now(),
  432. SysUserRealName: "admin",
  433. Level: 3,
  434. UniqueCode: utils.MD5(utils.DATA_PREFIX + "_" + timestamp),
  435. Sort: v.Sort,
  436. }
  437. if utils.RunMode == "debug" {
  438. classifyInfo.SysUserId = 11
  439. } else {
  440. classifyInfo.SysUserId = 163
  441. }
  442. id, err := AddEdbClassify(classifyInfo)
  443. if err != nil {
  444. return
  445. }
  446. err = FIXEdbClassifyId(int(id), v.ClassifyId)
  447. if err != nil {
  448. return
  449. }
  450. }
  451. }
  452. func FixPredictEdbClassify() {
  453. //新增三级分类
  454. list, err := GetEdbClassifyByLevel(2, 1)
  455. if err != nil {
  456. return
  457. }
  458. for _, v := range list {
  459. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  460. classifyInfo := &EdbClassify{
  461. //ClassifyId: 0,
  462. ClassifyType: 1,
  463. ClassifyName: v.ClassifyName,
  464. ParentId: v.ClassifyId,
  465. HasData: 0,
  466. CreateTime: time.Now(),
  467. ModifyTime: time.Now(),
  468. SysUserRealName: "admin",
  469. Level: 3,
  470. UniqueCode: utils.MD5(utils.DATA_PREFIX + "_" + timestamp),
  471. Sort: v.Sort,
  472. }
  473. if utils.RunMode == "debug" {
  474. classifyInfo.SysUserId = 11
  475. } else {
  476. classifyInfo.SysUserId = 163
  477. }
  478. id, err := AddEdbClassify(classifyInfo)
  479. if err != nil {
  480. return
  481. }
  482. err = FIXEdbClassifyId(int(id), v.ClassifyId)
  483. if err != nil {
  484. return
  485. }
  486. }
  487. }
  488. func GetEdbClassify(classifyId int) (classifyIds string, err error) {
  489. o := orm.NewOrmUsingDB("data")
  490. sql := `SELECT GROUP_CONCAT(t.classify_id) AS classify_ids FROM (
  491. SELECT a.classify_id FROM edb_classify AS a
  492. WHERE a.classify_id=?
  493. UNION ALL
  494. SELECT a.classify_id FROM edb_classify AS a
  495. WHERE a.parent_id=? UNION ALL
  496. SELECT
  497. classify_id
  498. FROM
  499. edb_classify
  500. WHERE
  501. parent_id IN ( SELECT classify_id FROM edb_classify WHERE parent_id = ? )
  502. )AS t`
  503. err = o.Raw(sql, classifyId, classifyId, classifyId).QueryRow(&classifyIds)
  504. return
  505. }
  506. func FixEdbClassifySysUser() {
  507. list, err := GetEdbClassifyByLevel(2, 0)
  508. if err != nil {
  509. return
  510. }
  511. for _, v := range list {
  512. err := EditEdbClassifySysUser(v.ClassifyId, v.SysUserId, v.SysUserRealName)
  513. if err != nil {
  514. return
  515. }
  516. fmt.Println("ClassifyId:", v.ClassifyId)
  517. }
  518. fmt.Println("FixEdbClassifySysUser end")
  519. }
  520. func EditEdbClassifySysUser(classifyId, sysUserId int, chartClassifyName string) (err error) {
  521. o := orm.NewOrmUsingDB("data")
  522. sql := `UPDATE edb_classify SET sys_user_id=?,sys_user_real_name=?,modify_time=NOW() WHERE parent_id=? AND level=3 `
  523. _, err = o.Raw(sql, sysUserId, chartClassifyName, classifyId).Exec()
  524. return
  525. }
  526. func FixPredictEdbClassifySysUser() {
  527. list, err := GetEdbClassifyByLevel(2, 1)
  528. if err != nil {
  529. return
  530. }
  531. for _, v := range list {
  532. err := EditEdbClassifySysUser(v.ClassifyId, v.SysUserId, v.SysUserRealName)
  533. if err != nil {
  534. return
  535. }
  536. fmt.Println("ClassifyId:", v.ClassifyId)
  537. }
  538. fmt.Println("EditChartClassifySysUser end")
  539. }
  540. func UpdateEdbClassifyChildByParentClassifyId(classifyIds []int, rootId int, levelStep int) (err error) {
  541. o := orm.NewOrmUsingDB("data")
  542. var pars []interface{}
  543. pars = append(pars, rootId, levelStep)
  544. pars = append(pars, classifyIds)
  545. // 更新相关联的二级分类的parentId,和classify_name_second
  546. sql := `update edb_classify
  547. SET root_id = ?, level = level+?
  548. where classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `)`
  549. _, err = o.Raw(sql, pars).Exec()
  550. if err != nil {
  551. return
  552. }
  553. return
  554. }
  555. // GetEdbClassifyByParentIdAndName 根据父级ID和名称获取分类
  556. func GetEdbClassifyByParentIdAndName(parentId int, classifyName string, classifyId int) (item *EdbClassify, err error) {
  557. o := orm.NewOrmUsingDB("data")
  558. sql := `SELECT * FROM edb_classify WHERE parent_id = ? AND classify_name = ? AND classify_id <> ? LIMIT 1`
  559. err = o.Raw(sql, parentId, classifyName, classifyId).QueryRow(&item)
  560. return
  561. }
  562. // GetEdbClassifyByIdList
  563. // @Description: 根据分类id列表获取分类列表
  564. // @author: Roc
  565. // @datetime 2024-03-29 10:30:21
  566. // @param classifyId int
  567. // @return item *EdbClassify
  568. // @return err error
  569. func GetEdbClassifyByIdList(classifyIdList []int) (item []*EdbClassify, err error) {
  570. num := len(classifyIdList)
  571. if num <= 0 {
  572. return
  573. }
  574. o := orm.NewOrmUsingDB("data")
  575. sql := `SELECT * FROM edb_classify WHERE classify_id in (` + utils.GetOrmInReplace(num) + `) `
  576. _, err = o.Raw(sql, classifyIdList).QueryRows(&item)
  577. return
  578. }
  579. // GetEdbClassifyByClassifyTypeAndIsJoinPermission
  580. // @Description: 根据分类类型和是否涉密获取分类列表
  581. // @author: Roc
  582. // @datetime 2024-03-29 10:30:21
  583. // @param classifyType int
  584. // @param isJoinPermission int
  585. // @return items []*EdbClassify
  586. // @return err error
  587. func GetEdbClassifyByClassifyTypeAndIsJoinPermission(classifyType, isJoinPermission int) (items []*EdbClassify, err error) {
  588. o := orm.NewOrmUsingDB("data")
  589. sql := `SELECT * FROM edb_classify WHERE classify_type = ? AND is_join_permission = ? `
  590. _, err = o.Raw(sql, classifyType, isJoinPermission).QueryRows(&items)
  591. return
  592. }
  593. // GetEdbClassifyRootIdsByClassifyIds 获取普通指标的顶级分类列表
  594. func GetEdbClassifyRootIdsByClassifyIds(classifyIds []int) (items []int, err error) {
  595. o := orm.NewOrmUsingDB("data")
  596. sql := `SELECT distinct root_id FROM edb_classify WHERE classify_type=0 and classify_id in (` + utils.GetOrmInReplace(len(classifyIds)) + `)`
  597. _, err = o.Raw(sql, classifyIds).QueryRows(&items)
  598. return
  599. }