chart_classify.go 16 KB

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