package data_manage import ( "fmt" "github.com/shopspring/decimal" "hongze/hongze_task/utils" "github.com/beego/beego/v2/client/orm" "strconv" "strings" "time" ) type EdbInfoCalculateBp 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:"修改时间"` } //变频 func RefreshCalculateBp(edbInfoId int, fromEdbInfo *EdbInfo, edbCode, startDate, endDate string) (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() } }() if err != nil { return } edbInfoIdStr := strconv.Itoa(edbInfoId) //计算数据 var condition string var pars []interface{} condition += " AND edb_info_id=? " pars = append(pars, fromEdbInfo.EdbInfoId) if startDate != "" { condition += " AND data_time>=? " pars = append(pars, startDate) } if endDate != "" { condition += " AND data_time<=? " pars = append(pars, endDate) } dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 0) if err != nil { return err } var existCondition string var existPars []interface{} existCondition += " AND edb_info_id=? " existPars = append(existPars, edbInfoId) dataExistList, err := GetEdbDataListAll(existCondition, existPars, utils.DATA_SOURCE_CALCULATE_BP, 0) if err != nil { return err } existDataMap := make(map[string]*EdbInfoSearchData) for _, v := range dataExistList { existDataMap[v.DataTime] = v } addExistMap := make(map[string]string) dataLen := len(dataList) addSql := ` INSERT INTO edb_data_calculate_bp(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values ` var isAdd bool for i := 0; i < dataLen; i++ { //当期 currentItem := dataList[i] currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime) var day int if i == 0 { day = int(time.Now().Sub(currentDate).Hours() / float64(24)) } else { j := i + 1 if j < dataLen { preItem := dataList[j] preDate, _ := time.Parse(utils.FormatDate, preItem.DataTime) day = int(currentDate.Sub(preDate).Hours() / float64(24)) } } for k := 1; k <= day; k++ { needDayForm := currentDate.AddDate(0, 0, k) if needDayForm.Before(time.Now()) { needDay := needDayForm.Format(utils.FormatDate) if _, ok := existDataMap[needDay]; !ok { existKey := edbCode + needDay if _, ok := addExistMap[existKey]; !ok { currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime) timestamp := currentDate.UnixNano() / 1e6 timestampStr := fmt.Sprintf("%d", timestamp) valStr := decimal.NewFromFloat(currentItem.Value).String() isAdd = true addSql += GetAddSql(edbInfoIdStr, edbCode, needDay, timestampStr, valStr) } addExistMap[existKey] = needDay } } } if _, ok := existDataMap[currentItem.DataTime]; !ok { existKey := edbCode + currentItem.DataTime if _, ok := addExistMap[existKey]; !ok { currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime) timestamp := currentDate.UnixNano() / 1e6 timestampStr := fmt.Sprintf("%d", timestamp) valStr := decimal.NewFromFloat(currentItem.Value).String() isAdd = true addSql += GetAddSql(edbInfoIdStr, edbCode, currentItem.DataTime, timestampStr, valStr) } addExistMap[existKey] = currentItem.DataTime } } if isAdd { addSql = strings.TrimRight(addSql, ",") _, err = tx.Raw(addSql).Exec() if err != nil { fmt.Println("add err:" + err.Error()) return err } } return } type EdbInfoCalculateBpDetail 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:"结束日期"` } func GetEdbInfoCalculateBpDetail(edbInfoId int) (item *EdbInfoCalculateTbzDetail, err error) { o := orm.NewOrmUsingDB("data") sql := ` SELECT a.*,b.start_date,b.end_date FROM edb_info_calculate_mapping AS a INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id WHERE a.edb_info_id=? ` err = o.Raw(sql, edbInfoId).QueryRow(&item) return }