chart.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package models
  2. import (
  3. "encoding/json"
  4. "eta/eta_index_lib/global"
  5. "eta/eta_index_lib/utils"
  6. "fmt"
  7. "gorm.io/gorm"
  8. "time"
  9. )
  10. type EdbInfoReq struct {
  11. EdbInfoId int `description:"图表id,新增时传0"`
  12. }
  13. // DeleteChartInfoDataRedis 清除图表缓存
  14. func DeleteChartInfoDataRedis(bodyByte []byte) (err error) {
  15. var req EdbInfoReq
  16. err = json.Unmarshal(bodyByte, &req)
  17. if err != nil {
  18. return
  19. }
  20. if req.EdbInfoId > 0 {
  21. list, tmpErr := GetChartEdbMappingListByEdbInfoId(req.EdbInfoId)
  22. if tmpErr != nil {
  23. err = tmpErr
  24. return
  25. }
  26. // 删除图表的id
  27. for _, v := range list {
  28. _ = utils.Rc.Delete(GetChartInfoDataKey(v.ChartInfoId))
  29. }
  30. }
  31. return
  32. }
  33. // GetChartInfoDataKey 获取图表缓存的key
  34. func GetChartInfoDataKey(chartInfoId int) string {
  35. key := fmt.Sprint(utils.CACHE_CHART_INFO_DATA, chartInfoId)
  36. return key
  37. }
  38. type ChartInfo struct {
  39. ChartInfoId int `gorm:"column:chart_info_id;type:int(11);primaryKey;not null;" json:"chart_info_id"`
  40. ChartName string `description:"来源名称"`
  41. ChartNameEn string `description:"英文图表名称"`
  42. ChartClassifyId int `description:"图表分类id"`
  43. SysUserId int
  44. SysUserRealName string
  45. UniqueCode string `description:"图表唯一编码"`
  46. CreateTime time.Time
  47. ModifyTime time.Time
  48. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"`
  49. StartDate string `description:"自定义开始日期"`
  50. EndDate string `description:"自定义结束日期"`
  51. IsSetName int `description:"设置名称"`
  52. EdbInfoIds string `description:"指标id"`
  53. ChartType int `description:"生成样式:1:曲线图,2:季节性图,3:面积图,4:柱状图,5:散点图,6:组合图,7:柱方图,8:商品价格曲线图,9:相关性图"`
  54. Calendar string `description:"公历/农历"`
  55. SeasonStartDate string `description:"季节性图开始日期"`
  56. SeasonEndDate string `description:"季节性图开始日期"`
  57. ChartImage string `description:"图表图片"`
  58. Sort int `description:"排序字段,数字越小越排前面"`
  59. LeftMin string `description:"图表左侧最小值"`
  60. LeftMax string `description:"图表左侧最大值"`
  61. RightMin string `description:"图表右侧最小值"`
  62. RightMax string `description:"图表右侧最大值"`
  63. Disabled int `description:"是否禁用,0:启用,1:禁用,默认:0"`
  64. BarConfig string `description:"柱方图的配置,json数据"`
  65. Source int `description:"1:ETA图库;2:商品价格曲线"`
  66. ExtraConfig string `description:"图表额外配置,json数据"`
  67. }
  68. // AfterFind 在该模型上设置钩子函数,把日期转成正确的string,所以查询函数只能用Find函数,First或者Scan是不会触发该函数的来获取数据
  69. func (m *ChartInfo) AfterFind(db *gorm.DB) (err error) {
  70. m.StartDate = utils.GormDateStrToDateStr(m.StartDate)
  71. m.EndDate = utils.GormDateStrToDateStr(m.EndDate)
  72. m.SeasonStartDate = utils.GormDateStrToDateStr(m.SeasonStartDate)
  73. m.SeasonEndDate = utils.GormDateStrToDateStr(m.SeasonEndDate)
  74. return
  75. }
  76. // ConvDateTimeStr
  77. // @Description: ConvDateTimeStr
  78. // @author: Roc
  79. // @receiver m
  80. // @datetime 2025-02-13 09:54:02
  81. // @return err error
  82. func (m *ChartInfo) ConvDateTimeStr() {
  83. m.StartDate = utils.GormDateStrToDateStr(m.StartDate)
  84. m.EndDate = utils.GormDateStrToDateStr(m.EndDate)
  85. m.SeasonStartDate = utils.GormDateStrToDateStr(m.SeasonStartDate)
  86. m.SeasonEndDate = utils.GormDateStrToDateStr(m.SeasonEndDate)
  87. return
  88. }
  89. // GetChartInfoById 通过id获取图表信息
  90. func GetChartInfoById(chartInfoId int) (item *ChartInfo, err error) {
  91. sql := ` SELECT * FROM chart_info WHERE chart_info_id=? `
  92. err = global.DEFAULT_DB.Raw(sql, chartInfoId).First(&item).Error
  93. if err != nil {
  94. return
  95. }
  96. item.ConvDateTimeStr()
  97. return
  98. }