123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- package models
- import (
- "errors"
- "fmt"
- "github.com/beego/beego/v2/client/orm"
- "github.com/shopspring/decimal"
- "hongze/hongze_edb_lib/utils"
- "strconv"
- "strings"
- "time"
- )
- // AddCalculateNh 年化
- func AddCalculateNh(req *EdbInfoCalculateBatchSaveReq, fromEdbInfo *EdbInfo, edbCode, uniqueCode string, sysUserId int, sysUserRealName string) (edbInfo *EdbInfo, err error) {
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- fmt.Println("AddCalculateNh,Err:" + err.Error())
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- if req.EdbInfoId <= 0 {
- edbInfo = new(EdbInfo)
- edbInfo.Source = utils.DATA_SOURCE_CALCULATE_NH
- edbInfo.SourceName = utils.DATA_SOURCE_NAME_CALCULATE_NH
- edbInfo.EdbCode = edbCode
- edbInfo.EdbName = req.EdbName
- edbInfo.EdbNameSource = req.EdbName
- edbInfo.Frequency = req.Frequency
- edbInfo.Unit = req.Unit
- edbInfo.ClassifyId = req.ClassifyId
- edbInfo.SysUserId = sysUserId
- edbInfo.SysUserRealName = sysUserRealName
- edbInfo.CreateTime = time.Now()
- edbInfo.ModifyTime = time.Now()
- edbInfo.UniqueCode = uniqueCode
- edbInfo.CalculateFormula = req.Formula
- edbInfo.EdbType = 2
- newEdbInfoId, tmpErr := to.Insert(edbInfo)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- edbInfo.EdbInfoId = int(newEdbInfoId)
- //关联关系
- {
- calculateMappingItem := new(EdbInfoCalculateMapping)
- calculateMappingItem.CreateTime = time.Now()
- calculateMappingItem.ModifyTime = time.Now()
- calculateMappingItem.Sort = 1
- calculateMappingItem.EdbCode = edbCode
- calculateMappingItem.EdbInfoId = edbInfo.EdbInfoId
- calculateMappingItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
- calculateMappingItem.FromEdbCode = fromEdbInfo.EdbCode
- calculateMappingItem.FromEdbName = fromEdbInfo.EdbName
- calculateMappingItem.FromSource = fromEdbInfo.Source
- calculateMappingItem.FromSourceName = fromEdbInfo.SourceName
- calculateMappingItem.FromTag = ""
- calculateMappingItem.Source = edbInfo.Source
- calculateMappingItem.SourceName = edbInfo.SourceName
- _, err = to.Insert(calculateMappingItem)
- if err != nil {
- return
- }
- }
- } else {
- edbInfo, err = GetEdbInfoById(req.EdbInfoId)
- if err != nil {
- return
- }
- dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_CALCULATE_NH)
- fmt.Println("dataTableName:" + dataTableName)
- deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
- deleteSql = fmt.Sprintf(deleteSql, dataTableName)
- _, err = to.Raw(deleteSql, req.EdbInfoId).Exec()
- if err != nil {
- return
- }
- }
- //计算数据
- err = refreshAllCalculateNh(to, edbInfo.EdbInfoId, edbInfo.Source, fromEdbInfo, edbInfo.EdbCode)
- return
- }
- // EditCalculateNh 修改年化数据
- func EditCalculateNh(edbInfo *EdbInfo, req *EdbInfoCalculateBatchEditReq, fromEdbInfo *EdbInfo) (err error) {
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- fmt.Println("EditCalculateNh,Err:" + err.Error())
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- //修改指标信息
- edbInfo.EdbName = req.EdbName
- edbInfo.EdbNameSource = req.EdbName
- edbInfo.Frequency = req.Frequency
- edbInfo.Unit = req.Unit
- edbInfo.ClassifyId = req.ClassifyId
- edbInfo.CalculateFormula = req.Formula
- edbInfo.ModifyTime = time.Now()
- _, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "CalculateFormula", "ModifyTime")
- 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, req.FromEdbInfoId)
- 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 {
- err = errors.New("删除计算指标关联关系失败,Err:" + err.Error())
- return
- }
- //清空原有数据
- tableName := GetEdbDataTableName(edbInfo.Source)
- sql = ` DELETE FROM ` + tableName + ` WHERE edb_info_id = ? `
- _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
- if err != nil {
- return
- }
- //关联关系
- {
- calculateMappingItem := &EdbInfoCalculateMapping{
- EdbInfoCalculateMappingId: 0,
- EdbInfoId: edbInfo.EdbInfoId,
- Source: edbInfo.Source,
- SourceName: edbInfo.SourceName,
- EdbCode: edbInfo.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
- }
- }
- //计算数据
- err = refreshAllCalculateNh(to, edbInfo.EdbInfoId, edbInfo.Source, fromEdbInfo, edbInfo.EdbCode)
- return
- }
- func RefreshAllCalculateNh(edbInfoId, source int, fromEdbInfo *EdbInfo, edbCode string) (err error) {
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- fmt.Println("RefreshAllCalculateNh,Err:" + err.Error())
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- // 计算数据
- err = refreshAllCalculateNh(to, edbInfoId, source, fromEdbInfo, edbCode)
- return
- }
- // refreshAllCalculateNh 刷新年化数据
- func refreshAllCalculateNh(to orm.TxOrmer, edbInfoId, source int, fromEdbInfo *EdbInfo, edbCode string) (err error) {
- edbInfoIdStr := strconv.Itoa(edbInfoId)
- tableName := GetEdbDataTableName(utils.DATA_SOURCE_CALCULATE_NH)
- //获取年化指标所有数据
- existDataList, err := GetAllEdbDataListByTo(to, edbInfoId, source)
- if err != nil {
- return
- }
- //计算指标的map
- existDataMap := make(map[string]*EdbData, 0)
- removeDateMap := make(map[string]string)
- for _, v := range existDataList {
- existDataMap[v.DataTime] = v
- removeDateMap[v.DataTime] = ``
- }
- //计算数据
- var condition string
- var pars []interface{}
- condition += " AND edb_info_id=? "
- pars = append(pars, fromEdbInfo.EdbInfoId)
- //获取来源指标的数据
- fromDataList, err := GetEdbDataListAllByTo(to, condition, pars, fromEdbInfo.Source, 1)
- if err != nil {
- return err
- }
- // 插值法数据处理
- handleDataMap := make(map[string]float64)
- _, err = HandleDataByLinearRegression(fromDataList, handleDataMap)
- if err != nil {
- return
- }
- lenFromDataList := len(fromDataList)
- // 如果来源指标没有数据,那么就直接返回得了
- if lenFromDataList <= 0 {
- sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? `, tableName)
- _, err = to.Raw(sql, edbInfoId).Exec()
- if err != nil {
- err = fmt.Errorf("删除年化指标数据失败,Err:" + err.Error())
- return
- }
- return
- }
- // 每年的最后一天的数据值
- yearLastValMap := make(map[int]float64)
- startDataTime, _ := time.ParseInLocation(utils.FormatDate, fromDataList[0].DataTime, time.Local)
- endDataTime, _ := time.ParseInLocation(utils.FormatDate, fromDataList[lenFromDataList-1].DataTime, time.Local)
- for i := startDataTime.Year(); i <= endDataTime.Year(); i++ {
- tmpDateStr := fmt.Sprintf("%d-12-31", i)
- if tmpVal, ok := handleDataMap[tmpDateStr]; ok {
- yearLastValMap[i] = tmpVal
- }
- }
- addSql := ` INSERT INTO ` + tableName + ` (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
- var isAdd bool
- //来源指指标数据
- for _, v := range fromDataList {
- currDateStr := v.DataTime
- currDate, _ := time.ParseInLocation(utils.FormatDate, currDateStr, time.Local)
- perValMap := make(map[time.Time]float64)
- //前3年当日的数据
- for i := 1; i <= 3; i++ {
- tmpDateTime := currDate.AddDate(-i, 0, 0)
- if tmpVal, ok := handleDataMap[tmpDateTime.Format(utils.FormatDate)]; ok {
- perValMap[tmpDateTime] = tmpVal
- }
- }
- lenPerValMap := len(perValMap)
- // 如果数据少于2年,那么就不参与计算,结束当前循环,进入下一个循环
- if lenPerValMap < 2 {
- continue
- }
- // N年 当前值占全年比重 的值列表
- divValList := make([]decimal.Decimal, 0)
- for tmpDateTime, tmpVal := range perValMap {
- yearLastVal, ok2 := yearLastValMap[tmpDateTime.Year()]
- // 如果当年最后一天没有数据
- if !ok2 {
- continue
- }
- // 当前值占全年比重
- tmpYearLastVal := decimal.NewFromFloat(yearLastVal)
- if tmpYearLastVal.IsZero() { //如果是0,那么就退出当前循环,进入下一个循环
- continue
- }
- divVal := decimal.NewFromFloat(tmpVal).Div(tmpYearLastVal)
- divValList = append(divValList, divVal)
- }
- lenDivValList := len(divValList)
- // 如果 N年 当前值占全年比重 的值 小于 2个,那么就不参与计算,结束当前循环,进入下一个循环
- if lenDivValList < 2 {
- continue
- }
- divValSum := decimal.NewFromFloat(0)
- for _, divVal := range divValList {
- divValSum = divValSum.Add(divVal)
- }
- // 当前计算出来的结果
- tmpDivVal := divValSum.Div(decimal.NewFromInt(int64(lenDivValList)))
- if tmpDivVal.IsZero() { //如果是0,那么就退出当前循环,进入下一个循环
- continue
- }
- currVal, _ := decimal.NewFromFloat(v.Value).Div(tmpDivVal).Round(4).Float64()
- // 判断年化指标是否存在数据
- if existData, ok := existDataMap[currDateStr]; ok {
- // 处理年化数据的值
- existValStr := existData.Value
- existValDeci, tmpErr := decimal.NewFromString(existValStr)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- existVal, _ := existValDeci.Round(4).Float64()
- // 判断年化数据的值 与 当前计算出来的结果, 如果两个数据结果不相等的话,那么就修改咯
- if existVal != currVal {
- err = ModifyEdbDataById(source, existData.EdbDataId, fmt.Sprint(currVal))
- if err != nil {
- return err
- }
- }
- } else {
- // 直接入库
- timestamp := currDate.UnixNano() / 1e6
- timestampStr := fmt.Sprintf("%d", timestamp)
- addSql += GetAddSql(edbInfoIdStr, edbCode, currDateStr, timestampStr, fmt.Sprint(currVal))
- isAdd = true
- }
- delete(removeDateMap, currDateStr)
- }
- if isAdd {
- addSql = strings.TrimRight(addSql, ",")
- _, err = to.Raw(addSql).Exec()
- }
- // 移除不存在的日期数据
- if len(removeDateMap) > 0 {
- removeDateList := make([]string, 0) //需要移除的日期
- for k := range removeDateMap {
- removeDateList = append(removeDateList, k)
- }
- removeDateStr := strings.Join(removeDateList, `","`)
- removeDateStr = `"` + removeDateStr + `"`
- 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
- }
- }
- return
- }
|