package data_manage import ( "errors" "fmt" "github.com/shopspring/decimal" "hongze/hongze_task/utils" "github.com/beego/beego/v2/client/orm" "sort" "strings" "time" ) type EdbInfoCalculateLjztbpjDetail struct { EdbInfoCalculateBpId int `orm:"column(edb_info_calculate_bp_id);pk"` EdbInfoId int `description:"指标id"` EdbCode string `description:"指标编码"` FromEdbInfoId int `description:"计算指标id"` FromEdbCode string `description:"计算指标编码"` FromEdbName string `description:"计算指标名称"` FromSource int `description:"计算指标来源"` FromSourceName string `description:"计算指标来源名称"` FromTag string `description:"来源指标标签"` Sort int `description:"计算指标名称排序"` CreateTime time.Time `description:"创建时间"` ModifyTime time.Time `description:"修改时间"` StartDate string `description:"开始日期"` EndDate string `description:"结束日期"` } // RefreshAllCalculateLjztbpj 刷新所有 累计值同比拼接 数据 func RefreshAllCalculateLjztbpj(edbInfo *EdbInfo) (err error) { o := orm.NewOrmUsingDB("data") tx,err:=o.Begin() if err!=nil { return err } defer func() { if err != nil { tx.Rollback() } else { tx.Commit() } }() //查询当前指标现有的数据 var condition string var pars []interface{} condition += " AND edb_info_id=? " pars = append(pars, edbInfo.EdbInfoId) //所有的数据 dataList, err := GetAllEdbDataCalculateLjztbpjByEdbInfoId(edbInfo.EdbInfoId) if err != nil { return err } //待拼接指标map pjDataMap := make(map[string]float64) //需要入库的数据 nowEdbDataMap := make(map[string]float64) //当前指标的数据(已经在库里了,不需要重新) //拼接指标的日期切片数据 pjEdbDataTimeList := make([]string, 0) dataMap := make(map[string]*EdbDataCalculateLjztbpj) for _, v := range dataList { pjEdbDataTimeList = append(pjEdbDataTimeList, v.DataTime) dataMap[v.DataTime] = v nowEdbDataMap[v.DataTime] = v.Value } //查询关联指标信息 var existCondition string var existPars []interface{} existCondition += " AND edb_info_id=? " existPars = append(existPars, edbInfo.EdbInfoId) existList, err := GetEdbInfoCalculateListByCondition(existCondition, existPars) if err != nil { err = errors.New("判断指标是否改变失败,Err:" + err.Error()) return } var existItemA, existItemB *EdbInfoCalculateMapping for _, existItem := range existList { if existItem.FromTag == "A" { existItemA = existItem } else if existItem.FromTag == "B" { existItemB = existItem } } // 原数据开始计算日期 startCalculationDate, _ := time.ParseInLocation(utils.FormatDate, edbInfo.CalculateFormula, time.Local) //待拼接指标 { var condition string var pars []interface{} condition += " AND data_time <= ? AND edb_info_id=? " pars = append(pars, startCalculationDate, existItemA.FromEdbInfoId) //第一个指标的数据列表 firstDataList, tmpErr := GetEdbDataListAll(condition, pars, existItemA.FromSource, 0) if tmpErr != nil { return tmpErr } for _, v := range firstDataList { //时间戳 if edbData, ok := dataMap[v.DataTime]; ok { if edbData.Value != v.Value { //更新指标数据 edbData.Value = v.Value o.Update(edbData, "Value") //将新的数据存入已入库指标map里面,以便后续计算 nowEdbDataMap[edbData.DataTime] = v.Value } } } } //同比值指标map tbzEdbDataMap := make(map[string]float64) //同比值日期切片列表 tbzEdbDataTimeList := make([]string, 0) //同比值指标 { var condition string var pars []interface{} condition += " AND data_time > ? AND edb_info_id = ? " pars = append(pars, startCalculationDate, existItemB.FromEdbInfoId) //第二个指标的数据列表 secondDataList, tmpErr := GetEdbDataListAll(condition, pars, existItemB.FromSource, 0) if tmpErr != nil { return tmpErr } for _, v := range secondDataList { tbzEdbDataMap[v.DataTime] = v.Value tbzEdbDataTimeList = append(tbzEdbDataTimeList, v.DataTime) } } sort.Strings(tbzEdbDataTimeList) // 遍历现有的数据,判断拼接指标中是否存在该日期数据,如果拼接指标无此数据,那么需要删除该日期数据(日期的判断:需要在开始计算日期之后) removeDateList := make([]string, 0) for nowEdbDate := range nowEdbDataMap { nowEdbDateTime, _ := time.ParseInLocation(utils.FormatDate, nowEdbDate, time.Local) //校验日期 需要 大于 拼接前日期 if startCalculationDate.Before(nowEdbDateTime) { if _, ok := tbzEdbDataMap[nowEdbDate]; !ok { // 同比指标中,不存在该日期数据,那么需要移除 现有数据 中该日期的数据 removeDateList = append(removeDateList, nowEdbDate) } } } //待修改的指标数据map(index:日期,value:值) updateEdbDataMap := make(map[string]float64) for _, v := range tbzEdbDataTimeList { tbzDataTime, _ := time.ParseInLocation(utils.FormatDate, v, time.Local) //获取拼接指标上一年同一天的数据 var pjDataTime time.Time if tbzDataTime.Month() == 2 { pjDataTime = tbzDataTime.AddDate(0, -11, 0) pjDataTime = time.Date(pjDataTime.Year(), pjDataTime.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 0, -1) } else { pjDataTime = tbzDataTime.AddDate(-1, 0, 0) } //校验现有数据中,是否存在该日期的数据,如果存在的话,那么就要去校验 最新计算数据 与 现有数据 是否一致 if nowEdbDataValue, isHas := nowEdbDataMap[v]; isHas { //获取去年今日的数据,获取到后,然后是去修改该日期的数据 if lastYearEdbDataValue, ok := nowEdbDataMap[pjDataTime.Format(utils.FormatDate)]; ok { if v == "2021-08-31" { fmt.Println("进来了") } tbzDataValue := tbzEdbDataMap[v] //同比值 currValue := lastYearEdbDataValue * (1 + tbzDataValue/100) currValue, _ = decimal.NewFromFloat(currValue).Truncate(4).Float64() //保留4位小数 //如果计算出来的值与库里面的值不匹配,那么就去修改该值 if nowEdbDataValue != currValue { //将计算后的数据存入待拼接指标map里面,以便后续计算 updateEdbDataMap[v] = currValue } } } else { //因为 现有数据中 不存在该日期数据,那么需要做新增数据处理 //如果去年今日存在该数据,那么就去计算当前的数据 if pjDataValue, ok := nowEdbDataMap[pjDataTime.Format(utils.FormatDate)]; ok { tbzDataValue := tbzEdbDataMap[v] //同比值 currValue := pjDataValue * (1 + tbzDataValue/100) currValue, _ = decimal.NewFromFloat(currValue).Truncate(4).Float64() //将计算后的数据存入已入库指标map里面,以便后续计算 nowEdbDataMap[v] = currValue //将计算后的数据存入待拼接指标map里面,以便后续入库 pjDataMap[v] = currValue pjEdbDataTimeList = append(pjEdbDataTimeList, v) } } } //新增的数据入库 { addDataList := make([]*EdbDataCalculateLjztbpj, 0) for dataTime, dataValue := range pjDataMap { //时间戳 currentDate, _ := time.Parse(utils.FormatDate, dataTime) timestamp := currentDate.UnixNano() / 1e6 edbDataLjztbpj := &EdbDataCalculateLjztbpj{ EdbInfoId: edbInfo.EdbInfoId, EdbCode: edbInfo.EdbCode, DataTime: dataTime, Value: dataValue, Status: 1, CreateTime: time.Now(), ModifyTime: time.Now(), DataTimestamp: timestamp, } addDataList = append(addDataList, edbDataLjztbpj) } tmpAddDataList := make([]*EdbDataCalculateLjztbpj, 0) for _, v := range addDataList { tmpAddDataList = append(tmpAddDataList, v) if len(tmpAddDataList) >= 200 { _, tmpErr := tx.InsertMulti(len(tmpAddDataList), tmpAddDataList) if tmpErr != nil { err = tmpErr return } //重新初始化需要加入的数据切片 tmpAddDataList = make([]*EdbDataCalculateLjztbpj, 0) } } //最后如果还有需要新增的数据,那么就统一入库 if len(tmpAddDataList) > 0 { _, tmpErr := tx.InsertMulti(len(tmpAddDataList), tmpAddDataList) if tmpErr != nil { err = tmpErr return } } } //删除已经不存在的累计同比拼接指标数据(由于同比值当日的数据删除了) { if len(removeDateList) > 0 { removeDateStr := strings.Join(removeDateList, `","`) removeDateStr = `"` + removeDateStr + `"` //如果拼接指标变更了,那么需要删除所有的指标数据 tableName := GetEdbDataTableName(edbInfo.Source) sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (%s) `, tableName, removeDateStr) _, err = tx.Raw(sql, edbInfo.EdbInfoId).Exec() if err != nil { err = errors.New("删除不存在的累计值同比拼接指标数据失败,Err:" + err.Error()) return } } } //修改现有的数据中对应的值 { tableName := GetEdbDataTableName(edbInfo.Source) for edbDate, edbDataValue := range updateEdbDataMap { sql := fmt.Sprintf(` UPDATE %s set value = ?,modify_time=now() WHERE edb_info_id = ? and data_time = ? `, tableName) _, err = tx.Raw(sql, edbDataValue, edbInfo.EdbInfoId, edbDate).Exec() if err != nil { err = errors.New("更新现有的累计值同比拼接指标数据失败,Err:" + err.Error()) return } } } return }