chart_classify.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. }
  22. func AddChartClassify(item *ChartClassify) (lastId int64, err error) {
  23. o := orm.NewOrmUsingDB("data")
  24. lastId, err = o.Insert(item)
  25. return
  26. }
  27. type AddChartClassifyReq struct {
  28. ChartClassifyName string `description:"分类名称"`
  29. ParentId int `description:"父级id,第一级传0"`
  30. Level int `description:"层级,第一级传0,其余传上一级的层级"`
  31. }
  32. func GetChartClassifyCount(chartClassifyName string, parentId, source int) (count int, err error) {
  33. o := orm.NewOrmUsingDB("data")
  34. sql := `SELECT COUNT(1) AS count FROM chart_classify WHERE parent_id=? AND source = ? AND chart_classify_name=? `
  35. err = o.Raw(sql, parentId, source, chartClassifyName).QueryRow(&count)
  36. return
  37. }
  38. type EditChartClassifyReq struct {
  39. ChartClassifyName string `description:"分类名称"`
  40. ChartClassifyId int `description:"分类id"`
  41. }
  42. func GetChartClassifyById(classifyId int) (item *ChartClassify, err error) {
  43. o := orm.NewOrmUsingDB("data")
  44. sql := `SELECT * FROM chart_classify WHERE chart_classify_id=? `
  45. err = o.Raw(sql, classifyId).QueryRow(&item)
  46. return
  47. }
  48. func GetChartClassifyCountById(classifyId int) (count int, err error) {
  49. o := orm.NewOrmUsingDB("data")
  50. sql := `SELECT count(1) AS count FROM chart_classify WHERE chart_classify_id=? `
  51. err = o.Raw(sql, classifyId).QueryRow(&count)
  52. return
  53. }
  54. func EditChartClassify(classifyId, source int, chartClassifyName string) (err error) {
  55. o := orm.NewOrmUsingDB("data")
  56. sql := `UPDATE chart_classify SET chart_classify_name=?,modify_time=NOW() WHERE chart_classify_id=? AND source = ? `
  57. _, err = o.Raw(sql, chartClassifyName, classifyId, source).Exec()
  58. return
  59. }
  60. type DeleteChartClassifyReq struct {
  61. ChartClassifyId int `description:"分类id"`
  62. ChartInfoId int `description:"指标id"`
  63. }
  64. func GetChartInfoCountByClassifyId(classifyId int) (count int, err error) {
  65. o := orm.NewOrmUsingDB("data")
  66. sql := ` SELECT COUNT(1) AS count FROM chart_info AS a
  67. WHERE a.chart_classify_id IN(
  68. SELECT t.chart_classify_id FROM
  69. (
  70. SELECT rd.*
  71. FROM (SELECT * FROM chart_classify WHERE parent_id IS NOT NULL) rd,
  72. (SELECT @pid := ?) pd
  73. WHERE FIND_IN_SET(parent_id, @pid) > 0
  74. AND @pid := CONCAT(@pid, ',', chart_classify_id)
  75. UNION SELECT * FROM chart_classify WHERE chart_classify_id = @pid
  76. )AS t
  77. ) `
  78. err = o.Raw(sql, classifyId).QueryRow(&count)
  79. return
  80. }
  81. func DeleteChartClassify(classifyId int) (err error) {
  82. o := orm.NewOrmUsingDB("data")
  83. sql := ` DELETE FROM chart_classify
  84. WHERE 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).Exec()
  96. return
  97. }
  98. // GetChartClassifyByParentId
  99. // @param source int 1:ETA图库;2:商品价格曲线;3:相关性图表
  100. func GetChartClassifyByParentId(parentId, source int) (items []*ChartClassifyItems, err error) {
  101. o := orm.NewOrmUsingDB("data")
  102. sql := ` SELECT * FROM chart_classify WHERE parent_id=? AND source = ? order by sort asc,chart_classify_id asc`
  103. _, err = o.Raw(sql, parentId, source).QueryRows(&items)
  104. return
  105. }
  106. // GetChartClassifyAll
  107. // @param source int 1:ETA图库;2:商品价格曲线;3:相关性图表
  108. func GetChartClassifyAll(source int) (items []*ChartClassifyItems, err error) {
  109. o := orm.NewOrmUsingDB("data")
  110. sql := ` SELECT * FROM chart_classify WHERE parent_id<>0 AND source = ? order by sort asc,chart_classify_id asc`
  111. _, err = o.Raw(sql, source).QueryRows(&items)
  112. return
  113. }
  114. type ChartClassifyItems struct {
  115. ChartClassifyId int `description:"分类id"`
  116. ChartInfoId int `description:"指标id"`
  117. ChartClassifyName string
  118. ChartClassifyNameEn string
  119. ParentId int
  120. Level int `description:"层级"`
  121. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  122. UniqueCode string `description:"唯一编码"`
  123. Source int `description:"来源id"`
  124. SourceName string `description:"来源名称"`
  125. SysUserId int `description:"创建人id"`
  126. SysUserRealName string `description:"创建人姓名"`
  127. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"`
  128. StartDate string `description:"自定义开始日期"`
  129. EndDate string `description:"自定义结束日期"`
  130. ChartType int `description:"生成样式:1:曲线图,2:季节性图"`
  131. Calendar string `description:"公历/农历"`
  132. SeasonStartDate string `description:"季节性图开始日期"`
  133. SeasonEndDate string `description:"季节性图开始日期"`
  134. Children []*ChartClassifyItems
  135. Button ChartClassifyItemsButton `description:"按钮权限"`
  136. }
  137. // ChartClassifyItemsButton 操作按钮
  138. type ChartClassifyItemsButton struct {
  139. AddButton bool `description:"是否可添加"`
  140. OpButton bool `description:"是否可编辑"`
  141. DeleteButton bool `description:"是否可删除"`
  142. MoveButton bool `description:"是否可移动"`
  143. }
  144. type ChartClassifyListResp struct {
  145. AllNodes []*ChartClassifyItems
  146. Language string `description:"指标的展示语言,CN:中文,EN:英文"`
  147. CanOpClassify bool `description:"是否允许操作分类"`
  148. }
  149. type ChartClassifyDeleteCheckResp struct {
  150. DeleteStatus int `description:"检测状态:0:默认值,如果为0,继续走其他校验,1:该分类下关联图表不可删除,2:确认删除当前目录及包含的子目录吗"`
  151. TipsMsg string `description:"提示信息"`
  152. }
  153. type ChartClassifyDeleteCheckReq struct {
  154. ChartClassifyId int `description:"分类id"`
  155. ChartInfoId int `description:"指标id"`
  156. }
  157. func GetChartClassifyCountByClassifyId(chartClassifyId int) (count int, err error) {
  158. o := orm.NewOrmUsingDB("data")
  159. sql := ` SELECT COUNT(1) AS count FROM (
  160. SELECT rd.*
  161. FROM (SELECT * FROM chart_classify WHERE parent_id IS NOT NULL) rd,
  162. (SELECT @pid := ?) pd
  163. WHERE FIND_IN_SET(parent_id, @pid) > 0
  164. AND @pid := CONCAT(@pid, ',', chart_classify_id)
  165. UNION SELECT * FROM chart_classify WHERE chart_classify_id = @pid
  166. )AS t
  167. WHERE t.chart_classify_id<>? `
  168. err = o.Raw(sql, chartClassifyId, chartClassifyId).QueryRow(&count)
  169. return
  170. }
  171. func GetChartClassifyByCondition(condition string, pars []interface{}) (item *ChartClassify, err error) {
  172. o := orm.NewOrmUsingDB("data")
  173. sql := ` SELECT * FROM chart_classify WHERE 1=1 `
  174. if condition != "" {
  175. sql += condition
  176. }
  177. err = o.Raw(sql, pars).QueryRow(&item)
  178. return
  179. }
  180. // MoveChartClassifyReq 移动图表分类请求参数
  181. type MoveChartClassifyReq struct {
  182. ClassifyId int `description:"分类id"`
  183. ParentClassifyId int `description:"父级分类id"`
  184. PrevClassifyId int `description:"上一个兄弟节点分类id"`
  185. NextClassifyId int `description:"下一个兄弟节点分类id"`
  186. }
  187. // GetFirstChartClassifyByParentId 获取当前父级图表分类下的排序第一条的数据
  188. func GetFirstChartClassifyByParentId(parentId int) (item *ChartClassify, err error) {
  189. o := orm.NewOrmUsingDB("data")
  190. sql := ` SELECT * FROM chart_classify WHERE parent_id=? order by sort asc,chart_classify_id asc limit 1`
  191. err = o.Raw(sql, parentId).QueryRow(&item)
  192. return
  193. }
  194. // GetFirstChartClassifyByParentIdAndSource 获取当前父级图表分类下的排序第一条的数据
  195. func GetFirstChartClassifyByParentIdAndSource(parentId, source int) (item *ChartClassify, err error) {
  196. o := orm.NewOrmUsingDB("data")
  197. sql := ` SELECT * FROM chart_classify WHERE parent_id=? and source = ? order by sort asc,chart_classify_id asc limit 1`
  198. err = o.Raw(sql, parentId, source).QueryRow(&item)
  199. return
  200. }
  201. // UpdateChartClassifySortByParentId 根据图表父类id更新排序
  202. func UpdateChartClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string) (err error) {
  203. o := orm.NewOrmUsingDB("data")
  204. sql := ` update chart_classify set sort = ` + updateSort + ` WHERE parent_id=? and sort > ? `
  205. if classifyId > 0 {
  206. sql += ` or ( chart_classify_id > ` + fmt.Sprint(classifyId) + ` and sort= ` + fmt.Sprint(nowSort) + `)`
  207. }
  208. _, err = o.Raw(sql, parentId, nowSort).Exec()
  209. return
  210. }
  211. // Update 更新图表分类基础信息
  212. func (chartClassify *ChartClassify) Update(cols []string) (err error) {
  213. o := orm.NewOrmUsingDB("data")
  214. _, err = o.Update(chartClassify, cols...)
  215. return
  216. }
  217. // GetChartClassifyMaxSort 获取图表分类下最大的排序数
  218. func GetChartClassifyMaxSort(parentId, source int) (sort int, err error) {
  219. o := orm.NewOrmUsingDB("data")
  220. sql := `SELECT Max(sort) AS sort FROM chart_classify WHERE parent_id=? AND source = ? `
  221. err = o.Raw(sql, parentId, source).QueryRow(&sort)
  222. return
  223. }
  224. type ChartClassifyView struct {
  225. ChartClassifyId int `orm:"column(chart_classify_id);pk"`
  226. ChartClassifyName string `description:"分类名称"`
  227. ParentId int `description:"父级id"`
  228. }
  229. func GetChartClassifyViewById(classifyId int) (item *ChartClassifyView, err error) {
  230. o := orm.NewOrmUsingDB("data")
  231. sql := `SELECT * FROM chart_classify WHERE chart_classify_id=? `
  232. err = o.Raw(sql, classifyId).QueryRow(&item)
  233. return
  234. }
  235. func GetChartClassifyByParentIdFromETA(parentId int) (items []*ChartClassifyItems, err error) {
  236. o := orm.NewOrmUsingDB("data")
  237. sql := ` SELECT * FROM chart_classify WHERE parent_id=? AND source = 1 order by sort asc,chart_classify_id asc`
  238. _, err = o.Raw(sql, parentId).QueryRows(&items)
  239. return
  240. }
  241. func GetChartClassifyAllFromETA() (items []*ChartClassifyItems, err error) {
  242. o := orm.NewOrmUsingDB("data")
  243. sql := ` SELECT * FROM chart_classify WHERE parent_id<>0 AND source = 1 order by sort asc,chart_classify_id asc`
  244. _, err = o.Raw(sql).QueryRows(&items)
  245. return
  246. }
  247. func GetChartClassifyTwo(source int) (items []*ChartClassifyItems, err error) {
  248. o := orm.NewOrmUsingDB("data")
  249. sql := ` SELECT * FROM chart_classify WHERE level=3 AND source = ? order by sort asc,chart_classify_id asc`
  250. _, err = o.Raw(sql, source).QueryRows(&items)
  251. return
  252. }
  253. func GetChartClassifyByLevel(level, source int) (items []*ChartClassifyItems, err error) {
  254. o := orm.NewOrmUsingDB("data")
  255. sql := ` SELECT * FROM chart_classify WHERE level=? AND source = ? order by sort asc,chart_classify_id asc`
  256. _, err = o.Raw(sql, level, source).QueryRows(&items)
  257. return
  258. }
  259. func EditChartClassifySysUser(classifyId, sysUserId int, chartClassifyName string) (err error) {
  260. o := orm.NewOrmUsingDB("data")
  261. sql := `UPDATE chart_classify SET sys_user_id=?,sys_user_real_name=?,modify_time=NOW() WHERE parent_id=? AND level=3 `
  262. _, err = o.Raw(sql, sysUserId, chartClassifyName, classifyId).Exec()
  263. return
  264. }
  265. func GetAllChartClassifyItemsBySource(source int) (items []*ChartClassifyItems, err error) {
  266. o := orm.NewOrmUsingDB("data")
  267. sql := ` SELECT * FROM chart_classify WHERE source = ? order by sort ASC,chart_classify_id ASC`
  268. _, err = o.Raw(sql, source).QueryRows(&items)
  269. return
  270. }
  271. // GetCrossVarietyChartClassifyBySysUserId
  272. // @Description: 根据创建人获取跨品种分析的分类
  273. // @author: Roc
  274. // @datetime 2023-11-24 14:05:43
  275. // @param sysUserId int
  276. // @return item *ChartClassify
  277. // @return err error
  278. func GetCrossVarietyChartClassifyBySysUserId(sysUserId int) (item *ChartClassify, err error) {
  279. o := orm.NewOrmUsingDB("data")
  280. sql := `SELECT * FROM chart_classify WHERE source = ? AND sys_user_id=?`
  281. err = o.Raw(sql, utils.CHART_SOURCE_CROSS_HEDGING, sysUserId).QueryRow(&item)
  282. return
  283. }