edb_info_calculate_hbz.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 EdbInfoCalculateHbz struct {
  12. EdbInfoCalculateHbzId int `orm:"column(edb_info_calculate_hbz_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. //环比值,current:当期,pre:上期 公式: (当期-上期)/上期
  26. func HbzDiv(current, pre float64) string {
  27. if pre == 0 {
  28. return ""
  29. }
  30. currentVal := decimal.NewFromFloat(float64(current))
  31. preVal := decimal.NewFromFloat(float64(pre))
  32. val, _ := currentVal.Sub(preVal).Div(preVal).Float64()
  33. valStr := utils.SubFloatToString(val, 4)
  34. return valStr
  35. }
  36. //刷新环比值数据
  37. func RefreshCalculateHbz(edbInfoId, source int, fromEdbInfo *EdbInfo, edbCode, startDate, endDate string, formulaInt int) (err error) {
  38. o := orm.NewOrmUsingDB("data")
  39. tx,err:=o.Begin()
  40. if err!=nil {
  41. return err
  42. }
  43. defer func() {
  44. if err != nil {
  45. tx.Rollback()
  46. } else {
  47. tx.Commit()
  48. }
  49. }()
  50. if err != nil {
  51. return
  52. }
  53. edbInfoIdStr := strconv.Itoa(edbInfoId)
  54. var condition string
  55. var pars []interface{}
  56. condition += " AND edb_info_id=? "
  57. pars = append(pars, fromEdbInfo.EdbInfoId)
  58. if startDate != "" {
  59. condition += " AND data_time>=? "
  60. pars = append(pars, startDate)
  61. }
  62. if endDate != "" {
  63. condition += " AND data_time<=? "
  64. pars = append(pars, endDate)
  65. }
  66. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 0)
  67. if err != nil {
  68. return err
  69. }
  70. //获取指标所有数据
  71. existDataList := make([]*EdbDataBase, 0)
  72. dataTableName := GetEdbDataTableName(source)
  73. fmt.Println("dataTableName:", dataTableName)
  74. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  75. sql = fmt.Sprintf(sql, dataTableName)
  76. _, err = o.Raw(sql, edbInfoId).QueryRows(&existDataList)
  77. if err != nil {
  78. return err
  79. }
  80. existDataMap := make(map[string]string)
  81. for _, v := range existDataList {
  82. existDataMap[v.DataTime] = v.Value
  83. }
  84. addSql := ` INSERT INTO edb_data_calculate_hbz(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  85. var isAdd bool
  86. existMap := make(map[string]string)
  87. dataLen := len(dataList)
  88. fmt.Println("dataLen:", dataLen)
  89. for i := 0; i < dataLen; i++ {
  90. j := i + formulaInt
  91. if j < dataLen {
  92. //当期
  93. currentItem := dataList[i]
  94. preItem := dataList[j]
  95. if currentItem != nil && preItem != nil {
  96. existKey := edbCode + currentItem.DataTime
  97. if _, ok := existMap[existKey]; !ok {
  98. currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime)
  99. timestamp := currentDate.UnixNano() / 1e6
  100. timestampStr := fmt.Sprintf("%d", timestamp)
  101. val := HbzDiv(currentItem.Value, preItem.Value)
  102. if val != "" {
  103. if existVal, findOk := existDataMap[currentItem.DataTime]; !findOk {
  104. addSql += GetAddSql(edbInfoIdStr, edbCode, currentItem.DataTime, timestampStr, val)
  105. isAdd = true
  106. } else {
  107. if existVal != val {
  108. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  109. sql = fmt.Sprintf(sql, dataTableName)
  110. _, err = tx.Raw(sql, val, edbInfoId, currentItem.DataTime).Exec()
  111. if err != nil {
  112. return err
  113. }
  114. }
  115. }
  116. }
  117. }
  118. existMap[existKey] = currentItem.DataTime
  119. }
  120. }
  121. }
  122. if isAdd {
  123. addSql = strings.TrimRight(addSql, ",")
  124. _, err = o.Raw(addSql).Exec()
  125. if err != nil {
  126. return err
  127. }
  128. }
  129. return
  130. }
  131. type EdbInfoCalculateHbzDetail struct {
  132. EdbInfoCalculateTbzId int `orm:"column(edb_info_calculate_tbz_id);pk"`
  133. EdbInfoId int `description:"指标id"`
  134. EdbCode string `description:"指标编码"`
  135. FromEdbInfoId int `description:"计算指标id"`
  136. FromEdbCode string `description:"计算指标编码"`
  137. FromEdbName string `description:"计算指标名称"`
  138. FromSource int `description:"计算指标来源"`
  139. FromSourceName string `description:"计算指标来源名称"`
  140. FromTag string `description:"来源指标标签"`
  141. Sort int `description:"计算指标名称排序"`
  142. CreateTime time.Time `description:"创建时间"`
  143. ModifyTime time.Time `description:"修改时间"`
  144. StartDate string `description:"开始日期"`
  145. EndDate string `description:"结束日期"`
  146. }
  147. func GetEdbInfoCalculateHbzDetail(edbInfoId int) (item *EdbInfoCalculateTbzDetail, err error) {
  148. o := orm.NewOrmUsingDB("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.edb_info_id=b.edb_info_id
  151. WHERE a.edb_info_id=? `
  152. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  153. return
  154. }