edb_info_calculate_bp.go 4.7 KB

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