query.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. if s == "" {
  78. return nil
  79. }
  80. t, err := time.ParseInLocation(utils.FormatDate, s, time.Local)
  81. if err != nil {
  82. return err
  83. }
  84. ct.Time = t
  85. return nil
  86. }
  87. // GetMappingListByChartInfoId 根据图表ID获取指标映射列表
  88. func GetMappingListByChartInfoId(chartInfoId int) (list []*ChartEdbInfoMapping, err error) {
  89. 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`
  90. 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,
  91. a.is_convert, a.convert_type, a.convert_value, a.convert_unit, a.convert_en_unit
  92. FROM chart_edb_mapping AS a
  93. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  94. WHERE chart_info_id=?
  95. ORDER BY chart_edb_mapping_id ASC `
  96. err = global.MYSQL["data"].Raw(sql, chartInfoId).Scan(&list).Error
  97. return
  98. }
  99. // GetMappingListByEdbInfoId 根据指标ID获取指标映射列表
  100. func GetMappingListByEdbInfoId(edbInfoStr string) (list []*ChartEdbInfoMapping, err error) {
  101. 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 + `) `
  102. err = global.MYSQL["data"].Raw(sql).Scan(&list).Error
  103. return
  104. }
  105. // GetEtaEdbChartEdbMapping 商品曲线图查询对应的普通指标
  106. func GetEtaEdbChartEdbMapping(chartInfoId int) (item *ChartEdbInfoMappingList, err error) {
  107. 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`
  108. 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
  109. FROM chart_edb_mapping AS a
  110. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  111. WHERE a.chart_info_id=? AND a.source = ?
  112. ORDER BY chart_edb_mapping_id ASC `
  113. err = global.MYSQL["data"].Raw(sql, chartInfoId, utils.CHART_SOURCE_DEFAULT).First(&item).Error
  114. return
  115. }
  116. // GetEtaEdbChartEdbMappingList 商品曲线图查询对应的普通指标
  117. func GetEtaEdbChartEdbMappingList(chartInfoId int) (items []*ChartEdbInfoMappingList, err error) {
  118. 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`
  119. 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
  120. FROM chart_edb_mapping AS a
  121. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  122. WHERE a.chart_info_id=? AND a.source = ?
  123. ORDER BY chart_edb_mapping_id ASC `
  124. err = global.MYSQL["data"].Raw(sql, chartInfoId, utils.CHART_SOURCE_DEFAULT).Scan(&items).Error
  125. return
  126. }
  127. // GetFutureGoodEdbChartEdbMapping 商品曲线图查询对应的商品指标
  128. func GetFutureGoodEdbChartEdbMapping(chartInfoId int) (item *ChartEdbInfoMappingList, err error) {
  129. 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`
  130. 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
  131. FROM chart_edb_mapping AS a
  132. INNER JOIN future_good_edb_info AS b ON a.edb_info_id=b.future_good_edb_info_id
  133. WHERE a.chart_info_id=? AND a.source = ?
  134. ORDER BY chart_edb_mapping_id ASC `
  135. err = global.MYSQL["data"].Raw(sql, chartInfoId, utils.CHART_SOURCE_FUTURE_GOOD).First(&item).Error
  136. return
  137. }
  138. // GetFutureGoodEdbChartEdbMappingList 商品曲线图查询对应的商品指标列表
  139. func GetFutureGoodEdbChartEdbMappingList(chartInfoId int) (items []*ChartEdbInfoMappingList, err error) {
  140. 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`
  141. 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
  142. FROM chart_edb_mapping AS a
  143. INNER JOIN future_good_edb_info AS b ON a.edb_info_id=b.future_good_edb_info_id
  144. WHERE a.chart_info_id=? AND a.source = ?
  145. ORDER BY chart_edb_mapping_id ASC `
  146. err = global.MYSQL["data"].Raw(sql, chartInfoId, utils.CHART_SOURCE_FUTURE_GOOD).Find(&items).Error
  147. return
  148. }
  149. // GetChartEdbMappingByEdbInfoId 根据指标id获取edb_mapping
  150. func GetChartEdbMappingByEdbInfoId(edbInfoId int) (item *ChartEdbInfoMapping, err error) {
  151. 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
  152. FROM edb_info
  153. WHERE edb_info_id = ? limit 1`
  154. err = global.MYSQL["data"].Raw(sql, edbInfoId).First(&item).Error
  155. return
  156. }
  157. // GetChartEdbMappingListByEdbInfoIdList 根据指标id列表获取关联关系
  158. func GetChartEdbMappingListByEdbInfoIdList(edbIdList []int) (list []*ChartEdbInfoMapping, err error) {
  159. num := len(edbIdList)
  160. if num <= 0 {
  161. return
  162. }
  163. 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
  164. FROM edb_info
  165. WHERE edb_info_id IN(?)
  166. ORDER BY FIELD(edb_info_id,?)
  167. `
  168. edbIdStrList := make([]string, 0)
  169. for _, v := range edbIdList {
  170. edbIdStrList = append(edbIdStrList, fmt.Sprint(v))
  171. }
  172. err = global.MYSQL["data"].Raw(sql, edbIdList, strings.Join(edbIdStrList, ",")).Find(&list).Error
  173. return
  174. }