chart_info_range_analysis.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. package data_manage
  2. import (
  3. "eta_gn/eta_api/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  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. o := orm.NewOrmUsingDB("data")
  92. tx, err := o.Begin()
  93. if err != nil {
  94. return
  95. }
  96. defer func() {
  97. if err != nil {
  98. _ = tx.Rollback()
  99. } else {
  100. _ = tx.Commit()
  101. }
  102. }()
  103. // 新增图表信息
  104. newId, err := tx.Insert(chartInfo)
  105. if err != nil {
  106. return
  107. }
  108. // 指标mapping
  109. chartInfo.ChartInfoId = int(newId)
  110. chartInfoId = int(newId)
  111. if len(edbMappingList) > 0 {
  112. for i := range edbMappingList {
  113. edbMappingList[i].ChartInfoId = chartInfoId
  114. }
  115. _, err = tx.InsertMulti(len(edbMappingList), edbMappingList)
  116. if err != nil {
  117. return
  118. }
  119. }
  120. return
  121. }
  122. // EditRangeChartInfoAndMapping 修改区间计算图表的 图表与指标 的关系
  123. func EditRangeChartInfoAndMapping(req *EditChartInfoReq, edbInfoIdStr, lang string, calendar string, dateType, disabled int, barChartConf string) (err error) {
  124. o := orm.NewOrmUsingDB("data")
  125. to, err := o.Begin()
  126. if err != nil {
  127. return
  128. }
  129. defer func() {
  130. if err != nil {
  131. _ = to.Rollback()
  132. } else {
  133. _ = to.Commit()
  134. }
  135. }()
  136. var pars []interface{}
  137. pars = append(pars, req.ChartName)
  138. pars = append(pars, req.ChartNameEn)
  139. pars = append(pars, edbInfoIdStr)
  140. pars = append(pars, req.ChartType)
  141. pars = append(pars, req.ChartClassifyId)
  142. pars = append(pars, disabled)
  143. pars = append(pars, barChartConf)
  144. pars = append(pars, req.ExtraConfig)
  145. pars = append(pars, req.StartYear)
  146. pars = append(pars, req.ChartThemeId)
  147. pars = append(pars, req.SourcesFrom)
  148. pars = append(pars, req.Instructions)
  149. pars = append(pars, req.MarkersLines)
  150. pars = append(pars, req.MarkersAreas)
  151. pars = append(pars, req.Unit)
  152. pars = append(pars, req.UnitEn)
  153. sql := ` UPDATE chart_info
  154. SET
  155. chart_name =?,
  156. chart_name_en =?,
  157. edb_info_ids=?,
  158. chart_type=?,
  159. chart_classify_id = ?,
  160. modify_time = NOW(),
  161. disabled = ?,
  162. bar_config = ?,
  163. extra_config = ?,
  164. start_year = ?,
  165. chart_theme_id = ?,
  166. sources_from = ?,
  167. instructions = ?,
  168. markers_lines = ?,
  169. markers_areas = ?,
  170. unit = ?,
  171. unit_en = ?
  172. `
  173. if calendar != "" {
  174. sql += `,calendar = ? `
  175. pars = append(pars, calendar)
  176. }
  177. if dateType > 0 {
  178. sql += `,date_type = ? `
  179. pars = append(pars, dateType)
  180. }
  181. sql += `,start_date = ? `
  182. pars = append(pars, req.StartDate)
  183. sql += `,end_date = ? `
  184. pars = append(pars, req.EndDate)
  185. sql += `,season_start_date = ? `
  186. pars = append(pars, req.StartDate)
  187. sql += `,season_end_date = ? `
  188. pars = append(pars, req.EndDate)
  189. sql += `,left_min = ? `
  190. pars = append(pars, req.LeftMin)
  191. sql += `,left_max = ? `
  192. pars = append(pars, req.LeftMax)
  193. sql += `,right_min = ? `
  194. pars = append(pars, req.RightMin)
  195. sql += `,right_max = ? `
  196. pars = append(pars, req.RightMax)
  197. sql += `,right2_min = ? `
  198. pars = append(pars, req.Right2Min)
  199. sql += `,right2_max = ? `
  200. pars = append(pars, req.Right2Max)
  201. sql += `,min_max_save = ? `
  202. pars = append(pars, req.MinMaxSave)
  203. sql += `WHERE chart_info_id = ?`
  204. pars = append(pars, req.ChartInfoId)
  205. _, err = to.Raw(sql, pars).Exec()
  206. if err != nil {
  207. fmt.Println("UPDATE chart_info Err:", err.Error())
  208. return err
  209. }
  210. chartEdbMappingIdList := make([]string, 0)
  211. for _, v := range req.ChartEdbInfoList {
  212. // 查询该指标是否存在,如果存在的话,那么就去修改,否则新增
  213. var tmpChartEdbMapping *ChartEdbMapping
  214. csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id=? AND source = ? `
  215. err = to.Raw(csql, req.ChartInfoId, v.EdbInfoId, utils.CHART_SOURCE_RANGE_ANALYSIS).QueryRow(&tmpChartEdbMapping)
  216. if err != nil && err.Error() != utils.ErrNoRow() {
  217. fmt.Println("QueryRow Err:", err.Error())
  218. return err
  219. }
  220. if tmpChartEdbMapping != nil {
  221. chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(tmpChartEdbMapping.ChartEdbMappingId))
  222. tmpChartEdbMapping.ModifyTime = time.Now()
  223. //tmpChartEdbMapping.MaxData = v.MaxData
  224. //tmpChartEdbMapping.MinData = v.MinData
  225. //tmpChartEdbMapping.IsOrder = v.IsOrder
  226. tmpChartEdbMapping.EdbAliasName = v.EdbAliasName
  227. tmpChartEdbMapping.IsAxis = v.IsAxis
  228. //tmpChartEdbMapping.EdbInfoType = v.EdbInfoType
  229. //tmpChartEdbMapping.LeadValue = v.LeadValue
  230. //tmpChartEdbMapping.LeadUnit = v.LeadUnit
  231. //tmpChartEdbMapping.ChartStyle = v.ChartStyle
  232. //tmpChartEdbMapping.ChartColor = v.ChartColor
  233. //tmpChartEdbMapping.PredictChartColor = v.PredictChartColor
  234. //tmpChartEdbMapping.ChartWidth = v.ChartWidth
  235. _, err = to.Update(tmpChartEdbMapping, "ModifyTime", "IsAxis", "EdbAliasName")
  236. if err != nil {
  237. fmt.Println("chart_edb_mapping Err:" + err.Error())
  238. return err
  239. }
  240. } else {
  241. mapItem := new(ChartEdbMapping)
  242. mapItem.ChartInfoId = req.ChartInfoId
  243. mapItem.EdbInfoId = v.EdbInfoId
  244. mapItem.EdbAliasName = v.EdbAliasName
  245. mapItem.IsAxis = v.IsAxis
  246. mapItem.CreateTime = time.Now()
  247. mapItem.ModifyTime = time.Now()
  248. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  249. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp + "_" + strconv.Itoa(v.EdbInfoId))
  250. //mapItem.MaxData = v.MaxData
  251. //mapItem.MinData = v.MinData
  252. //mapItem.IsOrder = true
  253. mapItem.EdbInfoType = 1
  254. //mapItem.LeadValue = v.LeadValue
  255. //mapItem.LeadUnit = v.LeadUnit
  256. //mapItem.ChartStyle = v.ChartStyle
  257. //mapItem.ChartColor = v.ChartColor
  258. //mapItem.PredictChartColor = v.PredictChartColor
  259. //mapItem.ChartWidth = v.ChartWidth
  260. mapItem.Source = utils.CHART_SOURCE_RANGE_ANALYSIS
  261. tmpId, err := to.Insert(mapItem)
  262. if err != nil {
  263. fmt.Println("AddChartEdbMapping Err:" + err.Error())
  264. return err
  265. }
  266. mapItem.ChartEdbMappingId = int(tmpId)
  267. chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(mapItem.ChartEdbMappingId))
  268. }
  269. }
  270. if len(chartEdbMappingIdList) > 0 {
  271. chartEdbMappingIdStr := strings.Join(chartEdbMappingIdList, ",")
  272. dsql := `DELETE FROM chart_edb_mapping WHERE chart_info_id=? AND chart_edb_mapping_id NOT IN(` + chartEdbMappingIdStr + `)`
  273. _, err = to.Raw(dsql, req.ChartInfoId).Exec()
  274. if err != nil {
  275. fmt.Println("delete err:" + err.Error())
  276. return err
  277. }
  278. }
  279. return
  280. }
  281. // EditRangeChartInfo 修改区间计算图表基本信息
  282. func EditRangeChartInfo(req *SaveChartRangeReq) (err error) {
  283. o := orm.NewOrmUsingDB("data")
  284. to, err := o.Begin()
  285. if err != nil {
  286. return
  287. }
  288. defer func() {
  289. if err != nil {
  290. _ = to.Rollback()
  291. } else {
  292. _ = to.Commit()
  293. }
  294. }()
  295. var pars []interface{}
  296. pars = append(pars, req.StartYear)
  297. sql := ` UPDATE chart_info
  298. SET
  299. modify_time = NOW(),
  300. start_year = ?
  301. `
  302. if req.DateType > 0 {
  303. sql += `,date_type = ? `
  304. pars = append(pars, req.DateType)
  305. }
  306. sql += `,start_date = ? `
  307. pars = append(pars, req.StartDate)
  308. sql += `,end_date = ? `
  309. pars = append(pars, req.EndDate)
  310. sql += `,left_min = ? `
  311. pars = append(pars, req.LeftMin)
  312. sql += `,left_max = ? `
  313. pars = append(pars, req.LeftMax)
  314. sql += `,right_min = ? `
  315. pars = append(pars, req.RightMin)
  316. sql += `,right_max = ? `
  317. pars = append(pars, req.RightMax)
  318. sql += `,right2_min = ? `
  319. pars = append(pars, req.Right2Min)
  320. sql += `,right2_max = ? `
  321. pars = append(pars, req.Right2Max)
  322. sql += `,min_max_save = ? `
  323. pars = append(pars, req.MinMaxSave)
  324. sql += `WHERE chart_info_id = ?`
  325. pars = append(pars, req.ChartInfoId)
  326. _, err = to.Raw(sql, pars).Exec()
  327. if err != nil {
  328. fmt.Println("UPDATE chart_info Err:", err.Error())
  329. return err
  330. }
  331. return
  332. }
  333. // SaveChartRangeAnalysisEdbReq 指标保存请求
  334. type SaveChartRangeAnalysisEdbReq struct {
  335. EdbInfoList []*CalculateEdbInfoItem `description:"指标列表"`
  336. ExtraConfig string
  337. MultipleGraphConfigId int `description:"配置id"`
  338. IsSaveAs bool `description:"是否另存为,true的话,就是另存为,不会建立与配置的关系"`
  339. EdbInfoType int `description:"指标类型"`
  340. }
  341. // ChartRangeAnalysisConfigEdbResp 指标列表
  342. type ChartRangeAnalysisConfigEdbResp struct {
  343. EdbInfoList []*ChartRangeAnalysisConfigEdbItem `description:"指标列表"`
  344. }
  345. type ChartRangeAnalysisConfigEdbItem struct {
  346. EdbInfoId int `description:"指标id"`
  347. EdbName string `description:"指标名称"`
  348. EdbNameEn string `description:"指标名称"`
  349. Frequency string `description:"频度"`
  350. Unit string `description:"单位"`
  351. UnitEn string `description:"单位"`
  352. ClassifyId int `description:"分类id"`
  353. FromEdbInfoId int `description:"计算来源指标id"`
  354. EdbTypeInfo int `description:"指标类型,0普通指标,1预测指标"`
  355. }
  356. type SortEdbDataList []*EdbDataList
  357. func (m SortEdbDataList) Len() int {
  358. return len(m)
  359. }
  360. func (m SortEdbDataList) Less(i, j int) bool {
  361. return m[i].DataTime > m[j].DataTime
  362. }
  363. func (m SortEdbDataList) Swap(i, j int) {
  364. m[i], m[j] = m[j], m[i]
  365. }
  366. type EditChartRangeBaseReq struct {
  367. ChartInfoId int `description:"图表ID"`
  368. ChartName string `description:"英文图表名称"`
  369. }
  370. type SaveChartRangeReq struct {
  371. ChartInfoId int `description:"图表ID"`
  372. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:起始日期至今,20:N年至今"`
  373. StartDate string `description:"自定义开始日期"`
  374. EndDate string `description:"自定义结束日期"`
  375. Calendar string `description:"公历/农历"`
  376. LeftMin string `description:"图表左侧最小值"`
  377. LeftMax string `description:"图表左侧最大值"`
  378. RightMin string `description:"图表右侧最小值"`
  379. RightMax string `description:"图表右侧最大值"`
  380. Right2Min string `description:"图表右侧最小值"`
  381. Right2Max string `description:"图表右侧最大值"`
  382. MinMaxSave int `description:"是否手动保存过上下限:0-否;1-是"`
  383. StartYear int `description:"当选择的日期类型为最近N年类型时,即date_type=20, 用start_year表示N"`
  384. }