edb_info_calculate_bp.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 EdbInfoCalculateBp struct {
  12. EdbInfoCalculateBpId int `orm:"column(edb_info_calculate_bp_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. //变频
  26. func RefreshCalculateBp(edbInfoId int, fromEdbInfo *EdbInfo, edbCode, startDate, endDate 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, fromEdbInfo.EdbInfoId)
  48. if startDate != "" {
  49. condition += " AND data_time>=? "
  50. pars = append(pars, startDate)
  51. }
  52. if endDate != "" {
  53. condition += " AND data_time<=? "
  54. pars = append(pars, endDate)
  55. }
  56. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 0)
  57. if err != nil {
  58. return err
  59. }
  60. var existCondition string
  61. var existPars []interface{}
  62. existCondition += " AND edb_info_id=? "
  63. existPars = append(existPars, edbInfoId)
  64. dataExistList, err := GetEdbDataListAll(existCondition, existPars, utils.DATA_SOURCE_CALCULATE_BP, 0)
  65. if err != nil {
  66. return err
  67. }
  68. existDataMap := make(map[string]*EdbInfoSearchData)
  69. for _, v := range dataExistList {
  70. existDataMap[v.DataTime] = v
  71. }
  72. addExistMap := make(map[string]string)
  73. dataLen := len(dataList)
  74. addSql := ` INSERT INTO edb_data_calculate_bp(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  75. var isAdd bool
  76. for i := 0; i < dataLen; i++ {
  77. //当期
  78. currentItem := dataList[i]
  79. currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime)
  80. var day int
  81. if i == 0 {
  82. day = int(time.Now().Sub(currentDate).Hours() / float64(24))
  83. } else {
  84. j := i + 1
  85. if j < dataLen {
  86. preItem := dataList[j]
  87. preDate, _ := time.Parse(utils.FormatDate, preItem.DataTime)
  88. day = int(currentDate.Sub(preDate).Hours() / float64(24))
  89. }
  90. }
  91. for k := 1; k <= day; k++ {
  92. needDayForm := currentDate.AddDate(0, 0, k)
  93. if needDayForm.Before(time.Now()) {
  94. needDay := needDayForm.Format(utils.FormatDate)
  95. if _, ok := existDataMap[needDay]; !ok {
  96. existKey := edbCode + needDay
  97. if _, ok := addExistMap[existKey]; !ok {
  98. currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime)
  99. timestamp := currentDate.UnixNano() / 1e6
  100. timestampStr := fmt.Sprintf("%d", timestamp)
  101. valStr := decimal.NewFromFloat(currentItem.Value).String()
  102. isAdd = true
  103. addSql += GetAddSql(edbInfoIdStr, edbCode, needDay, timestampStr, valStr)
  104. }
  105. addExistMap[existKey] = needDay
  106. }
  107. }
  108. }
  109. if _, ok := existDataMap[currentItem.DataTime]; !ok {
  110. existKey := edbCode + currentItem.DataTime
  111. if _, ok := addExistMap[existKey]; !ok {
  112. currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime)
  113. timestamp := currentDate.UnixNano() / 1e6
  114. timestampStr := fmt.Sprintf("%d", timestamp)
  115. valStr := decimal.NewFromFloat(currentItem.Value).String()
  116. isAdd = true
  117. addSql += GetAddSql(edbInfoIdStr, edbCode, currentItem.DataTime, timestampStr, valStr)
  118. }
  119. addExistMap[existKey] = currentItem.DataTime
  120. }
  121. }
  122. if isAdd {
  123. addSql = strings.TrimRight(addSql, ",")
  124. _, err = tx.Raw(addSql).Exec()
  125. if err != nil {
  126. fmt.Println("add err:" + err.Error())
  127. return err
  128. }
  129. }
  130. return
  131. }
  132. type EdbInfoCalculateBpDetail struct {
  133. EdbInfoCalculateBpId int `orm:"column(edb_info_calculate_bp_id);pk"`
  134. EdbInfoId int `description:"指标id"`
  135. EdbCode string `description:"指标编码"`
  136. FromEdbInfoId int `description:"计算指标id"`
  137. FromEdbCode string `description:"计算指标编码"`
  138. FromEdbName string `description:"计算指标名称"`
  139. FromSource int `description:"计算指标来源"`
  140. FromSourceName string `description:"计算指标来源名称"`
  141. FromTag string `description:"来源指标标签"`
  142. Sort int `description:"计算指标名称排序"`
  143. CreateTime time.Time `description:"创建时间"`
  144. ModifyTime time.Time `description:"修改时间"`
  145. StartDate string `description:"开始日期"`
  146. EndDate string `description:"结束日期"`
  147. }
  148. func GetEdbInfoCalculateBpDetail(edbInfoId int) (item *EdbInfoCalculateTbzDetail, err error) {
  149. o := orm.NewOrmUsingDB("data")
  150. sql := ` SELECT a.*,b.start_date,b.end_date FROM edb_info_calculate_mapping AS a
  151. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  152. WHERE a.edb_info_id=? `
  153. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  154. return
  155. }