chart_edb_mapping.go 16 KB

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