edb_info_calculate_nszydpjjs.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package data_manage
  2. import (
  3. "fmt"
  4. "github.com/shopspring/decimal"
  5. "hongze/hongze_task/utils"
  6. "github.com/beego/beego/v2/client/orm"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. type EdbInfoCalculateNszydpjjs struct {
  12. EdbInfoCalculateNszydpjjsId int `orm:"column(edb_info_calculate_nszydpjjs_id);pk"`
  13. EdbInfoId int `description:"指标id"`
  14. EdbCode string `description:"指标编码"`
  15. FromEdbInfoId int `description:"计算指标id"`
  16. FromEdbCode string `description:"计算指标编码"`
  17. FromEdbName string `description:"计算指标名称"`
  18. FromSource int `description:"计算指标来源"`
  19. FromSourceName string `description:"计算指标来源名称"`
  20. FromTag string `description:"来源指标标签"`
  21. Sort int `description:"计算指标名称排序"`
  22. CreateTime time.Time `description:"创建时间"`
  23. ModifyTime time.Time `description:"修改时间"`
  24. }
  25. //刷新 N数值移动平均计算
  26. func RefreshCalculateNszydpjjs(edbInfoId, source, formulaInt int, fromEdbInfo *EdbInfo, edbCode string) (err error) {
  27. o := orm.NewOrmUsingDB("data")
  28. tx,err:=o.Begin()
  29. if err!=nil {
  30. return err
  31. }
  32. defer func() {
  33. if err != nil {
  34. tx.Rollback()
  35. } else {
  36. tx.Commit()
  37. }
  38. }()
  39. if err != nil {
  40. return
  41. }
  42. edbInfoIdStr := strconv.Itoa(edbInfoId)
  43. //计算数据
  44. var condition string
  45. var pars []interface{}
  46. condition += " AND edb_info_id=? "
  47. pars = append(pars, edbInfoId)
  48. dataList, err := GetEdbDataListAllV1(condition, pars, source, 0)
  49. if err != nil {
  50. return err
  51. }
  52. existDataMap := make(map[string]*EdbInfoSearchDataV1)
  53. for _, v := range dataList {
  54. existDataMap[v.DataTime] = v
  55. }
  56. //计算来源数据
  57. var fromCondition string
  58. var fromPars []interface{}
  59. fromCondition += " AND edb_info_id=? "
  60. fromPars = append(fromPars, fromEdbInfo.EdbInfoId)
  61. fromDataList, err := GetEdbDataListAll(fromCondition, fromPars, fromEdbInfo.Source, 0)
  62. if err != nil {
  63. return err
  64. }
  65. var fromDateArr []string
  66. fromDataMap := make(map[string]*EdbInfoSearchData)
  67. for _, v := range fromDataList {
  68. fromDateArr = append(fromDateArr, v.DataTime)
  69. fromDataMap[v.DataTime] = v
  70. }
  71. addSql := ` INSERT INTO edb_data_calculate_nszydpjjs(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  72. var isAdd bool
  73. arrLen := len(fromDateArr)
  74. for ak, av := range fromDateArr {
  75. //处理第一个值
  76. var valArr []float64
  77. if findItem, ok := fromDataMap[av]; ok {
  78. valArr = append(valArr, findItem.Value)
  79. } else {
  80. continue
  81. }
  82. if ak+1 != arrLen {
  83. //处理除第一个值之外的N-1个值
  84. for i := 1; i < formulaInt; i++ {
  85. arrIndex := ak + i
  86. if arrIndex >= arrLen {
  87. break
  88. }
  89. arrVal := fromDateArr[arrIndex]
  90. if findItem, ok := fromDataMap[arrVal]; ok {
  91. valArr = append(valArr, findItem.Value)
  92. } else {
  93. continue
  94. }
  95. }
  96. }
  97. valArrLen := len(valArr)
  98. totalVal := decimal.NewFromFloat(0.00)
  99. for _, v := range valArr {
  100. //totalVal = totalVal + v
  101. newDecimal := decimal.NewFromFloat(v)
  102. totalVal = totalVal.Add(newDecimal)
  103. }
  104. //af := decimal.NewFromFloat(totalVal)
  105. af := totalVal
  106. bf := decimal.NewFromFloat(float64(valArrLen))
  107. val, _ := af.Div(bf).Float64()
  108. currentDate, err := time.Parse(utils.FormatDate, av)
  109. if err != nil {
  110. return err
  111. }
  112. timestamp := currentDate.UnixNano() / 1e6
  113. timestampStr := fmt.Sprintf("%d", timestamp)
  114. valStr := utils.SubFloatToString(val, 4)
  115. if existItem, existOk := existDataMap[av]; !existOk {
  116. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, valStr)
  117. isAdd = true
  118. } else {
  119. if existItem != nil && existItem.Value != valStr {
  120. err = ModifyEdbDataCalculateNszydpjjs(int64(edbInfoId), av, valStr)
  121. if err != nil {
  122. return err
  123. }
  124. }
  125. }
  126. }
  127. if isAdd {
  128. addSql = strings.TrimRight(addSql, ",")
  129. _, err = tx.Raw(addSql).Exec()
  130. if err != nil {
  131. return err
  132. }
  133. }
  134. return
  135. }
  136. type EdbInfoCalculateNszydpjjsDetail struct {
  137. EdbInfoCalculateNszydpjjsId int `orm:"column(edb_info_calculate_nszydpjjs_id);pk"`
  138. EdbInfoId int `description:"指标id"`
  139. EdbCode string `description:"指标编码"`
  140. FromEdbInfoId int `description:"计算指标id"`
  141. FromEdbCode string `description:"计算指标编码"`
  142. FromEdbName string `description:"计算指标名称"`
  143. FromSource int `description:"计算指标来源"`
  144. FromSourceName string `description:"计算指标来源名称"`
  145. FromTag string `description:"来源指标标签"`
  146. Sort int `description:"计算指标名称排序"`
  147. CreateTime time.Time `description:"创建时间"`
  148. ModifyTime time.Time `description:"修改时间"`
  149. StartDate string `description:"开始日期"`
  150. EndDate string `description:"结束日期"`
  151. }
  152. func GetEdbInfoCalculateNszydpjjsDetail(edbInfoId int) (item *EdbInfoCalculateNszydpjjsDetail, err error) {
  153. o := orm.NewOrmUsingDB("data")
  154. sql := ` SELECT a.*,b.start_date,b.end_date FROM edb_info_calculate_mapping AS a
  155. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  156. WHERE a.edb_info_id=? `
  157. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  158. return
  159. }
  160. func GetEdbDataCalculateNszydpjjsByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  161. o := orm.NewOrmUsingDB("data")
  162. sql := ` SELECT COUNT(1) AS count FROM edb_data_calculate_nszydpjjs WHERE edb_code=? AND data_time=? `
  163. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  164. return
  165. }
  166. func ModifyEdbDataCalculateNszydpjjs(edbInfoId int64, dataTime, value string) (err error) {
  167. o := orm.NewOrmUsingDB("data")
  168. sql := ` UPDATE edb_data_calculate_nszydpjjs SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  169. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  170. return
  171. }