edb_data_calculate_bp.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 AddCalculateBp(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("AddCalculateBp,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_BP
  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. newEdbInfoId, tmpErr := to.Insert(edbInfo)
  44. if tmpErr != nil {
  45. return edbInfoId, tmpErr
  46. }
  47. edbInfoId = int(newEdbInfoId)
  48. //关联关系
  49. {
  50. calculateMappingItem := new(EdbInfoCalculateMapping)
  51. calculateMappingItem.CreateTime = time.Now()
  52. calculateMappingItem.ModifyTime = time.Now()
  53. calculateMappingItem.Sort = 1
  54. calculateMappingItem.EdbCode = edbCode
  55. calculateMappingItem.EdbInfoId = edbInfoId
  56. calculateMappingItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  57. calculateMappingItem.FromEdbCode = fromEdbInfo.EdbCode
  58. calculateMappingItem.FromEdbName = fromEdbInfo.EdbName
  59. calculateMappingItem.FromSource = fromEdbInfo.Source
  60. calculateMappingItem.FromSourceName = fromEdbInfo.SourceName
  61. calculateMappingItem.FromTag = ""
  62. calculateMappingItem.Source = edbInfo.Source
  63. calculateMappingItem.SourceName = edbInfo.SourceName
  64. _, err = to.Insert(calculateMappingItem)
  65. if err != nil {
  66. return
  67. }
  68. }
  69. } else {
  70. edbInfoId = req.EdbInfoId
  71. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_CALCULATE_BP)
  72. fmt.Println("dataTableName:" + dataTableName)
  73. deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
  74. deleteSql = fmt.Sprintf(deleteSql, dataTableName)
  75. _, err = to.Raw(deleteSql, req.EdbInfoId).Exec()
  76. if err != nil {
  77. return
  78. }
  79. }
  80. edbInfoIdStr := strconv.Itoa(edbInfoId)
  81. fmt.Println("edbInfoIdStr:" + edbInfoIdStr)
  82. //计算数据
  83. var condition string
  84. var pars []interface{}
  85. condition += " AND edb_info_id=? "
  86. if req.EdbInfoId <= 0 {
  87. pars = append(pars, req.FromEdbInfoId)
  88. } else {
  89. pars = append(pars, fromEdbInfo.EdbInfoId)
  90. }
  91. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 0)
  92. if err != nil {
  93. return edbInfoId, err
  94. }
  95. addSql := ` INSERT INTO edb_data_calculate_bp(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  96. var isAdd bool
  97. existMap := make(map[string]string)
  98. dataLen := len(dataList)
  99. fmt.Println("dataLen:", dataLen)
  100. for i := 0; i < dataLen; i++ {
  101. //当期
  102. currentItem := dataList[i]
  103. currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime)
  104. var day int
  105. var preItem *EdbInfoSearchData
  106. var preDate time.Time
  107. if i == 0 {
  108. day = int(time.Now().Sub(currentDate).Hours() / float64(24))
  109. preDate = time.Now()
  110. } else {
  111. j := i - 1
  112. if j < dataLen {
  113. preItem = dataList[j]
  114. preDate, _ = time.Parse(utils.FormatDate, preItem.DataTime)
  115. day = int(preDate.Sub(currentDate).Hours() / float64(24))
  116. utils.FileLog.Info("preItem.DataTime:" + preItem.DataTime + ";currentItem.DataTime" + currentItem.DataTime)
  117. }
  118. }
  119. for k := 0; k <= day; k++ {
  120. needDay := preDate.AddDate(0, 0, -k)
  121. needDayStr := needDay.Format(utils.FormatDate)
  122. existKey := edbCode + needDayStr
  123. if _, ok := existMap[existKey]; !ok {
  124. timestamp := needDay.UnixNano() / 1e6
  125. timestampStr := fmt.Sprintf("%d", timestamp)
  126. valStr := decimal.NewFromFloat(currentItem.Value).String()
  127. addSql += GetAddSql(edbInfoIdStr, edbCode, needDayStr, timestampStr, valStr)
  128. isAdd = true
  129. }
  130. existMap[existKey] = needDayStr
  131. }
  132. existKey := edbCode + currentItem.DataTime
  133. if _, ok := existMap[existKey]; !ok {
  134. currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime)
  135. timestamp := currentDate.UnixNano() / 1e6
  136. timestampStr := fmt.Sprintf("%d", timestamp)
  137. valStr := decimal.NewFromFloat(currentItem.Value).String()
  138. addSql += GetAddSql(edbInfoIdStr, edbCode, currentItem.DataTime, timestampStr, valStr)
  139. isAdd = true
  140. }
  141. existMap[existKey] = currentItem.DataTime
  142. }
  143. if isAdd {
  144. addSql = strings.TrimRight(addSql, ",")
  145. _, err = to.Raw(addSql).Exec()
  146. if err != nil {
  147. return edbInfoId, err
  148. }
  149. }
  150. return
  151. }