chart_edb_mapping.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. package data_manage
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/utils"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/rdlucklib/rdluck_tools/paging"
  10. )
  11. type ChartEdbMapping struct {
  12. ChartEdbMappingId int `gorm:"column(chart_edb_mapping_id);primaryKey"`
  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. ChartScale float64 `description:"参考刻度线"`
  30. Source int `description:"1:ETA图库;2:商品价格曲线"`
  31. EdbAliasName string `description:"中文别名"`
  32. IsConvert int `description:"是否数据转换 0不转 1转"`
  33. ConvertType int `description:"数据转换类型 1乘 2除 3对数"`
  34. ConvertValue float64 `description:"数据转换值"`
  35. ConvertUnit string `description:"数据转换单位"`
  36. ConvertEnUnit string `description:"数据转换单位"`
  37. }
  38. func (m *ChartEdbMapping) TableName() string {
  39. return "chart_edb_mapping"
  40. }
  41. func AddChartEdbMapping(items []*ChartEdbMapping) (err error) {
  42. o := global.DbMap[utils.DbNameIndex]
  43. err = o.CreateInBatches(items, utils.MultiAddNum).Error
  44. return
  45. }
  46. func GetChartEdbMappingList(chartInfoId int) (list []*ChartEdbInfoMapping, err error) {
  47. o := global.DbMap[utils.DbNameIndex]
  48. sql := `SELECT a.*, b.source_name, b.source, b.sub_source, b.classify_id, b.edb_code, b.edb_name, b.edb_name_en,
  49. b.frequency, b.unit, b.unit_en, b.start_date, b.end_date, b.modify_time, b.latest_date,
  50. b.latest_value, b.unique_code, b.edb_info_type AS edb_info_category_type,
  51. b.edb_type, b.is_join_permission, a.is_convert, a.convert_type,
  52. a.convert_value, a.convert_unit, a.convert_en_unit
  53. FROM chart_edb_mapping AS a
  54. INNER JOIN edb_info AS b ON a.edb_info_id = b.edb_info_id
  55. WHERE chart_info_id = ?
  56. ORDER BY chart_edb_mapping_id ASC`
  57. err = o.Raw(sql, chartInfoId).Find(&list).Error
  58. return
  59. }
  60. func GetChartMappingList(chartInfoId int) (list []*ChartEdbMapping, err error) {
  61. o := global.DbMap[utils.DbNameIndex]
  62. sql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id = ? ORDER BY chart_edb_mapping_id ASC`
  63. err = o.Raw(sql, chartInfoId).Find(&list).Error
  64. return
  65. }
  66. func GetChartMappingListOrderByEdbInfoId(chartInfoId int) (list []*ChartEdbMapping, err error) {
  67. o := global.DbMap[utils.DbNameIndex]
  68. sql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id = ? ORDER BY edb_info_id ASC`
  69. err = o.Raw(sql, chartInfoId).Find(&list).Error
  70. return
  71. }
  72. func GetChartEdbMappingListByChartInfoIds(chartInfoIds string) (list []*ChartEdbInfoMapping, err error) {
  73. o := global.DbMap[utils.DbNameIndex]
  74. sql := `SELECT a.*, b.source_name, b.source, b.sub_source, b.classify_id, b.edb_code, b.edb_name, b.edb_name_en,
  75. b.frequency, b.unit, b.unit_en, b.start_date, b.end_date, b.modify_time, b.latest_date,
  76. b.latest_value, b.unique_code, b.edb_info_type AS edb_info_category_type,
  77. b.edb_type, b.is_join_permission
  78. FROM chart_edb_mapping AS a
  79. INNER JOIN edb_info AS b ON a.edb_info_id = b.edb_info_id
  80. WHERE chart_info_id IN (` + chartInfoIds + `)
  81. ORDER BY chart_edb_mapping_id ASC`
  82. err = o.Raw(sql).Find(&list).Error
  83. return
  84. }
  85. func GetChartEdbMappingListByEdbInfoId(edbInfoStr string) (list []*ChartEdbInfoMapping, err error) {
  86. o := global.DbMap[utils.DbNameIndex]
  87. sql := `SELECT edb_info_id, source_name, source, sub_source, edb_code, edb_name, edb_name_en,
  88. frequency, unit, unit_en, start_date, end_date, modify_time, latest_date, latest_value,
  89. unique_code, edb_info_type AS edb_info_category_type, max_value, min_value, edb_type, classify_id,
  90. is_join_permission
  91. FROM edb_info
  92. WHERE edb_info_id IN (` + edbInfoStr + `)
  93. ORDER BY FIELD(edb_info_id, ` + edbInfoStr + `)`
  94. err = o.Raw(sql).Find(&list).Error
  95. return
  96. }
  97. // GetChartEdbMappingListByEdbInfoIdList 根据指标ID列表获取映射
  98. func GetChartEdbMappingListByEdbInfoIdList(edbIdList []int) (list []*ChartEdbInfoMapping, err error) {
  99. num := len(edbIdList)
  100. if num <= 0 {
  101. return
  102. }
  103. o := global.DbMap[utils.DbNameIndex]
  104. sql := `SELECT edb_info_id, source_name, source, sub_source, edb_code, edb_name, edb_name_en,
  105. frequency, unit, unit_en, start_date, end_date, modify_time, latest_date, latest_value,
  106. unique_code, edb_info_type AS edb_info_category_type, max_value, min_value, edb_type, classify_id,
  107. is_join_permission
  108. FROM edb_info
  109. WHERE edb_info_id IN (` + utils.GetOrmInReplace(num) + `)`
  110. template := make([]string, num)
  111. for i := 0; i < num; i++ {
  112. template[i] = "?"
  113. }
  114. sql += ` ORDER BY FIELD(edb_info_id, ` + strings.Join(template, ",") + `) `
  115. pars := make([]interface{}, 0)
  116. pars = append(pars, edbIdList)
  117. for _, v := range edbIdList {
  118. pars = append(pars, v)
  119. }
  120. err = o.Raw(sql, pars...).Find(&list).Error
  121. return
  122. }
  123. // GetChartEdbMappingListByIdList 通过图表ID列表获取映射
  124. func GetChartEdbMappingListByIdList(chartInfoIdList []int) (list []*ChartEdbInfoMapping, err error) {
  125. num := len(chartInfoIdList)
  126. if num <= 0 {
  127. return
  128. }
  129. o := global.DbMap[utils.DbNameIndex]
  130. sql := `SELECT a.*, b.source_name, b.source, b.sub_source, b.edb_code, b.edb_name, b.edb_name_en,
  131. b.frequency, b.unit, b.unit_en, b.start_date, b.end_date, b.modify_time, b.latest_date,
  132. b.latest_value, b.unique_code, b.edb_info_type AS edb_info_category_type,
  133. b.edb_type, b.is_join_permission
  134. FROM chart_edb_mapping AS a
  135. INNER JOIN edb_info AS b ON a.edb_info_id = b.edb_info_id
  136. WHERE chart_info_id IN (` + utils.GetOrmInReplace(num) + `)
  137. ORDER BY chart_edb_mapping_id ASC`
  138. err = o.Raw(sql, chartInfoIdList).Find(&list).Error
  139. return
  140. }
  141. // RelationEdbInfoListResp 关联指标列表数据返回
  142. type RelationEdbInfoListResp struct {
  143. Paging *paging.PagingItem
  144. List []*ChartEdbInfoMapping
  145. }
  146. func GetRelationEdbInfoListByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*ChartEdbInfoMapping, err error) {
  147. o := global.DbMap[utils.DbNameIndex]
  148. sql := ` SELECT a.* FROM edb_info AS a
  149. JOIN edb_info_calculate_mapping AS b on a.edb_info_id = b.edb_info_id WHERE 1=1 `
  150. if condition != "" {
  151. sql += condition
  152. }
  153. //sql += " ORDER BY sort ASC,chart_info_id DESC LIMIT ?,? "
  154. sql += " ORDER BY a.create_time DESC LIMIT ?,? "
  155. pars = append(pars, startSize, pageSize)
  156. err = o.Raw(sql, pars...).Find(&item).Error
  157. return
  158. }
  159. func GetRelationEdbInfoListCountByCondition(condition string, pars []interface{}) (count int, err error) {
  160. o := global.DbMap[utils.DbNameIndex]
  161. sql := ` SELECT COUNT(1) AS count FROM edb_info AS a
  162. JOIN edb_info_calculate_mapping AS b on a.edb_info_id = b.edb_info_id WHERE 1=1 `
  163. if condition != "" {
  164. sql += condition
  165. }
  166. err = o.Raw(sql, pars...).Scan(&count).Error
  167. return
  168. }
  169. // GetEtaEdbChartEdbMapping 商品曲线图查询对应的普通指标
  170. func GetEtaEdbChartEdbMapping(chartInfoId int) (item *ChartEdbInfoMapping, err error) {
  171. o := global.DbMap[utils.DbNameIndex]
  172. 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`
  173. 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
  174. FROM chart_edb_mapping AS a
  175. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  176. WHERE a.chart_info_id=? AND a.source = ?
  177. ORDER BY chart_edb_mapping_id ASC `
  178. if item != nil {
  179. item.ConvertTimeStr()
  180. }
  181. err = o.Raw(sql, chartInfoId, utils.CHART_SOURCE_DEFAULT).First(&item).Error
  182. return
  183. }
  184. // GetEtaEdbChartEdbMappingList 商品曲线图查询对应的普通指标
  185. func GetEtaEdbChartEdbMappingList(chartInfoId int) (items []*ChartEdbInfoMapping, err error) {
  186. o := global.DbMap[utils.DbNameIndex]
  187. 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`
  188. 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
  189. FROM chart_edb_mapping AS a
  190. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  191. WHERE a.chart_info_id=? AND a.source = ?
  192. ORDER BY chart_edb_mapping_id ASC `
  193. err = o.Raw(sql, chartInfoId, utils.CHART_SOURCE_DEFAULT).Find(&items).Error
  194. return
  195. }
  196. // GetFutureGoodEdbChartEdbMapping 商品曲线图查询对应的商品指标
  197. func GetFutureGoodEdbChartEdbMapping(chartInfoId int) (item *ChartEdbInfoMapping, err error) {
  198. o := global.DbMap[utils.DbNameIndex]
  199. 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`
  200. 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
  201. FROM chart_edb_mapping AS a
  202. INNER JOIN future_good_edb_info AS b ON a.edb_info_id=b.future_good_edb_info_id
  203. WHERE a.chart_info_id=? AND a.source = ?
  204. ORDER BY chart_edb_mapping_id ASC `
  205. err = o.Raw(sql, chartInfoId, utils.CHART_SOURCE_FUTURE_GOOD).First(&item).Error
  206. return
  207. }
  208. // GetFutureGoodEdbChartEdbMappingList 商品曲线图查询对应的商品指标
  209. func GetFutureGoodEdbChartEdbMappingList(chartInfoId int) (items []*ChartEdbInfoMapping, err error) {
  210. o := global.DbMap[utils.DbNameIndex]
  211. 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`
  212. 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
  213. FROM chart_edb_mapping AS a
  214. INNER JOIN future_good_edb_info AS b ON a.edb_info_id=b.future_good_edb_info_id
  215. WHERE a.chart_info_id=? AND a.source = ?
  216. ORDER BY chart_edb_mapping_id ASC `
  217. err = o.Raw(sql, chartInfoId, utils.CHART_SOURCE_FUTURE_GOOD).Find(&items).Error
  218. return
  219. }
  220. // GetChartEdbMappingListV2 根据图表id获取指标信息,不连表查询指标表
  221. func GetChartEdbMappingListV2(chartInfoId int) (list []*ChartEdbInfoMapping, err error) {
  222. o := global.DbMap[utils.DbNameIndex]
  223. sql := ` SELECT a.* FROM chart_edb_mapping AS a
  224. WHERE chart_info_id=?
  225. ORDER BY chart_edb_mapping_id ASC `
  226. err = o.Raw(sql, chartInfoId).Find(&list).Error
  227. return
  228. }
  229. // GetChartEdbMappingByEdbInfoId 根据指标id获取edb_mapping
  230. func GetChartEdbMappingByEdbInfoId(edbInfoId int) (item *ChartEdbInfoMapping, err error) {
  231. o := global.DbMap[utils.DbNameIndex]
  232. 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
  233. FROM edb_info
  234. WHERE edb_info_id = ? limit 1`
  235. err = o.Raw(sql, edbInfoId).First(&item).Error
  236. return
  237. }
  238. // GetEdbMappingListByEdbInfoId 根据指标id获取edb_mapping
  239. func GetEdbMappingListByEdbInfoId(edbInfoId int) (item []*ChartEdbInfoMapping, err error) {
  240. o := global.DbMap[utils.DbNameIndex]
  241. sql := ` SELECT * FROM chart_edb_mapping
  242. WHERE edb_info_id = ? `
  243. err = o.Raw(sql, edbInfoId).Find(&item).Error
  244. return
  245. }
  246. // GetChartEdbMappingByFutureGoodEdbInfoId 根据指标id获取edb_mapping
  247. func GetChartEdbMappingByFutureGoodEdbInfoId(edbInfoId int) (item *ChartEdbInfoMapping, err error) {
  248. o := global.DbMap[utils.DbNameIndex]
  249. 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
  250. FROM future_good_edb_info b
  251. WHERE future_good_edb_info_id = ? limit 1 `
  252. err = o.Raw(sql, edbInfoId).First(&item).Error
  253. return
  254. }
  255. // ModifyChartEdbMapping
  256. // @Description: 修改图表的关系表
  257. // @author: Roc
  258. // @datetime 2023-12-11 17:23:32
  259. // @param chartInfoId int
  260. // @param chartSource int
  261. // @param edbInfoList []*EdbInfo
  262. // @return err error
  263. func ModifyChartEdbMapping(chartInfoId, chartSource int, edbInfoList []*EdbInfo) (err error) {
  264. o := global.DbMap[utils.DbNameIndex].Begin()
  265. if err != nil {
  266. return
  267. }
  268. defer func() {
  269. if err != nil {
  270. fmt.Println("AddCalculateHcz,Err:" + err.Error())
  271. _ = o.Rollback()
  272. } else {
  273. _ = o.Commit()
  274. }
  275. }()
  276. list := make([]*ChartEdbMapping, 0)
  277. sql := ` SELECT a.*
  278. FROM chart_edb_mapping AS a
  279. WHERE chart_info_id=?
  280. ORDER BY chart_edb_mapping_id ASC `
  281. err = o.Raw(sql, chartInfoId).Find(&list).Error
  282. if err != nil {
  283. return
  284. }
  285. mappingIdMap := make(map[int]*ChartEdbMapping)
  286. removeMapping := make(map[int]int)
  287. for _, v := range list {
  288. mappingIdMap[v.EdbInfoId] = v
  289. removeMapping[v.EdbInfoId] = v.ChartEdbMappingId
  290. }
  291. addList := make([]*ChartEdbMapping, 0)
  292. for _, v := range edbInfoList {
  293. _, ok := mappingIdMap[v.EdbInfoId]
  294. // 存在该指标关系就不处理了
  295. if ok {
  296. delete(removeMapping, v.EdbInfoId)
  297. continue
  298. }
  299. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  300. // 不存在就添加
  301. addList = append(addList, &ChartEdbMapping{
  302. //ChartEdbMappingId: 0,
  303. ChartInfoId: chartInfoId,
  304. EdbInfoId: v.EdbInfoId,
  305. CreateTime: time.Now(),
  306. ModifyTime: time.Now(),
  307. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + fmt.Sprint(chartInfoId) + "_" + fmt.Sprint(v.EdbInfoId) + "_" + timestamp),
  308. MaxData: v.MaxValue,
  309. MinData: v.MinValue,
  310. //IsOrder: v.IsOrder,
  311. //IsAxis: v.IsAxis,
  312. EdbInfoType: v.EdbInfoType,
  313. //LeadValue: v.LeadValue,
  314. //LeadUnit: v.LeadUnit,
  315. //ChartStyle: v.ChartStyle,
  316. //ChartColor: v.ChartColor,
  317. //ChartWidth: v.ChartWidth,
  318. Source: chartSource,
  319. })
  320. }
  321. // 需要添加的话,那就添加吧
  322. if len(addList) > 0 {
  323. err = o.CreateInBatches(addList, utils.MultiAddNum).Error
  324. if err != nil {
  325. return
  326. }
  327. }
  328. // 移除不必要的mapping
  329. if len(removeMapping) > 0 {
  330. removeIdList := make([]string, 0) //需要移除的日期
  331. for _, v := range removeMapping {
  332. removeIdList = append(removeIdList, fmt.Sprint(v))
  333. }
  334. removeIdStr := strings.Join(removeIdList, `','`)
  335. removeIdStr = `'` + removeIdStr + `'`
  336. //如果拼接指标变更了,那么需要删除所有的指标数据
  337. sql := fmt.Sprintf(` DELETE FROM chart_edb_mapping WHERE chart_edb_mapping_id in (%s) `, removeIdStr)
  338. err = o.Exec(sql).Error
  339. if err != nil {
  340. err = fmt.Errorf("移除不必要的mapping失败,Err:" + err.Error())
  341. return
  342. }
  343. }
  344. return
  345. }
  346. func GetRelationEdbInfoListMappingByCondition(condition string, pars []interface{}) (item []*ChartEdbInfoMapping, err error) {
  347. o := global.DbMap[utils.DbNameIndex]
  348. sql := ` SELECT a.* FROM edb_info AS a
  349. JOIN edb_info_calculate_mapping AS b on a.edb_info_id = b.edb_info_id WHERE 1=1 `
  350. if condition != "" {
  351. sql += condition
  352. }
  353. err = o.Raw(sql, pars...).Find(&item).Error
  354. return
  355. }