edb_info_calculate_bp.go 5.4 KB

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