package data_manage import ( "errors" "fmt" "hongze/hongze_task/utils" "github.com/beego/beego/v2/client/orm" "strings" "time" ) type EdbInfoCalculateZjpjDetail 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:"结束日期"` } // RefreshAllCalculateZjpj 刷新所有 直接拼接 数据 func RefreshAllCalculateZjpj(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 := GetAllEdbDataCalculateZjpjByEdbInfoId(edbInfo.EdbInfoId) if err != nil { return err } var dateArr []string dataMap := make(map[string]*EdbDataCalculateZjpj) removeDataTimeMap := make(map[string]int) //需要移除的日期数据 for _, v := range dataList { dateArr = append(dateArr, v.DataTime) dataMap[v.DataTime] = v removeDataTimeMap[v.DataTime] = 1 } //查询关联指标信息 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 } } addDataList := make([]*EdbDataCalculateZjpj, 0) //第一个指标 { var condition string var pars []interface{} condition += " AND data_time < ? AND edb_info_id=? " pars = append(pars, edbInfo.CalculateFormula, existItemA.FromEdbInfoId) //第一个指标的数据列表 firstDataList, tmpErr := GetEdbDataListAll(condition, pars, existItemA.FromSource, 0) if tmpErr != nil { return tmpErr } for _, v := range firstDataList { //校验待删除日期数据里面是否存在该元素,如果存在的话,那么移除该元素 if _, ok := removeDataTimeMap[v.DataTime]; ok { delete(removeDataTimeMap, v.DataTime) } //时间戳 if edbData, ok := dataMap[v.DataTime]; ok { if edbData.Value != v.Value { //更新指标数据 edbData.Value = v.Value o.Update(edbData, "Value") } } else { //时间戳 currentDate, _ := time.Parse(utils.FormatDate, v.DataTime) timestamp := currentDate.UnixNano() / 1e6 edbDataZjpj := &EdbDataCalculateZjpj{ EdbInfoId: edbInfo.EdbInfoId, EdbCode: edbInfo.EdbCode, DataTime: v.DataTime, Value: v.Value, Status: 1, CreateTime: time.Now(), ModifyTime: time.Now(), DataTimestamp: timestamp, } addDataList = append(addDataList, edbDataZjpj) } } } //第二个指标 { condition = `` pars = make([]interface{}, 0) condition += " AND data_time >= ? AND edb_info_id = ? " pars = append(pars, edbInfo.CalculateFormula, existItemB.FromEdbInfoId) //第二个指标的数据列表 secondDataList, tmpErr := GetEdbDataListAll(condition, pars, existItemB.FromSource, 0) if tmpErr != nil { return tmpErr } for _, v := range secondDataList { //校验待删除日期数据里面是否存在该元素,如果存在的话,那么移除该元素 if _, ok := removeDataTimeMap[v.DataTime]; ok { delete(removeDataTimeMap, v.DataTime) } if edbData, ok := dataMap[v.DataTime]; ok { if edbData.Value != v.Value { //更新指标数据 edbData.Value = v.Value edbData.ModifyTime = time.Now() _, tmpErr := o.Update(edbData, "Value", "ModifyTime") if tmpErr != nil { fmt.Println("tmpErr:", tmpErr) } } } else { //时间戳 currentDate, _ := time.Parse(utils.FormatDate, v.DataTime) timestamp := currentDate.UnixNano() / 1e6 edbDataZjpj := &EdbDataCalculateZjpj{ EdbInfoId: edbInfo.EdbInfoId, EdbCode: edbInfo.EdbCode, DataTime: v.DataTime, Value: v.Value, Status: 1, CreateTime: time.Now(), ModifyTime: time.Now(), DataTimestamp: timestamp, } addDataList = append(addDataList, edbDataZjpj) } } } //删除已经不存在的累计同比拼接指标数据(由于同比值当日的数据删除了) { removeDateList := make([]string, 0) for dateTime := range removeDataTimeMap { removeDateList = append(removeDateList, dateTime) } 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 = o.Raw(sql, edbInfo.EdbInfoId).Exec() if err != nil { err = errors.New("删除不存在的直接拼接指标数据失败,Err:" + err.Error()) return } } } //数据入库 tmpAddDataList := make([]*EdbDataCalculateZjpj, 0) for _, v := range addDataList { tmpAddDataList = append(tmpAddDataList, v) if len(tmpAddDataList) >= 200 { _, tmpErr := o.InsertMulti(len(tmpAddDataList), tmpAddDataList) if tmpErr != nil { err = tmpErr return } //重新初始化需要加入的数据切片 tmpAddDataList = make([]*EdbDataCalculateZjpj, 0) } } //最后如果还有需要新增的数据,那么就统一入库 if len(tmpAddDataList) > 0 { _, tmpErr := tx.InsertMulti(len(tmpAddDataList), tmpAddDataList) if tmpErr != nil { err = tmpErr return } } return }