edb_classify.go 15 KB

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