edb_info_calculate_ljztbpj.go 9.4 KB

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