chart_edb_mapping.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package models
  2. import (
  3. "eta/eta_forum_admin/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. type ChartEdbMapping struct {
  11. ChartEdbMappingId int `orm:"column(chart_edb_mapping_id);pk"`
  12. ChartInfoId int `description:"图表id"`
  13. EdbInfoId int `description:"指标id"`
  14. CreateTime time.Time `description:"创建时间"`
  15. ModifyTime time.Time `description:"修改时间"`
  16. UniqueCode string `description:"唯一编码"`
  17. MaxData float64 `description:"上限"`
  18. MinData float64 `description:"下限"`
  19. IsOrder bool `description:"true:正序,false:逆序"`
  20. IsAxis int `description:"true:左轴,false:右轴"`
  21. EdbInfoType int `description:"true:标准指标,false:领先指标"`
  22. LeadValue int `description:"领先值"`
  23. LeadUnit string `description:"领先单位"`
  24. ChartStyle string `description:"图表类型"`
  25. ChartColor string `description:"颜色"`
  26. PredictChartColor string `description:"预测数据的颜色"`
  27. ChartWidth float64 `description:"线条大小"`
  28. Source int `description:"1:ETA图库;2:商品价格曲线"`
  29. EdbAliasName string `description:"中文别名"`
  30. IsConvert int `description:"是否数据转换 0不转 1转"`
  31. ConvertType int `description:"数据转换类型 1乘 2除 3对数"`
  32. ConvertValue float64 `description:"数据转换值"`
  33. ConvertUnit string `description:"数据转换单位"`
  34. ConvertEnUnit string `description:"数据转换单位"`
  35. EdbCode string `description:"指标编码"`
  36. }
  37. func AddChartEdbMapping(items []*ChartEdbMapping) (err error) {
  38. o := orm.NewOrm()
  39. _, err = o.InsertMulti(len(items), items)
  40. return
  41. }
  42. func GetChartEdbMappingList(chartInfoId int) (list []*ChartEdbInfoMapping, err error) {
  43. o := orm.NewOrm()
  44. sql := ` SELECT a.*,b.source_name,b.source,b.sub_source,b.classify_id,b.edb_code,b.edb_name,b.edb_name_en,b.frequency,b.unit,b.unit_en,b.start_date,
  45. 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,b.is_join_permission,
  46. a.is_convert, a.convert_type, a.convert_value, a.convert_unit, a.convert_en_unit
  47. FROM chart_edb_mapping AS a
  48. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  49. WHERE chart_info_id=?
  50. ORDER BY chart_edb_mapping_id ASC `
  51. _, err = o.Raw(sql, chartInfoId).QueryRows(&list)
  52. return
  53. }
  54. func GetChartEdbMappingListByChartInfoIds(chartInfoIds string) (list []*ChartEdbInfoMapping, err error) {
  55. o := orm.NewOrm()
  56. sql := ` SELECT a.*,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,b.classify_id,b.is_join_permission
  57. FROM chart_edb_mapping AS a
  58. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  59. WHERE chart_info_id in (` + chartInfoIds + `)
  60. ORDER BY chart_edb_mapping_id ASC `
  61. _, err = o.Raw(sql).QueryRows(&list)
  62. return
  63. }
  64. // GetChartEdbMappingListByEdbInfoIdList 根据指标id列表获取关联关系
  65. func GetChartEdbMappingListByEdbInfoIdList(edbIdList []int) (list []*ChartEdbInfoMapping, err error) {
  66. num := len(edbIdList)
  67. if num <= 0 {
  68. return
  69. }
  70. o := orm.NewOrm()
  71. 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,edb_type,classify_id,is_join_permission
  72. FROM edb_info
  73. WHERE edb_info_id IN(` + utils.GetOrmInReplace(num) + `)
  74. ORDER BY FIELD(edb_info_id,` + utils.GetOrmInReplace(num) + `)
  75. `
  76. _, err = o.Raw(sql, edbIdList, edbIdList).QueryRows(&list)
  77. return
  78. }
  79. // GetChartEdbMappingListByIdList 通过图表id列表获取
  80. func GetChartEdbMappingListByIdList(chartInfoIdList []int) (list []*ChartEdbInfoMapping, err error) {
  81. num := len(chartInfoIdList)
  82. if num <= 0 {
  83. return
  84. }
  85. o := orm.NewOrm()
  86. sql := ` SELECT a.*,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,b.edb_type AS edb_info_category_type,b.classify_id,b.is_join_permission
  87. FROM chart_edb_mapping AS a
  88. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  89. WHERE chart_info_id in (` + utils.GetOrmInReplace(num) + `)
  90. ORDER BY chart_edb_mapping_id ASC `
  91. _, err = o.Raw(sql, chartInfoIdList).QueryRows(&list)
  92. return
  93. }
  94. func GetRelationEdbInfoListByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*ChartEdbInfoMapping, err error) {
  95. o := orm.NewOrm()
  96. sql := ` SELECT a.* FROM edb_info AS a
  97. JOIN edb_info_calculate_mapping AS b on a.edb_info_id = b.edb_info_id WHERE 1=1 `
  98. if condition != "" {
  99. sql += condition
  100. }
  101. //sql += " ORDER BY sort ASC,chart_info_id DESC LIMIT ?,? "
  102. sql += " ORDER BY a.create_time DESC LIMIT ?,? "
  103. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
  104. return
  105. }
  106. func GetRelationEdbInfoListCountByCondition(condition string, pars []interface{}) (count int, err error) {
  107. o := orm.NewOrm()
  108. sql := ` SELECT COUNT(1) AS count FROM edb_info AS a
  109. JOIN edb_info_calculate_mapping AS b on a.edb_info_id = b.edb_info_id WHERE 1=1 `
  110. if condition != "" {
  111. sql += condition
  112. }
  113. err = o.Raw(sql, pars).QueryRow(&count)
  114. return
  115. }
  116. // GetEtaEdbChartEdbMapping 商品曲线图查询对应的普通指标
  117. func GetEtaEdbChartEdbMapping(chartInfoId int) (item *ChartEdbInfoMapping, err error) {
  118. o := orm.NewOrm()
  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 = o.Raw(sql, chartInfoId, utils.CHART_SOURCE_DEFAULT).QueryRow(&item)
  126. return
  127. }
  128. // GetFutureGoodEdbChartEdbMapping 商品曲线图查询对应的商品指标
  129. func GetFutureGoodEdbChartEdbMapping(chartInfoId int) (item *ChartEdbInfoMapping, err error) {
  130. o := orm.NewOrm()
  131. 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`
  132. 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
  133. FROM chart_edb_mapping AS a
  134. INNER JOIN future_good_edb_info AS b ON a.edb_info_id=b.future_good_edb_info_id
  135. WHERE a.chart_info_id=? AND a.source = ?
  136. ORDER BY chart_edb_mapping_id ASC `
  137. err = o.Raw(sql, chartInfoId, utils.CHART_SOURCE_FUTURE_GOOD).QueryRow(&item)
  138. return
  139. }
  140. // GetFutureGoodEdbChartEdbMappingList 商品曲线图查询对应的商品指标
  141. func GetFutureGoodEdbChartEdbMappingList(chartInfoId int) (items []*ChartEdbInfoMapping, err error) {
  142. o := orm.NewOrm()
  143. 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`
  144. 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
  145. FROM chart_edb_mapping AS a
  146. INNER JOIN future_good_edb_info AS b ON a.edb_info_id=b.future_good_edb_info_id
  147. WHERE a.chart_info_id=? AND a.source = ?
  148. ORDER BY chart_edb_mapping_id ASC `
  149. _, err = o.Raw(sql, chartInfoId, utils.CHART_SOURCE_FUTURE_GOOD).QueryRows(&items)
  150. return
  151. }
  152. // GetChartEdbMappingListV2 根据图表id获取指标信息,不连表查询指标表
  153. func GetChartEdbMappingListV2(chartInfoId int) (list []*ChartEdbMapping, err error) {
  154. o := orm.NewOrm()
  155. sql := ` SELECT a.* FROM chart_edb_mapping AS a
  156. WHERE chart_info_id=?
  157. ORDER BY chart_edb_mapping_id ASC `
  158. _, err = o.Raw(sql, chartInfoId).QueryRows(&list)
  159. return
  160. }
  161. // GetChartEdbMappingByEdbInfoId 根据指标id获取edb_mapping
  162. func GetChartEdbMappingByEdbInfoId(edbInfoId int) (item *ChartEdbInfoMapping, err error) {
  163. o := orm.NewOrm()
  164. sql := ` SELECT edb_info_id,source_name,classify_id,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,edb_type,max_value,min_value,is_join_permission
  165. FROM edb_info
  166. WHERE edb_info_id = ? limit 1`
  167. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  168. return
  169. }
  170. // GetChartEdbMappingByFutureGoodEdbInfoId 根据指标id获取edb_mapping
  171. func GetChartEdbMappingByFutureGoodEdbInfoId(edbInfoId int) (item *ChartEdbInfoMapping, err error) {
  172. o := orm.NewOrm()
  173. sql := ` SELECT future_good_edb_info_id as edb_info_id,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,b.region_type
  174. FROM future_good_edb_info b
  175. WHERE future_good_edb_info_id = ? limit 1 `
  176. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  177. return
  178. }
  179. // ModifyChartEdbMapping
  180. // @Description: 修改图表的关系表
  181. // @author: Roc
  182. // @datetime 2023-12-11 17:23:32
  183. // @param chartInfoId int
  184. // @param edbInfoList []*EdbInfo
  185. // @return err error
  186. func ModifyChartEdbMapping(chartInfoId int, edbInfoList []*EdbInfo) (err error) {
  187. o, err := orm.NewOrm().Begin()
  188. if err != nil {
  189. return
  190. }
  191. defer func() {
  192. if err != nil {
  193. fmt.Println("AddCalculateHcz,Err:" + err.Error())
  194. _ = o.Rollback()
  195. } else {
  196. _ = o.Commit()
  197. }
  198. }()
  199. list := make([]*ChartEdbMapping, 0)
  200. sql := ` SELECT a.*
  201. FROM chart_edb_mapping AS a
  202. WHERE chart_info_id=?
  203. ORDER BY chart_edb_mapping_id ASC `
  204. _, err = o.Raw(sql, chartInfoId).QueryRows(&list)
  205. if err != nil {
  206. return
  207. }
  208. mappingIdMap := make(map[int]*ChartEdbMapping)
  209. removeMapping := make(map[int]int)
  210. for _, v := range list {
  211. mappingIdMap[v.EdbInfoId] = v
  212. removeMapping[v.EdbInfoId] = v.ChartEdbMappingId
  213. }
  214. addList := make([]*ChartEdbMapping, 0)
  215. for _, v := range edbInfoList {
  216. _, ok := mappingIdMap[v.EdbInfoId]
  217. // 存在该指标关系就不处理了
  218. if ok {
  219. delete(removeMapping, v.EdbInfoId)
  220. continue
  221. }
  222. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  223. // 不存在就添加
  224. addList = append(addList, &ChartEdbMapping{
  225. //ChartEdbMappingId: 0,
  226. ChartInfoId: chartInfoId,
  227. EdbInfoId: v.EdbInfoId,
  228. CreateTime: time.Now(),
  229. ModifyTime: time.Now(),
  230. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + fmt.Sprint(chartInfoId) + "_" + fmt.Sprint(v.EdbInfoId) + "_" + timestamp),
  231. MaxData: v.MaxValue,
  232. MinData: v.MinValue,
  233. //IsOrder: v.IsOrder,
  234. //IsAxis: v.IsAxis,
  235. EdbInfoType: v.EdbInfoType,
  236. //LeadValue: v.LeadValue,
  237. //LeadUnit: v.LeadUnit,
  238. //ChartStyle: v.ChartStyle,
  239. //ChartColor: v.ChartColor,
  240. //ChartWidth: v.ChartWidth,
  241. Source: v.Source,
  242. })
  243. }
  244. // 需要添加的话,那就添加吧
  245. if len(addList) > 0 {
  246. _, err = o.InsertMulti(len(addList), addList)
  247. if err != nil {
  248. return
  249. }
  250. }
  251. // 移除不必要的mapping
  252. if len(removeMapping) > 0 {
  253. removeIdList := make([]string, 0) //需要移除的日期
  254. for _, v := range removeMapping {
  255. removeIdList = append(removeIdList, fmt.Sprint(v))
  256. }
  257. removeIdStr := strings.Join(removeIdList, `","`)
  258. removeIdStr = `"` + removeIdStr + `"`
  259. //如果拼接指标变更了,那么需要删除所有的指标数据
  260. sql := fmt.Sprintf(` DELETE FROM chart_edb_mapping WHERE chart_edb_mapping_id in (%s) `, removeIdStr)
  261. _, err = o.Raw(sql).Exec()
  262. if err != nil {
  263. err = fmt.Errorf("移除不必要的mapping失败,Err:" + err.Error())
  264. return
  265. }
  266. }
  267. return
  268. }
  269. func DeleteChartEdbMappingByChartEdbMappingId(removeIdStr string) (err error) {
  270. // 删除映射关系
  271. o := orm.NewOrm()
  272. sql := fmt.Sprintf(` DELETE FROM chart_edb_mapping WHERE chart_edb_mapping_id in (%s) `, removeIdStr)
  273. _, err = o.Raw(sql).Exec()
  274. if err != nil {
  275. err = fmt.Errorf("删除映射关系失败,Err:" + err.Error())
  276. return
  277. }
  278. return
  279. }
  280. func (c *ChartEdbMapping) Update(col []string) (err error) {
  281. // 删除映射关系
  282. o := orm.NewOrm()
  283. _, err = o.Update(c, col...)
  284. return
  285. }
  286. func GetChartEdbMappingCount(edbInfoId int) (count int, err error) {
  287. o := orm.NewOrm()
  288. sql := ` SELECT COUNT(1) AS count FROM chart_edb_mapping WHERE edb_info_id=? `
  289. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  290. return
  291. }