chart_classify.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. type ChartClassifyItems struct {
  138. ChartClassifyId int `description:"分类id"`
  139. ChartInfoId int `description:"指标id"`
  140. ChartClassifyName string
  141. ChartClassifyNameEn string
  142. ParentId int
  143. Level int `description:"层级"`
  144. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  145. UniqueCode string `description:"唯一编码"`
  146. Source int `description:"来源id"`
  147. SourceName string `description:"来源名称"`
  148. SysUserId int `description:"创建人id"`
  149. SysUserRealName string `description:"创建人姓名"`
  150. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"`
  151. StartDate string `description:"自定义开始日期"`
  152. EndDate string `description:"自定义结束日期"`
  153. ChartType int `description:"生成样式:1:曲线图,2:季节性图"`
  154. Calendar string `description:"公历/农历"`
  155. SeasonStartDate string `description:"季节性图开始日期"`
  156. SeasonEndDate string `description:"季节性图开始日期"`
  157. Children []*ChartClassifyItems
  158. Button ChartClassifyItemsButton `description:"按钮权限"`
  159. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  160. HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
  161. Company string
  162. }
  163. // ChartClassifyItemsButton 操作按钮
  164. type ChartClassifyItemsButton struct {
  165. AddButton bool `description:"是否可添加"`
  166. OpButton bool `description:"是否可编辑"`
  167. DeleteButton bool `description:"是否可删除"`
  168. MoveButton bool `description:"是否可移动"`
  169. }
  170. type ChartClassifyListResp struct {
  171. AllNodes []*ChartClassifyItems
  172. Language string `description:"指标的展示语言,CN:中文,EN:英文"`
  173. CanOpClassify bool `description:"是否允许操作分类"`
  174. Paging *paging.PagingItem `description:"分页数据"`
  175. }
  176. type ChartClassifyDeleteCheckResp struct {
  177. DeleteStatus int `description:"检测状态:0:默认值,如果为0,继续走其他校验,1:该分类下关联图表不可删除,2:确认删除当前目录及包含的子目录吗"`
  178. TipsMsg string `description:"提示信息"`
  179. }
  180. type ChartClassifyDeleteCheckReq struct {
  181. ChartClassifyId int `description:"分类id"`
  182. ChartInfoId int `description:"指标id"`
  183. }
  184. func GetChartClassifyCountByClassifyId(chartClassifyId int) (count int, err error) {
  185. o := orm.NewOrm()
  186. sql := ` SELECT COUNT(1) AS count FROM (
  187. SELECT rd.*
  188. FROM (SELECT * FROM chart_classify WHERE parent_id IS NOT NULL) rd,
  189. (SELECT @pid := ?) pd
  190. WHERE FIND_IN_SET(parent_id, @pid) > 0
  191. AND @pid := CONCAT(@pid, ',', chart_classify_id)
  192. UNION SELECT * FROM chart_classify WHERE chart_classify_id = @pid
  193. )AS t
  194. WHERE t.chart_classify_id<>? `
  195. err = o.Raw(sql, chartClassifyId, chartClassifyId).QueryRow(&count)
  196. return
  197. }
  198. func GetChartClassifyByCondition(condition string, pars []interface{}) (item *ChartClassify, err error) {
  199. o := orm.NewOrm()
  200. sql := ` SELECT * FROM chart_classify WHERE 1=1 `
  201. if condition != "" {
  202. sql += condition
  203. }
  204. err = o.Raw(sql, pars).QueryRow(&item)
  205. return
  206. }
  207. // MoveChartClassifyReq 移动图表分类请求参数
  208. type MoveChartClassifyReq struct {
  209. ClassifyId int `description:"分类id"`
  210. ParentClassifyId int `description:"父级分类id"`
  211. PrevClassifyId int `description:"上一个兄弟节点分类id"`
  212. NextClassifyId int `description:"下一个兄弟节点分类id"`
  213. ChartInfoId int `description:"图表ID"`
  214. PrevChartInfoId int `description:"上一个图表ID"`
  215. NextChartInfoId int `description:"下一个图表ID"`
  216. }
  217. // ModifyChartClassifyReq 设置图表分类请求参数
  218. type ModifyChartClassifyReq struct {
  219. SelectAll bool
  220. ChartClassifyIds string
  221. SysUserIds string
  222. ChartName string
  223. ChartInfoIds string `description:"图表ID"`
  224. ChartClassifyId int `description:"新图表分类"`
  225. }
  226. // GetFirstChartClassifyByParentId 获取当前父级图表分类下的排序第一条的数据
  227. func GetFirstChartClassifyByParentId(parentId int) (item *ChartClassify, err error) {
  228. o := orm.NewOrm()
  229. sql := ` SELECT * FROM chart_classify WHERE parent_id=? order by sort asc,chart_classify_id asc limit 1`
  230. err = o.Raw(sql, parentId).QueryRow(&item)
  231. return
  232. }
  233. // GetFirstChartClassifyByParentIdAndSource 获取当前父级图表分类下的排序第一条的数据
  234. func GetFirstChartClassifyByParentIdAndSource(parentId, source int) (item *ChartClassify, err error) {
  235. o := orm.NewOrm()
  236. sql := ` SELECT * FROM chart_classify WHERE parent_id=? and source = ? order by sort asc,chart_classify_id asc limit 1`
  237. err = o.Raw(sql, parentId, source).QueryRow(&item)
  238. return
  239. }
  240. // UpdateChartClassifySortByParentId 根据图表父类id更新排序
  241. func UpdateChartClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string, source int) (err error) {
  242. o := orm.NewOrm()
  243. sql := ` update chart_classify set sort = ` + updateSort + ` WHERE parent_id=? and source=? and sort > ? `
  244. if classifyId > 0 {
  245. sql += ` or ( chart_classify_id > ` + fmt.Sprint(classifyId) + ` and sort= ` + fmt.Sprint(nowSort) + `)`
  246. }
  247. _, err = o.Raw(sql, parentId, source, nowSort).Exec()
  248. return
  249. }
  250. // Update 更新图表分类基础信息
  251. func (chartClassify *ChartClassify) Update(cols []string) (err error) {
  252. o := orm.NewOrm()
  253. _, err = o.Update(chartClassify, cols...)
  254. return
  255. }
  256. // GetChartClassifyMaxSort 获取图表分类下最大的排序数
  257. func GetChartClassifyMaxSort(parentId, source int) (maxSort int, err error) {
  258. o := orm.NewOrm()
  259. var classifyMaxSort int
  260. var chartMaxSort int
  261. sql := `SELECT Max(sort) AS sort FROM chart_classify WHERE parent_id=? AND source = ? `
  262. err = o.Raw(sql, parentId, source).QueryRow(&classifyMaxSort)
  263. if err != nil {
  264. return
  265. }
  266. maxSort = classifyMaxSort
  267. sql = `SELECT Max(sort) AS sort FROM chart_info WHERE chart_classify_id=? AND source = ? `
  268. err = o.Raw(sql, parentId, source).QueryRow(&chartMaxSort)
  269. if err != nil {
  270. return
  271. }
  272. if maxSort < chartMaxSort {
  273. maxSort = chartMaxSort
  274. }
  275. return
  276. }
  277. type ChartClassifyView struct {
  278. ChartClassifyId int `orm:"column(chart_classify_id);pk"`
  279. ChartClassifyName string `description:"分类名称"`
  280. ParentId int `description:"父级id"`
  281. }
  282. func GetChartClassifyViewById(classifyId int) (item *ChartClassifyView, err error) {
  283. o := orm.NewOrm()
  284. sql := `SELECT * FROM chart_classify WHERE chart_classify_id=? `
  285. err = o.Raw(sql, classifyId).QueryRow(&item)
  286. return
  287. }
  288. func GetChartClassifyByParentIdFromETA(parentId int) (items []*ChartClassifyItems, err error) {
  289. o := orm.NewOrm()
  290. sql := ` SELECT * FROM chart_classify WHERE parent_id=? AND source = 1 order by sort asc,chart_classify_id asc`
  291. _, err = o.Raw(sql, parentId).QueryRows(&items)
  292. return
  293. }
  294. func GetChartClassifyAllFromETA() (items []*ChartClassifyItems, err error) {
  295. o := orm.NewOrm()
  296. sql := ` SELECT * FROM chart_classify WHERE parent_id<>0 AND source = 1 order by sort asc,chart_classify_id asc`
  297. _, err = o.Raw(sql).QueryRows(&items)
  298. return
  299. }
  300. func GetChartClassifyTwo(source int) (items []*ChartClassifyItems, err error) {
  301. o := orm.NewOrm()
  302. sql := ` SELECT * FROM chart_classify WHERE level=3 AND source = ? order by sort asc,chart_classify_id asc`
  303. _, err = o.Raw(sql, source).QueryRows(&items)
  304. return
  305. }
  306. func GetChartClassifyByLevel(level, source int) (items []*ChartClassifyItems, err error) {
  307. o := orm.NewOrm()
  308. sql := ` SELECT * FROM chart_classify WHERE level=? AND source = ? order by sort asc,chart_classify_id asc`
  309. _, err = o.Raw(sql, level, source).QueryRows(&items)
  310. return
  311. }
  312. func EditChartClassifySysUser(classifyId, sysUserId int, chartClassifyName string) (err error) {
  313. o := orm.NewOrm()
  314. sql := `UPDATE chart_classify SET sys_user_id=?,sys_user_real_name=?,modify_time=NOW() WHERE parent_id=? AND level=3 `
  315. _, err = o.Raw(sql, sysUserId, chartClassifyName, classifyId).Exec()
  316. return
  317. }
  318. func GetAllChartClassifyItemsBySource(source int) (items []*ChartClassifyItems, err error) {
  319. o := orm.NewOrm()
  320. sql := ` SELECT * FROM chart_classify WHERE source = ? order by sort ASC,chart_classify_id ASC`
  321. _, err = o.Raw(sql, source).QueryRows(&items)
  322. return
  323. }
  324. // GetChartClassifyByParentIdAndName 根据父级ID和名称获取分类
  325. func GetChartClassifyByParentIdAndName(parentId int, classifyName string, classifyId int) (item *ChartClassify, err error) {
  326. o := orm.NewOrm()
  327. sql := `SELECT * FROM chart_classify WHERE parent_id = ? AND chart_classify_name = ? AND chart_classify_id <> ? LIMIT 1`
  328. err = o.Raw(sql, parentId, classifyName, classifyId).QueryRow(&item)
  329. return
  330. }
  331. // GetChartClassifyByIdList
  332. // @Description: 根据分类ID列表获取分类
  333. // @author: Roc
  334. // @datetime 2024-04-01 13:21:48
  335. // @param classifyId []int
  336. // @return items []*ChartClassify
  337. // @return err error
  338. func GetChartClassifyByIdList(classifyIdList []int) (items []*ChartClassify, err error) {
  339. num := len(classifyIdList)
  340. if num <= 0 {
  341. return
  342. }
  343. o := orm.NewOrm()
  344. sql := `SELECT * FROM chart_classify WHERE chart_classify_id in (` + utils.GetOrmInReplace(num) + `) `
  345. _, err = o.Raw(sql, classifyIdList).QueryRows(&items)
  346. return
  347. }
  348. // GetChartClassifyBySourceAndIsJoinPermission
  349. // @Description: 根据分类类型和是否涉密获取分类列表
  350. // @author: Roc
  351. // @datetime 2024-03-29 10:30:21
  352. // @param source int
  353. // @param isJoinPermission int
  354. // @return items []*ChartClassify
  355. // @return err error
  356. func GetChartClassifyBySourceAndIsJoinPermission(source, isJoinPermission int) (items []*ChartClassify, err error) {
  357. o := orm.NewOrm()
  358. sql := `SELECT * FROM chart_classify WHERE source = ? AND is_join_permission = ? `
  359. _, err = o.Raw(sql, source, isJoinPermission).QueryRows(&items)
  360. return
  361. }
  362. type ChartClassifyItemList []*ChartClassifyItems
  363. func (m ChartClassifyItemList) Len() int {
  364. return len(m)
  365. }
  366. func (m ChartClassifyItemList) Less(i, j int) bool {
  367. return m[i].Sort < m[j].Sort
  368. }
  369. func (m ChartClassifyItemList) Swap(i, j int) {
  370. m[i], m[j] = m[j], m[i]
  371. }