123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- package data_manage
- import (
- "errors"
- "eta/eta_mobile/utils"
- "fmt"
- "github.com/beego/beego/v2/client/orm"
- "github.com/shopspring/decimal"
- "sort"
- "strings"
- "time"
- )
- // EditCalculateLjztbpj 编辑累计值同比拼接数据
- func EditCalculateLjztbpj(req *EdbInfoCalculateBatchEditReq, nowEdbInfo, firstEdbInfo, secondEdbInfo *EdbInfo) (edbInfoId int, err error) {
- edbInfoId = req.EdbInfoId
- o := orm.NewOrmUsingDB("data")
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- sql := ``
- var existCondition string
- var existPars []interface{}
- existCondition += " AND edb_info_id=? "
- existPars = append(existPars, edbInfoId)
- //查询出所有的关联指标
- existList, err := GetEdbInfoCalculateListByCondition(existCondition, existPars)
- if err != nil {
- err = errors.New("判断指标是否改变失败,Err:" + err.Error())
- return
- }
- var existItemA, existItemB *EdbInfoCalculateMapping
- for _, existItem := range existList {
- if existItem.FromTag == "A" {
- existItemA = existItem
- } else if existItem.FromTag == "B" {
- existItemB = existItem
- }
- }
- // 原数据开始计算日期
- startCalculationDate, _ := time.ParseInLocation(utils.FormatDate, nowEdbInfo.CalculateFormula, time.Local)
- //待拼接指标map
- pjDataMap := make(map[string]float64) //需要入库的数据
- nowEdbDataMap := make(map[string]float64) //当前指标的数据(已经在库里了,不需要重新)
- //拼接指标的日期切片数据
- pjEdbDataTimeList := make([]string, 0)
- //最近开始的时间
- lastDateTime := startCalculationDate
- //待拼接指标数据
- {
- //如果拼接指标变更了,那么需要删除所有的指标进行重新拼接
- if existItemA.FromEdbInfoId != firstEdbInfo.EdbInfoId {
- //删除旧的指标数据
- {
- //删除之前的A指标关联关系
- sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? and from_edb_info_id = ?`
- _, err = to.Raw(sql, edbInfoId, existItemA.FromEdbInfoId).Exec()
- if err != nil {
- err = errors.New("删除拼接日期之前的指标关联关系失败,Err:" + err.Error())
- return
- }
- //如果拼接指标变更了,那么需要删除所有的指标数据
- tableName := GetEdbDataTableName(nowEdbInfo.Source, nowEdbInfo.SubSource)
- sql = fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? `, tableName)
- _, err = to.Raw(sql, edbInfoId).Exec()
- if err != nil {
- err = errors.New("删除所有的累计值同比拼接指标数据失败,Err:" + err.Error())
- return
- }
- }
- //添加新的指标关系
- {
- calculateMappingItem := new(EdbInfoCalculateMapping)
- calculateMappingItem.CreateTime = time.Now()
- calculateMappingItem.ModifyTime = time.Now()
- calculateMappingItem.Sort = 1
- calculateMappingItem.EdbCode = nowEdbInfo.EdbCode
- calculateMappingItem.EdbInfoId = edbInfoId
- calculateMappingItem.FromEdbInfoId = firstEdbInfo.EdbInfoId
- calculateMappingItem.FromEdbCode = firstEdbInfo.EdbCode
- calculateMappingItem.FromEdbName = firstEdbInfo.EdbName
- calculateMappingItem.FromSource = firstEdbInfo.Source
- calculateMappingItem.FromSourceName = firstEdbInfo.SourceName
- calculateMappingItem.FromTag = "A"
- calculateMappingItem.Source = nowEdbInfo.Source
- calculateMappingItem.SourceName = nowEdbInfo.SourceName
- _, err = to.Insert(calculateMappingItem)
- if err != nil {
- return
- }
- }
- var condition string
- var pars []interface{}
- //获取待拼接指标最近的个12月31日有值的年份
- condition += " AND data_time like ? AND edb_info_id=? "
- pars = append(pars, "%12-31", firstEdbInfo.EdbInfoId)
- //获取最新的待拼接指标
- lastEdbData, tmpErr := GetLastEdbData(condition, pars, firstEdbInfo.Source, firstEdbInfo.SubSource)
- if tmpErr != nil {
- return edbInfoId, tmpErr
- }
- lastDateTime, _ = time.ParseInLocation(utils.FormatDate, lastEdbData.DataTime, time.Local)
- //获取待拼接指标的数据列表
- condition = ``
- pars = make([]interface{}, 0)
- condition += " AND data_time <= ? AND edb_info_id=? "
- pars = append(pars, lastEdbData.DataTime, firstEdbInfo.EdbInfoId)
- firstDataList, tmpErr := GetEdbDataListAll(condition, pars, firstEdbInfo.Source, firstEdbInfo.SubSource, 0)
- if tmpErr != nil {
- return edbInfoId, tmpErr
- }
- for _, v := range firstDataList {
- pjDataMap[v.DataTime] = v.Value
- pjEdbDataTimeList = append(pjEdbDataTimeList, v.DataTime)
- //将新的数据存入已入库指标map里面,以便后续计算
- nowEdbDataMap[v.DataTime] = v.Value
- }
- } else {
- // 获取当前指标的所有数据
- var condition string
- var pars []interface{}
- condition += " AND edb_info_id=? and data_time <= ?"
- pars = append(pars, nowEdbInfo.EdbInfoId, nowEdbInfo.CalculateFormula)
- nowEdbDataList, tmpErr := GetEdbDataListAll(condition, pars, nowEdbInfo.Source, nowEdbInfo.SubSource, 0)
- if tmpErr != nil {
- return edbInfoId, tmpErr
- }
- for _, v := range nowEdbDataList {
- nowEdbDataMap[v.DataTime] = v.Value
- pjEdbDataTimeList = append(pjEdbDataTimeList, v.DataTime)
- }
- }
- }
- //同比值指标map
- tbzEdbDataMap := make(map[string]float64)
- //同比值日期切片列表
- tbzEdbDataTimeList := make([]string, 0)
- //同比值指标
- {
- if existItemB.FromEdbInfoId != secondEdbInfo.EdbInfoId {
- //删除通过B指标生成的数据
- {
- //删除之前的B指标关联关系
- sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? and from_edb_info_id = ?`
- _, err = to.Raw(sql, edbInfoId, existItemB.FromEdbInfoId).Exec()
- if err != nil {
- err = errors.New("删除拼接日期之后的指标关联关系失败,Err:" + err.Error())
- return
- }
- //如果同比值指标变更了,那么需要删除所有计算出来的指标数据
- tableName := GetEdbDataTableName(nowEdbInfo.Source, nowEdbInfo.SubSource)
- sql = fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time > ? `, tableName)
- _, err = to.Raw(sql, edbInfoId, nowEdbInfo.CalculateFormula).Exec()
- if err != nil {
- err = errors.New("删除计算出来的累计值同比拼接指标数据失败,Err:" + err.Error())
- return
- }
- }
- //添加新的指标关系
- {
- calculateMappingItem := new(EdbInfoCalculateMapping)
- calculateMappingItem.CreateTime = time.Now()
- calculateMappingItem.ModifyTime = time.Now()
- calculateMappingItem.Sort = 1
- calculateMappingItem.EdbCode = nowEdbInfo.EdbCode
- calculateMappingItem.EdbInfoId = edbInfoId
- calculateMappingItem.FromEdbInfoId = secondEdbInfo.EdbInfoId
- calculateMappingItem.FromEdbCode = secondEdbInfo.EdbCode
- calculateMappingItem.FromEdbName = secondEdbInfo.EdbName
- calculateMappingItem.FromSource = secondEdbInfo.Source
- calculateMappingItem.FromSourceName = secondEdbInfo.SourceName
- calculateMappingItem.FromTag = "B"
- calculateMappingItem.Source = nowEdbInfo.Source
- calculateMappingItem.SourceName = nowEdbInfo.SourceName
- _, err = to.Insert(calculateMappingItem)
- if err != nil {
- return
- }
- }
- }
- var condition string
- var pars []interface{}
- condition += " AND data_time > ? AND edb_info_id = ? "
- pars = append(pars, lastDateTime, secondEdbInfo.EdbInfoId)
- //同比值指标的数据列表
- secondDataList, tmpErr := GetEdbDataListAll(condition, pars, secondEdbInfo.Source, secondEdbInfo.SubSource, 0)
- if tmpErr != nil {
- return edbInfoId, tmpErr
- }
- for _, v := range secondDataList {
- tbzEdbDataMap[v.DataTime] = v.Value
- tbzEdbDataTimeList = append(tbzEdbDataTimeList, v.DataTime)
- }
- }
- sort.Strings(tbzEdbDataTimeList)
- // 遍历现有的数据,判断拼接指标中是否存在该日期数据,如果拼接指标无此数据,那么需要删除该日期数据(日期的判断:需要在开始计算日期之后)
- removeDateList := make([]string, 0)
- for nowEdbDate := range nowEdbDataMap {
- nowEdbDateTime, _ := time.ParseInLocation(utils.FormatDate, nowEdbDate, time.Local)
- //校验日期 需要 大于 拼接前日期
- if startCalculationDate.Before(nowEdbDateTime) {
- if _, ok := tbzEdbDataMap[nowEdbDate]; !ok {
- // 同比指标中,不存在该日期数据,那么需要移除 现有数据 中该日期的数据
- removeDateList = append(removeDateList, nowEdbDate)
- }
- }
- }
- //待修改的指标数据map(index:日期,value:值)
- updateEdbDataMap := make(map[string]float64)
- for _, v := range tbzEdbDataTimeList {
- tbzDataTime, _ := time.ParseInLocation(utils.FormatDate, v, time.Local)
- //获取拼接指标上一年同一天的数据
- var pjDataTime time.Time
- if tbzDataTime.Month() == 2 {
- pjDataTime = tbzDataTime.AddDate(0, -11, 0)
- pjDataTime = time.Date(pjDataTime.Year(), pjDataTime.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 0, -1)
- } else {
- pjDataTime = tbzDataTime.AddDate(-1, 0, 0)
- }
- //校验现有数据中,是否存在该日期的数据,如果存在的话,那么就要去校验 最新计算数据 与 现有数据 是否一致
- if nowEdbDataValue, isHas := nowEdbDataMap[v]; isHas {
- //获取去年今日的数据,获取到后,然后是去修改该日期的数据
- if lastYearEdbDataValue, ok := nowEdbDataMap[pjDataTime.Format(utils.FormatDate)]; ok {
- tbzDataValue := tbzEdbDataMap[v] //同比值
- currValue := lastYearEdbDataValue * (1 + tbzDataValue/100)
- currValue, _ = decimal.NewFromFloat(currValue).Truncate(4).Float64() //保留4位小数
- //如果计算出来的值与库里面的值不匹配,那么就去修改该值
- if nowEdbDataValue != currValue {
- //将计算后的数据存入待拼接指标map里面,以便后续计算
- updateEdbDataMap[v] = currValue
- }
- }
- } else {
- //因为 现有数据中 不存在该日期数据,那么需要做新增数据处理
- //如果去年今日存在该数据,那么就去计算当前的数据
- if pjDataValue, ok := nowEdbDataMap[pjDataTime.Format(utils.FormatDate)]; ok {
- tbzDataValue := tbzEdbDataMap[v] //同比值
- currValue := pjDataValue * (1 + tbzDataValue/100)
- currValue, _ = decimal.NewFromFloat(currValue).Truncate(4).Float64()
- //将计算后的数据存入已入库指标map里面,以便后续计算
- nowEdbDataMap[v] = currValue
- //将计算后的数据存入待拼接指标map里面,以便后续入库
- pjDataMap[v] = currValue
- pjEdbDataTimeList = append(pjEdbDataTimeList, v)
- }
- }
- }
- //新增的数据入库
- {
- addDataList := make([]*EdbDataCalculateLjztbpj, 0)
- for dataTime, dataValue := range pjDataMap {
- //时间戳
- currentDate, _ := time.Parse(utils.FormatDate, dataTime)
- timestamp := currentDate.UnixNano() / 1e6
- edbDataLjztbpj := &EdbDataCalculateLjztbpj{
- EdbInfoId: edbInfoId,
- EdbCode: nowEdbInfo.EdbCode,
- DataTime: dataTime,
- Value: dataValue,
- Status: 1,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- DataTimestamp: timestamp,
- }
- addDataList = append(addDataList, edbDataLjztbpj)
- }
- tmpAddDataList := make([]*EdbDataCalculateLjztbpj, 0)
- for _, v := range addDataList {
- tmpAddDataList = append(tmpAddDataList, v)
- if len(tmpAddDataList) >= 200 {
- _, tmpErr := to.InsertMulti(len(tmpAddDataList), tmpAddDataList)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //重新初始化需要加入的数据切片
- tmpAddDataList = make([]*EdbDataCalculateLjztbpj, 0)
- }
- }
- //最后如果还有需要新增的数据,那么就统一入库
- if len(tmpAddDataList) > 0 {
- _, tmpErr := to.InsertMulti(len(tmpAddDataList), tmpAddDataList)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- }
- }
- //删除已经不存在的累计同比拼接指标数据(由于同比值当日的数据删除了)
- {
- if len(removeDateList) > 0 {
- removeDateStr := strings.Join(removeDateList, `","`)
- removeDateStr = `"` + removeDateStr + `"`
- //如果拼接指标变更了,那么需要删除所有的指标数据
- tableName := GetEdbDataTableName(nowEdbInfo.Source, nowEdbInfo.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 = errors.New("删除不存在的累计值同比拼接指标数据失败,Err:" + err.Error())
- return
- }
- }
- }
- //修改现有的数据中对应的值
- {
- tableName := GetEdbDataTableName(nowEdbInfo.Source, nowEdbInfo.SubSource)
- for edbDate, edbDataValue := range updateEdbDataMap {
- sql = fmt.Sprintf(` UPDATE %s set value = %f,modify_time=now() WHERE edb_info_id = ? and data_time = %s `, tableName, edbDataValue, edbDate)
- _, err = to.Raw(sql, edbInfoId).Exec()
- if err != nil {
- err = errors.New("更新现有的累计值同比拼接指标数据失败,Err:" + err.Error())
- return
- }
- }
- }
- //修改指标信息
- sql = ` UPDATE edb_info SET
- edb_name =?,
- edb_name_source=?,
- frequency = ?,
- unit = ?,
- classify_id = ?,
- calculate_formula=?,
- modify_time = NOW()
- WHERE edb_info_id = ? `
- _, err = to.Raw(sql, req.EdbName, req.EdbName, req.Frequency, req.Unit, req.ClassifyId, lastDateTime.Format(utils.FormatDate), edbInfoId).Exec()
- if err != nil {
- return
- }
- return
- }
|