|
@@ -4,6 +4,7 @@ import (
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
"github.com/beego/beego/v2/client/orm"
|
|
|
+ "github.com/nosixtools/solarlunar"
|
|
|
"github.com/shopspring/decimal"
|
|
|
"hongze/hongze_edb_lib/utils"
|
|
|
"strconv"
|
|
@@ -88,7 +89,7 @@ func AddCalculateCjjx(req *EdbInfoCalculateBatchSaveReq, fromEdbInfo *EdbInfo, e
|
|
|
}
|
|
|
|
|
|
|
|
|
- err = refreshAllCalculateCjjx(to, edbInfo.EdbInfoId, edbInfo.Source, fromEdbInfo, edbInfo.EdbCode, "", "", formulaInt)
|
|
|
+ err = refreshAllCalculateCjjx(to, edbInfo.EdbInfoId, edbInfo.Source, fromEdbInfo, edbInfo.EdbCode, "", "", "公历", formulaInt)
|
|
|
|
|
|
return
|
|
|
}
|
|
@@ -187,7 +188,7 @@ func EditCalculateCjjx(req *EdbInfoCalculateBatchEditReq, edbInfo, fromEdbInfo *
|
|
|
}
|
|
|
|
|
|
|
|
|
- err = refreshAllCalculateCjjx(to, edbInfo.EdbInfoId, edbInfo.Source, fromEdbInfo, edbInfo.EdbCode, "", "", formulaInt)
|
|
|
+ err = refreshAllCalculateCjjx(to, edbInfo.EdbInfoId, edbInfo.Source, fromEdbInfo, edbInfo.EdbCode, "", "", "公历", formulaInt)
|
|
|
|
|
|
return
|
|
|
}
|
|
@@ -209,13 +210,13 @@ func RefreshAllCalculateCjjx(edbInfoId, source int, fromEdbInfo *EdbInfo, edbCod
|
|
|
}()
|
|
|
|
|
|
|
|
|
- err = refreshAllCalculateCjjx(to, edbInfoId, source, fromEdbInfo, edbCode, startDate, endDate, formulaInt)
|
|
|
+ err = refreshAllCalculateCjjx(to, edbInfoId, source, fromEdbInfo, edbCode, startDate, endDate, "农历", formulaInt)
|
|
|
|
|
|
return
|
|
|
}
|
|
|
|
|
|
|
|
|
-func refreshAllCalculateCjjx(to orm.TxOrmer, edbInfoId, source int, fromEdbInfo *EdbInfo, edbCode, startDate, endDate string, formulaInt int) (err error) {
|
|
|
+func refreshAllCalculateCjjx(to orm.TxOrmer, edbInfoId, source int, fromEdbInfo *EdbInfo, edbCode, startDate, endDate, calendar string, formulaInt int) (err error) {
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
@@ -257,8 +258,10 @@ func refreshAllCalculateCjjx(to orm.TxOrmer, edbInfoId, source int, fromEdbInfo
|
|
|
return err
|
|
|
}
|
|
|
existDataMap := make(map[string]string)
|
|
|
+ removeDataTimeMap := make(map[string]int)
|
|
|
for _, v := range existDataList {
|
|
|
existDataMap[edbCode+v.DataTime] = v.Value
|
|
|
+ removeDataTimeMap[v.DataTime] = 1
|
|
|
}
|
|
|
|
|
|
addSql := ` INSERT INTO edb_data_calculate_cjjx(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
|
|
@@ -269,6 +272,12 @@ func refreshAllCalculateCjjx(to orm.TxOrmer, edbInfoId, source int, fromEdbInfo
|
|
|
isCompatibility = true
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ moveDayMap := make(map[int]int, 0)
|
|
|
+ var lastDataDay time.Time
|
|
|
+ if len(dataList) > 0 {
|
|
|
+ lastDataDay, _ = time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
|
|
|
+ }
|
|
|
for _, av := range dateArr {
|
|
|
currentItem := dataMap[av]
|
|
|
if currentItem != nil {
|
|
@@ -280,15 +289,31 @@ func refreshAllCalculateCjjx(to orm.TxOrmer, edbInfoId, source int, fromEdbInfo
|
|
|
return
|
|
|
}
|
|
|
pastValueList = append(pastValueList, currentItem.Value)
|
|
|
+
|
|
|
for i := 1; i < formulaInt; i++ {
|
|
|
-
|
|
|
- preDate := currentDate.AddDate(-i, 0, 0)
|
|
|
- preDateStr := preDate.Format(utils.FormatDate)
|
|
|
- if findItem, ok := dataMap[preDateStr]; ok {
|
|
|
+
|
|
|
+ hisoryPreDate := currentDate.AddDate(-i, 0, 0)
|
|
|
+ moveDay := 0
|
|
|
+ if calendar == "农历" {
|
|
|
+ if tmpMoveDay, ok := moveDayMap[hisoryPreDate.Year()]; !ok {
|
|
|
+ moveDay, err = getMoveDay(lastDataDay, hisoryPreDate)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ moveDay = tmpMoveDay
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ hisoryPreDate = hisoryPreDate.AddDate(0, 0, moveDay)
|
|
|
+ }
|
|
|
+
|
|
|
+ hisoryPreDateStr := hisoryPreDate.Format(utils.FormatDate)
|
|
|
+ if findItem, ok := dataMap[hisoryPreDateStr]; ok {
|
|
|
pastValueList = append(pastValueList, findItem.Value)
|
|
|
} else if isCompatibility {
|
|
|
- nextDateDay := preDate
|
|
|
- preDateDay := preDate
|
|
|
+ nextDateDay := hisoryPreDate
|
|
|
+ preDateDay := hisoryPreDate
|
|
|
for i := 0; i < 35; i++ {
|
|
|
nextDateDayStr := nextDateDay.Format(utils.FormatDate)
|
|
|
if findItem, ok := dataMap[nextDateDayStr]; ok {
|
|
@@ -305,9 +330,17 @@ func refreshAllCalculateCjjx(to orm.TxOrmer, edbInfoId, source int, fromEdbInfo
|
|
|
preDateDay = preDateDay.AddDate(0, 0, -1)
|
|
|
}
|
|
|
}
|
|
|
+ if av == "2022-11-28" {
|
|
|
+ fmt.Println(moveDay)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if av == "2022-11-28" {
|
|
|
+ fmt.Println(pastValueList)
|
|
|
}
|
|
|
|
|
|
if len(pastValueList) == formulaInt {
|
|
|
+ delete(removeDataTimeMap, av)
|
|
|
+
|
|
|
val := CjjxSub(currentItem.Value, pastValueList)
|
|
|
|
|
|
if existVal, ok := existDataMap[edbCode+av]; !ok {
|
|
@@ -331,6 +364,26 @@ func refreshAllCalculateCjjx(to orm.TxOrmer, edbInfoId, source int, fromEdbInfo
|
|
|
existDataMap[edbCode+av] = av
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ {
|
|
|
+ removeDateList := make([]string, 0)
|
|
|
+ for dateTime := range removeDataTimeMap {
|
|
|
+ removeDateList = append(removeDateList, dateTime)
|
|
|
+ }
|
|
|
+ removeNum := len(removeDateList)
|
|
|
+ if removeNum > 0 {
|
|
|
+
|
|
|
+ tableName := GetEdbDataTableName(source)
|
|
|
+ sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (`+utils.GetOrmInReplace(removeNum)+`) `, tableName)
|
|
|
+ _, err = to.Raw(sql, edbInfoId, removeDateList).Exec()
|
|
|
+ if err != nil {
|
|
|
+ err = fmt.Errorf("删除不存在的超季节性指标数据失败,Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
if isAdd {
|
|
|
addSql = strings.TrimRight(addSql, ",")
|
|
|
_, err = to.Raw(addSql).Exec()
|
|
@@ -341,6 +394,39 @@ func refreshAllCalculateCjjx(to orm.TxOrmer, edbInfoId, source int, fromEdbInfo
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+func getMoveDay(lastDataDay, currentDataDay time.Time) (moveDay int, err error) {
|
|
|
+ if lastDataDay.Month() >= 11 {
|
|
|
+ lastDataDay = lastDataDay.AddDate(1, 0, 0)
|
|
|
+ }
|
|
|
+
|
|
|
+ currentYear := lastDataDay.Year()
|
|
|
+ currentYearCjnl := fmt.Sprintf("%d-01-01", currentYear)
|
|
|
+ currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false)
|
|
|
+ currentYearCjglTime, tmpErr := time.ParseInLocation(utils.FormatDate, currentYearCjgl, time.Local)
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = errors.New("当前春节公历日期转换失败:" + tmpErr.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ tmpYearCjnl := fmt.Sprintf("%d-01-01", currentDataDay.Year())
|
|
|
+
|
|
|
+ tmpYearCjgl := solarlunar.LunarToSolar(tmpYearCjnl, false)
|
|
|
+
|
|
|
+ tmpYearCjglTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpYearCjgl, time.Local)
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = errors.New(fmt.Sprintf("%d公历日期转换失败:%s", currentDataDay.Year(), tmpErr.Error()))
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ tmpCurrentYearCjglTime := currentYearCjglTime.AddDate(currentDataDay.Year()-currentYear, 0, 0)
|
|
|
+ moveDay = utils.GetTimeSubDay(tmpYearCjglTime, tmpCurrentYearCjglTime)
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
@@ -352,7 +438,7 @@ func CjjxSub(currValue float64, pastValue []float64) (value string) {
|
|
|
numDecimal := decimal.NewFromInt(int64(num))
|
|
|
|
|
|
af := decimal.NewFromFloat(currValue)
|
|
|
- fmt.Println(af)
|
|
|
+
|
|
|
|
|
|
bf := decimal.NewFromFloat(pastValue[0])
|
|
|
|