line_feature.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package line_feature
  2. import (
  3. "eta/eta_mobile/models/data_manage"
  4. "eta/eta_mobile/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. type BatchAddChart struct {
  12. ChartInfo *data_manage.ChartInfo
  13. EdbMappingList []*data_manage.ChartEdbMapping
  14. MultipleGraphConfigChartMapping *data_manage.MultipleGraphConfigChartMapping
  15. }
  16. func CreateLineFeatureChartAndEdb(chartInfo *data_manage.ChartInfo, edbMappingList []*data_manage.ChartEdbMapping) (chartInfoId int, err error) {
  17. o := orm.NewOrmUsingDB("data")
  18. tx, err := o.Begin()
  19. if err != nil {
  20. return
  21. }
  22. defer func() {
  23. if err != nil {
  24. _ = tx.Rollback()
  25. } else {
  26. _ = tx.Commit()
  27. }
  28. }()
  29. // 新增图表信息
  30. newId, err := tx.Insert(chartInfo)
  31. if err != nil {
  32. return
  33. }
  34. // 指标mapping
  35. chartInfo.ChartInfoId = int(newId)
  36. chartInfoId = int(newId)
  37. if len(edbMappingList) > 0 {
  38. for i := range edbMappingList {
  39. edbMappingList[i].ChartInfoId = chartInfoId
  40. }
  41. _, err = tx.InsertMulti(len(edbMappingList), edbMappingList)
  42. if err != nil {
  43. return
  44. }
  45. }
  46. return
  47. }
  48. // EditLineFeatureChartAndMapping 批量新增/编辑拟合方程图表
  49. func EditLineFeatureChartAndMapping(req *data_manage.EditChartInfoReq, edbInfoIdStr string, calendar string, dateType, disabled int, extraConfig string) (err error) {
  50. o := orm.NewOrmUsingDB("data")
  51. to, err := o.Begin()
  52. if err != nil {
  53. return
  54. }
  55. defer func() {
  56. if err != nil {
  57. _ = to.Rollback()
  58. } else {
  59. _ = to.Commit()
  60. }
  61. }()
  62. var pars []interface{}
  63. pars = append(pars, req.ChartName)
  64. pars = append(pars, edbInfoIdStr)
  65. pars = append(pars, req.ChartClassifyId)
  66. pars = append(pars, disabled)
  67. pars = append(pars, extraConfig)
  68. sql := ` UPDATE chart_info
  69. SET
  70. chart_name =?,
  71. edb_info_ids=?,
  72. chart_classify_id = ?,
  73. modify_time = NOW(),
  74. disabled = ?,
  75. extra_config = ?
  76. `
  77. if calendar != "" {
  78. sql += `,calendar = ? `
  79. pars = append(pars, calendar)
  80. }
  81. if dateType > 0 {
  82. sql += `,date_type = ? `
  83. pars = append(pars, dateType)
  84. }
  85. sql += `,start_date = ? `
  86. pars = append(pars, req.StartDate)
  87. sql += `,end_date = ? `
  88. pars = append(pars, req.EndDate)
  89. sql += `,season_start_date = ? `
  90. pars = append(pars, req.SeasonStartDate)
  91. sql += `,season_end_date = ? `
  92. pars = append(pars, req.SeasonEndDate)
  93. sql += `,left_min = ? `
  94. pars = append(pars, req.LeftMin)
  95. sql += `,left_max = ? `
  96. pars = append(pars, req.LeftMax)
  97. sql += `,right_min = ? `
  98. pars = append(pars, req.RightMin)
  99. sql += `,right_max = ? `
  100. pars = append(pars, req.RightMax)
  101. sql += `WHERE chart_info_id = ?`
  102. pars = append(pars, req.ChartInfoId)
  103. _, err = to.Raw(sql, pars).Exec()
  104. if err != nil {
  105. fmt.Println("UPDATE chart_info Err:", err.Error())
  106. return err
  107. }
  108. chartEdbMappingIdList := make([]string, 0)
  109. for _, v := range req.CorrelationChartInfo.EdbInfoIdList {
  110. // 查询该指标是否存在,如果存在的话,那么就去修改,否则新增
  111. var tmpChartEdbMapping *data_manage.ChartEdbMapping
  112. csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id=? AND source = ? `
  113. err = to.Raw(csql, req.ChartInfoId, v.EdbInfoId, utils.CHART_SOURCE_CORRELATION).QueryRow(&tmpChartEdbMapping)
  114. if err != nil && err.Error() != utils.ErrNoRow() {
  115. fmt.Println("QueryRow Err:", err.Error())
  116. return err
  117. }
  118. if tmpChartEdbMapping != nil {
  119. chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(tmpChartEdbMapping.ChartEdbMappingId))
  120. //tmpChartEdbMapping.ModifyTime = time.Now()
  121. //tmpChartEdbMapping.MaxData = v.MaxData
  122. //tmpChartEdbMapping.MinData = v.MinData
  123. //tmpChartEdbMapping.IsOrder = v.IsOrder
  124. //tmpChartEdbMapping.IsAxis = v.IsAxis
  125. //tmpChartEdbMapping.EdbInfoType = v.EdbInfoType
  126. //tmpChartEdbMapping.LeadValue = v.LeadValue
  127. //tmpChartEdbMapping.LeadUnit = v.LeadUnit
  128. //tmpChartEdbMapping.ChartStyle = v.ChartStyle
  129. //tmpChartEdbMapping.ChartColor = v.ChartColor
  130. //tmpChartEdbMapping.PredictChartColor = v.PredictChartColor
  131. //tmpChartEdbMapping.ChartWidth = v.ChartWidth
  132. //_, err = to.Update(tmpChartEdbMapping, "ModifyTime", "MaxData", "MinData", "IsOrder", "IsAxis", "EdbInfoType", "LeadValue", "LeadUnit", "ChartStyle", "ChartColor", "PredictChartColor", "ChartWidth")
  133. //if err != nil {
  134. // fmt.Println("chart_edb_mapping Err:" + err.Error())
  135. // return err
  136. //}
  137. } else {
  138. mapItem := new(data_manage.ChartEdbMapping)
  139. mapItem.ChartInfoId = req.ChartInfoId
  140. mapItem.EdbInfoId = v.EdbInfoId
  141. mapItem.CreateTime = time.Now()
  142. mapItem.ModifyTime = time.Now()
  143. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  144. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp + "_" + strconv.Itoa(v.EdbInfoId))
  145. //mapItem.MaxData = v.MaxData
  146. //mapItem.MinData = v.MinData
  147. mapItem.IsOrder = true
  148. mapItem.IsAxis = 1
  149. mapItem.EdbInfoType = 1
  150. //mapItem.LeadValue = v.LeadValue
  151. //mapItem.LeadUnit = v.LeadUnit
  152. //mapItem.ChartStyle = v.ChartStyle
  153. //mapItem.ChartColor = v.ChartColor
  154. //mapItem.PredictChartColor = v.PredictChartColor
  155. //mapItem.ChartWidth = v.ChartWidth
  156. mapItem.Source = utils.CHART_SOURCE_CORRELATION
  157. tmpId, err := to.Insert(mapItem)
  158. if err != nil {
  159. fmt.Println("AddChartEdbMapping Err:" + err.Error())
  160. return err
  161. }
  162. mapItem.ChartEdbMappingId = int(tmpId)
  163. chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(mapItem.ChartEdbMappingId))
  164. }
  165. }
  166. if len(chartEdbMappingIdList) > 0 {
  167. chartEdbMappingIdStr := strings.Join(chartEdbMappingIdList, ",")
  168. dsql := `DELETE FROM chart_edb_mapping WHERE chart_info_id=? AND chart_edb_mapping_id NOT IN(` + chartEdbMappingIdStr + `)`
  169. _, err = to.Raw(dsql, req.ChartInfoId).Exec()
  170. if err != nil {
  171. fmt.Println("delete err:" + err.Error())
  172. return err
  173. }
  174. }
  175. return
  176. }