edb_classify.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. package data_manage
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "time"
  7. )
  8. type EdbClassify struct {
  9. ClassifyId int `gorm:"primaryKey" `
  10. ClassifyType uint8 `description:"分类类型,0:普通指标分类,1:预测指标分类"`
  11. ClassifyName string `description:"分类名称"`
  12. ParentId int `description:"父级id"`
  13. RootId 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. LastModifyUserId int `description:"最后修改人ID"`
  23. LastModifyUserRealName string `description:"最后修改人姓名"`
  24. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  25. ClassifyNameEn string `description:"英文分类名称"`
  26. }
  27. func AddEdbClassify(item *EdbClassify) (lastId int64, err error) {
  28. err = global.DmSQL["data"].Create(item).Error
  29. if err != nil {
  30. return
  31. }
  32. lastId = int64(item.ClassifyId)
  33. return
  34. }
  35. type AddEdbClassifyReq struct {
  36. ClassifyName string `description:"分类名称"`
  37. ParentId int `description:"父级id,第一级传0"`
  38. Level int `description:"层级,第一级传0,其余传上一级的层级"`
  39. ClassifyType uint8 `description:"分类类型,0:普通指标分类,1:预测指标分类"`
  40. }
  41. func GetEdbClassifyCount(classifyName string, parentId int, classifyType uint8) (count int, err error) {
  42. sql := `SELECT COUNT(1) AS count FROM edb_classify WHERE parent_id=? AND classify_name=? AND classify_type = ? `
  43. err = global.DmSQL["data"].Raw(sql, parentId, classifyName, classifyType).Scan(&count).Error
  44. return
  45. }
  46. func GetEdbClassifyEnCount(classifyNameEn string, parentId int, classifyType uint8) (count int, err error) {
  47. sql := `SELECT COUNT(1) AS count FROM edb_classify WHERE parent_id=? AND classify_name_en = ? AND classify_type = ? `
  48. err = global.DmSQL["data"].Raw(sql, parentId, classifyNameEn, classifyType).Scan(&count).Error
  49. return
  50. }
  51. type EditEdbClassifyReq struct {
  52. ClassifyName string `description:"分类名称"`
  53. ClassifyId int `description:"分类名称"`
  54. }
  55. func GetEdbClassifyById(classifyId int) (item *EdbClassify, err error) {
  56. sql := `SELECT * FROM edb_classify WHERE classify_id=? `
  57. err = global.DmSQL["data"].Raw(sql, classifyId).First(&item).Error
  58. return
  59. }
  60. func GetEdbClassifyCountById(classifyId int) (count int, err error) {
  61. sql := `SELECT count(1) AS count FROM edb_classify WHERE classify_id=? `
  62. err = global.DmSQL["data"].Raw(sql, classifyId).Scan(&count).Error
  63. return
  64. }
  65. func EditEdbClassify(classifyId int, classifyName string, userId int, userRealName string) (err error) {
  66. sql := `UPDATE edb_classify SET classify_name=?,modify_time=NOW(), last_modify_user_id = ?, last_modify_user_real_name = ? WHERE classify_id=? `
  67. err = global.DmSQL["data"].Exec(sql, classifyName, userId, userRealName, classifyId).Error
  68. return
  69. }
  70. type DeleteEdbClassifyReq struct {
  71. ClassifyId int `description:"分类id"`
  72. EdbInfoId int `description:"指标id"`
  73. }
  74. func GetEdbInfoCountByClassifyId(classifyId int) (count int, err error) {
  75. sql := `WITH RECURSIVE classify_cte(classify_id, parent_id) AS (
  76. SELECT classify_id, parent_id
  77. FROM edb_classify
  78. WHERE parent_id = ?
  79. UNION ALL
  80. SELECT e.classify_id, e.parent_id
  81. FROM edb_classify e
  82. INNER JOIN classify_cte c ON e.parent_id = c.classify_id
  83. )
  84. SELECT COUNT(1) AS COUNT
  85. FROM edb_info AS a
  86. WHERE a.classify_id IN (SELECT classify_id FROM classify_cte);`
  87. err = global.DmSQL["data"].Raw(sql, classifyId).Scan(&count).Error
  88. return
  89. }
  90. func DeleteEdbClassify(classifyId int) (err error) {
  91. sql := `WITH RECURSIVE ClassifyHierarchy ( classify_id, parent_id ) AS
  92. (
  93. SELECT classify_id,
  94. parent_id
  95. FROM edb_classify
  96. WHERE parent_id = ? OR classify_id=?
  97. UNION ALL
  98. SELECT e.classify_id,
  99. e.parent_id
  100. FROM edb_classify e
  101. INNER JOIN ClassifyHierarchy ch
  102. ON e.parent_id = ch.classify_id
  103. )
  104. DELETE
  105. FROM edb_classify
  106. WHERE classify_id IN ( SELECT classify_id
  107. FROM ClassifyHierarchy
  108. );`
  109. err = global.DmSQL["data"].Exec(sql, classifyId, classifyId).Error
  110. return
  111. }
  112. func GetEdbClassifyByParentId(parentId int, classifyType int8) (items []*EdbClassifyItems, err error) {
  113. o := global.DmSQL["data"]
  114. sql := ` SELECT * FROM edb_classify WHERE parent_id=? AND classify_type = ? order by sort asc,classify_id asc`
  115. err = o.Raw(sql, parentId, classifyType).Scan(&items).Error
  116. return
  117. }
  118. func GetEdbClassifyByRootIdLevel(rootId int, classifyType uint8, orderStr string) (items []*EdbClassifyItems, err error) {
  119. o := global.DmSQL["data"]
  120. sql := ` SELECT * FROM edb_classify WHERE root_id=? AND classify_type = ? `
  121. if orderStr != "" {
  122. sql += orderStr
  123. } else {
  124. sql += ` order by level desc, sort asc, classify_id asc`
  125. }
  126. err = o.Raw(sql, rootId, classifyType).Scan(&items).Error
  127. return
  128. }
  129. func GetAllEdbClassify() (items []*EdbClassifyItems, err error) {
  130. o := global.DmSQL["data"]
  131. sql := ` SELECT * FROM edb_classify WHERE classify_type = 0 order by sort asc,classify_id asc`
  132. err = o.Raw(sql).Scan(&items).Error
  133. return
  134. }
  135. func GetPredictEdbClassifyAll() (items []*EdbClassifyItems, err error) {
  136. o := global.DmSQL["data"]
  137. sql := ` SELECT * FROM edb_classify WHERE classify_type = 1 order by sort asc,classify_id asc`
  138. err = o.Raw(sql).Scan(&items).Error
  139. return
  140. }
  141. func GetAllEdbClassifyByType(classifyType int) (items []*EdbClassifyItems, err error) {
  142. o := global.DmSQL["data"]
  143. sql := ` SELECT * FROM edb_classify WHERE classify_type = ? order by sort ASC,classify_id ASC`
  144. err = o.Raw(sql, classifyType).Scan(&items).Error
  145. return
  146. }
  147. type EdbClassifyItems struct {
  148. ClassifyId int `description:"分类id"`
  149. EdbInfoId int `description:"指标id"`
  150. ClassifyName string
  151. ClassifyNameEn string
  152. ParentId int
  153. RootId int `description:"顶级id"`
  154. Level int `description:"层级"`
  155. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  156. UniqueCode string `description:"唯一编码"`
  157. Source int `description:"来源id"`
  158. SourceName string `description:"来源名称"`
  159. SysUserId int `description:"创建人id"`
  160. SysUserRealName string `description:"创建人姓名"`
  161. StartDate string
  162. EdbCode string
  163. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  164. Children []*EdbClassifyItems `gorm:"-"`
  165. Button EdbClassifyItemsButton `gorm:"-" description:"操作权限"`
  166. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  167. HaveOperaAuth bool `description:"是否有数据权限"`
  168. }
  169. type EdbClassifyIdItems struct {
  170. ClassifyId int `description:"分类id"`
  171. ClassifyName string
  172. ClassifyNameEn string
  173. UniqueCode string `description:"唯一编码"`
  174. ParentId int `description:"父级分类id"`
  175. Level int `description:"层级"`
  176. RootId int `description:"顶级分类id"`
  177. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0" json:"-"`
  178. HaveOperaAuth bool `description:"是否有该数据权限,默认:false"`
  179. }
  180. type EdbClassifyItemList []*EdbClassifyItems
  181. func (m EdbClassifyItemList) Len() int {
  182. return len(m)
  183. }
  184. func (m EdbClassifyItemList) Less(i, j int) bool {
  185. return m[i].Sort < m[j].Sort
  186. }
  187. func (m EdbClassifyItemList) Swap(i, j int) {
  188. m[i], m[j] = m[j], m[i]
  189. }
  190. type EdbClassifyItemsButton struct {
  191. AddButton bool `description:"是否可添加"`
  192. OpButton bool `description:"是否可编辑"`
  193. DeleteButton bool `description:"是否可删除"`
  194. MoveButton bool `description:"是否可移动"`
  195. ShowEdbRelation bool `description:"是否展示关联指标"`
  196. ShowChartRelation bool `description:"是否展示关联图表"`
  197. InsertNewDataButton bool `description:"是否可插入最新值"`
  198. }
  199. type EdbClassifyListResp struct {
  200. AllNodes EdbClassifyItemList
  201. CanOpClassify bool `description:"是否允许操作分类"`
  202. Language string `description:"指标的展示语言,CN:中文,EN:英文"`
  203. }
  204. type ClassifyDeleteCheckResp struct {
  205. DeleteStatus int `description:"检测状态:0:默认值,如果为0,继续走其他校验,1:若目录关联指标不可删除,2:确认删除当前目录及包含的子目录吗,3:当前指标已用作画图,不可删除"`
  206. TipsMsg string `description:"提示信息"`
  207. TableList []*ExcelBaseInfo `description:"关联的表格"`
  208. }
  209. type ExcelBaseInfo struct {
  210. ExcelInfoId int `orm:"column(excel_info_id);pk" gorm:"primaryKey" `
  211. Source int `description:"表格来源,1:excel插件的表格,2:自定义表格,3:混合表格,4:自定义分析,默认:1"`
  212. ExcelType int `description:"表格类型,1:指标列,2:日期列,默认:1"`
  213. ExcelName string `description:"表格名称"`
  214. UniqueCode string `description:"表格唯一编码"`
  215. ExcelClassifyId int `description:"表格分类id"`
  216. }
  217. type ClassifyDeleteCheckReq struct {
  218. ClassifyId int `description:"分类id"`
  219. EdbInfoId int `description:"指标id"`
  220. }
  221. func GetClassifyCountByClassifyId(classifyId int) (count int, err error) {
  222. o := global.DmSQL["data"]
  223. sql := `WITH RECURSIVE classify_cte(classify_id, parent_id) AS (
  224. SELECT classify_id, parent_id
  225. FROM edb_classify
  226. WHERE parent_id = ?
  227. UNION ALL
  228. SELECT e.classify_id, e.parent_id
  229. FROM edb_classify e
  230. INNER JOIN classify_cte c ON e.parent_id = c.classify_id
  231. )
  232. SELECT COUNT(1) AS COUNT
  233. FROM classify_cte
  234. WHERE classify_id <> ? ;`
  235. err = o.Raw(sql, classifyId, classifyId).Scan(&count).Error
  236. return
  237. }
  238. type EdbClassifySimplify struct {
  239. ClassifyId int `description:"分类id"`
  240. ClassifyName string `description:"分类名称"`
  241. ParentId int
  242. }
  243. func GetClassifyAllByClassifyId(classifyId int) (resultList []*EdbClassifySimplify, err error) {
  244. for {
  245. sql := ` SELECT * FROM edb_classify AS a
  246. WHERE a.classify_id=? order by sort asc,classify_id asc`
  247. item := new(EdbClassifySimplify)
  248. err = global.DmSQL["data"].Raw(sql, classifyId).Scan(&item).Error
  249. if err != nil {
  250. break
  251. }
  252. if item.ClassifyId > 0 {
  253. resultList = append(resultList, item)
  254. classifyId = item.ParentId
  255. } else {
  256. break
  257. }
  258. }
  259. return resultList, err
  260. }
  261. type MoveEdbClassifyReq struct {
  262. ClassifyId int `description:"分类id"`
  263. ParentClassifyId int `description:"父级分类id"`
  264. PrevClassifyId int `description:"上一个兄弟节点分类id"`
  265. NextClassifyId int `description:"下一个兄弟节点分类id"`
  266. EdbInfoId int `description:"指标ID, 如果指标ID有值,则移动对象为指标,否则认为移动对象为分类"`
  267. PrevEdbInfoId int `description:"上一个指标ID"`
  268. NextEdbInfoId int `description:"下一个指标ID"`
  269. ClassifyType int `description:"分类类型:0-指标库;2-计算指标"`
  270. }
  271. func GetFirstEdbClassifyByParentId(parentId int) (item *EdbClassify, err error) {
  272. o := global.DmSQL["data"]
  273. sql := ` SELECT * FROM edb_classify WHERE parent_id=? order by sort asc,classify_id asc limit 1`
  274. err = o.Raw(sql, parentId).First(&item).Error
  275. return
  276. }
  277. func UpdateEdbClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string, classifyType uint8) (err error) {
  278. sql := ` update edb_classify set sort = ` + updateSort + ` WHERE parent_id=? AND sort > ? AND classify_type = ? `
  279. if classifyId > 0 {
  280. sql += ` or ( classify_id > ` + fmt.Sprint(classifyId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  281. }
  282. err = global.DmSQL["data"].Exec(sql, parentId, nowSort, classifyType).Error
  283. return
  284. }
  285. func (edbClassify *EdbClassify) Update(cols []string) (err error) {
  286. err = global.DmSQL["data"].Select(cols).Updates(edbClassify).Error
  287. return
  288. }
  289. func GetEdbClassifyMaxSort(parentId int, classifyType uint8) (sort int, err error) {
  290. sql := `SELECT COALESCE(Max(sort), 0) AS sort FROM edb_classify WHERE parent_id=? AND classify_type=? `
  291. err = global.DmSQL["data"].Raw(sql, parentId, classifyType).Scan(&sort).Error
  292. return
  293. }
  294. func GetEdbInfoMaxSortByClassifyId(classifyId int) (sort int, err error) {
  295. sql := `SELECT COALESCE(Max(sort), 0) AS sort FROM edb_info WHERE classify_id=? `
  296. err = global.DmSQL["data"].Raw(sql, classifyId).Scan(&sort).Error
  297. return
  298. }
  299. type ClassifyEdbInfoListResp struct {
  300. EdbInfoList []*EdbClassifyItems
  301. }
  302. func GetEdbClassifyAllV2(classifyType int8) (items []*EdbClassifyItems, err error) {
  303. sql := ` SELECT * FROM edb_classify WHERE parent_id <>0 AND classify_type = ? order by sort asc,classify_id asc`
  304. err = global.DmSQL["data"].Raw(sql, classifyType).Scan(&items).Error
  305. return
  306. }
  307. func GetEdbClassifyByParentIdTwo(classifyType int8) (items []*EdbClassifyItems, err error) {
  308. sql := ` SELECT * FROM edb_classify WHERE level=2 AND classify_type = ? order by sort asc,classify_id asc`
  309. err = global.DmSQL["data"].Raw(sql, classifyType).Scan(&items).Error
  310. return
  311. }
  312. type AddEdbClassifyReqV2 struct {
  313. ClassifyName string `description:"分类名称"`
  314. ParentId int `description:"父级id,第一级传0"`
  315. Level int `description:"层级,第一级传0,其余传上一级的层级"`
  316. ClassifyType uint8 `description:"分类类型,0:普通指标分类,1:预测指标分类"`
  317. }
  318. func GetEdbClassifyMaxSortV2(parentId int, classifyType uint8) (sort int, err error) {
  319. sql := `SELECT COALESCE(MAX(sort), 0) AS sort FROM edb_classify WHERE parent_id=? AND parent_id_two=? AND classify_type=? `
  320. err = global.DmSQL["data"].Raw(sql, parentId, classifyType).Scan(&sort).Error
  321. return
  322. }
  323. func GetEdbClassifyByLevel(level, source int) (items []*EdbClassifyItems, err error) {
  324. sql := ` SELECT * FROM edb_classify WHERE level=? AND classify_type = ? `
  325. err = global.DmSQL["data"].Raw(sql, level, source).Scan(&items).Error
  326. return
  327. }
  328. func FIXEdbClassifyId(newId, oldId int) (err error) {
  329. sql := ` UPDATE edb_info SET classify_id=? WHERE classify_id=? `
  330. err = global.DmSQL["data"].Exec(sql, newId, oldId).Error
  331. return
  332. }
  333. func EditEdbClassifySysUser(classifyId, sysUserId int, chartClassifyName string) (err error) {
  334. sql := `UPDATE edb_classify SET sys_user_id=?,sys_user_real_name=?,modify_time=NOW() WHERE parent_id=? AND level=3 `
  335. err = global.DmSQL["data"].Exec(sql, sysUserId, chartClassifyName, classifyId).Error
  336. return
  337. }
  338. func UpdateEdbClassifyChildByParentClassifyId(classifyIds []int, rootId int, levelStep int) (err error) {
  339. var pars []interface{}
  340. pars = append(pars, rootId, levelStep)
  341. pars = append(pars, classifyIds)
  342. sql := `update edb_classify
  343. SET root_id = ?, level = level+?
  344. where classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `)`
  345. err = global.DmSQL["data"].Exec(sql, pars...).Error
  346. if err != nil {
  347. return
  348. }
  349. return
  350. }
  351. func GetEdbClassifyByParentIdAndName(parentId int, classifyName string, classifyId int, classifyType uint8) (item *EdbClassify, err error) {
  352. sql := `SELECT * FROM edb_classify WHERE parent_id = ? AND classify_name = ? AND classify_id <> ? and classify_type=? LIMIT 1`
  353. err = global.DmSQL["data"].Raw(sql, parentId, classifyName, classifyId, classifyType).First(&item).Error
  354. return
  355. }
  356. func GetEdbClassifyByIdList(classifyIdList []int) (item []*EdbClassify, err error) {
  357. num := len(classifyIdList)
  358. if num <= 0 {
  359. return
  360. }
  361. sql := `SELECT * FROM edb_classify WHERE classify_id in (` + utils.GetOrmInReplace(num) + `) `
  362. err = global.DmSQL["data"].Raw(sql, classifyIdList).Scan(&item).Error
  363. return
  364. }
  365. func GetEdbClassifyByClassifyTypeAndIsJoinPermission(classifyType, isJoinPermission int) (items []*EdbClassify, err error) {
  366. sql := `SELECT * FROM edb_classify WHERE classify_type = ? AND is_join_permission = ? `
  367. err = global.DmSQL["data"].Raw(sql, classifyType, isJoinPermission).Scan(&items).Error
  368. return
  369. }
  370. func GetEdbClassifyRootIdsByClassifyIds(classifyIds []int) (items []int, err error) {
  371. o := global.DmSQL["data"]
  372. sql := `SELECT distinct root_id FROM edb_classify WHERE classify_type=0 and classify_id in (` + utils.GetOrmInReplace(len(classifyIds)) + `)`
  373. err = o.Raw(sql, classifyIds).Scan(&items).Error
  374. return
  375. }
  376. func GetChildEdbClassifyByClassifyType(classifyType int) (items []*EdbClassifyItems, err error) {
  377. o := global.DmSQL["data"]
  378. sql := `SELECT * FROM edb_classify WHERE parent_id <> 0 AND classify_type = ? ORDER BY sort ASC,classify_id ASC`
  379. err = o.Raw(sql, classifyType).Find(&items).Error
  380. return
  381. }
  382. func GetIsJoinPermissionEdbClassifyIdList(classifyType int) (list []int, err error) {
  383. pars := make([]interface{}, 0)
  384. sql := ` SELECT classify_id FROM edb_classify WHERE 1=1 AND is_join_permission = 1 `
  385. if classifyType >= 0 {
  386. sql += ` AND classify_type = ? `
  387. pars = append(pars, classifyType)
  388. }
  389. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&list).Error
  390. return
  391. }
  392. func GetEdbClassifyByClassifyTypes(classifyTypes []int) (items []*EdbClassifyItems, err error) {
  393. if len(classifyTypes) == 0 {
  394. return
  395. }
  396. o := global.DmSQL["data"]
  397. sql := `SELECT * FROM edb_classify WHERE classify_type IN (?) ORDER BY classify_type ASC, sort ASC, classify_id ASC`
  398. err = o.Raw(sql, classifyTypes).Scan(&items).Error
  399. return
  400. }