edb_info_calculate_ljzzy.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 EdbInfoCalculateLjzzy struct {
  12. EdbInfoCalculateLjzzyId int `orm:"column(edb_info_calculate_ljzzy_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 RefreshCalculateLjzzy(edbInfoId int, fromEdbInfo *EdbInfo, edbCode, startDate, endDate string) (err error) {
  27. o := orm.NewOrmUsingDB("data")
  28. tx,err:=o.Begin()
  29. if err!=nil {
  30. return err
  31. }
  32. defer func() {
  33. if err != nil {
  34. tx.Rollback()
  35. } else {
  36. tx.Commit()
  37. }
  38. }()
  39. if err != nil {
  40. return
  41. }
  42. edbInfoIdStr := strconv.Itoa(edbInfoId)
  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, 1)
  57. if err != nil {
  58. return err
  59. }
  60. yearMap := make(map[int]map[int]*EdbInfoSearchData)
  61. dataLen := len(dataList)
  62. for i := 0; i < dataLen; i++ {
  63. item := dataList[i]
  64. //日其中获取年
  65. itemDate, err := time.Parse(utils.FormatDate, item.DataTime)
  66. if err != nil {
  67. return err
  68. }
  69. year := itemDate.Year()
  70. month := int(itemDate.Month())
  71. if monthMap, yok := yearMap[year]; yok {
  72. monthMap[month] = item
  73. yearMap[year] = monthMap
  74. } else {
  75. monthMap = make(map[int]*EdbInfoSearchData)
  76. monthMap[month] = item
  77. yearMap[year] = monthMap
  78. }
  79. }
  80. addSql := ` INSERT INTO edb_data_calculate_ljzzy(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  81. nowStr := time.Now().Format(utils.FormatDateTime)
  82. var isAdd bool
  83. for yk, yv := range yearMap {
  84. _, oneMonthOk := yv[1]
  85. _, twoMonthOk := yv[2]
  86. if !oneMonthOk && !twoMonthOk {
  87. continue
  88. }
  89. for i := 1; i <= 12; i++ {
  90. fmt.Println(yk, i, yv[i])
  91. dataCurrentItem := yv[i]
  92. var date string
  93. var val float64
  94. if i == 1 || i == 2 {
  95. if _, mok := yv[1]; mok { //1月有值
  96. if i == 1 {
  97. date = dataCurrentItem.DataTime
  98. val, _ = decimal.NewFromFloat(dataCurrentItem.Value).Float64() //a.Div(b).Float64()
  99. }
  100. if i == 2 {
  101. dataOneItem := yv[1]
  102. date = dataCurrentItem.DataTime
  103. twoMonth := decimal.NewFromFloat(dataCurrentItem.Value)
  104. oneMonth := decimal.NewFromFloat(dataOneItem.Value)
  105. val, _ = twoMonth.Sub(oneMonth).Float64()
  106. }
  107. } else { //1月无值
  108. dataTwoItem := yv[2]
  109. if i == 1 {
  110. date = strconv.Itoa(yk) + "-01-31"
  111. a := decimal.NewFromFloat(dataTwoItem.Value)
  112. b := decimal.NewFromFloat(2.0)
  113. val, _ = a.Div(b).Float64()
  114. }
  115. if i == 2 {
  116. date = dataCurrentItem.DataTime
  117. a := decimal.NewFromFloat(dataTwoItem.Value)
  118. b := decimal.NewFromFloat(2.0)
  119. val, _ = a.Div(b).Float64()
  120. }
  121. }
  122. } else {
  123. dataPreItem := yv[i-1]
  124. if dataCurrentItem != nil && dataPreItem != nil {
  125. date = dataCurrentItem.DataTime
  126. //val, _ = decimal.NewFromFloat(dataCurrentItem.Value).Sub(decimal.NewFromFloat(dataPreItem.Value)).Float64()
  127. a := decimal.NewFromFloat(dataCurrentItem.Value)
  128. b := decimal.NewFromFloat(dataPreItem.Value)
  129. val, _ = a.Sub(b).Float64()
  130. }
  131. }
  132. if date != "" {
  133. //判断数据是否存在
  134. count, err := GetEdbDataCalculateLjzzyByCodeAndDate(edbCode, date)
  135. if err != nil && err.Error() != utils.ErrNoRow() {
  136. return err
  137. }
  138. if count <= 0 {
  139. dataTime, _ := time.Parse(utils.FormatDate, date)
  140. timestamp := dataTime.UnixNano() / 1e6
  141. timeStr := fmt.Sprintf("%d", timestamp)
  142. addSql += "("
  143. addSql += edbInfoIdStr + "," + "'" + edbCode + "'" + "," + "'" + date + "'" + "," + utils.SubFloatToString(val, 4) + "," + "'" + nowStr + "'" +
  144. "," + "'" + nowStr + "'" + "," + "1"
  145. addSql += "," + "'" + timeStr + "'"
  146. addSql += "),"
  147. isAdd = true
  148. } else {
  149. //val = utils.FixFloat(val, 4)
  150. valStr := utils.SubFloatToString(val, 4)
  151. err = ModifyEdbDataCalculateLjzzy(int64(edbInfoId), date, valStr)
  152. if err != nil {
  153. return err
  154. }
  155. }
  156. }
  157. }
  158. }
  159. if isAdd {
  160. addSql = strings.TrimRight(addSql, ",")
  161. _, err = tx.Raw(addSql).Exec()
  162. if err != nil {
  163. return err
  164. }
  165. }
  166. return
  167. }
  168. type EdbInfoCalculateLjzzyDetail struct {
  169. EdbInfoCalculateLjzzyId int `orm:"column(edb_info_calculate_ljzzy_id);pk"`
  170. EdbInfoId int `description:"指标id"`
  171. EdbCode string `description:"指标编码"`
  172. FromEdbInfoId int `description:"计算指标id"`
  173. FromEdbCode string `description:"计算指标编码"`
  174. FromEdbName string `description:"计算指标名称"`
  175. FromSource int `description:"计算指标来源"`
  176. FromSourceName string `description:"计算指标来源名称"`
  177. FromTag string `description:"来源指标标签"`
  178. Sort int `description:"计算指标名称排序"`
  179. CreateTime time.Time `description:"创建时间"`
  180. ModifyTime time.Time `description:"修改时间"`
  181. StartDate string `description:"开始日期"`
  182. EndDate string `description:"结束日期"`
  183. }
  184. func GetEdbInfoCalculateLjzzyDetail(edbInfoId int) (item *EdbInfoCalculateLjzzyDetail, err error) {
  185. o := orm.NewOrmUsingDB("data")
  186. sql := ` SELECT a.*,b.start_date,b.end_date FROM edb_info_calculate_mapping AS a
  187. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  188. WHERE a.edb_info_id=? `
  189. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  190. return
  191. }
  192. func GetEdbDataCalculateLjzzyByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  193. o := orm.NewOrmUsingDB("data")
  194. sql := ` SELECT COUNT(1) AS count FROM edb_data_calculate_ljzzy WHERE edb_code=? AND data_time=? `
  195. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  196. return
  197. }
  198. func ModifyEdbDataCalculateLjzzy(edbInfoId int64, dataTime, value string) (err error) {
  199. o := orm.NewOrmUsingDB("data")
  200. sql := ` UPDATE edb_data_calculate_ljzzy SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  201. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  202. return
  203. }