chart_edb_mapping.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. package data_manage
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/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 `orm:"column(chart_edb_mapping_id);pk" gorm:"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 AddChartEdbMapping(items []*ChartEdbMapping) (err error) {
  38. err = global.DmSQL["data"].CreateInBatches(items, utils.MultiAddNum).Error
  39. return
  40. }
  41. func GetChartEdbMappingList(chartInfoId int) (list []*ChartEdbInfoMapping, err error) {
  42. o := global.DmSQL["data"]
  43. 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,
  44. 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,
  45. a.is_convert, a.convert_type, a.convert_value, a.convert_unit, a.convert_en_unit
  46. FROM chart_edb_mapping AS a
  47. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  48. WHERE chart_info_id=?
  49. ORDER BY chart_edb_mapping_id ASC `
  50. err = o.Raw(sql, chartInfoId).Scan(&list).Error
  51. if err != nil {
  52. return
  53. }
  54. // 日期格式转换
  55. for _, v := range list {
  56. v.ConvertToResp()
  57. }
  58. return
  59. }
  60. func GetChartMappingList(chartInfoId int) (list []*ChartEdbMapping, err error) {
  61. o := global.DmSQL["data"]
  62. sql := ` SELECT a.*
  63. FROM chart_edb_mapping AS a
  64. WHERE chart_info_id=?
  65. ORDER BY chart_edb_mapping_id ASC `
  66. err = o.Raw(sql, chartInfoId).Scan(&list).Error
  67. return
  68. }
  69. func GetChartEdbMappingListByChartInfoIds(chartInfoIds string) (list []*ChartEdbInfoMapping, err error) {
  70. o := global.DmSQL["data"]
  71. 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
  72. FROM chart_edb_mapping AS a
  73. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  74. WHERE chart_info_id in (` + chartInfoIds + `)
  75. ORDER BY chart_edb_mapping_id ASC `
  76. err = o.Raw(sql).Scan(&list).Error
  77. if err != nil {
  78. return
  79. }
  80. // 日期格式转换
  81. for _, v := range list {
  82. v.ConvertToResp()
  83. }
  84. return
  85. }
  86. func GetChartEdbMappingListByChartInfoIdList(chartInfoIdList []int) (list []*ChartEdbInfoMapping, err error) {
  87. if len(chartInfoIdList) <= 0 {
  88. return
  89. }
  90. o := global.DmSQL["data"]
  91. 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
  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 in (?)
  95. ORDER BY chart_edb_mapping_id ASC `
  96. err = o.Raw(sql, chartInfoIdList).Scan(&list).Error
  97. if err != nil {
  98. return
  99. }
  100. // 日期格式转换
  101. for _, v := range list {
  102. v.ConvertToResp()
  103. }
  104. return
  105. }
  106. func GetChartEdbMappingListByEdbInfoId(edbInfoStr string) (list []*ChartEdbInfoMapping, err error) {
  107. o := global.DmSQL["data"]
  108. 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
  109. FROM edb_info
  110. WHERE edb_info_id IN(` + edbInfoStr + `)
  111. ORDER BY FIELD(edb_info_id,` + edbInfoStr + `)
  112. `
  113. err = o.Raw(sql).Scan(&list).Error
  114. if err != nil {
  115. return
  116. }
  117. // 日期格式转换
  118. for _, v := range list {
  119. v.ConvertToResp()
  120. }
  121. return
  122. }
  123. // GetChartEdbMappingListByEdbInfoIdList 根据指标id列表获取关联关系
  124. func GetChartEdbMappingListByEdbInfoIdList(edbIdList []int) (list []*ChartEdbInfoMapping, err error) {
  125. num := len(edbIdList)
  126. if num <= 0 {
  127. return
  128. }
  129. edbIdArr := make([]string, 0)
  130. for _, v := range edbIdList {
  131. edbIdArr = append(edbIdArr, strconv.Itoa(v))
  132. }
  133. edbIdStr := strings.Join(edbIdArr, ",")
  134. o := global.DmSQL["data"]
  135. 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
  136. FROM edb_info
  137. WHERE edb_info_id IN(` + edbIdStr + `)
  138. ORDER BY FIELD(edb_info_id,` + edbIdStr + `) `
  139. err = o.Raw(sql).Find(&list).Error
  140. if err != nil {
  141. return
  142. }
  143. // 日期格式转换
  144. for _, v := range list {
  145. v.ConvertToResp()
  146. }
  147. return
  148. }
  149. // GetChartEdbMappingListByIdList 通过图表id列表获取
  150. func GetChartEdbMappingListByIdList(chartInfoIdList []int) (list []*ChartEdbInfoMapping, err error) {
  151. num := len(chartInfoIdList)
  152. if num <= 0 {
  153. return
  154. }
  155. o := global.DmSQL["data"]
  156. 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
  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 chart_info_id in (` + utils.GetOrmInReplace(num) + `)
  160. ORDER BY chart_edb_mapping_id ASC `
  161. err = o.Raw(sql, chartInfoIdList).Scan(&list).Error
  162. if err != nil {
  163. return
  164. }
  165. // 日期格式转换
  166. for _, v := range list {
  167. v.ConvertToResp()
  168. }
  169. return
  170. }
  171. // RelationEdbInfoListResp 关联指标列表数据返回
  172. type RelationEdbInfoListResp struct {
  173. Paging *paging.PagingItem
  174. List []*ChartEdbInfoMapping
  175. }
  176. func GetRelationEdbInfoListByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*ChartEdbInfoMapping, err error) {
  177. o := global.DmSQL["data"]
  178. sql := ` SELECT a.* FROM edb_info AS a
  179. JOIN edb_info_calculate_mapping AS b on a.edb_info_id = b.edb_info_id WHERE 1=1 `
  180. if condition != "" {
  181. sql += condition
  182. }
  183. //sql += " ORDER BY sort ASC,chart_info_id DESC LIMIT ?,? "
  184. sql += " ORDER BY a.create_time DESC LIMIT ?,? "
  185. pars = append(pars, startSize)
  186. pars = append(pars, pageSize)
  187. err = o.Raw(sql, pars...).Scan(&item).Error
  188. return
  189. }
  190. func GetRelationEdbInfoListCountByCondition(condition string, pars []interface{}) (count int, err error) {
  191. o := global.DmSQL["data"]
  192. sql := ` SELECT COUNT(1) AS count FROM edb_info AS a
  193. JOIN edb_info_calculate_mapping AS b on a.edb_info_id = b.edb_info_id WHERE 1=1 `
  194. if condition != "" {
  195. sql += condition
  196. }
  197. err = o.Raw(sql, pars...).Scan(&count).Error
  198. return
  199. }
  200. // GetEtaEdbChartEdbMapping 商品曲线图查询对应的普通指标
  201. func GetEtaEdbChartEdbMapping(chartInfoId int) (item *ChartEdbInfoMapping, err error) {
  202. o := global.DmSQL["data"]
  203. 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`
  204. 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
  205. FROM chart_edb_mapping AS a
  206. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  207. WHERE a.chart_info_id=? AND a.source = ?
  208. ORDER BY chart_edb_mapping_id ASC `
  209. err = o.Raw(sql, chartInfoId, utils.CHART_SOURCE_DEFAULT).Scan(&item).Error
  210. if err != nil {
  211. return
  212. }
  213. // 日期格式转换
  214. item.ConvertToResp()
  215. return
  216. }
  217. // GetEtaEdbChartEdbMappingList 商品曲线图查询对应的普通指标
  218. func GetEtaEdbChartEdbMappingList(chartInfoId int) (items []*ChartEdbInfoMapping, err error) {
  219. o := global.DmSQL["data"]
  220. 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`
  221. 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
  222. FROM chart_edb_mapping AS a
  223. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  224. WHERE a.chart_info_id=? AND a.source = ?
  225. ORDER BY chart_edb_mapping_id ASC `
  226. err = o.Raw(sql, chartInfoId, utils.CHART_SOURCE_DEFAULT).Scan(&items).Error
  227. if err != nil {
  228. return
  229. }
  230. // 日期格式转换
  231. for _, v := range items {
  232. v.ConvertToResp()
  233. }
  234. return
  235. }
  236. // GetFutureGoodEdbChartEdbMapping 商品曲线图查询对应的商品指标
  237. func GetFutureGoodEdbChartEdbMapping(chartInfoId int) (item *ChartEdbInfoMapping, err error) {
  238. o := global.DmSQL["data"]
  239. 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`
  240. 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
  241. FROM chart_edb_mapping AS a
  242. INNER JOIN future_good_edb_info AS b ON a.edb_info_id=b.future_good_edb_info_id
  243. WHERE a.chart_info_id=? AND a.source = ?
  244. ORDER BY chart_edb_mapping_id ASC `
  245. err = o.Raw(sql, chartInfoId, utils.CHART_SOURCE_FUTURE_GOOD).Scan(&item).Error
  246. if err != nil {
  247. return
  248. }
  249. // 日期格式转换
  250. item.ConvertToResp()
  251. return
  252. }
  253. // GetFutureGoodEdbChartEdbMappingList 商品曲线图查询对应的商品指标
  254. func GetFutureGoodEdbChartEdbMappingList(chartInfoId int) (items []*ChartEdbInfoMapping, err error) {
  255. o := global.DmSQL["data"]
  256. 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`
  257. 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
  258. FROM chart_edb_mapping AS a
  259. INNER JOIN future_good_edb_info AS b ON a.edb_info_id=b.future_good_edb_info_id
  260. WHERE a.chart_info_id=? AND a.source = ?
  261. ORDER BY chart_edb_mapping_id ASC `
  262. err = o.Raw(sql, chartInfoId, utils.CHART_SOURCE_FUTURE_GOOD).Scan(&items).Error
  263. if err != nil {
  264. return
  265. }
  266. // 日期格式转换
  267. for _, v := range items {
  268. v.ConvertToResp()
  269. }
  270. return
  271. }
  272. // GetChartEdbMappingListV2 根据图表id获取指标信息,不连表查询指标表
  273. func GetChartEdbMappingListV2(chartInfoId int) (list []*ChartEdbInfoMapping, err error) {
  274. o := global.DmSQL["data"]
  275. sql := ` SELECT a.* FROM chart_edb_mapping AS a
  276. WHERE chart_info_id=?
  277. ORDER BY chart_edb_mapping_id ASC `
  278. err = o.Raw(sql, chartInfoId).Scan(&list).Error
  279. if err != nil {
  280. return
  281. }
  282. // 日期格式转换
  283. for _, v := range list {
  284. v.ConvertToResp()
  285. }
  286. return
  287. }
  288. // GetChartEdbMappingByEdbInfoId 根据指标id获取edb_mapping
  289. func GetChartEdbMappingByEdbInfoId(edbInfoId int) (item *ChartEdbInfoMapping, err error) {
  290. o := global.DmSQL["data"]
  291. 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
  292. FROM edb_info
  293. WHERE edb_info_id = ? `
  294. err = o.Raw(sql, edbInfoId).First(&item).Error
  295. if err != nil {
  296. return
  297. }
  298. // 日期格式转换
  299. item.ConvertToResp()
  300. return
  301. }
  302. // GetEdbMappingListByEdbInfoId 根据指标id获取edb_mapping
  303. func GetEdbMappingListByEdbInfoId(edbInfoId int) (list []*ChartEdbInfoMapping, err error) {
  304. o := global.DmSQL["data"]
  305. sql := ` SELECT * FROM chart_edb_mapping
  306. WHERE edb_info_id = ? `
  307. err = o.Raw(sql, edbInfoId).Scan(&list).Error
  308. if err != nil {
  309. return
  310. }
  311. // 日期格式转换
  312. for _, v := range list {
  313. v.ConvertToResp()
  314. }
  315. return
  316. }
  317. // GetChartEdbMappingByFutureGoodEdbInfoId 根据指标id获取edb_mapping
  318. func GetChartEdbMappingByFutureGoodEdbInfoId(edbInfoId int) (item *ChartEdbInfoMapping, err error) {
  319. o := global.DmSQL["data"]
  320. 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
  321. FROM future_good_edb_info b
  322. WHERE future_good_edb_info_id = ? limit 1 `
  323. err = o.Raw(sql, edbInfoId).Scan(&item).Error
  324. if err != nil {
  325. return
  326. }
  327. // 日期格式转换
  328. item.ConvertToResp()
  329. return
  330. }
  331. // ModifyChartEdbMapping
  332. // @Description: 修改图表的关系表
  333. // @author: Roc
  334. // @datetime 2023-12-11 17:23:32
  335. // @param chartInfoId int
  336. // @param edbInfoList []*EdbInfo
  337. // @return err error
  338. func ModifyChartEdbMapping(chartInfoId int, edbInfoList []*EdbInfo) (err error) {
  339. o := global.DmSQL["data"].Begin()
  340. defer func() {
  341. if err != nil {
  342. fmt.Println("AddCalculateHcz,Err:" + err.Error())
  343. _ = o.Rollback()
  344. } else {
  345. _ = o.Commit()
  346. }
  347. }()
  348. list := make([]*ChartEdbMapping, 0)
  349. sql := ` SELECT a.*
  350. FROM chart_edb_mapping AS a
  351. WHERE chart_info_id=?
  352. ORDER BY chart_edb_mapping_id ASC `
  353. err = o.Raw(sql, chartInfoId).Scan(&list).Error
  354. if err != nil {
  355. return
  356. }
  357. mappingIdMap := make(map[int]*ChartEdbMapping)
  358. removeMapping := make(map[int]int)
  359. for _, v := range list {
  360. mappingIdMap[v.EdbInfoId] = v
  361. removeMapping[v.EdbInfoId] = v.ChartEdbMappingId
  362. }
  363. addList := make([]*ChartEdbMapping, 0)
  364. for _, v := range edbInfoList {
  365. _, ok := mappingIdMap[v.EdbInfoId]
  366. // 存在该指标关系就不处理了
  367. if ok {
  368. delete(removeMapping, v.EdbInfoId)
  369. continue
  370. }
  371. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  372. // 不存在就添加
  373. addList = append(addList, &ChartEdbMapping{
  374. //ChartEdbMappingId: 0,
  375. ChartInfoId: chartInfoId,
  376. EdbInfoId: v.EdbInfoId,
  377. CreateTime: time.Now(),
  378. ModifyTime: time.Now(),
  379. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + fmt.Sprint(chartInfoId) + "_" + fmt.Sprint(v.EdbInfoId) + "_" + timestamp),
  380. MaxData: v.MaxValue,
  381. MinData: v.MinValue,
  382. EdbInfoType: v.EdbInfoType,
  383. Source: v.Source,
  384. })
  385. }
  386. // 需要添加的话,那就添加吧
  387. if len(addList) > 0 {
  388. err = o.CreateInBatches(addList, utils.MultiAddNum).Error
  389. if err != nil {
  390. return
  391. }
  392. }
  393. // 移除不必要的mapping
  394. if len(removeMapping) > 0 {
  395. removeIdList := make([]string, 0) //需要移除的日期
  396. for _, v := range removeMapping {
  397. removeIdList = append(removeIdList, fmt.Sprint(v))
  398. }
  399. removeIdStr := strings.Join(removeIdList, `","`)
  400. removeIdStr = `"` + removeIdStr + `"`
  401. //如果拼接指标变更了,那么需要删除所有的指标数据
  402. sql := fmt.Sprintf(` DELETE FROM chart_edb_mapping WHERE chart_edb_mapping_id in (%s) `, removeIdStr)
  403. err = o.Exec(sql).Error
  404. if err != nil {
  405. err = fmt.Errorf("移除不必要的mapping失败,Err:" + err.Error())
  406. return
  407. }
  408. }
  409. return
  410. }
  411. func GetRelationEdbInfoListMappingByCondition(condition string, pars []interface{}) (item []*ChartEdbInfoMapping, err error) {
  412. o := global.DmSQL["data"]
  413. sql := ` SELECT a.* FROM edb_info AS a
  414. JOIN edb_info_calculate_mapping AS b on a.edb_info_id = b.edb_info_id WHERE 1=1 `
  415. if condition != "" {
  416. sql += condition
  417. }
  418. err = o.Raw(sql, pars...).Scan(&item).Error
  419. return
  420. }