edb_info_calculate_bp.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package data_manage
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/shopspring/decimal"
  6. "hongze/hongze_task/utils"
  7. "rdluck_tools/orm"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. type EdbInfoCalculateBp struct {
  13. EdbInfoCalculateBpId int `orm:"column(edb_info_calculate_bp_id);pk"`
  14. EdbInfoId int `description:"指标id"`
  15. EdbCode string `description:"指标编码"`
  16. FromEdbInfoId int `description:"计算指标id"`
  17. FromEdbCode string `description:"计算指标编码"`
  18. FromEdbName string `description:"计算指标名称"`
  19. FromSource int `description:"计算指标来源"`
  20. FromSourceName string `description:"计算指标来源名称"`
  21. FromTag string `description:"来源指标标签"`
  22. Sort int `description:"计算指标名称排序"`
  23. CreateTime time.Time `description:"创建时间"`
  24. ModifyTime time.Time `description:"修改时间"`
  25. }
  26. //变频
  27. func RefreshCalculateBp(edbInfoId int, fromEdbInfo *EdbInfo, edbCode, startDate, endDate string) (err error) {
  28. o := orm.NewOrm()
  29. o.Using("data")
  30. o.Begin()
  31. defer func() {
  32. if err != nil {
  33. o.Rollback()
  34. } else {
  35. o.Commit()
  36. }
  37. }()
  38. if err != nil {
  39. return
  40. }
  41. edbInfoIdStr := strconv.Itoa(edbInfoId)
  42. //计算数据
  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. existMap := make(map[string]string)
  61. dataLen := len(dataList)
  62. addSql := ` INSERT INTO edb_data_calculate_bp(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  63. var isAdd bool
  64. for i := 0; i < dataLen; i++ {
  65. //当期
  66. currentItem := dataList[i]
  67. currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime)
  68. var day int
  69. if i == 0 {
  70. day = int(time.Now().Sub(currentDate).Hours() / float64(24))
  71. } else {
  72. j := i + 1
  73. if j < dataLen {
  74. preItem := dataList[j]
  75. preDate, _ := time.Parse(utils.FormatDate, preItem.DataTime)
  76. day = int(currentDate.Sub(preDate).Hours() / float64(24))
  77. }
  78. }
  79. for k := 1; k <= day; k++ {
  80. needDay := currentDate.AddDate(0, 0, k).Format(utils.FormatDate)
  81. existKey := edbCode + needDay
  82. if _, ok := existMap[existKey]; !ok {
  83. currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime)
  84. timestamp := currentDate.UnixNano() / 1e6
  85. timestampStr := fmt.Sprintf("%d", timestamp)
  86. valStr := decimal.NewFromFloat(currentItem.Value).String()
  87. addSql += GetAddSql(edbInfoIdStr, edbCode, needDay, timestampStr, valStr)
  88. }
  89. existMap[existKey] = needDay
  90. }
  91. existKey := edbCode + currentItem.DataTime
  92. if _, ok := existMap[existKey]; !ok {
  93. currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime)
  94. timestamp := currentDate.UnixNano() / 1e6
  95. timestampStr := fmt.Sprintf("%d", timestamp)
  96. valStr := decimal.NewFromFloat(currentItem.Value).String()
  97. addSql += GetAddSql(edbInfoIdStr, edbCode, currentItem.DataTime, timestampStr, valStr)
  98. }
  99. existMap[existKey] = currentItem.DataTime
  100. }
  101. if isAdd {
  102. addSql = strings.TrimRight(addSql, ",")
  103. _, err = o.Raw(addSql).Exec()
  104. if err != nil {
  105. return err
  106. }
  107. }
  108. return
  109. }
  110. type EdbInfoCalculateBpDetail struct {
  111. EdbInfoCalculateBpId int `orm:"column(edb_info_calculate_bp_id);pk"`
  112. EdbInfoId int `description:"指标id"`
  113. EdbCode string `description:"指标编码"`
  114. FromEdbInfoId int `description:"计算指标id"`
  115. FromEdbCode string `description:"计算指标编码"`
  116. FromEdbName string `description:"计算指标名称"`
  117. FromSource int `description:"计算指标来源"`
  118. FromSourceName string `description:"计算指标来源名称"`
  119. FromTag string `description:"来源指标标签"`
  120. Sort int `description:"计算指标名称排序"`
  121. CreateTime time.Time `description:"创建时间"`
  122. ModifyTime time.Time `description:"修改时间"`
  123. StartDate string `description:"开始日期"`
  124. EndDate string `description:"结束日期"`
  125. }
  126. func GetEdbInfoCalculateBpDetail(edbInfoId int) (item *EdbInfoCalculateTbzDetail, err error) {
  127. o := orm.NewOrm()
  128. o.Using("data")
  129. sql := ` SELECT a.*,b.start_date,b.end_date FROM edb_info_calculate_bp AS a
  130. INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
  131. WHERE a.edb_info_id=? `
  132. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  133. return
  134. }