chart_classify.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. Disable bool `description:"勾选是否禁用"`
  192. }
  193. // ChartClassifyItemsButton 操作按钮
  194. type ChartClassifyItemsButton struct {
  195. AddButton bool `description:"是否可添加"`
  196. OpButton bool `description:"是否可编辑"`
  197. DeleteButton bool `description:"是否可删除"`
  198. MoveButton bool `description:"是否可移动"`
  199. }
  200. type ChartClassifyListResp struct {
  201. AllNodes []*ChartClassifyItems
  202. Language string `description:"指标的展示语言,CN:中文,EN:英文"`
  203. CanOpClassify bool `description:"是否允许操作分类"`
  204. }
  205. type ChartClassifyDeleteCheckResp struct {
  206. DeleteStatus int `description:"检测状态:0:默认值,如果为0,继续走其他校验,1:该分类下关联图表不可删除,2:确认删除当前目录及包含的子目录吗"`
  207. TipsMsg string `description:"提示信息"`
  208. }
  209. type ChartClassifyDeleteCheckReq struct {
  210. ChartClassifyId int `description:"分类id"`
  211. ChartInfoId int `description:"指标id"`
  212. }
  213. func GetChartClassifyCountByClassifyId(chartClassifyId int) (count int, err error) {
  214. o := orm.NewOrmUsingDB("data")
  215. sql := ` SELECT COUNT(1) AS count FROM (
  216. SELECT rd.*
  217. FROM (SELECT * FROM chart_classify WHERE parent_id IS NOT NULL) rd,
  218. (SELECT @pid := ?) pd
  219. WHERE FIND_IN_SET(parent_id, @pid) > 0
  220. AND @pid := CONCAT(@pid, ',', chart_classify_id)
  221. UNION SELECT * FROM chart_classify WHERE chart_classify_id = @pid
  222. )AS t
  223. WHERE t.chart_classify_id<>? `
  224. err = o.Raw(sql, chartClassifyId, chartClassifyId).QueryRow(&count)
  225. return
  226. }
  227. func GetChartClassifyByCondition(condition string, pars []interface{}) (item *ChartClassify, err error) {
  228. o := orm.NewOrmUsingDB("data")
  229. sql := ` SELECT * FROM chart_classify WHERE 1=1 `
  230. if condition != "" {
  231. sql += condition
  232. }
  233. err = o.Raw(sql, pars).QueryRow(&item)
  234. return
  235. }
  236. // MoveChartClassifyReq 移动图表分类请求参数
  237. type MoveChartClassifyReq struct {
  238. ClassifyId int `description:"分类id"`
  239. ParentClassifyId int `description:"父级分类id"`
  240. PrevClassifyId int `description:"上一个兄弟节点分类id"`
  241. NextClassifyId int `description:"下一个兄弟节点分类id"`
  242. ChartInfoId int `description:"图表ID, 如果图表ID有值,则移动对象为图表,否则认为移动对象分类"`
  243. PrevChartInfoId int `description:"上一个图表ID"`
  244. NextChartInfoId int `description:"下一个图表ID"`
  245. }
  246. // GetFirstChartClassifyByParentId 获取当前父级图表分类下的排序第一条的数据
  247. func GetFirstChartClassifyByParentId(parentId int) (item *ChartClassify, err error) {
  248. o := orm.NewOrmUsingDB("data")
  249. sql := ` SELECT * FROM chart_classify WHERE parent_id=? order by sort asc,chart_classify_id asc limit 1`
  250. err = o.Raw(sql, parentId).QueryRow(&item)
  251. return
  252. }
  253. // GetFirstChartClassifyByParentIdAndSource 获取当前父级图表分类下的排序第一条的数据
  254. func GetFirstChartClassifyByParentIdAndSource(parentId, source int) (item *ChartClassify, err error) {
  255. o := orm.NewOrmUsingDB("data")
  256. sql := ` SELECT * FROM chart_classify WHERE parent_id=? and source = ? order by sort asc,chart_classify_id asc limit 1`
  257. err = o.Raw(sql, parentId, source).QueryRow(&item)
  258. return
  259. }
  260. // UpdateChartClassifySortByParentId 根据图表父类id更新排序
  261. func UpdateChartClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string) (err error) {
  262. o := orm.NewOrmUsingDB("data")
  263. sql := ` update chart_classify set sort = ` + updateSort + ` WHERE parent_id=? and sort > ? `
  264. if classifyId > 0 {
  265. sql += ` or ( chart_classify_id > ` + fmt.Sprint(classifyId) + ` and sort= ` + fmt.Sprint(nowSort) + `)`
  266. }
  267. _, err = o.Raw(sql, parentId, nowSort).Exec()
  268. return
  269. }
  270. // Update 更新图表分类基础信息
  271. func (chartClassify *ChartClassify) Update(cols []string) (err error) {
  272. o := orm.NewOrmUsingDB("data")
  273. _, err = o.Update(chartClassify, cols...)
  274. return
  275. }
  276. // GetChartClassifyMaxSort 获取图表分类下最大的排序数
  277. func GetChartClassifyMaxSort(parentId, source int) (sort int, err error) {
  278. o := orm.NewOrmUsingDB("data")
  279. sql := `SELECT Max(sort) AS sort FROM chart_classify WHERE parent_id=? AND source = ? `
  280. err = o.Raw(sql, parentId, source).QueryRow(&sort)
  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.NewOrmUsingDB("data")
  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.NewOrmUsingDB("data")
  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.NewOrmUsingDB("data")
  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.NewOrmUsingDB("data")
  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.NewOrmUsingDB("data")
  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.NewOrmUsingDB("data")
  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.NewOrmUsingDB("data")
  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. // GetCrossVarietyChartClassifyBySysUserId
  331. // @Description: 根据创建人获取跨品种分析的分类
  332. // @author: Roc
  333. // @datetime 2023-11-24 14:05:43
  334. // @param sysUserId int
  335. // @return item *ChartClassify
  336. // @return err error
  337. func GetCrossVarietyChartClassifyBySysUserId(sysUserId int) (item *ChartClassify, err error) {
  338. o := orm.NewOrmUsingDB("data")
  339. sql := `SELECT * FROM chart_classify WHERE source = ? AND sys_user_id=?`
  340. err = o.Raw(sql, utils.CHART_SOURCE_CROSS_HEDGING, sysUserId).QueryRow(&item)
  341. return
  342. }
  343. // GetChartClassifyByParentIdAndName 根据父级ID和名称获取分类
  344. func GetChartClassifyByParentIdAndName(parentId int, classifyName string, classifyId int) (item *ChartClassify, err error) {
  345. o := orm.NewOrmUsingDB("data")
  346. sql := `SELECT * FROM chart_classify WHERE parent_id = ? AND chart_classify_name = ? AND chart_classify_id <> ? LIMIT 1`
  347. err = o.Raw(sql, parentId, classifyName, classifyId).QueryRow(&item)
  348. return
  349. }
  350. // GetChartClassifyByIdList
  351. // @Description: 根据分类ID列表获取分类
  352. // @author: Roc
  353. // @datetime 2024-04-01 13:21:48
  354. // @param classifyId []int
  355. // @return items []*ChartClassify
  356. // @return err error
  357. func GetChartClassifyByIdList(classifyIdList []int) (items []*ChartClassify, err error) {
  358. num := len(classifyIdList)
  359. if num <= 0 {
  360. return
  361. }
  362. o := orm.NewOrmUsingDB("data")
  363. sql := `SELECT * FROM chart_classify WHERE chart_classify_id in (` + utils.GetOrmInReplace(num) + `) `
  364. _, err = o.Raw(sql, classifyIdList).QueryRows(&items)
  365. return
  366. }
  367. // GetChartClassifyBySourceAndIsJoinPermission
  368. // @Description: 根据分类类型和是否涉密获取分类列表
  369. // @author: Roc
  370. // @datetime 2024-03-29 10:30:21
  371. // @param source int
  372. // @param isJoinPermission int
  373. // @return items []*ChartClassify
  374. // @return err error
  375. func GetChartClassifyBySourceAndIsJoinPermission(source, isJoinPermission int) (items []*ChartClassify, err error) {
  376. o := orm.NewOrmUsingDB("data")
  377. sql := `SELECT * FROM chart_classify WHERE source = ? AND is_join_permission = ? `
  378. _, err = o.Raw(sql, source, isJoinPermission).QueryRows(&items)
  379. return
  380. }
  381. type ChartClassifyIdItems struct {
  382. ChartClassifyId int `description:"分类id"`
  383. ChartClassifyName string `description:"分类名称"`
  384. UniqueCode string `description:"唯一编码"`
  385. ParentId int `description:"父级分类id"`
  386. Level int `description:"层级"`
  387. RootId int `description:"顶级分类id"`
  388. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0" json:"-"`
  389. HaveOperaAuth bool `description:"是否有该数据权限,默认:false"`
  390. }
  391. func GetChartClassifyByRootIdLevel(rootId, source int, orderStr string) (items []*ChartClassifyIdItems, err error) {
  392. o := orm.NewOrmUsingDB("data")
  393. sql := ` SELECT * FROM chart_classify WHERE root_id = ? AND source = ? `
  394. if orderStr != "" {
  395. sql += orderStr
  396. } else {
  397. sql += ` order by level desc, sort asc, chart_classify_id asc`
  398. }
  399. _, err = o.Raw(sql, rootId, source).QueryRows(&items)
  400. return
  401. }
  402. // UpdateChartClassifySortByParentIdAndSource 根据图表父类id更新排序
  403. func UpdateChartClassifySortByParentIdAndSource(parentId, classifyId, nowSort int, updateSort string, source int) (err error) {
  404. o := orm.NewOrmUsingDB("data")
  405. sql := ` update chart_classify set sort = ` + updateSort + ` WHERE parent_id=? and sort > ? AND source = ? `
  406. if classifyId > 0 {
  407. sql += ` or ( chart_classify_id > ` + fmt.Sprint(classifyId) + ` and sort= ` + fmt.Sprint(nowSort) + `)`
  408. }
  409. _, err = o.Raw(sql, parentId, nowSort, source).Exec()
  410. return
  411. }
  412. // GetChartClassifyAllBySource 根据来源获取所有分类
  413. func GetChartClassifyAllBySource(source int) (items []*ChartClassifyItems, err error) {
  414. o := orm.NewOrmUsingDB("data")
  415. sql := ` SELECT * FROM chart_classify WHERE source = ? ORDER BY parent_id ASC, sort ASC, chart_classify_id ASC`
  416. _, err = o.Raw(sql, source).QueryRows(&items)
  417. return
  418. }