public_chart_classify.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. // ChartPublicClassify
  10. // @Description: 公共分类
  11. type ChartPublicClassify struct {
  12. ChartPublicClassifyId int `gorm:"primaryKey" `
  13. Source int `description:"分类来源: 1-图库; 2-商品价格曲线; 3-相关性图表; 6-拟合方程图表; 7-统计特征"`
  14. ChartPublicClassifyName 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. ChartPublicClassifyNamePath string `description:"分类名称的完整路径,格式为:父级分类名称|当前分类名称"`
  28. ChartPublicClassifyIdPath string `description:"分类的完整路径,格式为:父级ID,当前ID"`
  29. }
  30. func (m ChartPublicClassify) ToChartClassifyItems() *ChartClassifyItems {
  31. return &ChartClassifyItems{
  32. ChartClassifyId: m.ChartPublicClassifyId,
  33. //ChartInfoId: 0,
  34. ChartClassifyName: m.ChartPublicClassifyName,
  35. ChartClassifyNameEn: m.ChartPublicClassifyName,
  36. ParentId: m.ParentId,
  37. Level: m.Level,
  38. Sort: m.Sort,
  39. UniqueCode: m.UniqueCode,
  40. Source: m.Source,
  41. //SourceName: "",
  42. SysUserId: m.SysUserId,
  43. SysUserRealName: m.SysUserRealName,
  44. //DateType: 0,
  45. //StartDate: "",
  46. //EndDate: "",
  47. //ChartType: 0,
  48. //Calendar: "",
  49. //SeasonStartDate: "",
  50. //SeasonEndDate: "",
  51. Children: nil,
  52. Button: ChartClassifyItemsButton{},
  53. IsJoinPermission: 0,
  54. HaveOperaAuth: false,
  55. ChartClassifyIdPath: m.ChartPublicClassifyIdPath,
  56. }
  57. }
  58. // Add
  59. // @Description: 添加分类
  60. // @author: Roc
  61. // @receiver m
  62. // @datetime 2024-12-04 17:00:18
  63. // @return err error
  64. func (m *ChartPublicClassify) Add() (err error) {
  65. err = global.DmSQL["data"].Create(m).Error
  66. return
  67. }
  68. // Update
  69. // @Description: 更新分类基础信息
  70. // @author: Roc
  71. // @receiver m
  72. // @datetime 2024-12-04 17:01:51
  73. // @param cols []string
  74. // @return err error
  75. func (m *ChartPublicClassify) Update(cols []string) (err error) {
  76. err = global.DmSQL["data"].Select(cols).Updates(m).Error
  77. return
  78. }
  79. // GetChartClassifyById
  80. // @Description: 根据id获取分类
  81. // @author: Roc
  82. // @receiver m
  83. // @datetime 2024-12-04 16:56:15
  84. // @param classifyId int
  85. // @return item *ChartPublicClassify
  86. // @return err error
  87. func (m ChartPublicClassify) GetChartClassifyById(classifyId int) (item *ChartPublicClassify, err error) {
  88. sql := `SELECT * FROM chart_public_classify WHERE chart_public_classify_id=? `
  89. err = global.DmSQL["data"].Raw(sql, classifyId).First(&item).Error
  90. return
  91. }
  92. // GetChartClassifyByIdList
  93. // @Description: 根据id列表获取分类列表
  94. // @author: Roc
  95. // @receiver m
  96. // @datetime 2024-12-04 16:56:15
  97. // @param classifyIdList []int
  98. // @return items []*ChartPublicClassify
  99. // @return err error
  100. func (m ChartPublicClassify) GetChartClassifyByIdList(classifyIdList []int) (items []*ChartPublicClassify, err error) {
  101. if len(classifyIdList) <= 0 {
  102. return
  103. }
  104. sql := `SELECT * FROM chart_public_classify WHERE chart_public_classify_id in (?) `
  105. err = global.DmSQL["data"].Raw(sql, classifyIdList).Find(&items).Error
  106. return
  107. }
  108. // GetChartClassifyListByParentId
  109. // @Description: 根据父级id获取下级分类列表
  110. // @author: Roc
  111. // @receiver m
  112. // @datetime 2024-12-03 13:57:48
  113. // @param parentId int
  114. // @param source int8
  115. // @return items []*ChartPublicClassify
  116. // @return err error
  117. func (m ChartPublicClassify) GetChartClassifyListByParentId(parentId int, source int8) (items []*ChartPublicClassify, err error) {
  118. o := global.DmSQL["data"]
  119. sql := `SELECT * FROM chart_public_classify WHERE parent_id=? AND source = ? order by sort asc,chart_public_classify_id asc `
  120. err = o.Raw(sql, parentId, source).Find(&items).Error
  121. return
  122. }
  123. // GetChartClassifyItemsByParentId
  124. // @Description: 根据父级id获取下级分类列表(结构型的)
  125. // @author: Roc
  126. // @receiver m
  127. // @datetime 2024-12-03 13:56:18
  128. // @param parentId int
  129. // @param source int8
  130. // @return items []*ChartClassifyItems
  131. // @return err error
  132. func (m ChartPublicClassify) GetChartClassifyItemsByParentId(parentId int, source int8) (items []*ChartClassifyItems, err error) {
  133. list, err := m.GetChartClassifyListByParentId(parentId, source)
  134. if err != nil {
  135. return
  136. }
  137. for _, v := range list {
  138. items = append(items, v.ToChartClassifyItems())
  139. }
  140. return
  141. }
  142. // GetChartClassifyListByParentId
  143. // @Description: 根据父级id获取下级分类列表
  144. // @author: Roc
  145. // @receiver m
  146. // @datetime 2024-12-03 13:57:48
  147. // @param parentId int
  148. // @param source int8
  149. // @return items []*ChartPublicClassify
  150. // @return err error
  151. func (m ChartPublicClassify) GetChartClassifyListByType(source int8) (items []*ChartPublicClassify, err error) {
  152. o := global.DmSQL["data"]
  153. sql := `SELECT * FROM chart_public_classify WHERE source = ? order by sort asc,chart_public_classify_id asc `
  154. err = o.Raw(sql, source).Find(&items).Error
  155. return
  156. }
  157. // GetAllChartClassifyByType 根据类型获取分类列表
  158. func (m ChartPublicClassify) GetAllChartClassifyByType(source int8) (items []*ChartClassifyItems, err error) {
  159. list, err := m.GetChartClassifyListByType(int8(source))
  160. if err != nil {
  161. return
  162. }
  163. for _, v := range list {
  164. items = append(items, v.ToChartClassifyItems())
  165. }
  166. return
  167. }
  168. func (m ChartPublicClassify) GetChartClassifyCount(classifyName string, parentId int, source int) (count int, err error) {
  169. sql := `SELECT COUNT(1) AS count FROM chart_public_classify WHERE parent_id=? AND chart_public_classify_name=? AND source = ? `
  170. err = global.DmSQL["data"].Raw(sql, parentId, classifyName, source).Scan(&count).Error
  171. return
  172. }
  173. // UpdateChartClassifyNameAndNamePath
  174. // @Description: 更新分类信息
  175. // @author: Roc
  176. // @receiver m
  177. // @datetime 2024-12-05 09:27:33
  178. // @param cols []string
  179. // @param oldClassifyNamePath string
  180. // @param newClassifyNamePath string
  181. // @return err error
  182. func (m *ChartPublicClassify) UpdateChartClassifyNameAndNamePath(cols []string, oldClassifyNamePath, newClassifyNamePath string) (err error) {
  183. tx := global.DmSQL["data"].Begin()
  184. defer func() {
  185. if err != nil {
  186. _ = tx.Rollback()
  187. return
  188. }
  189. _ = tx.Commit()
  190. }()
  191. // 变更分类信息
  192. err = tx.Select(cols).Updates(m).Error
  193. if err != nil {
  194. return
  195. }
  196. // 更改子分类的完整的路径
  197. if oldClassifyNamePath != `` && newClassifyNamePath != `` {
  198. sql := `UPDATE chart_public_classify SET chart_public_classify_name_path = REPLACE(chart_public_classify_name_path,?,?) WHERE chart_public_classify_id_path LIKE ?`
  199. err = tx.Exec(sql, oldClassifyNamePath, newClassifyNamePath, fmt.Sprint(m.ChartPublicClassifyIdPath+`,%`)).Error
  200. if err != nil {
  201. return
  202. }
  203. }
  204. return
  205. }
  206. // GetChartClassifyMaxSort 获取分类下最大的排序数
  207. func (m ChartPublicClassify) GetChartClassifyMaxSort(parentId int, source int) (sort int, err error) {
  208. sql := `SELECT COALESCE(Max(sort), 0) AS sort FROM chart_public_classify WHERE parent_id=? AND source=? `
  209. err = global.DmSQL["data"].Raw(sql, parentId, source).Scan(&sort).Error
  210. return
  211. }
  212. // GetChartInfoCountByClassifyIdList
  213. // @Description: 根据公共分类id集合获取名下指标数量
  214. // @author: Roc
  215. // @receiver m
  216. // @datetime 2024-12-05 09:59:56
  217. // @param classifyIdList []int
  218. // @return count int
  219. // @return err error
  220. func (m ChartPublicClassify) GetChartInfoCountByClassifyIdList(classifyIdList []int) (count int, err error) {
  221. if len(classifyIdList) <= 0 {
  222. return
  223. }
  224. sql := `SELECT COUNT(1) total FROM chart_info WHERE chart_public_classify_id IN (?) `
  225. err = global.DmSQL["data"].Raw(sql, classifyIdList).Scan(&count).Error
  226. return
  227. }
  228. func (m *ChartPublicClassify) GetAllChildClassifyIdList() (items []int, err error) {
  229. sql := ` SELECT chart_public_classify_id FROM chart_public_classify WHERE chart_public_classify_id_path LIKE ? ORDER BY create_time DESC `
  230. err = global.DmSQL["data"].Raw(sql, fmt.Sprint(m.ChartPublicClassifyIdPath+`,%`)).Scan(&items).Error
  231. return
  232. }
  233. // BatchesDel
  234. // @Description: 根据分类id批量删除
  235. // @author: Roc
  236. // @receiver m
  237. // @datetime 2024-12-05 11:13:06
  238. // @param classifyIdList []int
  239. // @return err error
  240. func (m *ChartPublicClassify) BatchesDel(classifyIdList []int) (err error) {
  241. sql := ` DELETE FROM chart_public_classify WHERE chart_public_classify_id IN (?) `
  242. err = global.DmSQL["data"].Exec(sql, classifyIdList).Error
  243. return
  244. }
  245. func (m *ChartPublicClassify) GetChildClassifyListByParentId(classifyIdPath, orderStr string) (items []*ChartPublicClassify, err error) {
  246. o := global.DmSQL["data"]
  247. sql := ` SELECT * FROM chart_public_classify WHERE chart_public_classify_id_path like ? `
  248. if orderStr != "" {
  249. sql += orderStr
  250. } else {
  251. sql += ` order by level asc, sort asc, chart_public_classify_id_path asc`
  252. }
  253. err = o.Raw(sql, classifyIdPath).Scan(&items).Error
  254. return
  255. }
  256. // GetAllChildClassifyByParentId
  257. // @Description: 根据上级id获取所有的下级id
  258. // @author: Roc
  259. // @datetime 2024-12-02 15:14:13
  260. // @param parentId int
  261. // @return targetList []*data_manage.ChartPublicClassify
  262. // @return err error
  263. // @return errMsg string
  264. func (m *ChartPublicClassify) GetAllChildClassifyByParentId(parentId int) (targetList []*ChartPublicClassify, err error, errMsg string) {
  265. //判断是否是挂在顶级目录下
  266. parentClassify, err := m.GetChartClassifyById(parentId)
  267. if err != nil {
  268. if utils.IsErrNoRow(err) {
  269. errMsg = "当前分类不存在"
  270. err = errors.New(errMsg)
  271. return
  272. }
  273. errMsg = "获取失败"
  274. err = errors.New("获取分类信息失败,Err:" + err.Error())
  275. return
  276. }
  277. orderStr := ` order by level asc, sort asc, chart_public_classify_id_path asc`
  278. targetList, err = m.GetChildClassifyListByParentId(fmt.Sprint(parentClassify.ChartPublicClassifyIdPath, ",%"), orderStr)
  279. return
  280. }
  281. // GetChartPublicClassifyByParentIdAndName 根据父级ID和名称获取分类
  282. func (m *ChartPublicClassify) GetChartPublicClassifyByParentIdAndName(source, parentId int, classifyName string, classifyId int) (item *ChartPublicClassify, err error) {
  283. o := global.DmSQL["data"]
  284. sql := `SELECT * FROM chart_public_classify WHERE source = ? AND parent_id = ? AND chart_public_classify_name = ? AND chart_public_classify_id <> ? LIMIT 1`
  285. err = o.Raw(sql, source, parentId, classifyName, classifyId).First(&item).Error
  286. return
  287. }
  288. // UpdateChartClassifySortByParentId 根据父类id更新排序
  289. func (m *ChartPublicClassify) UpdateChartClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string, source int) (err error) {
  290. sql := ` update chart_public_classify set sort = ` + updateSort + ` WHERE parent_id=? AND sort > ? AND source = ? `
  291. if classifyId > 0 {
  292. sql += ` or ( chart_public_classify_id > ` + fmt.Sprint(classifyId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  293. }
  294. err = global.DmSQL["data"].Exec(sql, parentId, nowSort, source).Error
  295. return
  296. }
  297. // GetFirstChartClassifyByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据
  298. func (m *ChartPublicClassify) GetFirstChartClassifyByParentId(parentId int) (item *ChartPublicClassify, err error) {
  299. o := global.DmSQL["data"]
  300. sql := ` SELECT * FROM chart_public_classify WHERE parent_id=? order by sort asc,chart_public_classify_id asc limit 1`
  301. err = o.Raw(sql, parentId).First(&item).Error
  302. return
  303. }
  304. func (m *ChartPublicClassify) GetChartClassifyByRootIdLevel(rootId int, source int, orderStr string) (items []*ChartPublicClassify, err error) {
  305. o := global.DmSQL["data"]
  306. sql := ` SELECT * FROM chart_public_classify WHERE root_id=? AND source = ? `
  307. if orderStr != "" {
  308. sql += orderStr
  309. } else {
  310. sql += ` order by level desc, sort asc, chart_public_classify_id asc`
  311. }
  312. err = o.Raw(sql, rootId, source).Scan(&items).Error
  313. return
  314. }
  315. func (m *ChartPublicClassify) UpdateChartClassifyChildByParentClassifyId(classifyIds []int, rootId int, levelStep int) (err error) {
  316. var pars []interface{}
  317. pars = append(pars, rootId, levelStep)
  318. pars = append(pars, classifyIds)
  319. // 更新相关联的二级分类的parentId,和classify_name_second
  320. sql := `update chart_public_classify
  321. SET root_id = ?, level = level+?
  322. where chart_public_classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `)`
  323. err = global.DmSQL["data"].Exec(sql, pars...).Error
  324. if err != nil {
  325. return
  326. }
  327. return
  328. }
  329. // UpdateChartClassifyNameAndNamePathByOldClassifyIdPath
  330. // @Description: 根据源分类id全路径更新分类名称和子分类的全路径
  331. // @author: Roc
  332. // @receiver chartClassify
  333. // @datetime 2024-11-26 14:39:09
  334. // @param cols []string
  335. // @param oldClassifyNamePath string
  336. // @param newClassifyNamePath string
  337. // @return err error
  338. func (m *ChartPublicClassify) UpdateChartClassifyNameAndNamePathByOldClassifyIdPath(oldClassifyIdPath, newClassifyIdPath, oldClassifyNamePath, newClassifyNamePath string) (err error) {
  339. // 更改子分类的完整的路径
  340. if oldClassifyIdPath != `` && newClassifyNamePath != `` && newClassifyIdPath != `` {
  341. sql := `UPDATE chart_public_classify SET chart_public_classify_id_path = REPLACE(chart_public_classify_id_path,?,?) ,chart_public_classify_name_path = REPLACE(chart_public_classify_name_path,?,?) WHERE chart_public_classify_id_path LIKE ?`
  342. err = global.DmSQL["data"].Exec(sql, oldClassifyIdPath, newClassifyIdPath, oldClassifyNamePath, newClassifyNamePath, fmt.Sprint(oldClassifyIdPath+`,%`)).Error
  343. if err != nil {
  344. return
  345. }
  346. }
  347. return
  348. }