chart_info_range_analysis.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. )
  10. type ChartRangeAnalysisExtraConf struct {
  11. EdbInfoMode int `description:"指标模式 0: 单指标,1: 多指标"`
  12. DateRangeType int `description:"区间划分类型 0:智能划分,1:手工划分,2:跨年划分"`
  13. AutoDateConf ChartRangeAnalysisAutoDateConf `description:"智能划分时间区间配置"`
  14. ManualDateConf []ChartRangeAnalysisManualDateConf `description:"手工划分时间区间配置"`
  15. YearDateConf ChartRangeAnalysisYearDateConf `description:"跨年划分时间区间配置"`
  16. CalculateType int `description:"计算类型 0: 区间均值,1: 区间累计值,2:区间涨幅,3:区间年化增长率,4:区间最大值,5:区间最小值"`
  17. UnNormalDataDealType int `description:"异常值处理配置 0:不处理,1:剔除,2替换"`
  18. UnNormalDataConf ChartRangeAnalysisDeleteDataConf
  19. DataConvertType int `description:"数据转换类型 0不转, 1乘 2除 3对数"`
  20. DataConvertConf ChartRangeAnalysisDataConvertConf `description:"数据转换详情"`
  21. SeriesName string `description:"指标系列名称"`
  22. EdbInfoType int `description:"指标类型:0普通指标,1预测指标"`
  23. MultipleGraphConfigId int `description:"配置ID"`
  24. }
  25. type ChartRangeAnalysisAutoDateChangeConf struct {
  26. BaseDateType int `description:"基准日期类型:0指标日期,1系统日期"`
  27. MoveForward int `description:"前移的期数"`
  28. DateChange []*EdbDataDateChangeConf
  29. }
  30. type EdbDataDateChangeConf struct {
  31. Year int
  32. Month int
  33. Day int
  34. Frequency string `description:"频度变换"`
  35. FrequencyDay string `description:"频度的固定日期"`
  36. ChangeType int `description:"日期变换类型1日期位移,2指定频率"`
  37. }
  38. type ChartRangeAnalysisDeleteDataConf struct {
  39. Formula string `description:"比较符号:=、>、<、>=、<="`
  40. Value float64 `description:"比较的值"`
  41. ReplaceValue float64 `description:"替换的值"`
  42. }
  43. type ChartRangeAnalysisDataConvertConf struct {
  44. Value float64 `description:"数据转换值"`
  45. Unit string `description:"数据转换单位"`
  46. EnUnit string `description:"数据转换单位"`
  47. }
  48. type ChartRangeAnalysisManualDateConf struct { //手工划分
  49. StartDate string `description:"开始日期"`
  50. EndDate string `description:"结束日期"`
  51. }
  52. type ChartRangeAnalysisAutoDateConf struct { //智能划分
  53. IsAutoStartDate int `description:"起始日期是否是动态设置:0固定,1动态"`
  54. StartDate string `description:"固定模式下的起始日期"`
  55. EndDate string `description:"固定模式下的截止日期"`
  56. IsAutoEndDate int `description:"截止日期是否是动态设置:0固定,1动态"`
  57. StartDateConf ChartRangeAnalysisAutoDateChangeConf `description:"动态起始日期配置"`
  58. EndDateConf ChartRangeAnalysisAutoDateChangeConf `description:"动态截止日期配置"`
  59. }
  60. type ChartRangeAnalysisYearDateConf struct {
  61. StartDay string `description:"开始日"`
  62. EndDay string `description:"结束日"`
  63. }
  64. // ChartRangeAnalysisPreviewReq 相关性图表请求体
  65. type ChartRangeAnalysisPreviewReq struct {
  66. ExtraConfig ChartRangeAnalysisExtraConf
  67. ChartEdbInfoList []*ChartSaveItem `description:"指标及配置信息"`
  68. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:起始日期至今 20:最近N年"`
  69. StartDate string `description:"自定义开始日期"`
  70. EndDate string `description:"自定义结束日期"`
  71. StartYear int `description:"当选择的日期类型为最近N年类型时,即date_type=20, 用start_year表示N"`
  72. }
  73. type ChartRangeAnalysisManualDateConfList []ChartRangeAnalysisManualDateConf
  74. func (a ChartRangeAnalysisManualDateConfList) Len() int { return len(a) }
  75. func (a ChartRangeAnalysisManualDateConfList) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  76. func (a ChartRangeAnalysisManualDateConfList) Less(i, j int) bool {
  77. return a[i].StartDate < a[j].StartDate
  78. }
  79. type ChartRangeAnalysisDataResp struct { //图表详情返回值
  80. *ChartRangeAnalysisExtraConf
  81. SeriesId int `description:"指标系列ID"`
  82. ConfigEdbNum int `description:"生成的指标数"`
  83. }
  84. type ChartRangeAnalysisDateDataItem struct {
  85. StartDate time.Time
  86. EndDate time.Time
  87. DataList []*EdbDataList
  88. }
  89. // CreateRangeChartAndEdb 新增区间计算图表
  90. func CreateRangeChartAndEdb(chartInfo *ChartInfo, edbMappingList []*ChartEdbMapping) (chartInfoId int, err error) {
  91. tx := global.DmSQL["data"].Begin()
  92. defer func() {
  93. if err != nil {
  94. _ = tx.Rollback()
  95. } else {
  96. _ = tx.Commit()
  97. }
  98. }()
  99. // 新增图表信息
  100. err = tx.Create(chartInfo).Error
  101. if err != nil {
  102. return
  103. }
  104. // 指标mapping
  105. chartInfoId = chartInfo.ChartInfoId
  106. if len(edbMappingList) > 0 {
  107. for i := range edbMappingList {
  108. edbMappingList[i].ChartInfoId = chartInfoId
  109. }
  110. err = tx.CreateInBatches(edbMappingList, utils.MultiAddNum).Error
  111. if err != nil {
  112. return
  113. }
  114. }
  115. return
  116. }
  117. // EditRangeChartInfoAndMapping 修改区间计算图表的 图表与指标 的关系
  118. func EditRangeChartInfoAndMapping(req *EditChartInfoReq, edbInfoIdStr, lang string, calendar string, dateType, disabled int, barChartConf string) (err error) {
  119. to := global.DmSQL["data"].Begin()
  120. defer func() {
  121. if err != nil {
  122. _ = to.Rollback()
  123. } else {
  124. _ = to.Commit()
  125. }
  126. }()
  127. var pars []interface{}
  128. pars = append(pars, req.ChartName)
  129. pars = append(pars, req.ChartNameEn)
  130. pars = append(pars, edbInfoIdStr)
  131. pars = append(pars, req.ChartType)
  132. pars = append(pars, req.ChartClassifyId)
  133. pars = append(pars, disabled)
  134. pars = append(pars, barChartConf)
  135. pars = append(pars, req.ExtraConfig)
  136. pars = append(pars, req.StartYear)
  137. pars = append(pars, req.ChartThemeId)
  138. pars = append(pars, req.SourcesFrom)
  139. pars = append(pars, req.Instructions)
  140. pars = append(pars, req.MarkersLines)
  141. pars = append(pars, req.MarkersAreas)
  142. pars = append(pars, req.Unit)
  143. pars = append(pars, req.UnitEn)
  144. sql := ` UPDATE chart_info
  145. SET
  146. chart_name =?,
  147. chart_name_en =?,
  148. edb_info_ids=?,
  149. chart_type=?,
  150. chart_classify_id = ?,
  151. modify_time = NOW(),
  152. disabled = ?,
  153. bar_config = ?,
  154. extra_config = ?,
  155. start_year = ?,
  156. chart_theme_id = ?,
  157. sources_from = ?,
  158. instructions = ?,
  159. markers_lines = ?,
  160. markers_areas = ?,
  161. unit = ?,
  162. unit_en = ?
  163. `
  164. if calendar != "" {
  165. sql += `,calendar = ? `
  166. pars = append(pars, calendar)
  167. }
  168. if dateType > 0 {
  169. sql += `,date_type = ? `
  170. pars = append(pars, dateType)
  171. }
  172. sql += `,start_date = ? `
  173. pars = append(pars, req.StartDate)
  174. sql += `,end_date = ? `
  175. pars = append(pars, req.EndDate)
  176. sql += `,season_start_date = ? `
  177. pars = append(pars, req.StartDate)
  178. sql += `,season_end_date = ? `
  179. pars = append(pars, req.EndDate)
  180. sql += `,left_min = ? `
  181. pars = append(pars, req.LeftMin)
  182. sql += `,left_max = ? `
  183. pars = append(pars, req.LeftMax)
  184. sql += `,right_min = ? `
  185. pars = append(pars, req.RightMin)
  186. sql += `,right_max = ? `
  187. pars = append(pars, req.RightMax)
  188. sql += `,right2_min = ? `
  189. pars = append(pars, req.Right2Min)
  190. sql += `,right2_max = ? `
  191. pars = append(pars, req.Right2Max)
  192. sql += `,min_max_save = ? `
  193. pars = append(pars, req.MinMaxSave)
  194. sql += `WHERE chart_info_id = ?`
  195. pars = append(pars, req.ChartInfoId)
  196. err = to.Exec(sql, pars...).Error
  197. if err != nil {
  198. fmt.Println("UPDATE chart_info Err:", err.Error())
  199. return err
  200. }
  201. chartEdbMappingIdList := make([]string, 0)
  202. for _, v := range req.ChartEdbInfoList {
  203. // 查询该指标是否存在,如果存在的话,那么就去修改,否则新增
  204. var tmpChartEdbMapping *ChartEdbMapping
  205. csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id=? AND source = ? `
  206. err = to.Raw(csql, req.ChartInfoId, v.EdbInfoId, utils.CHART_SOURCE_RANGE_ANALYSIS).Scan(&tmpChartEdbMapping).Error
  207. if err != nil && !utils.IsErrNoRow(err) {
  208. fmt.Println("QueryRow Err:", err.Error())
  209. return err
  210. }
  211. if tmpChartEdbMapping != nil {
  212. chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(tmpChartEdbMapping.ChartEdbMappingId))
  213. tmpChartEdbMapping.ModifyTime = time.Now()
  214. tmpChartEdbMapping.EdbAliasName = v.EdbAliasName
  215. tmpChartEdbMapping.IsAxis = v.IsAxis
  216. err = to.Select("ModifyTime", "IsAxis", "EdbAliasName").Updates(tmpChartEdbMapping).Error
  217. if err != nil {
  218. fmt.Println("chart_edb_mapping Err:" + err.Error())
  219. return err
  220. }
  221. } else {
  222. mapItem := new(ChartEdbMapping)
  223. mapItem.ChartInfoId = req.ChartInfoId
  224. mapItem.EdbInfoId = v.EdbInfoId
  225. mapItem.EdbAliasName = v.EdbAliasName
  226. mapItem.IsAxis = v.IsAxis
  227. mapItem.CreateTime = time.Now()
  228. mapItem.ModifyTime = time.Now()
  229. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  230. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp + "_" + strconv.Itoa(v.EdbInfoId))
  231. mapItem.EdbInfoType = 1
  232. mapItem.Source = utils.CHART_SOURCE_RANGE_ANALYSIS
  233. err = to.Create(mapItem).Error
  234. if err != nil {
  235. fmt.Println("AddChartEdbMapping Err:" + err.Error())
  236. return err
  237. }
  238. chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(mapItem.ChartEdbMappingId))
  239. }
  240. }
  241. if len(chartEdbMappingIdList) > 0 {
  242. chartEdbMappingIdStr := strings.Join(chartEdbMappingIdList, ",")
  243. dsql := `DELETE FROM chart_edb_mapping WHERE chart_info_id=? AND chart_edb_mapping_id NOT IN(` + chartEdbMappingIdStr + `)`
  244. err = to.Exec(dsql, req.ChartInfoId).Error
  245. if err != nil {
  246. fmt.Println("delete err:" + err.Error())
  247. return err
  248. }
  249. }
  250. return
  251. }
  252. // EditRangeChartInfo 修改区间计算图表基本信息
  253. func EditRangeChartInfo(req *SaveChartRangeReq) (err error) {
  254. to := global.DmSQL["data"].Begin()
  255. defer func() {
  256. if err != nil {
  257. _ = to.Rollback()
  258. } else {
  259. _ = to.Commit()
  260. }
  261. }()
  262. var pars []interface{}
  263. pars = append(pars, req.StartYear)
  264. sql := ` UPDATE chart_info
  265. SET
  266. modify_time = NOW(),
  267. start_year = ?
  268. `
  269. if req.DateType > 0 {
  270. sql += `,date_type = ? `
  271. pars = append(pars, req.DateType)
  272. }
  273. sql += `,start_date = ? `
  274. pars = append(pars, req.StartDate)
  275. sql += `,end_date = ? `
  276. pars = append(pars, req.EndDate)
  277. sql += `,left_min = ? `
  278. pars = append(pars, req.LeftMin)
  279. sql += `,left_max = ? `
  280. pars = append(pars, req.LeftMax)
  281. sql += `,right_min = ? `
  282. pars = append(pars, req.RightMin)
  283. sql += `,right_max = ? `
  284. pars = append(pars, req.RightMax)
  285. sql += `,right2_min = ? `
  286. pars = append(pars, req.Right2Min)
  287. sql += `,right2_max = ? `
  288. pars = append(pars, req.Right2Max)
  289. sql += `,min_max_save = ? `
  290. pars = append(pars, req.MinMaxSave)
  291. sql += `WHERE chart_info_id = ?`
  292. pars = append(pars, req.ChartInfoId)
  293. err = to.Exec(sql, pars...).Error
  294. if err != nil {
  295. fmt.Println("UPDATE chart_info Err:", err.Error())
  296. return err
  297. }
  298. return
  299. }
  300. // SaveChartRangeAnalysisEdbReq 指标保存请求
  301. type SaveChartRangeAnalysisEdbReq struct {
  302. EdbInfoList []*CalculateEdbInfoItem `description:"指标列表"`
  303. ExtraConfig string
  304. MultipleGraphConfigId int `description:"配置id"`
  305. IsSaveAs bool `description:"是否另存为,true的话,就是另存为,不会建立与配置的关系"`
  306. EdbInfoType int `description:"指标类型"`
  307. }
  308. // ChartRangeAnalysisConfigEdbResp 指标列表
  309. type ChartRangeAnalysisConfigEdbResp struct {
  310. EdbInfoList []*ChartRangeAnalysisConfigEdbItem `description:"指标列表"`
  311. }
  312. type ChartRangeAnalysisConfigEdbItem struct {
  313. EdbInfoId int `description:"指标id"`
  314. EdbName string `description:"指标名称"`
  315. EdbNameEn string `description:"指标名称"`
  316. Frequency string `description:"频度"`
  317. Unit string `description:"单位"`
  318. UnitEn string `description:"单位"`
  319. ClassifyId int `description:"分类id"`
  320. FromEdbInfoId int `description:"计算来源指标id"`
  321. EdbTypeInfo int `description:"指标类型,0普通指标,1预测指标"`
  322. }
  323. type SortEdbDataList []*EdbDataList
  324. func (m SortEdbDataList) Len() int {
  325. return len(m)
  326. }
  327. func (m SortEdbDataList) Less(i, j int) bool {
  328. return m[i].DataTime > m[j].DataTime
  329. }
  330. func (m SortEdbDataList) Swap(i, j int) {
  331. m[i], m[j] = m[j], m[i]
  332. }
  333. type EditChartRangeBaseReq struct {
  334. ChartInfoId int `description:"图表ID"`
  335. ChartName string `description:"英文图表名称"`
  336. }
  337. type SaveChartRangeReq struct {
  338. ChartInfoId int `description:"图表ID"`
  339. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:起始日期至今,20:N年至今"`
  340. StartDate string `description:"自定义开始日期"`
  341. EndDate string `description:"自定义结束日期"`
  342. Calendar string `description:"公历/农历"`
  343. LeftMin string `description:"图表左侧最小值"`
  344. LeftMax string `description:"图表左侧最大值"`
  345. RightMin string `description:"图表右侧最小值"`
  346. RightMax string `description:"图表右侧最大值"`
  347. Right2Min string `description:"图表右侧最小值"`
  348. Right2Max string `description:"图表右侧最大值"`
  349. MinMaxSave int `description:"是否手动保存过上下限:0-否;1-是"`
  350. StartYear int `description:"当选择的日期类型为最近N年类型时,即date_type=20, 用start_year表示N"`
  351. }