edb_info_calculate_hcz.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 EdbInfoCalculateHcz struct {
  12. EdbInfoCalculateHczId int `orm:"column(edb_info_calculate_hcz_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 HczDiv(current, pre float64) string {
  27. if pre == 0 {
  28. return ""
  29. }
  30. currentVal := decimal.NewFromFloat(current)
  31. preVal := decimal.NewFromFloat(pre)
  32. val, _ := currentVal.Sub(preVal).Float64()
  33. valStr := utils.SubFloatToString(val, 4)
  34. return valStr
  35. }
  36. //刷新环比值数据
  37. func RefreshCalculateHcz(edbInfoId, source int, fromEdbInfo *EdbInfo, edbCode, startDate, endDate string) (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_hcz(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. for i := 0; i < dataLen; i++ {
  89. j := i + 1
  90. if j < dataLen {
  91. //当期
  92. currentItem := dataList[i]
  93. preItem := dataList[j]
  94. if currentItem != nil && preItem != nil {
  95. existKey := edbCode + currentItem.DataTime
  96. if _, ok := existMap[existKey]; !ok {
  97. currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime)
  98. timestamp := currentDate.UnixNano() / 1e6
  99. timestampStr := fmt.Sprintf("%d", timestamp)
  100. val := HczDiv(currentItem.Value, preItem.Value)
  101. if val != "" {
  102. if existVal, findOk := existDataMap[currentItem.DataTime]; !findOk {
  103. addSql += GetAddSql(edbInfoIdStr, edbCode, currentItem.DataTime, timestampStr, val)
  104. isAdd = true
  105. } else {
  106. if existVal != val {
  107. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  108. sql = fmt.Sprintf(sql, dataTableName)
  109. _, err = tx.Raw(sql, val, edbInfoId, currentItem.DataTime).Exec()
  110. if err != nil {
  111. return err
  112. }
  113. }
  114. }
  115. }
  116. }
  117. existMap[existKey] = currentItem.DataTime
  118. }
  119. }
  120. }
  121. if isAdd {
  122. addSql = strings.TrimRight(addSql, ",")
  123. _, err = o.Raw(addSql).Exec()
  124. if err != nil {
  125. return err
  126. }
  127. }
  128. return
  129. }
  130. type EdbInfoCalculateHczDetail struct {
  131. EdbInfoCalculateHczId int `orm:"column(edb_info_calculate_hcz_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 GetEdbInfoCalculateHczDetail(edbInfoId int) (item *EdbInfoCalculateHczDetail, err error) {
  147. o := orm.NewOrmUsingDB("data")
  148. sql := ` SELECT a.*,b.start_date,b.end_date FROM edb_info_calculate_mapping AS a
  149. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  150. WHERE a.edb_info_id=? `
  151. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  152. return
  153. }