chart_classify.go 18 KB

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