edb_data_calculate_time_shift.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "github.com/shopspring/decimal"
  6. "hongze/hongze_edb_lib/utils"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. //时间移位
  12. func AddCalculateTimeShift(req *EdbInfoCalculateBatchSaveReq, fromEdbInfo *EdbInfo, edbCode, uniqueCode string, sysUserId int, sysUserRealName string) (edbInfoId int, err error) {
  13. o := orm.NewOrm()
  14. to, err := o.Begin()
  15. if err != nil {
  16. return
  17. }
  18. defer func() {
  19. if err != nil {
  20. fmt.Println("AddCalculateTimeShift,Err:" + err.Error())
  21. _ = to.Rollback()
  22. } else {
  23. _ = to.Commit()
  24. }
  25. }()
  26. if req.EdbInfoId <= 0 {
  27. edbInfo := new(EdbInfo)
  28. edbInfo.Source = utils.DATA_SOURCE_CALCULATE_TIME_SHIFT
  29. edbInfo.SourceName = "时间移位"
  30. edbInfo.EdbCode = edbCode
  31. edbInfo.EdbName = req.EdbName
  32. edbInfo.EdbNameSource = req.EdbName
  33. edbInfo.Frequency = req.Frequency
  34. edbInfo.Unit = req.Unit
  35. edbInfo.ClassifyId = req.ClassifyId
  36. edbInfo.SysUserId = sysUserId
  37. edbInfo.SysUserRealName = sysUserRealName
  38. edbInfo.CreateTime = time.Now()
  39. edbInfo.ModifyTime = time.Now()
  40. edbInfo.UniqueCode = uniqueCode
  41. edbInfo.CalculateFormula = req.Formula
  42. edbInfo.EdbType = 2
  43. edbInfo.MoveType = req.MoveType
  44. edbInfo.MoveFrequency = req.MoveFrequency
  45. newEdbInfoId, tmpErr := o.Insert(edbInfo)
  46. if tmpErr != nil {
  47. return edbInfoId, tmpErr
  48. }
  49. edbInfoId = int(newEdbInfoId)
  50. //关联关系
  51. {
  52. calculateMappingItem := new(EdbInfoCalculateMapping)
  53. calculateMappingItem.CreateTime = time.Now()
  54. calculateMappingItem.ModifyTime = time.Now()
  55. calculateMappingItem.Sort = 1
  56. calculateMappingItem.EdbCode = edbCode
  57. calculateMappingItem.EdbInfoId = edbInfoId
  58. calculateMappingItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  59. calculateMappingItem.FromEdbCode = fromEdbInfo.EdbCode
  60. calculateMappingItem.FromEdbName = fromEdbInfo.EdbName
  61. calculateMappingItem.FromSource = fromEdbInfo.Source
  62. calculateMappingItem.FromSourceName = fromEdbInfo.SourceName
  63. calculateMappingItem.FromTag = ""
  64. calculateMappingItem.Source = edbInfo.Source
  65. calculateMappingItem.SourceName = edbInfo.SourceName
  66. _, err = to.Insert(calculateMappingItem)
  67. if err != nil {
  68. return
  69. }
  70. }
  71. } else {
  72. edbInfoId = req.EdbInfoId
  73. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_CALCULATE_TIME_SHIFT)
  74. fmt.Println("dataTableName:" + dataTableName)
  75. deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
  76. deleteSql = fmt.Sprintf(deleteSql, dataTableName)
  77. _, err = to.Raw(deleteSql, req.EdbInfoId).Exec()
  78. if err != nil {
  79. return 0, err
  80. }
  81. }
  82. var shiftDay int
  83. formulaInt, _ := strconv.Atoi(req.Formula)
  84. switch req.MoveFrequency {
  85. case "天":
  86. shiftDay = formulaInt
  87. case "周":
  88. shiftDay = formulaInt * 7
  89. case "月":
  90. shiftDay = formulaInt * 30
  91. case "季":
  92. shiftDay = formulaInt * 90
  93. case "年":
  94. shiftDay = formulaInt * 365
  95. default:
  96. shiftDay = formulaInt
  97. }
  98. edbInfoIdStr := strconv.Itoa(edbInfoId)
  99. //计算数据
  100. var condition string
  101. var pars []interface{}
  102. condition += " AND edb_info_id=? "
  103. if req.EdbInfoId <= 0 {
  104. pars = append(pars, req.FromEdbInfoId)
  105. } else {
  106. pars = append(pars, fromEdbInfo.EdbInfoId)
  107. }
  108. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 0)
  109. if err != nil {
  110. return edbInfoId, err
  111. }
  112. addSql := ` INSERT INTO edb_data_calculate_time_shift(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  113. var isAdd bool
  114. existMap := make(map[string]string)
  115. dataLen := len(dataList)
  116. if req.MoveType == 2 {
  117. shiftDay = -shiftDay
  118. }
  119. for i := 0; i < dataLen; i++ {
  120. //当期
  121. currentItem := dataList[i]
  122. currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime)
  123. existKey := edbCode + currentItem.DataTime
  124. if _, ok := existMap[existKey]; !ok {
  125. newDate := currentDate.AddDate(0, 0, shiftDay)
  126. timestamp := newDate.UnixNano() / 1e6
  127. timestampStr := fmt.Sprintf("%d", timestamp)
  128. valStr := decimal.NewFromFloat(currentItem.Value).String()
  129. addSql += GetAddSql(edbInfoIdStr, edbCode, newDate.Format(utils.FormatDate), timestampStr, valStr)
  130. isAdd = true
  131. }
  132. existMap[existKey] = currentItem.DataTime
  133. }
  134. if isAdd {
  135. addSql = strings.TrimRight(addSql, ",")
  136. _, err = to.Raw(addSql).Exec()
  137. if err != nil {
  138. return edbInfoId, err
  139. }
  140. }
  141. return
  142. }