edb_data_calculate_hcz.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 AddCalculateHcz(req *EdbInfoCalculateBatchSaveReq, fromEdbInfo *EdbInfo, edbCode, uniqueCode string, sysUserId int, sysUserRealName string, formulaInt int) (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("AddCalculateHcz,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_HCZ
  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_HCZ)
  72. deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
  73. deleteSql = fmt.Sprintf(deleteSql, dataTableName)
  74. _, err = to.Raw(deleteSql, req.EdbInfoId).Exec()
  75. if err != nil {
  76. return
  77. }
  78. }
  79. edbInfoIdStr := strconv.Itoa(edbInfoId)
  80. fmt.Println("edbInfoIdStr:" + edbInfoIdStr)
  81. //计算数据
  82. var condition string
  83. var pars []interface{}
  84. condition += " AND edb_info_id=? "
  85. if req.EdbInfoId <= 0 {
  86. pars = append(pars, req.FromEdbInfoId)
  87. } else {
  88. pars = append(pars, fromEdbInfo.EdbInfoId)
  89. }
  90. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 0)
  91. if err != nil {
  92. return edbInfoId, err
  93. }
  94. addSql := ` INSERT INTO edb_data_calculate_hcz(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  95. var isAdd bool
  96. existMap := make(map[string]string)
  97. dataLen := len(dataList)
  98. fmt.Println("dataLen:", dataLen)
  99. for i := 0; i < dataLen; i++ {
  100. j := i + formulaInt
  101. if j < dataLen {
  102. //当期
  103. currentItem := dataList[i]
  104. preItem := dataList[j]
  105. if currentItem != nil && preItem != nil {
  106. existKey := edbCode + currentItem.DataTime
  107. if _, ok := existMap[existKey]; !ok {
  108. currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime)
  109. timestamp := currentDate.UnixNano() / 1e6
  110. timestampStr := fmt.Sprintf("%d", timestamp)
  111. val := ""
  112. if preItem.Value == 0 {
  113. val = "0"
  114. } else {
  115. val = HczDiv(currentItem.Value, preItem.Value)
  116. }
  117. if val != "" {
  118. addSql += GetAddSql(edbInfoIdStr, edbCode, currentItem.DataTime, timestampStr, val)
  119. isAdd = true
  120. }
  121. }
  122. existMap[existKey] = currentItem.DataTime
  123. }
  124. }
  125. }
  126. if isAdd {
  127. addSql = strings.TrimRight(addSql, ",")
  128. _, err = to.Raw(addSql).Exec()
  129. if err != nil {
  130. return edbInfoId, err
  131. }
  132. }
  133. return
  134. }
  135. // HczDiv 环差值计算,current:当期,pre:上期 公式:当期-上期
  136. func HczDiv(current, pre float64) string {
  137. if pre == 0 {
  138. return ""
  139. }
  140. currentVal := decimal.NewFromFloat(current)
  141. preVal := decimal.NewFromFloat(pre)
  142. val, _ := currentVal.Sub(preVal).Float64()
  143. valStr := utils.SubFloatToString(val, 4)
  144. return valStr
  145. }