query.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. LatestDate time.Time `description:"数据最新日期"`
  37. LatestValue float64 `description:"数据最新值"`
  38. UniqueCode string `description:"指标唯一编码"`
  39. MinValue float64 `json:"-" description:"最小值"`
  40. MaxValue float64 `json:"-" description:"最大值"`
  41. MoveLatestDate time.Time `description:"移动后的数据最新日期"`
  42. EdbNameEn string `description:"英文指标名称"`
  43. FrequencyEn string `description:"英文频率"`
  44. UnitEn string `description:"英文单位"`
  45. EdbInfoCategoryType int `description:"0:普通指标,1:预测指标"`
  46. LeadUnitEn string `description:"领先英文单位"`
  47. PredictChartColor string `description:"预测数据的颜色"`
  48. ChartType int `description:"生成样式:1:曲线图,2:季节性图,3:面积图,4:柱状图,5:散点图,6:组合图"`
  49. MappingSource int `description:"1:ETA图库;2:商品价格曲线"`
  50. IsConvert int `description:"是否数据转换 0不转 1转"`
  51. ConvertType int `description:"数据转换类型 1乘 2除 3对数"`
  52. ConvertValue float64 `description:"数据转换值"`
  53. ConvertUnit string `description:"数据转换单位"`
  54. ConvertEnUnit string `description:"数据转换单位"`
  55. }
  56. type ChartEdbInfoMappingList struct {
  57. ChartEdbInfoMapping
  58. DataList interface{}
  59. IsNullData bool `json:"-" description:"是否空数据"`
  60. }
  61. type ChartEdbInfoMappingTimeString struct {
  62. ChartEdbInfoMapping
  63. DataList interface{}
  64. IsNullData bool `json:"-" description:"是否空数据"`
  65. LatestDate ChartLibEdbInfoMappingTime `description:"数据最新日期"`
  66. MoveLatestDate ChartLibEdbInfoMappingTime `description:"移动后的数据最新日期"`
  67. }
  68. type ChartLibEdbInfoMappingTime struct {
  69. time.Time
  70. }
  71. // 自定义时间解析规则
  72. func (ct *ChartLibEdbInfoMappingTime) UnmarshalJSON(data []byte) error {
  73. s := string(data)
  74. if s[0] == '"' {
  75. s = s[1 : len(s)-1]
  76. }
  77. t, err := time.ParseInLocation(utils.FormatDate, s, time.Local)
  78. if err != nil {
  79. return err
  80. }
  81. ct.Time = t
  82. return nil
  83. }
  84. // GetMappingListByChartInfoId 根据图表ID获取指标映射列表
  85. func GetMappingListByChartInfoId(chartInfoId int) (list []*ChartEdbInfoMapping, err error) {
  86. 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`
  87. 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,
  88. a.is_convert, a.convert_type, a.convert_value, a.convert_unit, a.convert_en_unit
  89. FROM chart_edb_mapping AS a
  90. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  91. WHERE chart_info_id=?
  92. ORDER BY chart_edb_mapping_id ASC `
  93. err = global.MYSQL["data"].Raw(sql, chartInfoId).Scan(&list).Error
  94. return
  95. }
  96. // GetMappingListByEdbInfoId 根据指标ID获取指标映射列表
  97. func GetMappingListByEdbInfoId(edbInfoStr string) (list []*ChartEdbInfoMapping, err error) {
  98. 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 + `) `
  99. err = global.MYSQL["data"].Raw(sql).Scan(&list).Error
  100. return
  101. }
  102. // GetEtaEdbChartEdbMapping 商品曲线图查询对应的普通指标
  103. func GetEtaEdbChartEdbMapping(chartInfoId int) (item *ChartEdbInfoMappingList, err error) {
  104. 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`
  105. 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
  106. FROM chart_edb_mapping AS a
  107. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  108. WHERE a.chart_info_id=? AND a.source = ?
  109. ORDER BY chart_edb_mapping_id ASC `
  110. err = global.MYSQL["data"].Raw(sql, chartInfoId, utils.CHART_SOURCE_DEFAULT).First(&item).Error
  111. return
  112. }
  113. // GetEtaEdbChartEdbMappingList 商品曲线图查询对应的普通指标
  114. func GetEtaEdbChartEdbMappingList(chartInfoId int) (items []*ChartEdbInfoMappingList, err error) {
  115. 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`
  116. 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
  117. FROM chart_edb_mapping AS a
  118. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  119. WHERE a.chart_info_id=? AND a.source = ?
  120. ORDER BY chart_edb_mapping_id ASC `
  121. err = global.MYSQL["data"].Raw(sql, chartInfoId, utils.CHART_SOURCE_DEFAULT).Scan(&items).Error
  122. return
  123. }
  124. // GetFutureGoodEdbChartEdbMapping 商品曲线图查询对应的商品指标
  125. func GetFutureGoodEdbChartEdbMapping(chartInfoId int) (item *ChartEdbInfoMappingList, err error) {
  126. 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`
  127. 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
  128. FROM chart_edb_mapping AS a
  129. INNER JOIN future_good_edb_info AS b ON a.edb_info_id=b.future_good_edb_info_id
  130. WHERE a.chart_info_id=? AND a.source = ?
  131. ORDER BY chart_edb_mapping_id ASC `
  132. err = global.MYSQL["data"].Raw(sql, chartInfoId, utils.CHART_SOURCE_FUTURE_GOOD).First(&item).Error
  133. return
  134. }
  135. // GetFutureGoodEdbChartEdbMappingList 商品曲线图查询对应的商品指标列表
  136. func GetFutureGoodEdbChartEdbMappingList(chartInfoId int) (items []*ChartEdbInfoMappingList, err error) {
  137. 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`
  138. 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
  139. FROM chart_edb_mapping AS a
  140. INNER JOIN future_good_edb_info AS b ON a.edb_info_id=b.future_good_edb_info_id
  141. WHERE a.chart_info_id=? AND a.source = ?
  142. ORDER BY chart_edb_mapping_id ASC `
  143. err = global.MYSQL["data"].Raw(sql, chartInfoId, utils.CHART_SOURCE_FUTURE_GOOD).Find(&items).Error
  144. return
  145. }
  146. // GetChartEdbMappingByEdbInfoId 根据指标id获取edb_mapping
  147. func GetChartEdbMappingByEdbInfoId(edbInfoId int) (item *ChartEdbInfoMapping, err error) {
  148. 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
  149. FROM edb_info
  150. WHERE edb_info_id = ? limit 1`
  151. err = global.MYSQL["data"].Raw(sql, edbInfoId).First(&item).Error
  152. return
  153. }
  154. // GetChartEdbMappingListByEdbInfoIdList 根据指标id列表获取关联关系
  155. func GetChartEdbMappingListByEdbInfoIdList(edbIdList []int) (list []*ChartEdbInfoMapping, err error) {
  156. num := len(edbIdList)
  157. if num <= 0 {
  158. return
  159. }
  160. 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
  161. FROM edb_info
  162. WHERE edb_info_id IN(?)
  163. ORDER BY FIELD(edb_info_id,?)
  164. `
  165. edbIdStrList := make([]string, 0)
  166. for _, v := range edbIdList {
  167. edbIdStrList = append(edbIdStrList, fmt.Sprint(v))
  168. }
  169. err = global.MYSQL["data"].Raw(sql, edbIdList, strings.Join(edbIdStrList, ",")).Find(&list).Error
  170. return
  171. }