edb_info_calculate_nszydpjjs.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. // N数值移动平均计算
  12. func AddCalculateNszydpjjs(req *EdbInfoCalculateBatchSaveReq, fromEdbInfo *EdbInfo, edbCode, uniqueCode string, sysUserId int, sysUserRealName string, formulaInt int) (edbInfoId int, err error) {
  13. o := orm.NewOrmUsingDB("data")
  14. to, err := o.Begin()
  15. if err != nil {
  16. return
  17. }
  18. defer func() {
  19. if err != nil {
  20. _ = to.Rollback()
  21. } else {
  22. _ = to.Commit()
  23. }
  24. }()
  25. if req.EdbInfoId <= 0 {
  26. edbInfo := new(EdbInfo)
  27. edbInfo.Source = utils.DATA_SOURCE_CALCULATE_NSZYDPJJS
  28. edbInfo.SourceName = "N数值移动平均计算"
  29. edbInfo.EdbCode = edbCode
  30. edbInfo.EdbName = req.EdbName
  31. edbInfo.EdbNameSource = req.EdbName
  32. edbInfo.Frequency = req.Frequency
  33. edbInfo.Unit = req.Unit
  34. edbInfo.ClassifyId = req.ClassifyId
  35. edbInfo.SysUserId = sysUserId
  36. edbInfo.SysUserRealName = sysUserRealName
  37. edbInfo.CreateTime = time.Now()
  38. edbInfo.ModifyTime = time.Now()
  39. edbInfo.UniqueCode = uniqueCode
  40. edbInfo.CalculateFormula = req.Formula
  41. edbInfo.EdbType = 2
  42. newEdbInfoId, tmpErr := to.Insert(edbInfo)
  43. if tmpErr != nil {
  44. return edbInfoId, tmpErr
  45. }
  46. edbInfoId = int(newEdbInfoId)
  47. //calculateItem := new(EdbInfoCalculateNszydpjjs)
  48. //calculateItem.CreateTime = time.Now()
  49. //calculateItem.ModifyTime = time.Now()
  50. //calculateItem.Sort = 1
  51. //calculateItem.EdbCode = edbCode
  52. //calculateItem.EdbInfoId = edbInfoId
  53. //calculateItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  54. //calculateItem.FromEdbCode = fromEdbInfo.EdbCode
  55. //calculateItem.FromEdbName = fromEdbInfo.EdbName
  56. //calculateItem.FromSource = fromEdbInfo.Source
  57. //calculateItem.FromSourceName = fromEdbInfo.SourceName
  58. //
  59. //_, err = to.Insert(calculateItem)
  60. //if err != nil {
  61. // return edbInfoId, err
  62. //}
  63. //关联关系
  64. {
  65. calculateMappingItem := new(EdbInfoCalculateMapping)
  66. calculateMappingItem.CreateTime = time.Now()
  67. calculateMappingItem.ModifyTime = time.Now()
  68. calculateMappingItem.Sort = 1
  69. calculateMappingItem.EdbCode = edbCode
  70. calculateMappingItem.EdbInfoId = edbInfoId
  71. calculateMappingItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  72. calculateMappingItem.FromEdbCode = fromEdbInfo.EdbCode
  73. calculateMappingItem.FromEdbName = fromEdbInfo.EdbName
  74. calculateMappingItem.FromSource = fromEdbInfo.Source
  75. calculateMappingItem.FromSourceName = fromEdbInfo.SourceName
  76. calculateMappingItem.FromTag = ""
  77. calculateMappingItem.Source = edbInfo.Source
  78. calculateMappingItem.SourceName = edbInfo.SourceName
  79. _, err = to.Insert(calculateMappingItem)
  80. if err != nil {
  81. return
  82. }
  83. }
  84. } else {
  85. edbInfoId = req.EdbInfoId
  86. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_CALCULATE_NSZYDPJJS, utils.DATA_SUB_SOURCE_EDB)
  87. deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
  88. deleteSql = fmt.Sprintf(deleteSql, dataTableName)
  89. _, err = to.Raw(deleteSql, req.EdbInfoId).Exec()
  90. }
  91. edbInfoIdStr := strconv.Itoa(edbInfoId)
  92. //计算数据
  93. var condition string
  94. var pars []interface{}
  95. condition += " AND edb_info_id=? "
  96. if req.EdbInfoId <= 0 {
  97. pars = append(pars, req.FromEdbInfoId)
  98. } else {
  99. pars = append(pars, fromEdbInfo.EdbInfoId)
  100. }
  101. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, fromEdbInfo.SubSource, 0)
  102. if err != nil {
  103. return edbInfoId, err
  104. }
  105. var dateArr []string
  106. dataMap := make(map[string]*EdbInfoSearchData)
  107. for _, v := range dataList {
  108. dateArr = append(dateArr, v.DataTime)
  109. dataMap[v.DataTime] = v
  110. }
  111. addSql := ` INSERT INTO edb_data_calculate_nszydpjjs(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  112. var isAdd bool
  113. arrLen := len(dateArr)
  114. for ak, av := range dateArr {
  115. //处理第一个值
  116. var valArr []float64
  117. if findItem, ok := dataMap[av]; ok {
  118. valArr = append(valArr, findItem.Value)
  119. } else {
  120. continue
  121. }
  122. if ak+1 != arrLen {
  123. //处理除第一个值之外的N-1个值
  124. for i := 1; i < formulaInt; i++ {
  125. arrIndex := ak + i
  126. if arrIndex >= arrLen {
  127. break
  128. }
  129. arrVal := dateArr[arrIndex]
  130. if findItem, ok := dataMap[arrVal]; ok {
  131. valArr = append(valArr, findItem.Value)
  132. } else {
  133. continue
  134. }
  135. }
  136. }
  137. valArrLen := len(valArr)
  138. totalVal := decimal.NewFromFloat(0.00)
  139. for _, v := range valArr {
  140. newDecimal := decimal.NewFromFloat(v)
  141. totalVal = totalVal.Add(newDecimal)
  142. }
  143. af := totalVal //decimal.NewFromFloat(totalVal)
  144. bf := decimal.NewFromFloat(float64(valArrLen))
  145. val, _ := af.Div(bf).Float64()
  146. currentDate, err := time.Parse(utils.FormatDate, av)
  147. if err != nil {
  148. return edbInfoId, err
  149. }
  150. timestamp := currentDate.UnixNano() / 1e6
  151. timestampStr := fmt.Sprintf("%d", timestamp)
  152. valStr := utils.SubFloatToString(val, 4)
  153. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, valStr)
  154. isAdd = true
  155. }
  156. if isAdd {
  157. addSql = strings.TrimRight(addSql, ",")
  158. _, err = to.Raw(addSql).Exec()
  159. if err != nil {
  160. return edbInfoId, err
  161. }
  162. }
  163. return
  164. }