chart_info_range_analysis.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package data_manage
  2. import (
  3. "eta/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. }
  24. type ChartRangeAnalysisAutoDateChangeConf struct {
  25. BaseDateType int `description:"基准日期类型:0指标日期,1系统日期,2固定日期"`
  26. MoveForward int `description:"前移的期数"`
  27. DateChange []*EdbDateConfDateChange
  28. }
  29. type ChartRangeAnalysisDeleteDataConf struct {
  30. Formula string `description:"比较符号:=、>、<、>=、<="`
  31. Value float64
  32. }
  33. type ChartRangeAnalysisDataConvertConf struct {
  34. Value float64 `description:"数据转换值"`
  35. Unit string `description:"数据转换单位"`
  36. EnUnit string `description:"数据转换单位"`
  37. }
  38. type ChartRangeAnalysisManualDateConf struct { //手工划分
  39. StartDate string `description:"开始日期"`
  40. EndDate string `description:"结束日期"`
  41. }
  42. type ChartRangeAnalysisAutoDateConf struct { //智能划分
  43. IsAutoStartDate int `description:"起始日期是否是动态设置:0固定,1动态"`
  44. StartDate string `description:"起始日期"` //固定模式下,截止日期为指标的最新日期
  45. StartDateConf ChartRangeAnalysisAutoDateChangeConf `description:"动态起始日期配置"`
  46. EndDateConf ChartRangeAnalysisAutoDateChangeConf `description:"动态截止日期配置"`
  47. }
  48. type ChartRangeAnalysisYearDateConf struct {
  49. StartDay string `description:"开始日"`
  50. EndDay string `description:"结束日"`
  51. }
  52. // ChartRangeAnalysisPreviewReq 相关性图表请求体
  53. type ChartRangeAnalysisPreviewReq struct {
  54. ExtraConfig ChartRangeAnalysisExtraConf
  55. ChartEdbInfoList []*ChartSaveItem `description:"指标及配置信息"`
  56. DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:起始日期至今 20:最近N年"`
  57. StartDate string `description:"自定义开始日期"`
  58. EndDate string `description:"自定义结束日期"`
  59. StartYear int `description:"当选择的日期类型为最近N年类型时,即date_type=20, 用start_year表示N"`
  60. }
  61. type ChartRangeAnalysisManualDateConfList []ChartRangeAnalysisManualDateConf
  62. func (a ChartRangeAnalysisManualDateConfList) Len() int { return len(a) }
  63. func (a ChartRangeAnalysisManualDateConfList) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  64. func (a ChartRangeAnalysisManualDateConfList) Less(i, j int) bool {
  65. return a[i].StartDate < a[j].StartDate
  66. }
  67. type ChartRangeAnalysisDataResp struct { //图表详情返回值
  68. *ChartRangeAnalysisExtraConf
  69. SeriesId int `description:"指标系列ID"`
  70. }
  71. type ChartRangeAnalysisDateDataItem struct {
  72. StartDate time.Time
  73. EndDate time.Time
  74. DataList []*EdbDataList
  75. }
  76. // CreateRangeChartAndEdb 新增区间计算图表
  77. func CreateRangeChartAndEdb(chartInfo *ChartInfo, edbMappingList []*ChartEdbMapping) (chartInfoId int, err error) {
  78. o := orm.NewOrmUsingDB("data")
  79. tx, err := o.Begin()
  80. if err != nil {
  81. return
  82. }
  83. defer func() {
  84. if err != nil {
  85. _ = tx.Rollback()
  86. } else {
  87. _ = tx.Commit()
  88. }
  89. }()
  90. // 新增图表信息
  91. newId, err := tx.Insert(chartInfo)
  92. if err != nil {
  93. return
  94. }
  95. // 指标mapping
  96. chartInfo.ChartInfoId = int(newId)
  97. chartInfoId = int(newId)
  98. if len(edbMappingList) > 0 {
  99. for i := range edbMappingList {
  100. edbMappingList[i].ChartInfoId = chartInfoId
  101. }
  102. _, err = tx.InsertMulti(len(edbMappingList), edbMappingList)
  103. if err != nil {
  104. return
  105. }
  106. }
  107. return
  108. }
  109. // EditRangeChartInfoAndMapping 修改区间计算图表的 图表与指标 的关系
  110. func EditRangeChartInfoAndMapping(req *EditChartInfoReq, edbInfoIdStr, lang string, calendar string, dateType, disabled int, barChartConf string) (err error) {
  111. o := orm.NewOrmUsingDB("data")
  112. to, err := o.Begin()
  113. if err != nil {
  114. return
  115. }
  116. defer func() {
  117. if err != nil {
  118. _ = to.Rollback()
  119. } else {
  120. _ = to.Commit()
  121. }
  122. }()
  123. var pars []interface{}
  124. pars = append(pars, req.ChartName)
  125. pars = append(pars, edbInfoIdStr)
  126. pars = append(pars, req.ChartType)
  127. pars = append(pars, req.ChartClassifyId)
  128. pars = append(pars, disabled)
  129. pars = append(pars, barChartConf)
  130. pars = append(pars, req.ExtraConfig)
  131. pars = append(pars, req.SourcesFrom)
  132. sql := ` UPDATE chart_info
  133. SET
  134. chart_name =?,
  135. edb_info_ids=?,
  136. chart_type=?,
  137. chart_classify_id = ?,
  138. modify_time = NOW(),
  139. disabled = ?,
  140. bar_config = ?,
  141. extra_config = ?,
  142. sources_from = ?
  143. `
  144. if calendar != "" {
  145. sql += `,calendar = ? `
  146. pars = append(pars, calendar)
  147. }
  148. if dateType > 0 {
  149. sql += `,date_type = ? `
  150. pars = append(pars, dateType)
  151. }
  152. sql += `,start_date = ? `
  153. pars = append(pars, req.StartDate)
  154. sql += `,end_date = ? `
  155. pars = append(pars, req.EndDate)
  156. sql += `,season_start_date = ? `
  157. pars = append(pars, req.StartDate)
  158. sql += `,season_end_date = ? `
  159. pars = append(pars, req.EndDate)
  160. sql += `,left_min = ? `
  161. pars = append(pars, req.LeftMin)
  162. sql += `,left_max = ? `
  163. pars = append(pars, req.LeftMax)
  164. sql += `,right_min = ? `
  165. pars = append(pars, req.RightMin)
  166. sql += `,right_max = ? `
  167. pars = append(pars, req.RightMax)
  168. sql += `WHERE chart_info_id = ?`
  169. pars = append(pars, req.ChartInfoId)
  170. _, err = to.Raw(sql, pars).Exec()
  171. if err != nil {
  172. fmt.Println("UPDATE chart_info Err:", err.Error())
  173. return err
  174. }
  175. chartEdbMappingIdList := make([]string, 0)
  176. for _, v := range req.ChartEdbInfoList {
  177. // 查询该指标是否存在,如果存在的话,那么就去修改,否则新增
  178. var tmpChartEdbMapping *ChartEdbMapping
  179. csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id=? AND source = ? `
  180. err = to.Raw(csql, req.ChartInfoId, v.EdbInfoId, utils.CHART_SOURCE_RANGE_ANALYSIS).QueryRow(&tmpChartEdbMapping)
  181. if err != nil && err.Error() != utils.ErrNoRow() {
  182. fmt.Println("QueryRow Err:", err.Error())
  183. return err
  184. }
  185. if tmpChartEdbMapping != nil {
  186. chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(tmpChartEdbMapping.ChartEdbMappingId))
  187. tmpChartEdbMapping.ModifyTime = time.Now()
  188. //tmpChartEdbMapping.MaxData = v.MaxData
  189. //tmpChartEdbMapping.MinData = v.MinData
  190. //tmpChartEdbMapping.IsOrder = v.IsOrder
  191. tmpChartEdbMapping.EdbAliasName = v.EdbAliasName
  192. tmpChartEdbMapping.IsAxis = v.IsAxis
  193. //tmpChartEdbMapping.EdbInfoType = v.EdbInfoType
  194. //tmpChartEdbMapping.LeadValue = v.LeadValue
  195. //tmpChartEdbMapping.LeadUnit = v.LeadUnit
  196. //tmpChartEdbMapping.ChartStyle = v.ChartStyle
  197. //tmpChartEdbMapping.ChartColor = v.ChartColor
  198. //tmpChartEdbMapping.PredictChartColor = v.PredictChartColor
  199. //tmpChartEdbMapping.ChartWidth = v.ChartWidth
  200. _, err = to.Update(tmpChartEdbMapping, "ModifyTime", "IsAxis", "EdbAliasName")
  201. if err != nil {
  202. fmt.Println("chart_edb_mapping Err:" + err.Error())
  203. return err
  204. }
  205. } else {
  206. mapItem := new(ChartEdbMapping)
  207. mapItem.ChartInfoId = req.ChartInfoId
  208. mapItem.EdbInfoId = v.EdbInfoId
  209. mapItem.EdbAliasName = v.EdbAliasName
  210. mapItem.IsAxis = v.IsAxis
  211. mapItem.CreateTime = time.Now()
  212. mapItem.ModifyTime = time.Now()
  213. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  214. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp + "_" + strconv.Itoa(v.EdbInfoId))
  215. //mapItem.MaxData = v.MaxData
  216. //mapItem.MinData = v.MinData
  217. mapItem.IsOrder = true
  218. mapItem.EdbInfoType = 1
  219. //mapItem.LeadValue = v.LeadValue
  220. //mapItem.LeadUnit = v.LeadUnit
  221. //mapItem.ChartStyle = v.ChartStyle
  222. //mapItem.ChartColor = v.ChartColor
  223. //mapItem.PredictChartColor = v.PredictChartColor
  224. //mapItem.ChartWidth = v.ChartWidth
  225. mapItem.Source = utils.CHART_SOURCE_RANGE_ANALYSIS
  226. tmpId, err := to.Insert(mapItem)
  227. if err != nil {
  228. fmt.Println("AddChartEdbMapping Err:" + err.Error())
  229. return err
  230. }
  231. mapItem.ChartEdbMappingId = int(tmpId)
  232. chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(mapItem.ChartEdbMappingId))
  233. }
  234. }
  235. if len(chartEdbMappingIdList) > 0 {
  236. chartEdbMappingIdStr := strings.Join(chartEdbMappingIdList, ",")
  237. dsql := `DELETE FROM chart_edb_mapping WHERE chart_info_id=? AND chart_edb_mapping_id NOT IN(` + chartEdbMappingIdStr + `)`
  238. _, err = to.Raw(dsql, req.ChartInfoId).Exec()
  239. if err != nil {
  240. fmt.Println("delete err:" + err.Error())
  241. return err
  242. }
  243. }
  244. return
  245. }
  246. // SaveChartRangeAnalysisEdbReq 指标保存请求
  247. type SaveChartRangeAnalysisEdbReq struct {
  248. EdbInfoList []*EdbInfoBase `description:"指标列表"`
  249. ExtraConfig ChartRangeAnalysisExtraConf
  250. MultipleGraphConfigId int `description:"配置id"`
  251. IsSaveAs bool `description:"是否另存为,true的话,就是另存为,不会建立与配置的关系"`
  252. }