public_edb_classify.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. package data_manage
  2. import (
  3. "errors"
  4. "eta_gn/eta_api/global"
  5. "eta_gn/eta_api/utils"
  6. "fmt"
  7. "time"
  8. )
  9. // EdbPublicClassify
  10. // @Description: 公共分类
  11. type EdbPublicClassify struct {
  12. EdbPublicClassifyId int `gorm:"primaryKey" `
  13. ClassifyType uint8 `description:"分类类型,0:普通指标分类,1:预测指标分类,2:指标加工的分类(普通指标的计算指标)"`
  14. EdbPublicClassifyName string `description:"分类名称"`
  15. ParentId int `description:"父级id"`
  16. HasData int `description:"是否含有指标数据"`
  17. RootId int `description:"顶级id"`
  18. CreateTime time.Time `description:"创建时间"`
  19. ModifyTime time.Time `description:"修改时间"`
  20. SysUserId int `description:"创建人id"`
  21. SysUserRealName string `description:"创建人姓名"`
  22. Level int `description:"层级"`
  23. UniqueCode string `description:"唯一编码"`
  24. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  25. LastModifyUserId int `description:"最后修改人ID"`
  26. LastModifyUserRealName string `description:"最后修改人姓名"`
  27. EdbPublicClassifyNamePath string `description:"分类名称的完整路径,格式为:父级分类名称|当前分类名称"`
  28. EdbPublicClassifyIdPath string `description:"分类的完整路径,格式为:父级ID,当前ID"`
  29. }
  30. func (m EdbPublicClassify) ToEdbClassifyItems() *EdbClassifyItems {
  31. return &EdbClassifyItems{
  32. ClassifyId: m.EdbPublicClassifyId,
  33. //EdbInfoId: 0,
  34. ClassifyType: m.ClassifyType,
  35. ClassifyName: m.EdbPublicClassifyName,
  36. ClassifyNameEn: m.EdbPublicClassifyName,
  37. ParentId: m.ParentId,
  38. RootId: m.RootId,
  39. Level: m.Level,
  40. Sort: m.Sort,
  41. UniqueCode: m.UniqueCode,
  42. //Source: 0,
  43. //SourceName: "",
  44. SysUserId: m.SysUserId,
  45. SysUserRealName: m.SysUserRealName,
  46. //StartDate: "",
  47. //EdbCode: "",
  48. //EdbType: 0,
  49. //Children: nil,
  50. //Button: EdbClassifyItemsButton{},
  51. //IsJoinPermission: 0,
  52. //HaveOperaAuth: false,
  53. ClassifyIdPath: m.EdbPublicClassifyIdPath,
  54. }
  55. }
  56. // Add
  57. // @Description: 添加分类
  58. // @author: Roc
  59. // @receiver m
  60. // @datetime 2024-12-04 17:00:18
  61. // @return err error
  62. func (m *EdbPublicClassify) Add() (err error) {
  63. err = global.DmSQL["data"].Create(m).Error
  64. return
  65. }
  66. // Update
  67. // @Description: 更新分类基础信息
  68. // @author: Roc
  69. // @receiver m
  70. // @datetime 2024-12-04 17:01:51
  71. // @param cols []string
  72. // @return err error
  73. func (m *EdbPublicClassify) Update(cols []string) (err error) {
  74. err = global.DmSQL["data"].Select(cols).Updates(m).Error
  75. return
  76. }
  77. // GetEdbClassifyById
  78. // @Description: 根据id获取分类
  79. // @author: Roc
  80. // @receiver m
  81. // @datetime 2024-12-04 16:56:15
  82. // @param classifyId int
  83. // @return item *EdbPublicClassify
  84. // @return err error
  85. func (m EdbPublicClassify) GetEdbClassifyById(classifyId int) (item *EdbPublicClassify, err error) {
  86. sql := `SELECT * FROM edb_public_classify WHERE edb_public_classify_id=? `
  87. err = global.DmSQL["data"].Raw(sql, classifyId).First(&item).Error
  88. return
  89. }
  90. // GetEdbClassifyByIdList
  91. // @Description: 根据id列表获取分类列表
  92. // @author: Roc
  93. // @receiver m
  94. // @datetime 2024-12-04 16:56:15
  95. // @param classifyIdList []int
  96. // @return items []*EdbPublicClassify
  97. // @return err error
  98. func (m EdbPublicClassify) GetEdbClassifyByIdList(classifyIdList []int) (items []*EdbPublicClassify, err error) {
  99. if len(classifyIdList) <= 0 {
  100. return
  101. }
  102. sql := `SELECT * FROM edb_public_classify WHERE edb_public_classify_id in (?) `
  103. err = global.DmSQL["data"].Raw(sql, classifyIdList).Find(&items).Error
  104. return
  105. }
  106. // GetEdbClassifyListByParentId
  107. // @Description: 根据父级id获取下级分类列表
  108. // @author: Roc
  109. // @receiver m
  110. // @datetime 2024-12-03 13:57:48
  111. // @param parentId int
  112. // @param classifyType int8
  113. // @return items []*EdbPublicClassify
  114. // @return err error
  115. func (m EdbPublicClassify) GetEdbClassifyListByParentId(parentId int, classifyType int8) (items []*EdbPublicClassify, err error) {
  116. o := global.DmSQL["data"]
  117. sql := `SELECT * FROM edb_public_classify WHERE parent_id=? AND classify_type = ? order by sort asc,edb_public_classify_id asc `
  118. err = o.Raw(sql, parentId, classifyType).Find(&items).Error
  119. return
  120. }
  121. // GetEdbClassifyItemsByParentId
  122. // @Description: 根据父级id获取下级分类列表(结构型的)
  123. // @author: Roc
  124. // @receiver m
  125. // @datetime 2024-12-03 13:56:18
  126. // @param parentId int
  127. // @param classifyType int8
  128. // @return items []*EdbClassifyItems
  129. // @return err error
  130. func (m EdbPublicClassify) GetEdbClassifyItemsByParentId(parentId int, classifyType int8) (items []*EdbClassifyItems, err error) {
  131. list, err := m.GetEdbClassifyListByParentId(parentId, classifyType)
  132. if err != nil {
  133. return
  134. }
  135. for _, v := range list {
  136. items = append(items, v.ToEdbClassifyItems())
  137. }
  138. return
  139. }
  140. // GetEdbClassifyListByParentId
  141. // @Description: 根据父级id获取下级分类列表
  142. // @author: Roc
  143. // @receiver m
  144. // @datetime 2024-12-03 13:57:48
  145. // @param parentId int
  146. // @param classifyType int8
  147. // @return items []*EdbPublicClassify
  148. // @return err error
  149. func (m EdbPublicClassify) GetEdbClassifyListByType(classifyType int8) (items []*EdbPublicClassify, err error) {
  150. o := global.DmSQL["data"]
  151. sql := `SELECT * FROM edb_public_classify WHERE classify_type = ? order by sort asc,edb_public_classify_id asc `
  152. err = o.Raw(sql, classifyType).Find(&items).Error
  153. return
  154. }
  155. // GetAllEdbClassifyByType 根据类型获取分类列表
  156. func (m EdbPublicClassify) GetAllEdbClassifyByType(classifyType int8) (items []*EdbClassifyItems, err error) {
  157. list, err := m.GetEdbClassifyListByType(int8(classifyType))
  158. if err != nil {
  159. return
  160. }
  161. for _, v := range list {
  162. items = append(items, v.ToEdbClassifyItems())
  163. }
  164. return
  165. }
  166. func (m EdbPublicClassify) GetEdbClassifyCount(classifyName string, parentId int, classifyType uint8) (count int, err error) {
  167. sql := `SELECT COUNT(1) AS count FROM edb_public_classify WHERE parent_id=? AND edb_public_classify_name=? AND classify_type = ? `
  168. err = global.DmSQL["data"].Raw(sql, parentId, classifyName, classifyType).Scan(&count).Error
  169. return
  170. }
  171. // UpdateEdbClassifyNameAndNamePath
  172. // @Description: 更新分类信息
  173. // @author: Roc
  174. // @receiver m
  175. // @datetime 2024-12-05 09:27:33
  176. // @param cols []string
  177. // @param oldClassifyNamePath string
  178. // @param newClassifyNamePath string
  179. // @return err error
  180. func (m *EdbPublicClassify) UpdateEdbClassifyNameAndNamePath(cols []string, oldClassifyNamePath, newClassifyNamePath string) (err error) {
  181. tx := global.DmSQL["data"].Begin()
  182. defer func() {
  183. if err != nil {
  184. _ = tx.Rollback()
  185. return
  186. }
  187. _ = tx.Commit()
  188. }()
  189. // 变更分类信息
  190. err = tx.Select(cols).Updates(m).Error
  191. if err != nil {
  192. return
  193. }
  194. // 更改子分类的完整的路径
  195. if oldClassifyNamePath != `` && newClassifyNamePath != `` {
  196. sql := `UPDATE edb_public_classify SET edb_public_classify_name_path = REPLACE(edb_public_classify_name_path,?,?) WHERE edb_public_classify_id_path LIKE ?`
  197. err = tx.Exec(sql, oldClassifyNamePath, newClassifyNamePath, fmt.Sprint(m.EdbPublicClassifyIdPath+`,%`)).Error
  198. if err != nil {
  199. return
  200. }
  201. }
  202. return
  203. }
  204. // GetEdbClassifyMaxSort 获取分类下最大的排序数
  205. func (m EdbPublicClassify) GetEdbClassifyMaxSort(parentId int, classifyType uint8) (sort int, err error) {
  206. sql := `SELECT COALESCE(Max(sort), 0) AS sort FROM edb_public_classify WHERE parent_id=? AND classify_type=? `
  207. err = global.DmSQL["data"].Raw(sql, parentId, classifyType).Scan(&sort).Error
  208. return
  209. }
  210. // GetEdbInfoCountByClassifyIdList
  211. // @Description: 根据公共分类id集合获取名下指标数量
  212. // @author: Roc
  213. // @receiver m
  214. // @datetime 2024-12-05 09:59:56
  215. // @param classifyIdList []int
  216. // @return count int
  217. // @return err error
  218. func (m EdbPublicClassify) GetEdbInfoCountByClassifyIdList(classifyIdList []int) (count int, err error) {
  219. if len(classifyIdList) <= 0 {
  220. return
  221. }
  222. sql := `SELECT COUNT(1) total FROM edb_info WHERE edb_public_classify_id IN (?) `
  223. err = global.DmSQL["data"].Raw(sql, classifyIdList).Scan(&count).Error
  224. return
  225. }
  226. func (m *EdbPublicClassify) GetAllChildClassifyIdList() (items []int, err error) {
  227. sql := ` SELECT edb_public_classify_id FROM edb_public_classify WHERE edb_public_classify_id_path LIKE ? ORDER BY create_time DESC `
  228. err = global.DmSQL["data"].Raw(sql, fmt.Sprint(m.EdbPublicClassifyIdPath+`,%`)).Scan(&items).Error
  229. return
  230. }
  231. // BatchesDel
  232. // @Description: 根据分类id批量删除
  233. // @author: Roc
  234. // @receiver m
  235. // @datetime 2024-12-05 11:13:06
  236. // @param classifyIdList []int
  237. // @return err error
  238. func (m *EdbPublicClassify) BatchesDel(classifyIdList []int) (err error) {
  239. sql := ` DELETE FROM edb_public_classify WHERE edb_public_classify_id IN (?) `
  240. err = global.DmSQL["data"].Exec(sql, classifyIdList).Error
  241. return
  242. }
  243. func (m *EdbPublicClassify) GetChildEdbClassifyListByParentId(classifyIdPath, orderStr string) (items []*EdbPublicClassify, err error) {
  244. o := global.DmSQL["data"]
  245. sql := ` SELECT * FROM edb_public_classify WHERE edb_public_classify_id_path like ? `
  246. if orderStr != "" {
  247. sql += orderStr
  248. } else {
  249. sql += ` order by level asc, sort asc, edb_public_classify_id asc`
  250. }
  251. err = o.Raw(sql, classifyIdPath).Scan(&items).Error
  252. return
  253. }
  254. // GetAllChildClassifyByParentId
  255. // @Description: 根据上级id获取所有的下级id
  256. // @author: Roc
  257. // @datetime 2024-12-02 15:14:13
  258. // @param parentId int
  259. // @return targetList []*data_manage.EdbClassifyItems
  260. // @return err error
  261. // @return errMsg string
  262. func (m *EdbPublicClassify) GetAllChildClassifyByParentId(parentId int) (targetList []*EdbPublicClassify, err error, errMsg string) {
  263. //判断是否是挂在顶级目录下
  264. parentClassify, err := m.GetEdbClassifyById(parentId)
  265. if err != nil {
  266. if utils.IsErrNoRow(err) {
  267. errMsg = "当前分类不存在"
  268. err = errors.New(errMsg)
  269. return
  270. }
  271. errMsg = "获取失败"
  272. err = errors.New("获取分类信息失败,Err:" + err.Error())
  273. return
  274. }
  275. orderStr := ` order by level asc, sort asc, edb_public_classify_id asc`
  276. targetList, err = m.GetChildEdbClassifyListByParentId(fmt.Sprint(parentClassify.EdbPublicClassifyIdPath, ",%"), orderStr)
  277. return
  278. }
  279. // GetEdbPublicClassifyByParentIdAndName 根据父级ID和名称获取分类
  280. func (m *EdbPublicClassify) GetEdbPublicClassifyByParentIdAndName(parentId int, classifyName string, classifyId int, classifyType uint8) (item *EdbPublicClassify, err error) {
  281. sql := `SELECT * FROM edb_public_classify WHERE parent_id = ? AND edb_public_classify_name = ? AND edb_public_classify_id <> ? and classify_type=? LIMIT 1`
  282. err = global.DmSQL["data"].Raw(sql, parentId, classifyName, classifyId, classifyType).First(&item).Error
  283. return
  284. }
  285. // UpdateEdbClassifySortByParentId 根据父类id更新排序
  286. func (m *EdbPublicClassify) UpdateEdbClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string, classifyType uint8) (err error) {
  287. sql := ` update edb_public_classify set sort = ` + updateSort + ` WHERE parent_id=? AND sort > ? AND classify_type = ? `
  288. if classifyId > 0 {
  289. sql += ` or ( edb_public_classify_id > ` + fmt.Sprint(classifyId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  290. }
  291. err = global.DmSQL["data"].Exec(sql, parentId, nowSort, classifyType).Error
  292. return
  293. }
  294. // GetFirstEdbClassifyByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据
  295. func (m *EdbPublicClassify) GetFirstEdbClassifyByParentId(parentId int) (item *EdbPublicClassify, err error) {
  296. o := global.DmSQL["data"]
  297. sql := ` SELECT * FROM edb_public_classify WHERE parent_id=? order by sort asc,edb_public_classify_id asc limit 1`
  298. err = o.Raw(sql, parentId).First(&item).Error
  299. return
  300. }
  301. func (m *EdbPublicClassify) GetEdbClassifyByRootIdLevel(rootId int, classifyType uint8, orderStr string) (items []*EdbPublicClassify, err error) {
  302. o := global.DmSQL["data"]
  303. sql := ` SELECT * FROM edb_public_classify WHERE root_id=? AND classify_type = ? `
  304. if orderStr != "" {
  305. sql += orderStr
  306. } else {
  307. sql += ` order by level desc, sort asc, edb_public_classify_id asc`
  308. }
  309. err = o.Raw(sql, rootId, classifyType).Scan(&items).Error
  310. return
  311. }
  312. func (m *EdbPublicClassify) UpdateEdbClassifyChildByParentClassifyId(classifyIds []int, rootId int, levelStep int) (err error) {
  313. var pars []interface{}
  314. pars = append(pars, rootId, levelStep)
  315. pars = append(pars, classifyIds)
  316. // 更新相关联的二级分类的parentId,和classify_name_second
  317. sql := `update edb_public_classify
  318. SET root_id = ?, level = level+?
  319. where edb_public_classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `)`
  320. err = global.DmSQL["data"].Exec(sql, pars...).Error
  321. if err != nil {
  322. return
  323. }
  324. return
  325. }
  326. // UpdateEdbClassifyNameAndNamePathByOldClassifyIdPath
  327. // @Description: 根据源分类id全路径更新分类名称和子分类的全路径
  328. // @author: Roc
  329. // @receiver edbClassify
  330. // @datetime 2024-11-26 14:39:09
  331. // @param cols []string
  332. // @param oldClassifyNamePath string
  333. // @param newClassifyNamePath string
  334. // @return err error
  335. func (m *EdbPublicClassify) UpdateEdbClassifyNameAndNamePathByOldClassifyIdPath(oldClassifyIdPath, newClassifyIdPath, oldClassifyNamePath, newClassifyNamePath string) (err error) {
  336. // 更改子分类的完整的路径
  337. if oldClassifyIdPath != `` && newClassifyNamePath != `` && newClassifyIdPath != `` {
  338. sql := `UPDATE edb_public_classify SET edb_public_classify_id_path = REPLACE(edb_public_classify_id_path,?,?) ,edb_public_classify_name_path = REPLACE(edb_public_classify_name_path,?,?) WHERE edb_public_classify_id_path LIKE ?`
  339. err = global.DmSQL["data"].Exec(sql, oldClassifyIdPath, newClassifyIdPath, oldClassifyNamePath, newClassifyNamePath, fmt.Sprint(oldClassifyIdPath+`,%`)).Error
  340. if err != nil {
  341. return
  342. }
  343. }
  344. return
  345. }