package data_manage import ( "fmt" "hongze/hongze_task/utils" "rdluck_tools/orm" "sort" "strconv" "strings" "time" ) func GetEdbDataQuarterCount(edbInfoId int) (count int, err error) { o := orm.NewOrm() o.Using("data") hsql := `SELECT COUNT(1) AS count FROM edb_data_quarter WHERE edb_info_id=? ` err = o.Raw(hsql, edbInfoId).QueryRow(&count) return } //指标季度数据计算 func RefreshCalculateQuarter(edbInfoId, source int, edbCode string) (err error) { o := orm.NewOrm() o.Using("data") o.Begin() defer func() { if err != nil { fmt.Println("Err:", err) o.Rollback() } else { o.Commit() } }() edbInfoIdStr := strconv.Itoa(edbInfoId) startDate := time.Now().AddDate(-2, -2, 0).Format(utils.FormatDate) //计算数据 var condition string var pars []interface{} condition += " AND edb_info_id=? " pars = append(pars, edbInfoId) condition += " AND data_time>=? " pars = append(pars, startDate) dataList, err := GetEdbDataListAll(condition, pars, source, 0) if err != nil { return err } //计算数据 var existCondition string var existPars []interface{} existCondition += " AND edb_info_id=? " existPars = append(existPars, edbInfoId) existCondition += " AND data_time>=? " existPars = append(existPars, startDate) existDataList, err := GetEdbDataListAll(existCondition, existPars, utils.DATA_SOURCE_QUARTER, 0) if err != nil { return err } existMap := make(map[string]float64) for _, v := range existDataList { existMap[v.DataTime] = v.Value } var yearArr []int yearMap := make(map[int]int) dataMap := make(map[string]*EdbInfoSearchData) for _, v := range dataList { //日其中获取年 itemDate, err := time.Parse(utils.FormatDate, v.DataTime) if err != nil { return err } year := itemDate.Year() if _, ok := yearMap[year]; !ok { yearArr = append(yearArr, year) yearMap[year] = year } dataMap[v.DataTime] = v } sort.Sort(sort.Reverse(sort.IntSlice(yearArr))) addSql := ` INSERT INTO edb_data_quarter(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values ` var isAdd bool yearLen := len(yearArr) fmt.Println(yearArr) for yk, yv := range yearArr { currentYear := yv hsql := `SELECT * FROM holiday WHERE year=? ` currentHolidayObj := new(Holiday) err = o.Raw(hsql, currentYear).QueryRow(¤tHolidayObj) if err != nil { return err } nextIndex := yk + 1 if nextIndex < yearLen { nextYear := yearArr[nextIndex] fmt.Println("year:", currentYear, nextYear) nextHolidayObj := new(Holiday) hsql := `SELECT * FROM holiday WHERE year=? ` err = o.Raw(hsql, nextYear).QueryRow(&nextHolidayObj) if err != nil { return err } nextHolidayStr := nextHolidayObj.HolidayDate.Format(utils.FormatDate) currentHolidayStr := currentHolidayObj.HolidayDate.Format(utils.FormatDate) if findItem, ok := dataMap[nextHolidayStr]; ok { timestamp := currentHolidayObj.HolidayDate.UnixNano() / 1e6 timestampStr := fmt.Sprintf("%d", timestamp) if _, ok := existMap[currentHolidayStr]; !ok { addSql += GetAddSql(edbInfoIdStr, edbCode, currentHolidayStr, timestampStr, findItem.Value) isAdd = true } } nextMinDate := strconv.Itoa(nextYear) + "-01-01" nextMaxDate := strconv.Itoa(nextYear) + "-12-31" currentMinDate := strconv.Itoa(currentYear) + "-01-01" currentMaxDate := strconv.Itoa(currentYear) + "-12-31" i := 0 for { i-- nextHolidayFormat := nextHolidayObj.HolidayDate.AddDate(0, 0, i) currentHolidayFormat := currentHolidayObj.HolidayDate.AddDate(0, 0, i) nextHolidayStr := nextHolidayFormat.Format(utils.FormatDate) currentHolidayStr := currentHolidayFormat.Format(utils.FormatDate) fmt.Println("currentHolidayStr:", currentHolidayStr, nextHolidayStr) if findItem, ok := dataMap[nextHolidayStr]; ok { timestamp := currentHolidayFormat.UnixNano() / 1e6 timestampStr := fmt.Sprintf("%d", timestamp) if _, ok := existMap[currentHolidayStr]; !ok { addSql += GetAddSql(edbInfoIdStr, edbCode, currentHolidayStr, timestampStr, findItem.Value) isAdd = true } } //fmt.Println("nextHolidayStr:", nextHolidayStr,nextMinDate) if nextHolidayStr == nextMinDate || currentHolidayStr == currentMinDate { fmt.Println("min跳出循环:" + nextMinDate) break } } n := 0 for { n++ nextHolidayFormat := nextHolidayObj.HolidayDate.AddDate(0, 0, n) currentHolidayFormat := currentHolidayObj.HolidayDate.AddDate(0, 0, n) nextHolidayStr := nextHolidayFormat.Format(utils.FormatDate) currentHolidayStr := currentHolidayFormat.Format(utils.FormatDate) fmt.Println("currentHolidayStr:", currentHolidayStr, nextHolidayStr) if findItem, ok := dataMap[nextHolidayStr]; ok { timestamp := currentHolidayFormat.UnixNano() / 1e6 timestampStr := fmt.Sprintf("%d", timestamp) if _, ok := existMap[currentHolidayStr]; !ok { addSql += GetAddSql(edbInfoIdStr, edbCode, currentHolidayStr, timestampStr, findItem.Value) isAdd = true } } //fmt.Println("nextHolidayStr:", nextHolidayStr,nextMaxDate) if nextHolidayStr == nextMaxDate || currentHolidayStr == currentMaxDate { fmt.Println("max跳出循环:" + nextMaxDate) break } } } } fmt.Println("isAdd", isAdd) if isAdd { addSql = strings.TrimRight(addSql, ",") utils.FileLog.Info(addSql) _, err = o.Raw(addSql).Exec() fmt.Println("err:", err) if err != nil { return err } } return }