|
@@ -210,25 +210,35 @@ func refreshAllCalculateTbz(to orm.TxOrmer, edbInfoId, source, subSource int, fr
|
|
|
edbInfoIdStr := strconv.Itoa(edbInfoId)
|
|
|
//计算数据
|
|
|
|
|
|
+ // 获取来源指标数据
|
|
|
dataList, err := GetEdbDataListAll(fromEdbInfo.Source, fromEdbInfo.SubSource, FindEdbDataListAllCond{
|
|
|
- EdbInfoId: fromEdbInfo.EdbInfoId,
|
|
|
- StartDataTime: startDate,
|
|
|
- StartDataTimeCond: ">=",
|
|
|
+ EdbInfoId: fromEdbInfo.EdbInfoId,
|
|
|
+ //StartDataTime: startDate,
|
|
|
+ //StartDataTimeCond: ">=",
|
|
|
}, 0)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
- var dateArr []string
|
|
|
- dataMap := make(map[string]*EdbInfoSearchData)
|
|
|
- for _, v := range dataList {
|
|
|
- dateArr = append(dateArr, v.DataTime)
|
|
|
- dataMap[v.DataTime] = v
|
|
|
+
|
|
|
+ // 来源指标数据参与计算
|
|
|
+ calculateDataList, err := EdbInfoSearchDataToEdbInfoData(dataList)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ baseCalculate := BaseCalculate{
|
|
|
+ DataList: calculateDataList,
|
|
|
+ Frequency: fromEdbInfo.Frequency,
|
|
|
+ Source: 3,
|
|
|
+ }
|
|
|
+ dateDataMap, err, _ := baseCalculate.Tbz()
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
}
|
|
|
- fmt.Println("source:", source)
|
|
|
- //获取指标所有数据
|
|
|
+
|
|
|
+ //获取当前已经入库的指标所有数据
|
|
|
existDataList := make([]*EdbData, 0)
|
|
|
dataTableName := GetEdbDataTableName(source, subSource)
|
|
|
- fmt.Println("dataTableName:", dataTableName)
|
|
|
+ //fmt.Println("dataTableName:", dataTableName)
|
|
|
sql := `SELECT * FROM %s WHERE edb_info_id=? `
|
|
|
sql = fmt.Sprintf(sql, dataTableName)
|
|
|
_, err = to.Raw(sql, edbInfoId).QueryRows(&existDataList)
|
|
@@ -236,206 +246,65 @@ func refreshAllCalculateTbz(to orm.TxOrmer, edbInfoId, source, subSource int, fr
|
|
|
return err
|
|
|
}
|
|
|
existDataMap := make(map[string]string)
|
|
|
+ removeDataTimeMap := make(map[string]bool) //需要移除的日期数据
|
|
|
for _, v := range existDataList {
|
|
|
existDataMap[v.DataTime] = v.Value
|
|
|
+ removeDataTimeMap[v.DataTime] = true
|
|
|
}
|
|
|
- fmt.Println("existDataMap:", existDataMap)
|
|
|
+ //fmt.Println("existDataMap:", existDataMap)
|
|
|
+
|
|
|
addSql := ` INSERT INTO edb_data_calculate_tbz(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
|
|
|
var isAdd bool
|
|
|
existAddDataMap := make(map[string]string)
|
|
|
- for _, av := range dateArr {
|
|
|
- currentItem := dataMap[av]
|
|
|
- if currentItem != nil {
|
|
|
- //当前日期
|
|
|
- currentDate, err := time.ParseInLocation(utils.FormatDate, av, time.Local)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- //上一年的日期
|
|
|
- preDate := currentDate.AddDate(-1, 0, 0)
|
|
|
- preDateStr := preDate.Format(utils.FormatDate)
|
|
|
- if findItem, ok := dataMap[preDateStr]; ok { //上一年同期找到
|
|
|
- //dataTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
|
|
|
- timestamp := currentDate.UnixNano() / 1e6
|
|
|
- timestampStr := fmt.Sprintf("%d", timestamp)
|
|
|
- val := TbzDiv(currentItem.Value, findItem.Value)
|
|
|
-
|
|
|
- if existVal, ok := existDataMap[av]; !ok {
|
|
|
- if _, existOk := existAddDataMap[av]; !existOk {
|
|
|
- addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
|
|
|
- isAdd = true
|
|
|
- }
|
|
|
- existAddDataMap[av] = av
|
|
|
- } else {
|
|
|
- existValDecimal, err := decimal.NewFromString(existVal)
|
|
|
- existStr := existValDecimal.String()
|
|
|
- if existStr != val {
|
|
|
- sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
|
|
|
- sql = fmt.Sprintf(sql, dataTableName)
|
|
|
- _, err = to.Raw(sql, val, edbInfoId, av).Exec()
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- continue
|
|
|
- } else {
|
|
|
- if fromEdbInfo.Frequency == "月度" { //向上和向下,各找一个月
|
|
|
- for i := 0; i <= 35; i++ {
|
|
|
- nextDateDay := preDate.AddDate(0, 0, i)
|
|
|
- nextDateDayStr := nextDateDay.Format(utils.FormatDate)
|
|
|
- if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
|
|
|
- timestamp := currentDate.UnixNano() / 1e6
|
|
|
- timestampStr := fmt.Sprintf("%d", timestamp)
|
|
|
- val := TbzDiv(currentItem.Value, findItem.Value)
|
|
|
-
|
|
|
- if existVal, ok := existDataMap[av]; !ok {
|
|
|
- if _, existOk := existAddDataMap[av]; !existOk {
|
|
|
- addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
|
|
|
- isAdd = true
|
|
|
- }
|
|
|
- existAddDataMap[av] = av
|
|
|
- } else {
|
|
|
- existValDecimal, err := decimal.NewFromString(existVal)
|
|
|
- existStr := existValDecimal.String()
|
|
|
- if existStr != val {
|
|
|
- sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
|
|
|
- sql = fmt.Sprintf(sql, dataTableName)
|
|
|
- _, err = to.Raw(sql, val, edbInfoId, av).Exec()
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- break
|
|
|
- } else {
|
|
|
- preDateDay := preDate.AddDate(0, 0, -i)
|
|
|
- preDateDayStr := preDateDay.Format(utils.FormatDate)
|
|
|
- if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
|
|
|
- timestamp := currentDate.UnixNano() / 1e6
|
|
|
- timestampStr := fmt.Sprintf("%d", timestamp)
|
|
|
- val := TbzDiv(currentItem.Value, findItem.Value)
|
|
|
|
|
|
- if existVal, ok := existDataMap[av]; !ok {
|
|
|
- if _, existOk := existAddDataMap[av]; !existOk {
|
|
|
- addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
|
|
|
- isAdd = true
|
|
|
- }
|
|
|
- existAddDataMap[av] = av
|
|
|
- } else {
|
|
|
- existValDecimal, err := decimal.NewFromString(existVal)
|
|
|
- existStr := existValDecimal.String()
|
|
|
- if existStr != val {
|
|
|
- sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
|
|
|
- sql = fmt.Sprintf(sql, dataTableName)
|
|
|
- _, err = to.Raw(sql, val, edbInfoId, av).Exec()
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- break
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- } else if fromEdbInfo.Frequency == "季度" || fromEdbInfo.Frequency == "年度" {
|
|
|
- if findItem, ok := dataMap[preDateStr]; ok { //上一年同期->下一个月找到
|
|
|
- timestamp := currentDate.UnixNano() / 1e6
|
|
|
- timestampStr := fmt.Sprintf("%d", timestamp)
|
|
|
- val := TbzDiv(currentItem.Value, findItem.Value)
|
|
|
+ for currentDate, valFloat := range dateDataMap {
|
|
|
+ currentDateStr := currentDate.Format(utils.FormatDate)
|
|
|
+ timestamp := currentDate.UnixNano() / 1e6
|
|
|
+ timestampStr := fmt.Sprintf("%d", timestamp)
|
|
|
+ val := decimal.NewFromFloat(valFloat).String()
|
|
|
|
|
|
- if existVal, ok := existDataMap[av]; !ok {
|
|
|
- if _, existOk := existAddDataMap[av]; !existOk {
|
|
|
- addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
|
|
|
- isAdd = true
|
|
|
- }
|
|
|
- existAddDataMap[av] = av
|
|
|
- } else {
|
|
|
- existValDecimal, err := decimal.NewFromString(existVal)
|
|
|
- existStr := existValDecimal.String()
|
|
|
- if existStr != val {
|
|
|
- sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
|
|
|
- sql = fmt.Sprintf(sql, dataTableName)
|
|
|
- _, err = to.Raw(sql, val, edbInfoId, av).Exec()
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- break
|
|
|
- }
|
|
|
- } else {
|
|
|
- nextDateDay := preDate.AddDate(0, 0, 1)
|
|
|
- nextDateDayStr := nextDateDay.Format(utils.FormatDate)
|
|
|
+ //校验待删除日期数据里面是否存在该元素,如果存在的话,那么移除该日期
|
|
|
+ delete(removeDataTimeMap, currentDateStr)
|
|
|
|
|
|
- preDateDay := preDate.AddDate(0, 0, -1)
|
|
|
- preDateDayStr := preDateDay.Format(utils.FormatDate)
|
|
|
-
|
|
|
- for i := 0; i < 35; i++ {
|
|
|
- if i >= 1 {
|
|
|
- nextDateDay = nextDateDay.AddDate(0, 0, i)
|
|
|
- nextDateDayStr = nextDateDay.Format(utils.FormatDate)
|
|
|
- }
|
|
|
- if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
|
|
|
- timestamp := currentDate.UnixNano() / 1e6
|
|
|
- timestampStr := fmt.Sprintf("%d", timestamp)
|
|
|
- val := TbzDiv(currentItem.Value, findItem.Value)
|
|
|
+ if existVal, ok := existDataMap[currentDateStr]; !ok {
|
|
|
+ if _, existOk := existAddDataMap[currentDateStr]; !existOk {
|
|
|
+ addSql += GetAddSql(edbInfoIdStr, edbCode, currentDateStr, timestampStr, val)
|
|
|
+ isAdd = true
|
|
|
+ }
|
|
|
+ existAddDataMap[currentDateStr] = currentDateStr
|
|
|
+ } else {
|
|
|
+ existValDecimal, err := decimal.NewFromString(existVal)
|
|
|
+ existStr := existValDecimal.String()
|
|
|
+ if existStr != val {
|
|
|
+ sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
|
|
|
+ sql = fmt.Sprintf(sql, dataTableName)
|
|
|
+ _, err = to.Raw(sql, val, edbInfoId, currentDateStr).Exec()
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- if existVal, ok := existDataMap[av]; !ok {
|
|
|
- if _, existOk := existAddDataMap[av]; !existOk {
|
|
|
- addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
|
|
|
- isAdd = true
|
|
|
- }
|
|
|
- existAddDataMap[av] = av
|
|
|
- } else {
|
|
|
- existValDecimal, err := decimal.NewFromString(existVal)
|
|
|
- existStr := existValDecimal.String()
|
|
|
- if existStr != val {
|
|
|
- sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
|
|
|
- sql = fmt.Sprintf(sql, dataTableName)
|
|
|
- _, err = to.Raw(sql, val, edbInfoId, av).Exec()
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- break
|
|
|
- } else {
|
|
|
- if i >= 1 {
|
|
|
- preDateDay = preDateDay.AddDate(0, 0, -i)
|
|
|
- preDateDayStr = preDateDay.Format(utils.FormatDate)
|
|
|
- }
|
|
|
- if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
|
|
|
- timestamp := currentDate.UnixNano() / 1e6
|
|
|
- timestampStr := fmt.Sprintf("%d", timestamp)
|
|
|
- val := TbzDiv(currentItem.Value, findItem.Value)
|
|
|
+ //删除已经不存在的指标数据(由于该指标当日的数据删除了)
|
|
|
+ {
|
|
|
+ removeDateList := make([]string, 0)
|
|
|
+ for dateTime := range removeDataTimeMap {
|
|
|
+ removeDateList = append(removeDateList, dateTime)
|
|
|
+ }
|
|
|
+ removeNum := len(removeDateList)
|
|
|
+ if removeNum > 0 {
|
|
|
+ //如果拼接指标变更了,那么需要删除所有的指标数据
|
|
|
+ sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (`+utils.GetOrmInReplace(removeNum)+`) `, dataTableName)
|
|
|
|
|
|
- if existVal, ok := existDataMap[av]; !ok {
|
|
|
- if _, existOk := existAddDataMap[av]; !existOk {
|
|
|
- addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
|
|
|
- isAdd = true
|
|
|
- }
|
|
|
- existAddDataMap[av] = av
|
|
|
- } else {
|
|
|
- existValDecimal, err := decimal.NewFromString(existVal)
|
|
|
- existStr := existValDecimal.String()
|
|
|
- if existStr != val {
|
|
|
- sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
|
|
|
- sql = fmt.Sprintf(sql, dataTableName)
|
|
|
- _, err = to.Raw(sql, val, edbInfoId, av).Exec()
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- break
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ _, 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()
|
|
@@ -443,6 +312,7 @@ func refreshAllCalculateTbz(to orm.TxOrmer, edbInfoId, source, subSource int, fr
|
|
|
return err
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
return
|
|
|
}
|
|
|
|