chart_info_range_analysis.go 10 KB

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