chart_edb_mapping.go 16 KB

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