edb_classify.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. package data_manage
  2. import (
  3. "eta/eta_mobile/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. }
  272. type ClassifyDeleteCheckReq struct {
  273. ClassifyId int `description:"分类id"`
  274. EdbInfoId int `description:"指标id"`
  275. }
  276. func GetClassifyCountByClassifyId(classifyId int) (count int, err error) {
  277. o := orm.NewOrmUsingDB("data")
  278. sql := ` SELECT COUNT(1) AS count FROM (
  279. SELECT rd.*
  280. FROM (SELECT * FROM edb_classify WHERE parent_id IS NOT NULL) rd,
  281. (SELECT @pid := ?) pd
  282. WHERE FIND_IN_SET(parent_id, @pid) > 0
  283. AND @pid := CONCAT(@pid, ',', classify_id)
  284. UNION SELECT * FROM edb_classify WHERE classify_id = @pid
  285. )AS t
  286. WHERE t.classify_id<>? `
  287. err = o.Raw(sql, classifyId, classifyId).QueryRow(&count)
  288. return
  289. }
  290. type EdbClassifySimplify struct {
  291. ClassifyId int `description:"分类id"`
  292. ClassifyName string `description:"分类名称"`
  293. ParentId int
  294. }
  295. // 递归获取所有目录
  296. func GetClassifyAllByClassifyId(classifyId int) (resultList []*EdbClassifySimplify, err error) {
  297. fmt.Println("185")
  298. o := orm.NewOrmUsingDB("data")
  299. for {
  300. sql := ` SELECT * FROM edb_classify AS a
  301. WHERE a.classify_id=? order by sort asc,classify_id asc`
  302. item := new(EdbClassifySimplify)
  303. err = o.Raw(sql, classifyId).QueryRow(&item)
  304. if err != nil {
  305. break
  306. }
  307. if item.ClassifyId > 0 {
  308. resultList = append(resultList, item)
  309. classifyId = item.ParentId
  310. } else {
  311. break
  312. }
  313. }
  314. return resultList, err
  315. }
  316. // MoveEdbClassifyReq 移动分类请求参数
  317. type MoveEdbClassifyReq struct {
  318. ClassifyId int `description:"分类id"`
  319. ParentClassifyId int `description:"父级分类id"`
  320. PrevClassifyId int `description:"上一个兄弟节点分类id"`
  321. NextClassifyId int `description:"下一个兄弟节点分类id"`
  322. EdbInfoId int `description:"指标ID, 如果指标ID有值,则移动对象为指标,否则认为移动对象为分类"`
  323. PrevEdbInfoId int `description:"上一个指标ID"`
  324. NextEdbInfoId int `description:"下一个指标ID"`
  325. }
  326. // GetFirstEdbClassifyByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据
  327. func GetFirstEdbClassifyByParentId(parentId int) (item *EdbClassify, err error) {
  328. o := orm.NewOrmUsingDB("data")
  329. sql := ` SELECT * FROM edb_classify WHERE parent_id=? order by sort asc,classify_id asc limit 1`
  330. err = o.Raw(sql, parentId).QueryRow(&item)
  331. return
  332. }
  333. // UpdateEdbClassifySortByParentId 根据父类id更新排序
  334. func UpdateEdbClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string, classifyType uint8) (err error) {
  335. o := orm.NewOrmUsingDB("data")
  336. sql := ` update edb_classify set sort = ` + updateSort + ` WHERE parent_id=? AND sort > ? AND classify_type = ? `
  337. if classifyId > 0 {
  338. sql += ` or ( classify_id > ` + fmt.Sprint(classifyId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  339. }
  340. _, err = o.Raw(sql, parentId, nowSort, classifyType).Exec()
  341. return
  342. }
  343. // Update 更新分类基础信息
  344. func (edbClassify *EdbClassify) Update(cols []string) (err error) {
  345. o := orm.NewOrmUsingDB("data")
  346. _, err = o.Update(edbClassify, cols...)
  347. return
  348. }
  349. // GetEdbClassifyMaxSort 获取分类下最大的排序数
  350. func GetEdbClassifyMaxSort(parentId int, classifyType uint8) (sort int, err error) {
  351. o := orm.NewOrmUsingDB("data")
  352. sql := `SELECT Max(sort) AS sort FROM edb_classify WHERE parent_id=? AND classify_type=? `
  353. err = o.Raw(sql, parentId, classifyType).QueryRow(&sort)
  354. return
  355. }
  356. // GetEdbInfoMaxSortByClassifyId 获取分类下指标的最大的排序数
  357. func GetEdbInfoMaxSortByClassifyId(classifyId int) (sort int, err error) {
  358. o := orm.NewOrmUsingDB("data")
  359. sql := `SELECT Max(sort) AS sort FROM edb_info WHERE classify_id=? `
  360. err = o.Raw(sql, classifyId).QueryRow(&sort)
  361. return
  362. }
  363. type ClassifyEdbInfoListResp struct {
  364. EdbInfoList []*EdbClassifyItems
  365. }
  366. // GetEdbClassifyAllV2 获取普通指标的分类列表
  367. func GetEdbClassifyAllV2(classifyType int8) (items []*EdbClassifyItems, err error) {
  368. o := orm.NewOrmUsingDB("data")
  369. sql := ` SELECT * FROM edb_classify WHERE parent_id <>0 AND classify_type = ? order by sort asc,classify_id asc`
  370. _, err = o.Raw(sql, classifyType).QueryRows(&items)
  371. return
  372. }
  373. func GetEdbClassifyByParentIdTwo(classifyType int8) (items []*EdbClassifyItems, err error) {
  374. o := orm.NewOrmUsingDB("data")
  375. sql := ` SELECT * FROM edb_classify WHERE level=2 AND classify_type = ? order by sort asc,classify_id asc`
  376. _, err = o.Raw(sql, classifyType).QueryRows(&items)
  377. return
  378. }
  379. type AddEdbClassifyReqV2 struct {
  380. ClassifyName string `description:"分类名称"`
  381. ParentId int `description:"父级id,第一级传0"`
  382. Level int `description:"层级,第一级传0,其余传上一级的层级"`
  383. ClassifyType uint8 `description:"分类类型,0:普通指标分类,1:预测指标分类"`
  384. }
  385. // GetEdbClassifyMaxSortV2 获取分类下最大的排序数
  386. func GetEdbClassifyMaxSortV2(parentId int, classifyType uint8) (sort int, err error) {
  387. o := orm.NewOrmUsingDB("data")
  388. sql := `SELECT Max(sort) AS sort FROM edb_classify WHERE parent_id=? AND parent_id_two=? AND classify_type=? `
  389. err = o.Raw(sql, parentId, classifyType).QueryRow(&sort)
  390. return
  391. }
  392. func GetEdbClassifyByLevel(level, source int) (items []*EdbClassifyItems, err error) {
  393. o := orm.NewOrmUsingDB("data")
  394. sql := ` SELECT * FROM edb_classify WHERE level=? AND classify_type = ? `
  395. _, err = o.Raw(sql, level, source).QueryRows(&items)
  396. return
  397. }
  398. func FIXEdbClassifyId(newId, oldId int) (err error) {
  399. o := orm.NewOrmUsingDB("data")
  400. sql := ` UPDATE edb_info SET classify_id=? WHERE classify_id=? `
  401. _, err = o.Raw(sql, newId, oldId).Exec()
  402. return
  403. }
  404. func FixEdbClassify() {
  405. //新增三级分类
  406. list, err := GetEdbClassifyByLevel(2, 0)
  407. if err != nil {
  408. return
  409. }
  410. for _, v := range list {
  411. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  412. classifyInfo := &EdbClassify{
  413. //ClassifyId: 0,
  414. ClassifyType: 0,
  415. ClassifyName: v.ClassifyName,
  416. ParentId: v.ClassifyId,
  417. HasData: 0,
  418. CreateTime: time.Now(),
  419. ModifyTime: time.Now(),
  420. SysUserRealName: "admin",
  421. Level: 3,
  422. UniqueCode: utils.MD5(utils.DATA_PREFIX + "_" + timestamp),
  423. Sort: v.Sort,
  424. }
  425. if utils.RunMode == "debug" {
  426. classifyInfo.SysUserId = 11
  427. } else {
  428. classifyInfo.SysUserId = 163
  429. }
  430. id, err := AddEdbClassify(classifyInfo)
  431. if err != nil {
  432. return
  433. }
  434. err = FIXEdbClassifyId(int(id), v.ClassifyId)
  435. if err != nil {
  436. return
  437. }
  438. }
  439. }
  440. func FixPredictEdbClassify() {
  441. //新增三级分类
  442. list, err := GetEdbClassifyByLevel(2, 1)
  443. if err != nil {
  444. return
  445. }
  446. for _, v := range list {
  447. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  448. classifyInfo := &EdbClassify{
  449. //ClassifyId: 0,
  450. ClassifyType: 1,
  451. ClassifyName: v.ClassifyName,
  452. ParentId: v.ClassifyId,
  453. HasData: 0,
  454. CreateTime: time.Now(),
  455. ModifyTime: time.Now(),
  456. SysUserRealName: "admin",
  457. Level: 3,
  458. UniqueCode: utils.MD5(utils.DATA_PREFIX + "_" + timestamp),
  459. Sort: v.Sort,
  460. }
  461. if utils.RunMode == "debug" {
  462. classifyInfo.SysUserId = 11
  463. } else {
  464. classifyInfo.SysUserId = 163
  465. }
  466. id, err := AddEdbClassify(classifyInfo)
  467. if err != nil {
  468. return
  469. }
  470. err = FIXEdbClassifyId(int(id), v.ClassifyId)
  471. if err != nil {
  472. return
  473. }
  474. }
  475. }
  476. func GetEdbClassify(classifyId int) (classifyIds string, err error) {
  477. o := orm.NewOrmUsingDB("data")
  478. sql := `SELECT GROUP_CONCAT(t.classify_id) AS classify_ids FROM (
  479. SELECT a.classify_id FROM edb_classify AS a
  480. WHERE a.classify_id=?
  481. UNION ALL
  482. SELECT a.classify_id FROM edb_classify AS a
  483. WHERE a.parent_id=? UNION ALL
  484. SELECT
  485. classify_id
  486. FROM
  487. edb_classify
  488. WHERE
  489. parent_id IN ( SELECT classify_id FROM edb_classify WHERE parent_id = ? )
  490. )AS t`
  491. err = o.Raw(sql, classifyId, classifyId, classifyId).QueryRow(&classifyIds)
  492. return
  493. }
  494. func FixEdbClassifySysUser() {
  495. list, err := GetEdbClassifyByLevel(2, 0)
  496. if err != nil {
  497. return
  498. }
  499. for _, v := range list {
  500. err := EditEdbClassifySysUser(v.ClassifyId, v.SysUserId, v.SysUserRealName)
  501. if err != nil {
  502. return
  503. }
  504. fmt.Println("ClassifyId:", v.ClassifyId)
  505. }
  506. fmt.Println("FixEdbClassifySysUser end")
  507. }
  508. func EditEdbClassifySysUser(classifyId, sysUserId int, chartClassifyName string) (err error) {
  509. o := orm.NewOrmUsingDB("data")
  510. sql := `UPDATE edb_classify SET sys_user_id=?,sys_user_real_name=?,modify_time=NOW() WHERE parent_id=? AND level=3 `
  511. _, err = o.Raw(sql, sysUserId, chartClassifyName, classifyId).Exec()
  512. return
  513. }
  514. func FixPredictEdbClassifySysUser() {
  515. list, err := GetEdbClassifyByLevel(2, 1)
  516. if err != nil {
  517. return
  518. }
  519. for _, v := range list {
  520. err := EditEdbClassifySysUser(v.ClassifyId, v.SysUserId, v.SysUserRealName)
  521. if err != nil {
  522. return
  523. }
  524. fmt.Println("ClassifyId:", v.ClassifyId)
  525. }
  526. fmt.Println("EditChartClassifySysUser end")
  527. }
  528. func UpdateEdbClassifyChildByParentClassifyId(classifyIds []int, rootId int, levelStep int) (err error) {
  529. o := orm.NewOrmUsingDB("data")
  530. var pars []interface{}
  531. pars = append(pars, rootId, levelStep)
  532. pars = append(pars, classifyIds)
  533. // 更新相关联的二级分类的parentId,和classify_name_second
  534. sql := `update edb_classify
  535. SET root_id = ?, level = level+?
  536. where classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `)`
  537. _, err = o.Raw(sql, pars).Exec()
  538. if err != nil {
  539. return
  540. }
  541. return
  542. }
  543. // GetEdbClassifyByParentIdAndName 根据父级ID和名称获取分类
  544. func GetEdbClassifyByParentIdAndName(parentId int, classifyName string, classifyId int) (item *EdbClassify, err error) {
  545. o := orm.NewOrmUsingDB("data")
  546. sql := `SELECT * FROM edb_classify WHERE parent_id = ? AND classify_name = ? AND classify_id <> ? LIMIT 1`
  547. err = o.Raw(sql, parentId, classifyName, classifyId).QueryRow(&item)
  548. return
  549. }
  550. // GetEdbClassifyByIdList
  551. // @Description: 根据分类id列表获取分类列表
  552. // @author: Roc
  553. // @datetime 2024-03-29 10:30:21
  554. // @param classifyId int
  555. // @return item *EdbClassify
  556. // @return err error
  557. func GetEdbClassifyByIdList(classifyIdList []int) (item []*EdbClassify, err error) {
  558. num := len(classifyIdList)
  559. if num <= 0 {
  560. return
  561. }
  562. o := orm.NewOrmUsingDB("data")
  563. sql := `SELECT * FROM edb_classify WHERE classify_id in (` + utils.GetOrmInReplace(num) + `) `
  564. _, err = o.Raw(sql, classifyIdList).QueryRows(&item)
  565. return
  566. }
  567. // GetEdbClassifyByClassifyTypeAndIsJoinPermission
  568. // @Description: 根据分类类型和是否涉密获取分类列表
  569. // @author: Roc
  570. // @datetime 2024-03-29 10:30:21
  571. // @param classifyType int
  572. // @param isJoinPermission int
  573. // @return items []*EdbClassify
  574. // @return err error
  575. func GetEdbClassifyByClassifyTypeAndIsJoinPermission(classifyType, isJoinPermission int) (items []*EdbClassify, err error) {
  576. o := orm.NewOrmUsingDB("data")
  577. sql := `SELECT * FROM edb_classify WHERE classify_type = ? AND is_join_permission = ? `
  578. _, err = o.Raw(sql, classifyType, isJoinPermission).QueryRows(&items)
  579. return
  580. }