123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705 |
- package models
- import (
- "errors"
- "eta/eta_index_lib/utils"
- "fmt"
- "github.com/beego/beego/v2/client/orm"
- "github.com/shopspring/decimal"
- "strconv"
- "strings"
- "time"
- )
- // SavePredictCalculateTcz 同差值
- func SavePredictCalculateTcz(reqEdbInfoId, classifyId int, edbName, frequency, unit, formula string, fromEdbInfo *EdbInfo, edbCode, uniqueCode string, sysUserId int, sysUserRealName, lang string) (edbInfo *EdbInfo, latestDateStr string, latestValue float64, err error, errMsg string) {
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- fmt.Println("AddCalculateTcz,Err:" + err.Error())
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- fmt.Println("reqEdbInfoId:", reqEdbInfoId)
- if reqEdbInfoId <= 0 {
- edbInfo = &EdbInfo{
- //EdbInfoId: 0,
- EdbInfoType: 1,
- SourceName: "预测同差",
- Source: utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ,
- EdbCode: edbCode,
- EdbName: edbName,
- EdbNameSource: edbName,
- Frequency: frequency,
- Unit: unit,
- //StartDate: "",
- //EndDate: "",
- ClassifyId: classifyId,
- SysUserId: sysUserId,
- SysUserRealName: sysUserRealName,
- UniqueCode: uniqueCode,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- MinValue: 0,
- MaxValue: 0,
- CalculateFormula: formula,
- EdbType: 2,
- Sort: GetAddEdbMaxSortByClassifyId(classifyId, utils.PREDICT_EDB_INFO_TYPE),
- MoveType: 0,
- MoveFrequency: "",
- NoUpdate: 0,
- ServerUrl: "",
- EdbNameEn: edbName,
- UnitEn: unit,
- LatestDate: "",
- LatestValue: 0,
- ChartImage: "",
- }
- newEdbInfoId, tmpErr := to.Insert(edbInfo)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- edbInfo.EdbInfoId = int(newEdbInfoId)
- // 添加关联关系
- {
- calculateMappingItem := &EdbInfoCalculateMapping{
- EdbInfoCalculateMappingId: 0,
- EdbInfoId: edbInfo.EdbInfoId,
- Source: edbInfo.Source,
- SourceName: edbInfo.SourceName,
- EdbCode: edbCode,
- FromEdbInfoId: fromEdbInfo.EdbInfoId,
- FromEdbCode: fromEdbInfo.EdbCode,
- FromEdbName: fromEdbInfo.EdbName,
- FromSource: fromEdbInfo.Source,
- FromSourceName: fromEdbInfo.SourceName,
- FromTag: "",
- Sort: 1,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- }
- _, err = to.Insert(calculateMappingItem)
- if err != nil {
- return
- }
- }
- } else {
- edbInfo, err = GetEdbInfoById(reqEdbInfoId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = `获取指标信息失败`
- }
- return
- }
- if edbInfo.EdbInfoType != 1 {
- errMsg = `该指标不是预测指标`
- err = errors.New(errMsg)
- return
- }
- latestDateStr = edbInfo.LatestDate
- latestValue = edbInfo.LatestValue
- //修改指标信息
- switch lang {
- case utils.EnLangVersion:
- edbInfo.EdbNameEn = edbName
- edbInfo.UnitEn = unit
- default:
- edbInfo.EdbName = edbName
- edbInfo.Unit = unit
- edbInfo.EdbNameSource = edbName
- }
- edbInfo.Frequency = frequency
- edbInfo.ClassifyId = classifyId
- edbInfo.ModifyTime = time.Now()
- _, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "ModifyTime", "EdbNameEn", "UnitEn")
- if err != nil {
- return
- }
- var existCondition string
- var existPars []interface{}
- existCondition += " AND edb_info_id=? AND from_edb_info_id=? "
- existPars = append(existPars, edbInfo.EdbInfoId, fromEdbInfo.EdbInfoId)
- //判断计算指标是否被更换
- var count int
- count, err = GetEdbInfoCalculateCountByCondition(existCondition, existPars)
- if err != nil {
- err = errors.New("判断指标是否改变失败,Err:" + err.Error())
- return
- }
- if count > 0 { // 指标未被替换,无需重新计算
- return
- }
- //删除,计算指标关联的,基础指标的关联关系
- sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? `
- _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
- if err != nil {
- return
- }
- // 删除该指标所有的同差数据
- dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ, utils.DATA_SUB_SOURCE_EDB)
- fmt.Println("dataTableName:", dataTableName)
- deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
- deleteSql = fmt.Sprintf(deleteSql, dataTableName)
- _, err = to.Raw(deleteSql, edbInfo.EdbInfoId).Exec()
- if err != nil {
- return
- }
- }
- edbInfoIdStr := strconv.Itoa(edbInfo.EdbInfoId)
- //获取源指标数据
- fmt.Println("EdbInfoId:", fromEdbInfo.EdbInfoId)
- dataList, err := GetPredictEdbDataListAll(fromEdbInfo, 0)
- if err != nil {
- return
- }
- latestDateStr = fromEdbInfo.LatestDate
- // 计算数据
- var dateArr []string
- dataMap := make(map[string]*EdbInfoSearchData)
- for _, v := range dataList {
- dateArr = append(dateArr, v.DataTime)
- dataMap[v.DataTime] = v
- }
- //fmt.Println("Frequency:", fromEdbInfo.Frequency)
- addSql := ` INSERT INTO edb_data_predict_calculate_tcz (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
- var isAdd bool
- existMap := make(map[string]string)
- for _, av := range dateArr {
- //fmt.Println("dateArr:", ak, av)
- currentItem := dataMap[av]
- if currentItem != nil {
- //当前日期
- currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, av, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //上一年的日期
- 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)
- if _, ok := existMap[edbCode+av]; !ok {
- timestamp := currentDate.UnixNano() / 1e6
- timestampStr := fmt.Sprintf("%d", timestamp)
- val := TczSub(currentItem.Value, findItem.Value)
- addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
- isAdd = true
- // 实际数据的值
- if fromEdbInfo.LatestDate == av {
- latestValueDecimal, tmpErr := decimal.NewFromString(val)
- if tmpErr != nil {
- return
- }
- latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
- }
- }
- existMap[edbCode+av] = av
- } else {
- if fromEdbInfo.Frequency == "月度" { //向上和向下,各找一个月
- nextDateDay := preDate
- preDateDay := preDate
- for i := 0; i <= 35; i++ {
- nextDateDayStr := nextDateDay.Format(utils.FormatDate)
- if findItem, ok := dataMap[nextDateDayStr]; ok { //下一年同期->下一个月找到
- if _, ok := existMap[edbCode+av]; !ok {
- timestamp := currentDate.UnixNano() / 1e6
- timestampStr := fmt.Sprintf("%d", timestamp)
- val := TczSub(currentItem.Value, findItem.Value)
- addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
- isAdd = true
- // 实际数据的值
- if fromEdbInfo.LatestDate == av {
- latestValueDecimal, tmpErr := decimal.NewFromString(val)
- if tmpErr != nil {
- return
- }
- latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
- }
- }
- existMap[edbCode+av] = av
- break
- } else {
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
- if _, ok := existMap[edbCode+av]; !ok {
- timestamp := currentDate.UnixNano() / 1e6
- timestampStr := fmt.Sprintf("%d", timestamp)
- val := TczSub(currentItem.Value, findItem.Value)
- addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
- isAdd = true
- // 实际数据的值
- if fromEdbInfo.LatestDate == av {
- latestValueDecimal, tmpErr := decimal.NewFromString(val)
- if tmpErr != nil {
- return
- }
- latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
- }
- }
- existMap[edbCode+av] = av
- break
- }
- }
- nextDateDay = nextDateDay.AddDate(0, 0, 1)
- preDateDay = preDateDay.AddDate(0, 0, -1)
- }
- } else if fromEdbInfo.Frequency == "季度" || fromEdbInfo.Frequency == "年度" {
- if findItem, ok := dataMap[preDateStr]; ok { //上一年同期->下一个月找到
- if _, ok := existMap[edbCode+av]; !ok {
- timestamp := currentDate.UnixNano() / 1e6
- timestampStr := fmt.Sprintf("%d", timestamp)
- val := TczSub(currentItem.Value, findItem.Value)
- addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
- isAdd = true
- // 实际数据的值
- if fromEdbInfo.LatestDate == av {
- latestValueDecimal, tmpErr := decimal.NewFromString(val)
- if tmpErr != nil {
- return
- }
- latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
- }
- }
- existMap[edbCode+av] = av
- break
- }
- } else {
- nextDateDay := preDate
- preDateDay := preDate
- for i := 0; i < 35; i++ {
- nextDateDayStr := nextDateDay.Format(utils.FormatDate)
- if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
- if _, ok := existMap[edbCode+av]; !ok {
- timestamp := currentDate.UnixNano() / 1e6
- timestampStr := fmt.Sprintf("%d", timestamp)
- val := TczSub(currentItem.Value, findItem.Value)
- addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
- isAdd = true
- // 实际数据的值
- if fromEdbInfo.LatestDate == av {
- latestValueDecimal, tmpErr := decimal.NewFromString(val)
- if tmpErr != nil {
- return
- }
- latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
- }
- }
- existMap[edbCode+av] = av
- break
- } else {
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
- if _, ok := existMap[edbCode+av]; !ok {
- timestamp := currentDate.UnixNano() / 1e6
- timestampStr := fmt.Sprintf("%d", timestamp)
- val := TczSub(currentItem.Value, findItem.Value)
- addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
- isAdd = true
- // 实际数据的值
- if fromEdbInfo.LatestDate == av {
- latestValueDecimal, tmpErr := decimal.NewFromString(val)
- if tmpErr != nil {
- return
- }
- latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
- }
- }
- existMap[edbCode+av] = av
- break
- } else {
- }
- }
- nextDateDay = nextDateDay.AddDate(0, 0, 1)
- preDateDay = preDateDay.AddDate(0, 0, -1)
- }
- }
- }
- }
- }
- if isAdd {
- addSql = strings.TrimRight(addSql, ",")
- _, err = to.Raw(addSql).Exec()
- if err != nil {
- return
- }
- }
- return
- }
- // RefreshAllPredictCalculateTcz 刷新全部同差值数据
- func RefreshAllPredictCalculateTcz(edbInfoId, source, subSource int, fromEdbInfo *EdbInfo, edbCode, startDate string) (latestDateStr string, latestValue float64, err error) {
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- fmt.Println("RefreshAllCalculateTcz,Err:" + err.Error())
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- if err != nil {
- return
- }
- edbInfoIdStr := strconv.Itoa(edbInfoId)
- // 获取关联指标数据
- dataList, err := GetPredictEdbDataListAllByStartDate(fromEdbInfo, 0, startDate)
- if err != nil {
- return
- }
- latestDateStr = fromEdbInfo.LatestDate
- //计算数据
- var dateArr []string
- dataMap := make(map[string]*EdbInfoSearchData)
- for _, v := range dataList {
- dateArr = append(dateArr, v.DataTime)
- dataMap[v.DataTime] = v
- }
- //获取指标所有数据
- existDataList := make([]*EdbData, 0)
- dataTableName := GetEdbDataTableName(source, subSource)
- sql := `SELECT * FROM %s WHERE edb_info_id=? `
- sql = fmt.Sprintf(sql, dataTableName)
- _, err = o.Raw(sql, edbInfoId).QueryRows(&existDataList)
- if err != nil {
- return
- }
- existDataMap := make(map[string]string)
- removeDateMap := make(map[string]bool) //需要移除的日期
- for _, v := range existDataList {
- existDataMap[v.DataTime] = v.Value
- removeDateMap[v.DataTime] = true
- }
- addSql := ` INSERT INTO edb_data_predict_calculate_tcz(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 {
- delete(removeDateMap, av)
- //当前日期
- currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, av, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //上一年的日期
- 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 := TczSub(currentItem.Value, findItem.Value)
- if existVal, ok := existDataMap[av]; !ok {
- if _, existOk := existAddDataMap[av]; !existOk {
- addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
- isAdd = true
- // 实际数据的值
- if fromEdbInfo.LatestDate == av {
- latestValueDecimal, tmpErr := decimal.NewFromString(val)
- if tmpErr != nil {
- return
- }
- latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
- }
- }
- existAddDataMap[av] = av
- } else {
- existValDecimal, tmpErr := decimal.NewFromString(existVal)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- 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 = o.Raw(sql, val, edbInfoId, av).Exec()
- if err != nil {
- return
- }
- }
- }
- utils.FileLog.Info("同期找到:" + av + ";" + preDateStr)
- continue
- } else {
- if fromEdbInfo.Frequency == "月度" { //向上和向下,各找一个月
- for i := 0; i <= 35; i++ {
- nextDateDay := preDate.AddDate(0, 0, 1)
- nextDateDayStr := nextDateDay.Format(utils.FormatDate)
- if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
- timestamp := currentDate.UnixNano() / 1e6
- timestampStr := fmt.Sprintf("%d", timestamp)
- val := TczSub(currentItem.Value, findItem.Value)
- if existVal, ok := existDataMap[av]; !ok {
- if _, existOk := existAddDataMap[av]; !existOk {
- addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
- isAdd = true
- // 实际数据的值
- if fromEdbInfo.LatestDate == av {
- latestValueDecimal, tmpErr := decimal.NewFromString(val)
- if tmpErr != nil {
- return
- }
- latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
- }
- }
- existAddDataMap[av] = av
- } else {
- existValDecimal, tmpErr := decimal.NewFromString(existVal)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- 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 = o.Raw(sql, val, edbInfoId, av).Exec()
- if err != nil {
- return
- }
- }
- }
- break
- } else {
- preDateDay := preDate.AddDate(0, 0, -1)
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
- timestamp := currentDate.UnixNano() / 1e6
- timestampStr := fmt.Sprintf("%d", timestamp)
- val := TczSub(currentItem.Value, findItem.Value)
- if existVal, ok := existDataMap[av]; !ok {
- if _, existOk := existAddDataMap[av]; !existOk {
- addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
- isAdd = true
- // 实际数据的值
- if fromEdbInfo.LatestDate == av {
- latestValueDecimal, tmpErr := decimal.NewFromString(val)
- if tmpErr != nil {
- return
- }
- latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
- }
- }
- existAddDataMap[av] = av
- } else {
- existValDecimal, tmpErr := decimal.NewFromString(existVal)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- 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 = o.Raw(sql, val, edbInfoId, av).Exec()
- if err != nil {
- return
- }
- }
- }
- break
- }
- }
- }
- } else if fromEdbInfo.Frequency == "季度" || fromEdbInfo.Frequency == "年度" {
- if findItem, ok := dataMap[preDateStr]; ok { //上一年同期->下一个月找到
- timestamp := currentDate.UnixNano() / 1e6
- timestampStr := fmt.Sprintf("%d", timestamp)
- val := TczSub(currentItem.Value, findItem.Value)
- if existVal, ok := existDataMap[av]; !ok {
- if _, existOk := existAddDataMap[av]; !existOk {
- addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
- isAdd = true
- // 实际数据的值
- if fromEdbInfo.LatestDate == av {
- latestValueDecimal, tmpErr := decimal.NewFromString(val)
- if tmpErr != nil {
- return
- }
- latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
- }
- }
- existAddDataMap[av] = av
- } else {
- existValDecimal, tmpErr := decimal.NewFromString(existVal)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- 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 = o.Raw(sql, val, edbInfoId, av).Exec()
- if err != nil {
- return
- }
- }
- }
- break
- }
- } else {
- nextDateDay := preDate
- preDateDay := preDate
- for i := 0; i < 35; i++ {
- nextDateDayStr := nextDateDay.Format(utils.FormatDate)
- if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
- timestamp := currentDate.UnixNano() / 1e6
- timestampStr := fmt.Sprintf("%d", timestamp)
- val := TczSub(currentItem.Value, findItem.Value)
- if existVal, ok := existDataMap[av]; !ok {
- if _, existOk := existAddDataMap[av]; !existOk {
- addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
- isAdd = true
- // 实际数据的值
- if fromEdbInfo.LatestDate == av {
- latestValueDecimal, tmpErr := decimal.NewFromString(val)
- if tmpErr != nil {
- return
- }
- latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
- }
- }
- existAddDataMap[av] = av
- } else {
- existValDecimal, tmpErr := decimal.NewFromString(existVal)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- 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 = o.Raw(sql, val, edbInfoId, av).Exec()
- if err != nil {
- return
- }
- }
- }
- break
- } else {
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
- timestamp := currentDate.UnixNano() / 1e6
- timestampStr := fmt.Sprintf("%d", timestamp)
- val := TczSub(currentItem.Value, findItem.Value)
- if existVal, ok := existDataMap[av]; !ok {
- if _, existOk := existAddDataMap[av]; !existOk {
- addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
- isAdd = true
- // 实际数据的值
- if fromEdbInfo.LatestDate == av {
- latestValueDecimal, tmpErr := decimal.NewFromString(val)
- if tmpErr != nil {
- return
- }
- latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
- }
- }
- existAddDataMap[av] = av
- } else {
- existValDecimal, tmpErr := decimal.NewFromString(existVal)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- 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 = o.Raw(sql, val, edbInfoId, av).Exec()
- if err != nil {
- return
- }
- }
- }
- break
- }
- }
- nextDateDay = nextDateDay.AddDate(0, 0, 1)
- preDateDay = preDateDay.AddDate(0, 0, -1)
- }
- }
- }
- }
- }
- if len(removeDateMap) > 0 {
- removeDateList := make([]string, 0) //需要移除的日期
- for k := range removeDateMap {
- removeDateList = append(removeDateList, k)
- }
- removeDateStr := strings.Join(removeDateList, `","`)
- removeDateStr = `"` + removeDateStr + `"`
- //如果拼接指标变更了,那么需要删除所有的指标数据
- tableName := GetEdbDataTableName(source, subSource)
- sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (%s) `, tableName, removeDateStr)
- _, err = to.Raw(sql, edbInfoId).Exec()
- if err != nil {
- err = fmt.Errorf("删除计算失败的计算指标数据失败,Err:" + err.Error())
- return
- }
- }
- if isAdd {
- addSql = strings.TrimRight(addSql, ",")
- _, err = o.Raw(addSql).Exec()
- if err != nil {
- return
- }
- }
- return
- }
|