edb_info_calculate_hcz.go 4.3 KB

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