edb_info_calculate_ljztbpj.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package data_manage
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/shopspring/decimal"
  6. "hongze/hongze_task/utils"
  7. "github.com/beego/beego/v2/client/orm"
  8. "sort"
  9. "strings"
  10. "time"
  11. )
  12. type EdbInfoCalculateLjztbpjDetail struct {
  13. EdbInfoCalculateBpId int `orm:"column(edb_info_calculate_bp_id);pk"`
  14. EdbInfoId int `description:"指标id"`
  15. EdbCode string `description:"指标编码"`
  16. FromEdbInfoId int `description:"计算指标id"`
  17. FromEdbCode string `description:"计算指标编码"`
  18. FromEdbName string `description:"计算指标名称"`
  19. FromSource int `description:"计算指标来源"`
  20. FromSourceName string `description:"计算指标来源名称"`
  21. FromTag string `description:"来源指标标签"`
  22. Sort int `description:"计算指标名称排序"`
  23. CreateTime time.Time `description:"创建时间"`
  24. ModifyTime time.Time `description:"修改时间"`
  25. StartDate string `description:"开始日期"`
  26. EndDate string `description:"结束日期"`
  27. }
  28. // RefreshAllCalculateLjztbpj 刷新所有 累计值同比拼接 数据
  29. func RefreshAllCalculateLjztbpj(edbInfo *EdbInfo) (err error) {
  30. o := orm.NewOrmUsingDB("data")
  31. tx,err:=o.Begin()
  32. if err!=nil {
  33. return err
  34. }
  35. defer func() {
  36. if err != nil {
  37. tx.Rollback()
  38. } else {
  39. tx.Commit()
  40. }
  41. }()
  42. //查询当前指标现有的数据
  43. var condition string
  44. var pars []interface{}
  45. condition += " AND edb_info_id=? "
  46. pars = append(pars, edbInfo.EdbInfoId)
  47. //所有的数据
  48. dataList, err := GetAllEdbDataCalculateLjztbpjByEdbInfoId(edbInfo.EdbInfoId)
  49. if err != nil {
  50. return err
  51. }
  52. //待拼接指标map
  53. pjDataMap := make(map[string]float64) //需要入库的数据
  54. nowEdbDataMap := make(map[string]float64) //当前指标的数据(已经在库里了,不需要重新)
  55. //拼接指标的日期切片数据
  56. pjEdbDataTimeList := make([]string, 0)
  57. dataMap := make(map[string]*EdbDataCalculateLjztbpj)
  58. for _, v := range dataList {
  59. pjEdbDataTimeList = append(pjEdbDataTimeList, v.DataTime)
  60. dataMap[v.DataTime] = v
  61. nowEdbDataMap[v.DataTime] = v.Value
  62. }
  63. //查询关联指标信息
  64. var existCondition string
  65. var existPars []interface{}
  66. existCondition += " AND edb_info_id=? "
  67. existPars = append(existPars, edbInfo.EdbInfoId)
  68. existList, err := GetEdbInfoCalculateListByCondition(existCondition, existPars)
  69. if err != nil {
  70. err = errors.New("判断指标是否改变失败,Err:" + err.Error())
  71. return
  72. }
  73. var existItemA, existItemB *EdbInfoCalculateMapping
  74. for _, existItem := range existList {
  75. if existItem.FromTag == "A" {
  76. existItemA = existItem
  77. } else if existItem.FromTag == "B" {
  78. existItemB = existItem
  79. }
  80. }
  81. // 原数据开始计算日期
  82. startCalculationDate, _ := time.ParseInLocation(utils.FormatDate, edbInfo.CalculateFormula, time.Local)
  83. //待拼接指标
  84. {
  85. var condition string
  86. var pars []interface{}
  87. condition += " AND data_time <= ? AND edb_info_id=? "
  88. pars = append(pars, startCalculationDate, existItemA.FromEdbInfoId)
  89. //第一个指标的数据列表
  90. firstDataList, tmpErr := GetEdbDataListAll(condition, pars, existItemA.FromSource, 0)
  91. if tmpErr != nil {
  92. return tmpErr
  93. }
  94. for _, v := range firstDataList {
  95. //时间戳
  96. if edbData, ok := dataMap[v.DataTime]; ok {
  97. if edbData.Value != v.Value {
  98. //更新指标数据
  99. edbData.Value = v.Value
  100. o.Update(edbData, "Value")
  101. //将新的数据存入已入库指标map里面,以便后续计算
  102. nowEdbDataMap[edbData.DataTime] = v.Value
  103. }
  104. }
  105. }
  106. }
  107. //同比值指标map
  108. tbzEdbDataMap := make(map[string]float64)
  109. //同比值日期切片列表
  110. tbzEdbDataTimeList := make([]string, 0)
  111. //同比值指标
  112. {
  113. var condition string
  114. var pars []interface{}
  115. condition += " AND data_time > ? AND edb_info_id = ? "
  116. pars = append(pars, startCalculationDate, existItemB.FromEdbInfoId)
  117. //第二个指标的数据列表
  118. secondDataList, tmpErr := GetEdbDataListAll(condition, pars, existItemB.FromSource, 0)
  119. if tmpErr != nil {
  120. return tmpErr
  121. }
  122. for _, v := range secondDataList {
  123. tbzEdbDataMap[v.DataTime] = v.Value
  124. tbzEdbDataTimeList = append(tbzEdbDataTimeList, v.DataTime)
  125. }
  126. }
  127. sort.Strings(tbzEdbDataTimeList)
  128. // 遍历现有的数据,判断拼接指标中是否存在该日期数据,如果拼接指标无此数据,那么需要删除该日期数据(日期的判断:需要在开始计算日期之后)
  129. removeDateList := make([]string, 0)
  130. for nowEdbDate := range nowEdbDataMap {
  131. nowEdbDateTime, _ := time.ParseInLocation(utils.FormatDate, nowEdbDate, time.Local)
  132. //校验日期 需要 大于 拼接前日期
  133. if startCalculationDate.Before(nowEdbDateTime) {
  134. if _, ok := tbzEdbDataMap[nowEdbDate]; !ok {
  135. // 同比指标中,不存在该日期数据,那么需要移除 现有数据 中该日期的数据
  136. removeDateList = append(removeDateList, nowEdbDate)
  137. }
  138. }
  139. }
  140. //待修改的指标数据map(index:日期,value:值)
  141. updateEdbDataMap := make(map[string]float64)
  142. for _, v := range tbzEdbDataTimeList {
  143. tbzDataTime, _ := time.ParseInLocation(utils.FormatDate, v, time.Local)
  144. //获取拼接指标上一年同一天的数据
  145. var pjDataTime time.Time
  146. if tbzDataTime.Month() == 2 {
  147. pjDataTime = tbzDataTime.AddDate(0, -11, 0)
  148. pjDataTime = time.Date(pjDataTime.Year(), pjDataTime.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 0, -1)
  149. } else {
  150. pjDataTime = tbzDataTime.AddDate(-1, 0, 0)
  151. }
  152. //校验现有数据中,是否存在该日期的数据,如果存在的话,那么就要去校验 最新计算数据 与 现有数据 是否一致
  153. if nowEdbDataValue, isHas := nowEdbDataMap[v]; isHas {
  154. //获取去年今日的数据,获取到后,然后是去修改该日期的数据
  155. if lastYearEdbDataValue, ok := nowEdbDataMap[pjDataTime.Format(utils.FormatDate)]; ok {
  156. if v == "2021-08-31" {
  157. fmt.Println("进来了")
  158. }
  159. tbzDataValue := tbzEdbDataMap[v] //同比值
  160. currValue := lastYearEdbDataValue * (1 + tbzDataValue/100)
  161. currValue, _ = decimal.NewFromFloat(currValue).Truncate(4).Float64() //保留4位小数
  162. //如果计算出来的值与库里面的值不匹配,那么就去修改该值
  163. if nowEdbDataValue != currValue {
  164. //将计算后的数据存入待拼接指标map里面,以便后续计算
  165. updateEdbDataMap[v] = currValue
  166. }
  167. }
  168. } else {
  169. //因为 现有数据中 不存在该日期数据,那么需要做新增数据处理
  170. //如果去年今日存在该数据,那么就去计算当前的数据
  171. if pjDataValue, ok := nowEdbDataMap[pjDataTime.Format(utils.FormatDate)]; ok {
  172. tbzDataValue := tbzEdbDataMap[v] //同比值
  173. currValue := pjDataValue * (1 + tbzDataValue/100)
  174. currValue, _ = decimal.NewFromFloat(currValue).Truncate(4).Float64()
  175. //将计算后的数据存入已入库指标map里面,以便后续计算
  176. nowEdbDataMap[v] = currValue
  177. //将计算后的数据存入待拼接指标map里面,以便后续入库
  178. pjDataMap[v] = currValue
  179. pjEdbDataTimeList = append(pjEdbDataTimeList, v)
  180. }
  181. }
  182. }
  183. //新增的数据入库
  184. {
  185. addDataList := make([]*EdbDataCalculateLjztbpj, 0)
  186. for dataTime, dataValue := range pjDataMap {
  187. //时间戳
  188. currentDate, _ := time.Parse(utils.FormatDate, dataTime)
  189. timestamp := currentDate.UnixNano() / 1e6
  190. edbDataLjztbpj := &EdbDataCalculateLjztbpj{
  191. EdbInfoId: edbInfo.EdbInfoId,
  192. EdbCode: edbInfo.EdbCode,
  193. DataTime: dataTime,
  194. Value: dataValue,
  195. Status: 1,
  196. CreateTime: time.Now(),
  197. ModifyTime: time.Now(),
  198. DataTimestamp: timestamp,
  199. }
  200. addDataList = append(addDataList, edbDataLjztbpj)
  201. }
  202. tmpAddDataList := make([]*EdbDataCalculateLjztbpj, 0)
  203. for _, v := range addDataList {
  204. tmpAddDataList = append(tmpAddDataList, v)
  205. if len(tmpAddDataList) >= 200 {
  206. _, tmpErr := tx.InsertMulti(len(tmpAddDataList), tmpAddDataList)
  207. if tmpErr != nil {
  208. err = tmpErr
  209. return
  210. }
  211. //重新初始化需要加入的数据切片
  212. tmpAddDataList = make([]*EdbDataCalculateLjztbpj, 0)
  213. }
  214. }
  215. //最后如果还有需要新增的数据,那么就统一入库
  216. if len(tmpAddDataList) > 0 {
  217. _, tmpErr := tx.InsertMulti(len(tmpAddDataList), tmpAddDataList)
  218. if tmpErr != nil {
  219. err = tmpErr
  220. return
  221. }
  222. }
  223. }
  224. //删除已经不存在的累计同比拼接指标数据(由于同比值当日的数据删除了)
  225. {
  226. if len(removeDateList) > 0 {
  227. removeDateStr := strings.Join(removeDateList, `","`)
  228. removeDateStr = `"` + removeDateStr + `"`
  229. //如果拼接指标变更了,那么需要删除所有的指标数据
  230. tableName := GetEdbDataTableName(edbInfo.Source)
  231. sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (%s) `, tableName, removeDateStr)
  232. _, err = tx.Raw(sql, edbInfo.EdbInfoId).Exec()
  233. if err != nil {
  234. err = errors.New("删除不存在的累计值同比拼接指标数据失败,Err:" + err.Error())
  235. return
  236. }
  237. }
  238. }
  239. //修改现有的数据中对应的值
  240. {
  241. tableName := GetEdbDataTableName(edbInfo.Source)
  242. for edbDate, edbDataValue := range updateEdbDataMap {
  243. sql := fmt.Sprintf(` UPDATE %s set value = ?,modify_time=now() WHERE edb_info_id = ? and data_time = ? `, tableName)
  244. _, err = tx.Raw(sql, edbDataValue, edbInfo.EdbInfoId, edbDate).Exec()
  245. if err != nil {
  246. err = errors.New("更新现有的累计值同比拼接指标数据失败,Err:" + err.Error())
  247. return
  248. }
  249. }
  250. }
  251. return
  252. }