query.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package chart_edb_mapping
  2. import (
  3. "fmt"
  4. "hongze/hongze_yb/global"
  5. "hongze/hongze_yb/utils"
  6. "strings"
  7. "time"
  8. )
  9. type ChartEdbInfoMapping struct {
  10. EdbInfoId int `description:"指标id"`
  11. SourceName string `description:"来源名称"`
  12. Source int `description:"来源id"`
  13. SubSource int `description:"来源id"`
  14. EdbCode string `description:"指标编码"`
  15. EdbName string `description:"指标名称"`
  16. EdbAliasName string `description:"指标名称(别名)"`
  17. EdbAliasNameEn string `description:"英文指标名称(别名)"`
  18. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  19. Frequency string `description:"频率"`
  20. Unit string `description:"单位"`
  21. StartDate string `description:"起始日期"`
  22. EndDate string `description:"终止日期"`
  23. ModifyTime string `description:"指标最后更新时间"`
  24. ChartEdbMappingId int `description:"图表指标id"`
  25. ChartInfoId int `description:"图表id"`
  26. MaxData float64 `description:"上限"`
  27. MinData float64 `description:"下限"`
  28. IsOrder bool `description:"true:正序,false:逆序"`
  29. IsAxis int `description:"1:左轴,0:右轴"`
  30. EdbInfoType int `description:"1:标准指标,0:领先指标"`
  31. LeadValue int `description:"领先值"`
  32. LeadUnit string `description:"领先单位"`
  33. ChartStyle string `description:"图表类型"`
  34. ChartColor string `description:"颜色"`
  35. ChartWidth float64 `description:"线条大小"`
  36. ChartScale float64 `description:"参考刻度线"`
  37. LatestDate time.Time `description:"数据最新日期"`
  38. LatestValue float64 `description:"数据最新值"`
  39. UniqueCode string `description:"指标唯一编码"`
  40. MinValue float64 `json:"-" description:"最小值"`
  41. MaxValue float64 `json:"-" description:"最大值"`
  42. MoveLatestDate time.Time `description:"移动后的数据最新日期"`
  43. EdbNameEn string `description:"英文指标名称"`
  44. FrequencyEn string `description:"英文频率"`
  45. UnitEn string `description:"英文单位"`
  46. EdbInfoCategoryType int `description:"0:普通指标,1:预测指标"`
  47. LeadUnitEn string `description:"领先英文单位"`
  48. PredictChartColor string `description:"预测数据的颜色"`
  49. ChartType int `description:"生成样式:1:曲线图,2:季节性图,3:面积图,4:柱状图,5:散点图,6:组合图"`
  50. MappingSource int `description:"1:ETA图库;2:商品价格曲线"`
  51. IsConvert int `description:"是否数据转换 0不转 1转"`
  52. ConvertType int `description:"数据转换类型 1乘 2除 3对数"`
  53. ConvertValue float64 `description:"数据转换值"`
  54. ConvertUnit string `description:"数据转换单位"`
  55. ConvertEnUnit string `description:"数据转换单位"`
  56. }
  57. type ChartEdbInfoMappingList struct {
  58. ChartEdbInfoMapping
  59. DataList interface{}
  60. IsNullData bool `json:"-" description:"是否空数据"`
  61. }
  62. type ChartEdbInfoMappingTimeString struct {
  63. ChartEdbInfoMapping
  64. DataList interface{}
  65. IsNullData bool `json:"-" description:"是否空数据"`
  66. LatestDate ChartLibEdbInfoMappingTime `description:"数据最新日期"`
  67. MoveLatestDate ChartLibEdbInfoMappingTime `description:"移动后的数据最新日期"`
  68. }
  69. type ChartLibEdbInfoMappingTime struct {
  70. time.Time
  71. }
  72. // 自定义时间解析规则
  73. func (ct *ChartLibEdbInfoMappingTime) UnmarshalJSON(data []byte) error {
  74. s := string(data)
  75. if s[0] == '"' {
  76. s = s[1 : len(s)-1]
  77. }
  78. if s == "" {
  79. return nil
  80. }
  81. t, err := time.ParseInLocation(utils.FormatDate, s, time.Local)
  82. if err != nil {
  83. return err
  84. }
  85. ct.Time = t
  86. return nil
  87. }
  88. // GetMappingListByChartInfoId 根据图表ID获取指标映射列表
  89. func GetMappingListByChartInfoId(chartInfoId int) (list []*ChartEdbInfoMapping, err error) {
  90. aField := `a.chart_edb_mapping_id,a.chart_info_id,a.edb_info_id,a.create_time,a.modify_time,a.unique_code,a.max_data,a.min_data,a.is_order,a.is_axis,a.edb_info_type,a.lead_value,a.lead_unit,a.chart_style,a.chart_color,a.predict_chart_color,a.chart_width,a.source as mapping_source,a.chart_scale`
  91. sql := ` SELECT ` + aField + `,b.source_name,b.source,b.sub_source,b.edb_code,b.edb_name,b.edb_name_en,b.frequency,b.unit,b.unit_en,b.start_date,b.end_date,b.modify_time,b.latest_date,b.latest_value,b.unique_code,b.edb_info_type AS edb_info_category_type,b.edb_type,
  92. a.is_convert, a.convert_type, a.convert_value, a.convert_unit, a.convert_en_unit
  93. FROM chart_edb_mapping AS a
  94. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  95. WHERE chart_info_id=?
  96. ORDER BY chart_edb_mapping_id ASC `
  97. err = global.MYSQL["data"].Raw(sql, chartInfoId).Scan(&list).Error
  98. return
  99. }
  100. // GetMappingListByEdbInfoId 根据指标ID获取指标映射列表
  101. func GetMappingListByEdbInfoId(edbInfoStr string) (list []*ChartEdbInfoMapping, err error) {
  102. sql := ` SELECT edb_info_id,source_name,source,sub_source,edb_code,edb_name,edb_name_en,frequency,unit,unit_en,start_date,end_date,modify_time,latest_date,latest_value,unique_code,edb_info_type AS edb_info_category_type,max_value,min_value FROM edb_info WHERE edb_info_id IN(` + edbInfoStr + `) ORDER BY FIELD(edb_info_id,` + edbInfoStr + `) `
  103. err = global.MYSQL["data"].Raw(sql).Scan(&list).Error
  104. return
  105. }
  106. // GetEtaEdbChartEdbMapping 商品曲线图查询对应的普通指标
  107. func GetEtaEdbChartEdbMapping(chartInfoId int) (item *ChartEdbInfoMappingList, err error) {
  108. aField := `a.chart_edb_mapping_id,a.chart_info_id,a.edb_info_id,a.create_time,a.modify_time,a.unique_code,a.max_data,a.min_data,a.is_order,a.is_axis,a.edb_info_type,a.lead_value,a.lead_unit,a.chart_style,a.chart_color,a.predict_chart_color,a.chart_width,a.source as mapping_source`
  109. sql := ` SELECT ` + aField + `,b.source_name,b.source,b.sub_source,b.edb_code,b.edb_name,b.edb_name_en,b.frequency,b.unit,b.unit_en,b.start_date,b.end_date,b.modify_time,b.latest_date,b.latest_value,b.unique_code,b.edb_info_type AS edb_info_category_type
  110. FROM chart_edb_mapping AS a
  111. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  112. WHERE a.chart_info_id=? AND a.source = ?
  113. ORDER BY chart_edb_mapping_id ASC `
  114. err = global.MYSQL["data"].Raw(sql, chartInfoId, utils.CHART_SOURCE_DEFAULT).First(&item).Error
  115. return
  116. }
  117. // GetEtaEdbChartEdbMappingList 商品曲线图查询对应的普通指标
  118. func GetEtaEdbChartEdbMappingList(chartInfoId int) (items []*ChartEdbInfoMappingList, err error) {
  119. aField := `a.chart_edb_mapping_id,a.chart_info_id,a.edb_info_id,a.create_time,a.modify_time,a.unique_code,a.max_data,a.min_data,a.is_order,a.is_axis,a.edb_info_type,a.lead_value,a.lead_unit,a.chart_style,a.chart_color,a.predict_chart_color,a.chart_width,a.source as mapping_source`
  120. sql := ` SELECT ` + aField + `,b.source_name,b.source,b.sub_source,b.edb_code,b.edb_name,b.edb_name_en,b.frequency,b.unit,b.unit_en,b.start_date,b.end_date,b.modify_time,b.latest_date,b.latest_value,b.unique_code,b.edb_info_type AS edb_info_category_type,b.classify_id,b.is_join_permission
  121. FROM chart_edb_mapping AS a
  122. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  123. WHERE a.chart_info_id=? AND a.source = ?
  124. ORDER BY chart_edb_mapping_id ASC `
  125. err = global.MYSQL["data"].Raw(sql, chartInfoId, utils.CHART_SOURCE_DEFAULT).Scan(&items).Error
  126. return
  127. }
  128. // GetFutureGoodEdbChartEdbMapping 商品曲线图查询对应的商品指标
  129. func GetFutureGoodEdbChartEdbMapping(chartInfoId int) (item *ChartEdbInfoMappingList, err error) {
  130. aField := `a.chart_edb_mapping_id,a.chart_info_id,a.edb_info_id,a.create_time,a.modify_time,a.unique_code,a.max_data,a.min_data,a.is_order,a.is_axis,a.edb_info_type,a.lead_value,a.lead_unit,a.chart_style,a.chart_color,a.predict_chart_color,a.chart_width,a.source as mapping_source`
  131. sql := ` SELECT ` + aField + `,b.future_good_edb_info_id,b.future_good_edb_code as edb_code,b.future_good_edb_name as edb_name,b.start_date,b.end_date,b.modify_time,b.latest_date,b.latest_value
  132. FROM chart_edb_mapping AS a
  133. INNER JOIN future_good_edb_info AS b ON a.edb_info_id=b.future_good_edb_info_id
  134. WHERE a.chart_info_id=? AND a.source = ?
  135. ORDER BY chart_edb_mapping_id ASC `
  136. err = global.MYSQL["data"].Raw(sql, chartInfoId, utils.CHART_SOURCE_FUTURE_GOOD).First(&item).Error
  137. return
  138. }
  139. // GetFutureGoodEdbChartEdbMappingList 商品曲线图查询对应的商品指标列表
  140. func GetFutureGoodEdbChartEdbMappingList(chartInfoId int) (items []*ChartEdbInfoMappingList, err error) {
  141. aField := `a.chart_edb_mapping_id,a.chart_info_id,a.edb_info_id,a.create_time,a.modify_time,a.unique_code,a.max_data,a.min_data,a.is_order,a.is_axis,a.edb_info_type,a.lead_value,a.lead_unit,a.chart_style,a.chart_color,a.predict_chart_color,a.chart_width,a.source as mapping_source`
  142. sql := ` SELECT ` + aField + `,b.future_good_edb_info_id,b.future_good_edb_code as edb_code,b.future_good_edb_name as edb_name,b.start_date,b.end_date,b.modify_time,b.latest_date,b.latest_value
  143. FROM chart_edb_mapping AS a
  144. INNER JOIN future_good_edb_info AS b ON a.edb_info_id=b.future_good_edb_info_id
  145. WHERE a.chart_info_id=? AND a.source = ?
  146. ORDER BY chart_edb_mapping_id ASC `
  147. err = global.MYSQL["data"].Raw(sql, chartInfoId, utils.CHART_SOURCE_FUTURE_GOOD).Find(&items).Error
  148. return
  149. }
  150. // GetChartEdbMappingByEdbInfoId 根据指标id获取edb_mapping
  151. func GetChartEdbMappingByEdbInfoId(edbInfoId int) (item *ChartEdbInfoMapping, err error) {
  152. sql := ` SELECT edb_info_id,source_name,source,sub_source,edb_code,edb_name,edb_name_en,frequency,unit,unit_en,start_date,end_date,modify_time,latest_date,latest_value,unique_code,edb_info_type AS edb_info_category_type,max_value,min_value
  153. FROM edb_info
  154. WHERE edb_info_id = ? limit 1`
  155. err = global.MYSQL["data"].Raw(sql, edbInfoId).First(&item).Error
  156. return
  157. }
  158. // GetChartEdbMappingListByEdbInfoIdList 根据指标id列表获取关联关系
  159. func GetChartEdbMappingListByEdbInfoIdList(edbIdList []int) (list []*ChartEdbInfoMapping, err error) {
  160. num := len(edbIdList)
  161. if num <= 0 {
  162. return
  163. }
  164. sql := ` SELECT edb_info_id,source_name,source,sub_source,edb_code,edb_name,edb_name_en,frequency,unit,unit_en,start_date,end_date,modify_time,latest_date,latest_value,unique_code,edb_info_type AS edb_info_category_type,max_value,min_value
  165. FROM edb_info
  166. WHERE edb_info_id IN(?)
  167. ORDER BY FIELD(edb_info_id,?)
  168. `
  169. edbIdStrList := make([]string, 0)
  170. for _, v := range edbIdList {
  171. edbIdStrList = append(edbIdStrList, fmt.Sprint(v))
  172. }
  173. err = global.MYSQL["data"].Raw(sql, edbIdList, strings.Join(edbIdStrList, ",")).Find(&list).Error
  174. return
  175. }