chart_classify.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. package models
  2. import (
  3. "eta/eta_forum_admin/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. "time"
  8. )
  9. type ChartClassify struct {
  10. ChartClassifyId int `orm:"column(chart_classify_id);pk"`
  11. ChartClassifyName string `description:"分类名称"`
  12. ParentId int `description:"父级id"`
  13. HasData int `description:"是否含有指标数据"`
  14. CreateTime time.Time `description:"创建时间"`
  15. ModifyTime time.Time `description:"修改时间"`
  16. SysUserId int `description:"创建人id"`
  17. SysUserRealName string `description:"创建人姓名"`
  18. Level int `description:"层级"`
  19. UniqueCode string `description:"唯一编码"`
  20. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  21. Source int `description:"1:ETA图库;2:商品价格曲线"`
  22. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  23. ChartClassifyNameEn string `description:"英文分类名称"`
  24. LevelPath string `description:"分类的所有上级分类包括自己"`
  25. }
  26. func AddChartClassify(item *ChartClassify) (lastId int64, err error) {
  27. o := orm.NewOrm()
  28. lastId, err = o.Insert(item)
  29. return
  30. }
  31. type AddChartClassifyReq struct {
  32. ChartClassifyName string `description:"分类名称"`
  33. ParentId int `description:"父级id,第一级传0"`
  34. }
  35. func GetChartClassifyCount(chartClassifyName string, parentId, source int) (count int, err error) {
  36. o := orm.NewOrm()
  37. sql := `SELECT COUNT(1) AS count FROM chart_classify WHERE parent_id=? AND source = ? AND chart_classify_name=? `
  38. err = o.Raw(sql, parentId, source, chartClassifyName).QueryRow(&count)
  39. return
  40. }
  41. // GetChartClassifyEnCount
  42. // @Description: 根据图表分类英文名称获取对应的数量
  43. // @author: Roc
  44. // @datetime 2024-04-16 18:13:24
  45. // @param chartClassifyNameEn string
  46. // @param parentId int
  47. // @param source int
  48. // @return count int
  49. // @return err error
  50. func GetChartClassifyEnCount(chartClassifyNameEn string, parentId, source int) (count int, err error) {
  51. o := orm.NewOrm()
  52. sql := `SELECT COUNT(1) AS count FROM chart_classify WHERE parent_id=? AND source = ? AND chart_classify_name_en = ? `
  53. err = o.Raw(sql, parentId, source, chartClassifyNameEn).QueryRow(&count)
  54. return
  55. }
  56. type EditChartClassifyReq struct {
  57. ChartClassifyName string `description:"分类名称"`
  58. ChartClassifyId int `description:"分类id"`
  59. }
  60. func GetChartClassifyById(classifyId int) (item *ChartClassify, err error) {
  61. o := orm.NewOrm()
  62. sql := `SELECT * FROM chart_classify WHERE chart_classify_id=? `
  63. err = o.Raw(sql, classifyId).QueryRow(&item)
  64. return
  65. }
  66. func GetChartClassifyCountById(classifyId int) (count int, err error) {
  67. o := orm.NewOrm()
  68. sql := `SELECT count(1) AS count FROM chart_classify WHERE chart_classify_id=? `
  69. err = o.Raw(sql, classifyId).QueryRow(&count)
  70. return
  71. }
  72. func EditChartClassify(classifyId, source int, chartClassifyName string) (err error) {
  73. o := orm.NewOrm()
  74. sql := `UPDATE chart_classify SET chart_classify_name=?,modify_time=NOW() WHERE chart_classify_id=? AND source = ? `
  75. _, err = o.Raw(sql, chartClassifyName, classifyId, source).Exec()
  76. return
  77. }
  78. type DeleteChartClassifyReq struct {
  79. ChartClassifyId int `description:"分类id"`
  80. //ChartInfoId int `description:"指标id"`
  81. }
  82. func GetChartInfoCountByClassifyId(classifyId int) (count int, err error) {
  83. o := orm.NewOrm()
  84. sql := ` SELECT COUNT(1) AS count FROM chart_info AS a
  85. WHERE a.chart_classify_id IN(
  86. SELECT t.chart_classify_id FROM
  87. (
  88. SELECT rd.*
  89. FROM (SELECT * FROM chart_classify WHERE parent_id IS NOT NULL) rd,
  90. (SELECT @pid := ?) pd
  91. WHERE FIND_IN_SET(parent_id, @pid) > 0
  92. AND @pid := CONCAT(@pid, ',', chart_classify_id)
  93. UNION SELECT * FROM chart_classify WHERE chart_classify_id = @pid
  94. )AS t
  95. ) `
  96. err = o.Raw(sql, classifyId).QueryRow(&count)
  97. return
  98. }
  99. func DeleteChartClassify(classifyId int) (err error) {
  100. o := orm.NewOrm()
  101. sql := ` DELETE FROM chart_classify
  102. WHERE chart_classify_id IN(
  103. SELECT t.chart_classify_id FROM
  104. (
  105. SELECT rd.*
  106. FROM (SELECT * FROM chart_classify WHERE parent_id IS NOT NULL) rd,
  107. (SELECT @pid := ?) pd
  108. WHERE FIND_IN_SET(parent_id, @pid) > 0
  109. AND @pid := CONCAT(@pid, ',', chart_classify_id)
  110. UNION SELECT * FROM chart_classify WHERE chart_classify_id = @pid
  111. )AS t
  112. ) `
  113. _, err = o.Raw(sql, classifyId).Exec()
  114. return
  115. }
  116. // GetChartClassifyByParentId
  117. // @param source int 1:ETA图库;2:商品价格曲线;3:相关性图表
  118. func GetChartClassifyByParentId(parentId, source int) (items []*ChartClassifyItems, err error) {
  119. o := orm.NewOrm()
  120. sql := ` SELECT * FROM chart_classify WHERE parent_id=? AND source = ? order by sort asc,chart_classify_id asc`
  121. _, err = o.Raw(sql, parentId, source).QueryRows(&items)
  122. return
  123. }
  124. // GetChartClassifyAll
  125. // @param source int 1:ETA图库;2:商品价格曲线;3:相关性图表
  126. func GetChartClassifyAll(source int) (items []*ChartClassifyItems, err error) {
  127. o := orm.NewOrm()
  128. sql := ` SELECT * FROM chart_classify WHERE parent_id<>0 AND source = ? order by sort asc,chart_classify_id asc`
  129. _, err = o.Raw(sql, source).QueryRows(&items)
  130. return
  131. }
  132. func GetChartClassify() (items []*ChartClassifyItems, err error) {
  133. o := orm.NewOrm()
  134. sql := ` SELECT * FROM chart_classify order by level desc, sort asc,chart_classify_id asc`
  135. _, err = o.Raw(sql).QueryRows(&items)
  136. return
  137. }
  138. func GetChartClassifyOrderByLevel() (items []*ChartClassifyItems, err error) {
  139. o := orm.NewOrm()
  140. sql := ` SELECT * FROM chart_classify order by level asc, sort asc,chart_classify_id asc`
  141. _, err = o.Raw(sql).QueryRows(&items)
  142. return
  143. }
  144. type ChartClassifyItems struct {
  145. ChartClassifyId int `description:"分类id"`
  146. ChartInfoId int `description:"指标id"`
  147. ChartClassifyName string
  148. ChartClassifyNameEn string
  149. ParentId int
  150. Level int `description:"层级"`
  151. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  152. UniqueCode string `description:"唯一编码"`
  153. Source int `description:"来源id"`
  154. SourceName string `description:"来源名称"`
  155. SysUserId int `description:"创建人id"`
  156. SysUserRealName string `description:"创建人姓名"`
  157. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"`
  158. StartDate string `description:"自定义开始日期"`
  159. EndDate string `description:"自定义结束日期"`
  160. ChartType int `description:"生成样式:1:曲线图,2:季节性图"`
  161. Calendar string `description:"公历/农历"`
  162. SeasonStartDate string `description:"季节性图开始日期"`
  163. SeasonEndDate string `description:"季节性图开始日期"`
  164. Children []*ChartClassifyItems
  165. Button ChartClassifyItemsButton `description:"按钮权限"`
  166. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  167. HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
  168. Company string
  169. }
  170. // ChartClassifyItemsButton 操作按钮
  171. type ChartClassifyItemsButton struct {
  172. AddButton bool `description:"是否可添加"`
  173. OpButton bool `description:"是否可编辑"`
  174. DeleteButton bool `description:"是否可删除"`
  175. MoveButton bool `description:"是否可移动"`
  176. }
  177. type ChartClassifyListResp struct {
  178. AllNodes []*ChartClassifyItems
  179. Language string `description:"指标的展示语言,CN:中文,EN:英文"`
  180. CanOpClassify bool `description:"是否允许操作分类"`
  181. Paging *paging.PagingItem `description:"分页数据"`
  182. }
  183. type ChartClassifyDeleteCheckResp struct {
  184. DeleteStatus int `description:"检测状态:0:默认值,如果为0,继续走其他校验,1:该分类下关联图表不可删除,2:确认删除当前目录及包含的子目录吗"`
  185. TipsMsg string `description:"提示信息"`
  186. }
  187. type ChartClassifyDeleteCheckReq struct {
  188. ChartClassifyId int `description:"分类id"`
  189. ChartInfoId int `description:"指标id"`
  190. }
  191. func GetChartClassifyCountByClassifyId(chartClassifyId int) (count int, err error) {
  192. o := orm.NewOrm()
  193. sql := ` SELECT COUNT(1) AS count FROM (
  194. SELECT rd.*
  195. FROM (SELECT * FROM chart_classify WHERE parent_id IS NOT NULL) rd,
  196. (SELECT @pid := ?) pd
  197. WHERE FIND_IN_SET(parent_id, @pid) > 0
  198. AND @pid := CONCAT(@pid, ',', chart_classify_id)
  199. UNION SELECT * FROM chart_classify WHERE chart_classify_id = @pid
  200. )AS t
  201. WHERE t.chart_classify_id<>? `
  202. err = o.Raw(sql, chartClassifyId, chartClassifyId).QueryRow(&count)
  203. return
  204. }
  205. func GetChartClassifyByCondition(condition string, pars []interface{}) (item *ChartClassify, err error) {
  206. o := orm.NewOrm()
  207. sql := ` SELECT * FROM chart_classify WHERE 1=1 `
  208. if condition != "" {
  209. sql += condition
  210. }
  211. err = o.Raw(sql, pars).QueryRow(&item)
  212. return
  213. }
  214. // MoveChartClassifyReq 移动图表分类请求参数
  215. type MoveChartClassifyReq struct {
  216. ClassifyId int `description:"分类id"`
  217. ParentClassifyId int `description:"父级分类id"`
  218. PrevClassifyId int `description:"上一个兄弟节点分类id"`
  219. NextClassifyId int `description:"下一个兄弟节点分类id"`
  220. ChartInfoId int `description:"图表ID"`
  221. PrevChartInfoId int `description:"上一个图表ID"`
  222. NextChartInfoId int `description:"下一个图表ID"`
  223. }
  224. // ModifyChartClassifyReq 设置图表分类请求参数
  225. type ModifyChartClassifyReq struct {
  226. SelectAll bool
  227. ChartClassifyIds string
  228. SysUserIds string
  229. ChartName string
  230. ChartInfoIds string `description:"图表ID"`
  231. ChartClassifyId int `description:"新图表分类"`
  232. }
  233. // GetFirstChartClassifyByParentId 获取当前父级图表分类下的排序第一条的数据
  234. func GetFirstChartClassifyByParentId(parentId int) (item *ChartClassify, err error) {
  235. o := orm.NewOrm()
  236. sql := ` SELECT * FROM chart_classify WHERE parent_id=? order by sort asc,chart_classify_id asc limit 1`
  237. err = o.Raw(sql, parentId).QueryRow(&item)
  238. return
  239. }
  240. // GetFirstChartClassifyByParentIdAndSource 获取当前父级图表分类下的排序第一条的数据
  241. func GetFirstChartClassifyByParentIdAndSource(parentId, source int) (item *ChartClassify, err error) {
  242. o := orm.NewOrm()
  243. sql := ` SELECT * FROM chart_classify WHERE parent_id=? and source = ? order by sort asc,chart_classify_id asc limit 1`
  244. err = o.Raw(sql, parentId, source).QueryRow(&item)
  245. return
  246. }
  247. // UpdateChartClassifySortByParentId 根据图表父类id更新排序
  248. func UpdateChartClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string, source int) (err error) {
  249. o := orm.NewOrm()
  250. sql := ` update chart_classify set sort = ` + updateSort + ` WHERE parent_id=? and source=? and sort > ? `
  251. if classifyId > 0 {
  252. sql += ` or ( chart_classify_id > ` + fmt.Sprint(classifyId) + ` and sort= ` + fmt.Sprint(nowSort) + `)`
  253. }
  254. _, err = o.Raw(sql, parentId, source, nowSort).Exec()
  255. return
  256. }
  257. // Update 更新图表分类基础信息
  258. func (chartClassify *ChartClassify) Update(cols []string) (err error) {
  259. o := orm.NewOrm()
  260. _, err = o.Update(chartClassify, cols...)
  261. return
  262. }
  263. // GetChartClassifyMaxSort 获取图表分类下最大的排序数
  264. func GetChartClassifyMaxSort(parentId, source int) (maxSort int, err error) {
  265. o := orm.NewOrm()
  266. var classifyMaxSort int
  267. var chartMaxSort int
  268. sql := `SELECT Max(sort) AS sort FROM chart_classify WHERE parent_id=? AND source = ? `
  269. err = o.Raw(sql, parentId, source).QueryRow(&classifyMaxSort)
  270. if err != nil {
  271. return
  272. }
  273. maxSort = classifyMaxSort
  274. sql = `SELECT Max(sort) AS sort FROM chart_info WHERE chart_classify_id=? AND source = ? `
  275. err = o.Raw(sql, parentId, source).QueryRow(&chartMaxSort)
  276. if err != nil {
  277. return
  278. }
  279. if maxSort < chartMaxSort {
  280. maxSort = chartMaxSort
  281. }
  282. return
  283. }
  284. type ChartClassifyView struct {
  285. ChartClassifyId int `orm:"column(chart_classify_id);pk"`
  286. ChartClassifyName string `description:"分类名称"`
  287. ParentId int `description:"父级id"`
  288. }
  289. func GetChartClassifyViewById(classifyId int) (item *ChartClassifyView, err error) {
  290. o := orm.NewOrm()
  291. sql := `SELECT * FROM chart_classify WHERE chart_classify_id=? `
  292. err = o.Raw(sql, classifyId).QueryRow(&item)
  293. return
  294. }
  295. func GetChartClassifyByParentIdFromETA(parentId int) (items []*ChartClassifyItems, err error) {
  296. o := orm.NewOrm()
  297. sql := ` SELECT * FROM chart_classify WHERE parent_id=? AND source = 1 order by sort asc,chart_classify_id asc`
  298. _, err = o.Raw(sql, parentId).QueryRows(&items)
  299. return
  300. }
  301. func GetChartClassifyAllFromETA() (items []*ChartClassifyItems, err error) {
  302. o := orm.NewOrm()
  303. sql := ` SELECT * FROM chart_classify WHERE parent_id<>0 AND source = 1 order by sort asc,chart_classify_id asc`
  304. _, err = o.Raw(sql).QueryRows(&items)
  305. return
  306. }
  307. func GetChartClassifyTwo(source int) (items []*ChartClassifyItems, err error) {
  308. o := orm.NewOrm()
  309. sql := ` SELECT * FROM chart_classify WHERE level=3 AND source = ? order by sort asc,chart_classify_id asc`
  310. _, err = o.Raw(sql, source).QueryRows(&items)
  311. return
  312. }
  313. func GetChartClassifyByLevel(level, source int) (items []*ChartClassifyItems, err error) {
  314. o := orm.NewOrm()
  315. sql := ` SELECT * FROM chart_classify WHERE level=? AND source = ? order by sort asc,chart_classify_id asc`
  316. _, err = o.Raw(sql, level, source).QueryRows(&items)
  317. return
  318. }
  319. func EditChartClassifySysUser(classifyId, sysUserId int, chartClassifyName string) (err error) {
  320. o := orm.NewOrm()
  321. sql := `UPDATE chart_classify SET sys_user_id=?,sys_user_real_name=?,modify_time=NOW() WHERE parent_id=? AND level=3 `
  322. _, err = o.Raw(sql, sysUserId, chartClassifyName, classifyId).Exec()
  323. return
  324. }
  325. func GetAllChartClassifyItemsBySource(source int) (items []*ChartClassifyItems, err error) {
  326. o := orm.NewOrm()
  327. sql := ` SELECT * FROM chart_classify WHERE source = ? order by sort ASC,chart_classify_id ASC`
  328. _, err = o.Raw(sql, source).QueryRows(&items)
  329. return
  330. }
  331. // GetChartClassifyByParentIdAndName 根据父级ID和名称获取分类
  332. func GetChartClassifyByParentIdAndName(parentId int, classifyName string, classifyId int) (item *ChartClassify, err error) {
  333. o := orm.NewOrm()
  334. sql := `SELECT * FROM chart_classify WHERE parent_id = ? AND chart_classify_name = ? AND chart_classify_id <> ? LIMIT 1`
  335. err = o.Raw(sql, parentId, classifyName, classifyId).QueryRow(&item)
  336. return
  337. }
  338. // GetChartClassifyByIdList
  339. // @Description: 根据分类ID列表获取分类
  340. // @author: Roc
  341. // @datetime 2024-04-01 13:21:48
  342. // @param classifyId []int
  343. // @return items []*ChartClassify
  344. // @return err error
  345. func GetChartClassifyByIdList(classifyIdList []int) (items []*ChartClassify, err error) {
  346. num := len(classifyIdList)
  347. if num <= 0 {
  348. return
  349. }
  350. o := orm.NewOrm()
  351. sql := `SELECT * FROM chart_classify WHERE chart_classify_id in (` + utils.GetOrmInReplace(num) + `) `
  352. _, err = o.Raw(sql, classifyIdList).QueryRows(&items)
  353. return
  354. }
  355. // GetChartClassifyBySourceAndIsJoinPermission
  356. // @Description: 根据分类类型和是否涉密获取分类列表
  357. // @author: Roc
  358. // @datetime 2024-03-29 10:30:21
  359. // @param source int
  360. // @param isJoinPermission int
  361. // @return items []*ChartClassify
  362. // @return err error
  363. func GetChartClassifyBySourceAndIsJoinPermission(source, isJoinPermission int) (items []*ChartClassify, err error) {
  364. o := orm.NewOrm()
  365. sql := `SELECT * FROM chart_classify WHERE source = ? AND is_join_permission = ? `
  366. _, err = o.Raw(sql, source, isJoinPermission).QueryRows(&items)
  367. return
  368. }
  369. type ChartClassifyItemList []*ChartClassifyItems
  370. func (m ChartClassifyItemList) Len() int {
  371. return len(m)
  372. }
  373. func (m ChartClassifyItemList) Less(i, j int) bool {
  374. return m[i].Sort < m[j].Sort
  375. }
  376. func (m ChartClassifyItemList) Swap(i, j int) {
  377. m[i], m[j] = m[j], m[i]
  378. }
  379. func GetChartClassifyByLevelPath(levelPath string) (items []*ChartClassify, err error) {
  380. o := orm.NewOrm()
  381. sql := `SELECT * FROM chart_classify where level_path like "` + levelPath + `%"`
  382. _, err = o.Raw(sql).QueryRows(&items)
  383. return
  384. }