chart_classify.go 15 KB

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