edb_info_calculate_ljztbpj.go 9.7 KB

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