edb_data_calculate_time_shift.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package data_manage
  2. import (
  3. "errors"
  4. "eta/eta_mobile/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "github.com/shopspring/decimal"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // 时间移位
  13. func EditCalculateTimeShift(req *EdbInfoCalculateBatchEditReq, fromEdbInfo *EdbInfo, edbCode string, oldEdbInfo *EdbInfo) (edbInfoId int, err error) {
  14. edbInfoId = req.EdbInfoId
  15. o := orm.NewOrmUsingDB("data")
  16. to, err := o.Begin()
  17. if err != nil {
  18. return
  19. }
  20. defer func() {
  21. if err != nil {
  22. _ = to.Rollback()
  23. } else {
  24. _ = to.Commit()
  25. }
  26. }()
  27. //修改指标信息
  28. sql := ` UPDATE edb_info
  29. SET
  30. edb_name =?,
  31. edb_name_source=?,
  32. frequency = ?,
  33. unit = ?,
  34. classify_id = ?,
  35. move_type=?,
  36. move_frequency=?,
  37. calculate_formula=?,
  38. modify_time = NOW()
  39. WHERE edb_info_id = ? `
  40. _, err = to.Raw(sql, req.EdbName, req.EdbName, req.Frequency, req.Unit, req.ClassifyId, req.MoveType, req.MoveFrequency, req.Formula, edbInfoId).Exec()
  41. if err != nil {
  42. return
  43. }
  44. var existCondition string
  45. var existPars []interface{}
  46. existCondition += " AND edb_info_id=? "
  47. existPars = append(existPars, edbInfoId)
  48. existCondition += " AND from_edb_info_id=? "
  49. existPars = append(existPars, req.FromEdbInfoId)
  50. //判断计算指标是否被更换
  51. count, err := GetEdbInfoCalculateCountByCondition(req.Source, existCondition, existPars)
  52. if err != nil {
  53. err = errors.New("判断指标是否改变失败,Err:" + err.Error())
  54. return
  55. }
  56. if count <= 0 || req.MoveType != oldEdbInfo.MoveType || req.MoveFrequency != oldEdbInfo.MoveFrequency || req.Formula != oldEdbInfo.CalculateFormula {
  57. if count <= 0 {
  58. //删除,计算指标关联的,基础指标的关联关系
  59. sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? `
  60. _, err = to.Raw(sql, edbInfoId).Exec()
  61. if err != nil {
  62. err = errors.New("删除计算指标关联关系失败,Err:" + err.Error())
  63. return
  64. }
  65. //关联关系
  66. {
  67. calculateMappingItem := new(EdbInfoCalculateMapping)
  68. calculateMappingItem.CreateTime = time.Now()
  69. calculateMappingItem.ModifyTime = time.Now()
  70. calculateMappingItem.Sort = 1
  71. calculateMappingItem.EdbCode = edbCode
  72. calculateMappingItem.EdbInfoId = edbInfoId
  73. calculateMappingItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  74. calculateMappingItem.FromEdbCode = fromEdbInfo.EdbCode
  75. calculateMappingItem.FromEdbName = fromEdbInfo.EdbName
  76. calculateMappingItem.FromSource = fromEdbInfo.Source
  77. calculateMappingItem.FromSourceName = fromEdbInfo.SourceName
  78. calculateMappingItem.FromTag = ""
  79. calculateMappingItem.Source = utils.DATA_SOURCE_CALCULATE_TIME_SHIFT
  80. calculateMappingItem.SourceName = "时间移位"
  81. _, err = to.Insert(calculateMappingItem)
  82. if err != nil {
  83. return
  84. }
  85. }
  86. }
  87. //清空原有数据
  88. sql = ` DELETE FROM edb_data_calculate_time_shift WHERE edb_info_id = ? `
  89. _, err = to.Raw(sql, edbInfoId).Exec()
  90. if err != nil {
  91. return edbInfoId, err
  92. }
  93. edbInfoIdStr := strconv.Itoa(edbInfoId)
  94. var shiftDay int
  95. formulaInt, _ := strconv.Atoi(req.Formula)
  96. switch req.MoveFrequency {
  97. case "天":
  98. shiftDay = formulaInt
  99. case "周":
  100. shiftDay = formulaInt * 7
  101. case "月":
  102. shiftDay = formulaInt * 30
  103. case "季":
  104. shiftDay = formulaInt * 90
  105. case "年":
  106. shiftDay = formulaInt * 365
  107. default:
  108. shiftDay = formulaInt
  109. }
  110. //计算数据
  111. var condition string
  112. var pars []interface{}
  113. condition += " AND edb_info_id=? "
  114. pars = append(pars, req.FromEdbInfoId)
  115. fmt.Println("EdbInfoId:", req.FromEdbInfoId)
  116. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 0)
  117. if err != nil {
  118. return edbInfoId, err
  119. }
  120. addSql := ` INSERT INTO edb_data_calculate_time_shift(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  121. var isAdd bool
  122. existMap := make(map[string]string)
  123. dataLen := len(dataList)
  124. if req.MoveType == 2 {
  125. shiftDay = -shiftDay
  126. }
  127. for i := 0; i < dataLen; i++ {
  128. //当期
  129. currentItem := dataList[i]
  130. currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime)
  131. newDate := currentDate.AddDate(0, 0, shiftDay)
  132. existKey := edbCode + newDate.Format(utils.FormatDate)
  133. if _, ok := existMap[existKey]; !ok {
  134. timestamp := newDate.UnixNano() / 1e6
  135. timestampStr := fmt.Sprintf("%d", timestamp)
  136. valStr := decimal.NewFromFloat(currentItem.Value).String()
  137. addSql += GetAddSql(edbInfoIdStr, edbCode, newDate.Format(utils.FormatDate), timestampStr, valStr)
  138. isAdd = true
  139. }
  140. existMap[existKey] = currentItem.DataTime
  141. }
  142. if isAdd {
  143. addSql = strings.TrimRight(addSql, ",")
  144. _, err = to.Raw(addSql).Exec()
  145. if err != nil {
  146. return edbInfoId, err
  147. }
  148. }
  149. }
  150. return
  151. }