edb_classify.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. }
  24. func AddEdbClassify(item *EdbClassify) (lastId int64, err error) {
  25. o := orm.NewOrmUsingDB("data")
  26. lastId, err = o.Insert(item)
  27. return
  28. }
  29. type AddEdbClassifyReq struct {
  30. ClassifyName string `description:"分类名称"`
  31. ParentId int `description:"父级id,第一级传0"`
  32. Level int `description:"层级,第一级传0,其余传上一级的层级"`
  33. ClassifyType uint8 `description:"分类类型,0:普通指标分类,1:预测指标分类"`
  34. }
  35. func GetEdbClassifyCount(classifyName string, parentId int, classifyType uint8) (count int, err error) {
  36. o := orm.NewOrmUsingDB("data")
  37. sql := `SELECT COUNT(1) AS count FROM edb_classify WHERE parent_id=? AND classify_name=? AND classify_type = ? `
  38. err = o.Raw(sql, parentId, classifyName, classifyType).QueryRow(&count)
  39. return
  40. }
  41. type EditEdbClassifyReq struct {
  42. ClassifyName string `description:"分类名称"`
  43. ClassifyId int `description:"分类名称"`
  44. }
  45. func GetEdbClassifyById(classifyId int) (item *EdbClassify, err error) {
  46. o := orm.NewOrmUsingDB("data")
  47. sql := `SELECT * FROM edb_classify WHERE classify_id=? `
  48. err = o.Raw(sql, classifyId).QueryRow(&item)
  49. return
  50. }
  51. func GetEdbClassifyCountById(classifyId int) (count int, err error) {
  52. o := orm.NewOrmUsingDB("data")
  53. sql := `SELECT count(1) AS count FROM edb_classify WHERE classify_id=? `
  54. err = o.Raw(sql, classifyId).QueryRow(&count)
  55. return
  56. }
  57. func EditEdbClassify(classifyId int, classifyName string) (err error) {
  58. o := orm.NewOrmUsingDB("data")
  59. sql := `UPDATE edb_classify SET classify_name=?,modify_time=NOW() WHERE classify_id=? `
  60. _, err = o.Raw(sql, classifyName, classifyId).Exec()
  61. return
  62. }
  63. type DeleteEdbClassifyReq struct {
  64. ClassifyId int `description:"分类id"`
  65. EdbInfoId int `description:"指标id"`
  66. }
  67. func GetEdbInfoCountByClassifyId(classifyId int) (count int, err error) {
  68. o := orm.NewOrmUsingDB("data")
  69. sql := ` SELECT COUNT(1) AS count FROM edb_info AS a
  70. WHERE a.classify_id IN(
  71. SELECT t.classify_id FROM
  72. (
  73. SELECT rd.*
  74. FROM (SELECT * FROM edb_classify WHERE parent_id IS NOT NULL) rd,
  75. (SELECT @pid := ?) pd
  76. WHERE FIND_IN_SET(parent_id, @pid) > 0
  77. AND @pid := CONCAT(@pid, ',', classify_id)
  78. UNION SELECT * FROM edb_classify WHERE classify_id = @pid
  79. )AS t
  80. ) `
  81. err = o.Raw(sql, classifyId).QueryRow(&count)
  82. return
  83. }
  84. func DeleteEdbClassify(classifyId int) (err error) {
  85. o := orm.NewOrmUsingDB("data")
  86. sql := ` DELETE FROM edb_classify
  87. WHERE classify_id IN(
  88. SELECT t.classify_id FROM
  89. (
  90. SELECT rd.*
  91. FROM (SELECT * FROM edb_classify WHERE parent_id IS NOT NULL) rd,
  92. (SELECT @pid := ?) pd
  93. WHERE FIND_IN_SET(parent_id, @pid) > 0
  94. AND @pid := CONCAT(@pid, ',', classify_id)
  95. UNION SELECT * FROM edb_classify WHERE classify_id = @pid
  96. )AS t
  97. ) `
  98. _, err = o.Raw(sql, classifyId).Exec()
  99. return
  100. }
  101. func GetEdbClassifyByParentId(parentId int, classifyType int8) (items []*EdbClassifyItems, err error) {
  102. o := orm.NewOrmUsingDB("data")
  103. sql := ` SELECT * FROM edb_classify WHERE parent_id=? AND classify_type = ? order by sort asc,classify_id asc`
  104. _, err = o.Raw(sql, parentId, classifyType).QueryRows(&items)
  105. return
  106. }
  107. // GetEdbClassifyAll 获取普通指标的分类列表
  108. func GetEdbClassifyAll() (items []*EdbClassifyItems, err error) {
  109. o := orm.NewOrmUsingDB("data")
  110. sql := ` SELECT * FROM edb_classify WHERE parent_id<>0 AND classify_type = 0 order by sort asc,classify_id asc`
  111. _, err = o.Raw(sql).QueryRows(&items)
  112. return
  113. }
  114. // GetPredictEdbClassifyAll 获取预测指标的分类列表
  115. func GetPredictEdbClassifyAll() (items []*EdbClassifyItems, err error) {
  116. o := orm.NewOrmUsingDB("data")
  117. sql := ` SELECT * FROM edb_classify WHERE classify_type = 1 order by sort asc,classify_id asc`
  118. _, err = o.Raw(sql).QueryRows(&items)
  119. return
  120. }
  121. type EdbClassifyItems struct {
  122. ClassifyId int `description:"分类id"`
  123. EdbInfoId int `description:"指标id"`
  124. ClassifyName string
  125. ClassifyNameEn string
  126. ParentId int
  127. RootId int `description:"顶级id"`
  128. Level int `description:"层级"`
  129. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  130. UniqueCode string `description:"唯一编码"`
  131. Source int `description:"来源id"`
  132. SourceName string `description:"来源名称"`
  133. SysUserId int `description:"创建人id"`
  134. SysUserRealName string `description:"创建人姓名"`
  135. StartDate string
  136. EdbCode string
  137. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  138. Children []*EdbClassifyItems
  139. Button EdbClassifyItemsButton `description:"操作权限"`
  140. }
  141. // EdbClassifyItemsButton 操作按钮
  142. type EdbClassifyItemsButton struct {
  143. AddButton bool `description:"是否可添加"`
  144. OpButton bool `description:"是否可编辑"`
  145. DeleteButton bool `description:"是否可删除"`
  146. MoveButton bool `description:"是否可移动"`
  147. ShowEdbRelation bool `description:"是否展示关联指标"`
  148. ShowChartRelation bool `description:"是否展示关联图表"`
  149. InsertNewDataButton bool `description:"是否可插入最新值"`
  150. }
  151. type EdbClassifyListResp struct {
  152. AllNodes []*EdbClassifyItems
  153. CanOpClassify bool `description:"是否允许操作分类"`
  154. Language string `description:"指标的展示语言,CN:中文,EN:英文"`
  155. }
  156. type ClassifyDeleteCheckResp struct {
  157. DeleteStatus int `description:"检测状态:0:默认值,如果为0,继续走其他校验,1:若目录关联指标不可删除,2:确认删除当前目录及包含的子目录吗,3:当前指标已用作画图,不可删除"`
  158. TipsMsg string `description:"提示信息"`
  159. }
  160. type ClassifyDeleteCheckReq struct {
  161. ClassifyId int `description:"分类id"`
  162. EdbInfoId int `description:"指标id"`
  163. }
  164. func GetClassifyCountByClassifyId(classifyId int) (count int, err error) {
  165. o := orm.NewOrmUsingDB("data")
  166. sql := ` SELECT COUNT(1) AS count FROM (
  167. SELECT rd.*
  168. FROM (SELECT * FROM edb_classify WHERE parent_id IS NOT NULL) rd,
  169. (SELECT @pid := ?) pd
  170. WHERE FIND_IN_SET(parent_id, @pid) > 0
  171. AND @pid := CONCAT(@pid, ',', classify_id)
  172. UNION SELECT * FROM edb_classify WHERE classify_id = @pid
  173. )AS t
  174. WHERE t.classify_id<>? `
  175. err = o.Raw(sql, classifyId, classifyId).QueryRow(&count)
  176. return
  177. }
  178. type EdbClassifySimplify struct {
  179. ClassifyId int `description:"分类id"`
  180. ClassifyName string `description:"分类名称"`
  181. ParentId int
  182. }
  183. // 递归获取所有目录
  184. func GetClassifyAllByClassifyId(classifyId int) (resultList []*EdbClassifySimplify, err error) {
  185. fmt.Println("185")
  186. o := orm.NewOrmUsingDB("data")
  187. for {
  188. sql := ` SELECT * FROM edb_classify AS a
  189. WHERE a.classify_id=? order by sort asc,classify_id asc`
  190. item := new(EdbClassifySimplify)
  191. err = o.Raw(sql, classifyId).QueryRow(&item)
  192. if err != nil {
  193. break
  194. }
  195. if item.ClassifyId > 0 {
  196. resultList = append(resultList, item)
  197. classifyId = item.ParentId
  198. } else {
  199. break
  200. }
  201. }
  202. return resultList, err
  203. }
  204. // MoveEdbClassifyReq 移动分类请求参数
  205. type MoveEdbClassifyReq struct {
  206. ClassifyId int `description:"分类id"`
  207. ParentClassifyId int `description:"父级分类id"`
  208. PrevClassifyId int `description:"上一个兄弟节点分类id"`
  209. NextClassifyId int `description:"下一个兄弟节点分类id"`
  210. EdbInfoId int `description:"指标ID, 如果指标ID有值,则移动对象为指标,否则认为移动对象为分类"`
  211. PrevEdbInfoId int `description:"上一个指标ID"`
  212. NextEdbInfoId int `description:"下一个指标ID"`
  213. }
  214. // GetFirstEdbClassifyByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据
  215. func GetFirstEdbClassifyByParentId(parentId int) (item *EdbClassify, err error) {
  216. o := orm.NewOrmUsingDB("data")
  217. sql := ` SELECT * FROM edb_classify WHERE parent_id=? order by sort asc,classify_id asc limit 1`
  218. err = o.Raw(sql, parentId).QueryRow(&item)
  219. return
  220. }
  221. // UpdateEdbClassifySortByParentId 根据父类id更新排序
  222. func UpdateEdbClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string, classifyType uint8) (err error) {
  223. o := orm.NewOrmUsingDB("data")
  224. sql := ` update edb_classify set sort = ` + updateSort + ` WHERE parent_id=? AND sort > ? AND classify_type = ? `
  225. if classifyId > 0 {
  226. sql += ` or ( classify_id > ` + fmt.Sprint(classifyId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  227. }
  228. _, err = o.Raw(sql, parentId, nowSort, classifyType).Exec()
  229. return
  230. }
  231. // Update 更新分类基础信息
  232. func (edbClassify *EdbClassify) Update(cols []string) (err error) {
  233. o := orm.NewOrmUsingDB("data")
  234. _, err = o.Update(edbClassify, cols...)
  235. return
  236. }
  237. // GetEdbClassifyMaxSort 获取分类下最大的排序数
  238. func GetEdbClassifyMaxSort(parentId int, classifyType uint8) (sort int, err error) {
  239. o := orm.NewOrmUsingDB("data")
  240. sql := `SELECT Max(sort) AS sort FROM edb_classify WHERE parent_id=? AND classify_type=? `
  241. err = o.Raw(sql, parentId, classifyType).QueryRow(&sort)
  242. return
  243. }
  244. type ClassifyEdbInfoListResp struct {
  245. EdbInfoList []*EdbClassifyItems
  246. }
  247. // GetEdbClassifyAllV2 获取普通指标的分类列表
  248. func GetEdbClassifyAllV2(classifyType int8) (items []*EdbClassifyItems, err error) {
  249. o := orm.NewOrmUsingDB("data")
  250. sql := ` SELECT * FROM edb_classify WHERE parent_id <>0 AND classify_type = ? order by sort asc,classify_id asc`
  251. _, err = o.Raw(sql, classifyType).QueryRows(&items)
  252. return
  253. }
  254. func GetEdbClassifyByParentIdTwo(classifyType int8) (items []*EdbClassifyItems, err error) {
  255. o := orm.NewOrmUsingDB("data")
  256. sql := ` SELECT * FROM edb_classify WHERE level=2 AND classify_type = ? order by sort asc,classify_id asc`
  257. _, err = o.Raw(sql, classifyType).QueryRows(&items)
  258. return
  259. }
  260. type AddEdbClassifyReqV2 struct {
  261. ClassifyName string `description:"分类名称"`
  262. ParentId int `description:"父级id,第一级传0"`
  263. Level int `description:"层级,第一级传0,其余传上一级的层级"`
  264. ClassifyType uint8 `description:"分类类型,0:普通指标分类,1:预测指标分类"`
  265. }
  266. // GetEdbClassifyMaxSortV2 获取分类下最大的排序数
  267. func GetEdbClassifyMaxSortV2(parentId int, classifyType uint8) (sort int, err error) {
  268. o := orm.NewOrmUsingDB("data")
  269. sql := `SELECT Max(sort) AS sort FROM edb_classify WHERE parent_id=? AND parent_id_two=? AND classify_type=? `
  270. err = o.Raw(sql, parentId, classifyType).QueryRow(&sort)
  271. return
  272. }
  273. func GetEdbClassifyByLevel(level, source int) (items []*EdbClassifyItems, err error) {
  274. o := orm.NewOrmUsingDB("data")
  275. sql := ` SELECT * FROM edb_classify WHERE level=? AND classify_type = ? `
  276. _, err = o.Raw(sql, level, source).QueryRows(&items)
  277. return
  278. }
  279. func FIXEdbClassifyId(newId, oldId int) (err error) {
  280. o := orm.NewOrmUsingDB("data")
  281. sql := ` UPDATE edb_info SET classify_id=? WHERE classify_id=? `
  282. _, err = o.Raw(sql, newId, oldId).Exec()
  283. return
  284. }
  285. func FixEdbClassify() {
  286. //新增三级分类
  287. list, err := GetEdbClassifyByLevel(2, 0)
  288. if err != nil {
  289. return
  290. }
  291. for _, v := range list {
  292. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  293. classifyInfo := &EdbClassify{
  294. //ClassifyId: 0,
  295. ClassifyType: 0,
  296. ClassifyName: v.ClassifyName,
  297. ParentId: v.ClassifyId,
  298. HasData: 0,
  299. CreateTime: time.Now(),
  300. ModifyTime: time.Now(),
  301. SysUserRealName: "admin",
  302. Level: 3,
  303. UniqueCode: utils.MD5(utils.DATA_PREFIX + "_" + timestamp),
  304. Sort: v.Sort,
  305. }
  306. if utils.RunMode == "debug" {
  307. classifyInfo.SysUserId = 11
  308. } else {
  309. classifyInfo.SysUserId = 163
  310. }
  311. id, err := AddEdbClassify(classifyInfo)
  312. if err != nil {
  313. return
  314. }
  315. err = FIXEdbClassifyId(int(id), v.ClassifyId)
  316. if err != nil {
  317. return
  318. }
  319. }
  320. }
  321. func FixPredictEdbClassify() {
  322. //新增三级分类
  323. list, err := GetEdbClassifyByLevel(2, 1)
  324. if err != nil {
  325. return
  326. }
  327. for _, v := range list {
  328. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  329. classifyInfo := &EdbClassify{
  330. //ClassifyId: 0,
  331. ClassifyType: 1,
  332. ClassifyName: v.ClassifyName,
  333. ParentId: v.ClassifyId,
  334. HasData: 0,
  335. CreateTime: time.Now(),
  336. ModifyTime: time.Now(),
  337. SysUserRealName: "admin",
  338. Level: 3,
  339. UniqueCode: utils.MD5(utils.DATA_PREFIX + "_" + timestamp),
  340. Sort: v.Sort,
  341. }
  342. if utils.RunMode == "debug" {
  343. classifyInfo.SysUserId = 11
  344. } else {
  345. classifyInfo.SysUserId = 163
  346. }
  347. id, err := AddEdbClassify(classifyInfo)
  348. if err != nil {
  349. return
  350. }
  351. err = FIXEdbClassifyId(int(id), v.ClassifyId)
  352. if err != nil {
  353. return
  354. }
  355. }
  356. }
  357. func GetEdbClassify(classifyId int) (classifyIds string, err error) {
  358. o := orm.NewOrmUsingDB("data")
  359. sql := `SELECT GROUP_CONCAT(t.classify_id) AS classify_ids FROM (
  360. SELECT a.classify_id FROM edb_classify AS a
  361. WHERE a.classify_id=?
  362. UNION ALL
  363. SELECT a.classify_id FROM edb_classify AS a
  364. WHERE a.parent_id=? UNION ALL
  365. SELECT
  366. classify_id
  367. FROM
  368. edb_classify
  369. WHERE
  370. parent_id IN ( SELECT classify_id FROM edb_classify WHERE parent_id = ? )
  371. )AS t`
  372. err = o.Raw(sql, classifyId, classifyId, classifyId).QueryRow(&classifyIds)
  373. return
  374. }
  375. func FixEdbClassifySysUser() {
  376. list, err := GetEdbClassifyByLevel(2, 0)
  377. if err != nil {
  378. return
  379. }
  380. for _, v := range list {
  381. err := EditEdbClassifySysUser(v.ClassifyId, v.SysUserId, v.SysUserRealName)
  382. if err != nil {
  383. return
  384. }
  385. fmt.Println("ClassifyId:", v.ClassifyId)
  386. }
  387. fmt.Println("FixEdbClassifySysUser end")
  388. }
  389. func EditEdbClassifySysUser(classifyId, sysUserId int, chartClassifyName string) (err error) {
  390. o := orm.NewOrmUsingDB("data")
  391. sql := `UPDATE edb_classify SET sys_user_id=?,sys_user_real_name=?,modify_time=NOW() WHERE parent_id=? AND level=3 `
  392. _, err = o.Raw(sql, sysUserId, chartClassifyName, classifyId).Exec()
  393. return
  394. }
  395. func FixPredictEdbClassifySysUser() {
  396. list, err := GetEdbClassifyByLevel(2, 1)
  397. if err != nil {
  398. return
  399. }
  400. for _, v := range list {
  401. err := EditEdbClassifySysUser(v.ClassifyId, v.SysUserId, v.SysUserRealName)
  402. if err != nil {
  403. return
  404. }
  405. fmt.Println("ClassifyId:", v.ClassifyId)
  406. }
  407. fmt.Println("EditChartClassifySysUser end")
  408. }
  409. type EdbClassifyItemList []*EdbClassifyItems
  410. func (m EdbClassifyItemList) Len() int {
  411. return len(m)
  412. }
  413. func (m EdbClassifyItemList) Less(i, j int) bool {
  414. return m[i].Sort < m[j].Sort
  415. }
  416. func (m EdbClassifyItemList) Swap(i, j int) {
  417. m[i], m[j] = m[j], m[i]
  418. }
  419. // GetNormalEdbClassifyAll 获取普通指标的分类列表
  420. func GetNormalEdbClassifyAll() (items []*EdbClassifyItems, err error) {
  421. o := orm.NewOrmUsingDB("data")
  422. sql := ` SELECT * FROM edb_classify WHERE classify_type = 0 order by sort asc,classify_id asc`
  423. _, err = o.Raw(sql).QueryRows(&items)
  424. return
  425. }
  426. type EdbClassifyIdItems struct {
  427. ClassifyId int `description:"分类id"`
  428. ClassifyName string
  429. UniqueCode string `description:"唯一编码"`
  430. ParentId int `description:"父级分类id"`
  431. Level int `description:"层级"`
  432. RootId int `description:"顶级分类id"`
  433. }
  434. func GetEdbClassifyByRootIdLevel(rootId int, classifyType uint8, orderStr string) (items []*EdbClassifyItems, err error) {
  435. o := orm.NewOrmUsingDB("data")
  436. sql := ` SELECT * FROM edb_classify WHERE root_id=? AND classify_type = ? `
  437. if orderStr != "" {
  438. sql += orderStr
  439. } else {
  440. sql += ` order by level desc, sort asc, classify_id asc`
  441. }
  442. _, err = o.Raw(sql, rootId, classifyType).QueryRows(&items)
  443. return
  444. }
  445. // GetEdbInfoMaxSortByClassifyId 获取分类下指标的最大的排序数
  446. func GetEdbInfoMaxSortByClassifyId(classifyId int) (sort int, err error) {
  447. o := orm.NewOrmUsingDB("data")
  448. sql := `SELECT Max(sort) AS sort FROM edb_info WHERE classify_id=? `
  449. err = o.Raw(sql, classifyId).QueryRow(&sort)
  450. return
  451. }
  452. func UpdateEdbClassifyChildByParentClassifyId(classifyIds []int, rootId int, levelStep int) (err error) {
  453. o := orm.NewOrmUsingDB("data")
  454. var pars []interface{}
  455. pars = append(pars, rootId, levelStep)
  456. pars = append(pars, classifyIds)
  457. // 更新相关联的二级分类的parentId,和classify_name_second
  458. sql := `update edb_classify
  459. SET root_id = ?, level = level+?
  460. where classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `)`
  461. _, err = o.Raw(sql, pars).Exec()
  462. if err != nil {
  463. return
  464. }
  465. return
  466. }