123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- package models
- import (
- "github.com/beego/beego/v2/client/orm"
- "hongze/hongze_edb_lib/services/alarm_msg"
- "hongze/hongze_edb_lib/utils"
- "time"
- )
- // AddPredictEdbInfoReq 添加预测指标请求
- type AddPredictEdbInfoReq struct {
- EdbInfoId int `description:"指标ID"`
- ClassifyId int `description:"分类id"`
- AdminId int `description:"添加人id"`
- AdminName string `description:"添加人名称"`
- SourceEdbInfoId int `description:"来源指标id"`
- EdbName string `description:"指标名称"`
- RuleList []RuleConfig `description:"配置规则列表"`
- MaxValue float64 `description:"最大值"`
- MinValue float64 `description:"最小值"`
- }
- // RuleConfig 预测规则配置
- type RuleConfig struct {
- RuleType int `description:"预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值,9:动态环差"`
- Value string `description:"值/计算公式"`
- EndDate string `description:"截止日期"`
- EdbInfoIdArr []EdbInfoFromTag `description:"指标信息"`
- }
- // EditPredictEdbInfoReq 编辑预测指标请求
- type EditPredictEdbInfoReq struct {
- EdbInfoId int `description:"指标ID"`
- ClassifyId int `description:"分类id"`
- EdbName string `description:"指标名称"`
- RuleList []RuleConfig `description:"配置规则列表"`
- }
- type PredictEdbConf struct {
- ConfigId int `orm:"column(config_id);pk" description:"规则id"`
- PredictEdbInfoId int `orm:"column(predict_edb_info_id)" description:"预测指标id"`
- SourceEdbInfoId int `description:"来源指标id"`
- RuleType int `description:"预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值"`
- FixedValue float64 `description:"固定值"`
- Value string `description:"配置的值"`
- EndDate time.Time `description:"截止日期"`
- ModifyTime time.Time `description:"修改时间"`
- CreateTime time.Time `description:"添加时间"`
- }
- // PredictEdbConfAndData 预测规则和其对应的动态数据
- type PredictEdbConfAndData struct {
- ConfigId int `orm:"column(config_id);pk" description:"规则id"`
- PredictEdbInfoId int `orm:"column(predict_edb_info_id)" description:"预测指标id"`
- SourceEdbInfoId int `description:"来源指标id"`
- RuleType int `description:"预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值,9:动态环差"`
- FixedValue float64 `description:"固定值"`
- Value string `description:"配置的值"`
- EndDate time.Time `description:"截止日期"`
- ModifyTime time.Time `description:"修改时间"`
- CreateTime time.Time `description:"添加时间"`
- DataList []*EdbInfoSearchData `description:"动态数据"`
- }
- // GetPredictEdbConfById 根据预测指标id获取预测指标配置信息
- func GetPredictEdbConfById(edbInfoId int) (item *PredictEdbConf, err error) {
- o := orm.NewOrm()
- sql := ` SELECT * FROM predict_edb_conf WHERE predict_edb_info_id=? `
- err = o.Raw(sql, edbInfoId).QueryRow(&item)
- return
- }
- // GetPredictEdbConfCount 根据来源指标id获取被引用的次数
- func GetPredictEdbConfCount(sourceEdbInfoId int) (count int, err error) {
- o := orm.NewOrm()
- sql := ` SELECT COUNT(1) AS count FROM predict_edb_conf WHERE source_edb_info_id=? `
- err = o.Raw(sql, sourceEdbInfoId).QueryRow(&count)
- return
- }
- // GetPredictEdbConfListById 根据预测指标id获取预测指标配置信息列表
- func GetPredictEdbConfListById(edbInfoId int) (items []*PredictEdbConf, err error) {
- o := orm.NewOrm()
- sql := ` SELECT * FROM predict_edb_conf WHERE predict_edb_info_id=? ORDER BY config_id ASC`
- _, err = o.Raw(sql, edbInfoId).QueryRows(&items)
- return
- }
- // GetPredictEdbConfAndDataListById 根据预测指标id获取预测指标配置信息列表
- func GetPredictEdbConfAndDataListById(edbInfoId int) (items []*PredictEdbConfAndData, err error) {
- o := orm.NewOrm()
- sql := ` SELECT * FROM predict_edb_conf WHERE predict_edb_info_id=? ORDER BY config_id ASC`
- _, err = o.Raw(sql, edbInfoId).QueryRows(&items)
- return
- }
- // GetPredictEdbConfListByConfigIdList 根据预测指标id列表获取预测指标配置信息列表
- func GetPredictEdbConfListByConfigIdList(configIdList []int) (items []*PredictEdbConf, err error) {
- num := len(configIdList)
- if num <= 0 {
- return
- }
- o := orm.NewOrm()
- sql := ` SELECT * FROM predict_edb_conf WHERE config_id in (` + utils.GetOrmInReplace(num) + `) ORDER BY config_id ASC`
- _, err = o.Raw(sql, configIdList).QueryRows(&items)
- return
- }
- // ModifyPredictEdbInfoMaxAndMinInfoBySourceEdbInfoId 根据来源指标修改预测指标的最新数据信息
- func ModifyPredictEdbInfoMaxAndMinInfoBySourceEdbInfoId(sourceEdbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) {
- //return
- o := orm.NewOrm()
- var list []*PredictEdbConf
- sql := ` SELECT * FROM predict_edb_conf WHERE source_edb_info_id=? `
- total, err := o.Raw(sql, sourceEdbInfoId).QueryRows(&list)
- if err != nil {
- return
- }
- if total > 0 {
- idList := make([]int, 0)
- for _, v := range list {
- idList = append(idList, v.PredictEdbInfoId)
- }
- //sql := ` UPDATE edb_info SET start_date=?,min_value=?,max_value=?,is_update=2,latest_date=?,latest_value=?,modify_time=NOW() WHERE edb_info_id in (` + utils.GetOrmInReplace(int(total)) + `) `
- //_, err = o.Raw(sql, item.MinDate, item.MinValue, item.MaxValue, item.MaxDate, item.LatestValue, idList).Exec()
- sql := ` UPDATE edb_info SET start_date=?,is_update=2,latest_date=?,latest_value=?,modify_time=NOW() WHERE edb_info_id in (` + utils.GetOrmInReplace(int(total)) + `) `
- _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.LatestValue, idList).Exec()
- }
- return
- }
- // GetPredictEdbConfBySourceEdbInfoId 根据来源指标id获取配置
- func GetPredictEdbConfBySourceEdbInfoId(sourceEdbInfoId int) (item *PredictEdbConf, err error) {
- o := orm.NewOrm()
- sql := ` SELECT * FROM predict_edb_conf WHERE source_edb_info_id=? `
- err = o.Raw(sql, sourceEdbInfoId).QueryRow(&item)
- return
- }
- // AddPredictEdbConf 添加预测指标规则
- func AddPredictEdbConf(item *PredictEdbConf) (lastId int64, err error) {
- o := orm.NewOrm()
- lastId, err = o.Insert(item)
- return
- }
- // AddPredictEdb 添加预测指标
- // edbInfo, calculateMappingList, predictEdbConfList,calculateRule9List,trendsMappingList
- func AddPredictEdb(item *EdbInfo, calculateMappingList []*EdbInfoCalculateMapping, predictEdbConfList []*PredictEdbConf, calculateRuleMap map[int]CalculateRule) (err error, errMsg string) {
- o := orm.NewOrm()
- tx, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- tmpErr := tx.Rollback()
- if tmpErr != nil {
- go alarm_msg.SendAlarmMsg("AddPredictEdb 事务回滚失败,Err:"+tmpErr.Error(), 3)
- }
- } else {
- err = tx.Commit()
- }
- }()
- // 新增预测指标
- edbInfoId, err := tx.Insert(item)
- if err != nil {
- return
- }
- item.EdbInfoId = int(edbInfoId)
- // 新增预测指标的关联关系
- lenCalculateMapping := len(calculateMappingList)
- if lenCalculateMapping > 0 {
- for _, calculateMappingItem := range calculateMappingList {
- calculateMappingItem.EdbInfoId = item.EdbInfoId
- calculateMappingItem.EdbCode = item.EdbCode
- }
- _, err = tx.InsertMulti(lenCalculateMapping, calculateMappingList)
- if err != nil {
- return
- }
- }
- predictEdbConfAndDataList := make([]*PredictEdbConfAndData, 0)
- // 新增预测指标配置
- for k, v := range predictEdbConfList {
- v.PredictEdbInfoId = item.EdbInfoId
- configId, tmpErr := tx.Insert(v)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- v.ConfigId = int(configId)
- // 每次规则计算的时候,产生的临时数据
- resultDataList := make([]*EdbInfoSearchData, 0)
- switch v.RuleType {
- case 9: //动态环差规则
- calculateRule := calculateRuleMap[k]
- calculateRule.ConfigId = v.ConfigId
- calculateRule.EdbInfoId = v.PredictEdbInfoId
- // 指标与规则的动态数据生成入库
- resultDataList, err = CalculateByRuleBy9(tx, calculateRule)
- if err != nil {
- return
- }
- // 规则与指标的关系入库
- lenTrendsCalculateMapping := len(calculateRule.TrendsCalculateMappingList)
- if lenTrendsCalculateMapping > 0 {
- for _, vv := range calculateRule.TrendsCalculateMappingList {
- vv.EdbInfoId = item.EdbInfoId
- vv.ConfigId = v.ConfigId
- }
- _, err = tx.InsertMulti(lenTrendsCalculateMapping, calculateRule.TrendsCalculateMappingList)
- if err != nil {
- return
- }
- }
- case 14: //14:根据 一元线性拟合 规则获取预测数据
- calculateRule := calculateRuleMap[k]
- calculateRule.ConfigId = v.ConfigId
- calculateRule.EdbInfoId = v.PredictEdbInfoId
- // 指标与规则的动态数据(拟合数据)生成入库
- err, errMsg = CalculateByRuleByRuleLineNh(tx, *item, predictEdbConfAndDataList, *v)
- if err != nil {
- return
- }
- // 规则与指标的关系入库
- lenTrendsCalculateMapping := len(calculateRule.TrendsCalculateMappingList)
- if lenTrendsCalculateMapping > 0 {
- for _, vv := range calculateRule.TrendsCalculateMappingList {
- vv.EdbInfoId = item.EdbInfoId
- vv.ConfigId = v.ConfigId
- }
- _, err = tx.InsertMulti(lenTrendsCalculateMapping, calculateRule.TrendsCalculateMappingList)
- if err != nil {
- return
- }
- }
- }
- // 规则配置(含数据)
- tmpPredictEdbConfAndData := &PredictEdbConfAndData{
- ConfigId: 0,
- PredictEdbInfoId: 0,
- SourceEdbInfoId: v.SourceEdbInfoId,
- RuleType: v.RuleType,
- FixedValue: v.FixedValue,
- Value: v.Value,
- EndDate: v.EndDate,
- ModifyTime: v.ModifyTime,
- CreateTime: v.CreateTime,
- DataList: resultDataList,
- }
- predictEdbConfAndDataList = append(predictEdbConfAndDataList, tmpPredictEdbConfAndData)
- }
- return
- }
- // EditPredictEdb 修改预测指标
- func EditPredictEdb(edbInfo *EdbInfo, updateEdbInfoCol []string, calculateMappingList []*EdbInfoCalculateMapping, predictEdbConfList []*PredictEdbConf, calculateRuleMap map[int]CalculateRule) (err error, errMsg string) {
- o := orm.NewOrm()
- tx, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- tmpErr := tx.Rollback()
- if tmpErr != nil {
- go alarm_msg.SendAlarmMsg("AddPredictEdb 事务回滚失败,Err:"+tmpErr.Error(), 3)
- }
- } else {
- err = tx.Commit()
- }
- }()
- // 修改预测指标
- _, err = tx.Update(edbInfo, updateEdbInfoCol...)
- if err != nil {
- return
- }
- // 先删除原有的预测指标 与 其他指标的 关联关系
- sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ?`
- _, err = tx.Raw(sql, edbInfo.EdbInfoId).Exec()
- if err != nil {
- return
- }
- // 先删除原有的配置
- sql = ` DELETE FROM predict_edb_conf WHERE predict_edb_info_id = ?`
- _, err = tx.Raw(sql, edbInfo.EdbInfoId).Exec()
- if err != nil {
- return
- }
- // 删除基础预测指标 规则配置 与 其他指标的 关联关系
- sql = ` DELETE FROM predict_edb_conf_calculate_mapping WHERE edb_info_id = ?`
- _, err = tx.Raw(sql, edbInfo.EdbInfoId).Exec()
- if err != nil {
- return
- }
- // 删除基础预测指标 规则配置 生成的动态数据值
- sql = ` DELETE FROM predict_edb_rule_data WHERE edb_info_id = ?`
- _, err = tx.Raw(sql, edbInfo.EdbInfoId).Exec()
- if err != nil {
- return
- }
- // 新增预测指标的关联关系
- lenCalculateMapping := len(calculateMappingList)
- if lenCalculateMapping > 0 {
- for _, calculateMappingItem := range calculateMappingList {
- calculateMappingItem.EdbInfoId = edbInfo.EdbInfoId
- calculateMappingItem.EdbCode = edbInfo.EdbCode
- }
- _, err = tx.InsertMulti(lenCalculateMapping, calculateMappingList)
- if err != nil {
- return
- }
- }
- calculateRuleIndex := 0 // 预测计算规则下标
- predictEdbConfAndDataList := make([]*PredictEdbConfAndData, 0)
- // 新增预测指标配置
- for confIndex, v := range predictEdbConfList {
- configId, tmpErr := tx.Insert(v)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- v.ConfigId = int(configId)
- // 每次规则计算的时候,产生的临时数据
- resultDataList := make([]*EdbInfoSearchData, 0)
- switch v.RuleType {
- case 9: //动态环差规则
- calculateRule := calculateRuleMap[confIndex]
- calculateRule.ConfigId = v.ConfigId
- calculateRule.EdbInfoId = v.PredictEdbInfoId
- // 指标与规则的动态数据生成入库
- resultDataList, err = CalculateByRuleBy9(tx, calculateRule)
- if err != nil {
- return
- }
- // 规则与指标的关系入库
- lenTrendsCalculateMapping := len(calculateRule.TrendsCalculateMappingList)
- if lenTrendsCalculateMapping > 0 {
- for _, vv := range calculateRule.TrendsCalculateMappingList {
- vv.EdbInfoId = edbInfo.EdbInfoId
- vv.ConfigId = v.ConfigId
- }
- _, err = tx.InsertMulti(lenTrendsCalculateMapping, calculateRule.TrendsCalculateMappingList)
- if err != nil {
- return
- }
- }
- case 14: //14:根据 一元线性拟合 规则获取预测数据
- calculateRule := calculateRuleMap[confIndex]
- calculateRule.ConfigId = v.ConfigId
- calculateRule.EdbInfoId = v.PredictEdbInfoId
- // 指标与规则的动态数据(拟合数据)生成入库
- err, errMsg = CalculateByRuleByRuleLineNh(tx, *edbInfo, predictEdbConfAndDataList, *v)
- if err != nil {
- return
- }
- // 规则与指标的关系入库
- lenTrendsCalculateMapping := len(calculateRule.TrendsCalculateMappingList)
- if lenTrendsCalculateMapping > 0 {
- for _, vv := range calculateRule.TrendsCalculateMappingList {
- vv.EdbInfoId = edbInfo.EdbInfoId
- vv.ConfigId = v.ConfigId
- }
- _, err = tx.InsertMulti(lenTrendsCalculateMapping, calculateRule.TrendsCalculateMappingList)
- if err != nil {
- return
- }
- }
- }
- calculateRuleIndex++
- // 规则配置(含数据)
- tmpPredictEdbConfAndData := &PredictEdbConfAndData{
- ConfigId: 0,
- PredictEdbInfoId: 0,
- SourceEdbInfoId: v.SourceEdbInfoId,
- RuleType: v.RuleType,
- FixedValue: v.FixedValue,
- Value: v.Value,
- EndDate: v.EndDate,
- ModifyTime: v.ModifyTime,
- CreateTime: v.CreateTime,
- DataList: resultDataList,
- }
- predictEdbConfAndDataList = append(predictEdbConfAndDataList, tmpPredictEdbConfAndData)
- }
- return
- }
- // GetPredictEdbInfoAllCalculate 根据基础预测指标id集合 获取 所有的普通指标列表数据
- func GetPredictEdbInfoAllCalculate(edbInfoIdList []int) (list []*EdbInfo, err error) {
- num := len(edbInfoIdList)
- if num <= 0 {
- return
- }
- o := orm.NewOrm()
- sql := ` SELECT b.* FROM predict_edb_conf AS a
- INNER JOIN edb_info AS b ON a.source_edb_info_id=b.edb_info_id
- WHERE a.predict_edb_info_id in (` + utils.GetOrmInReplace(num) + `)
- GROUP BY a.source_edb_info_id
- ORDER BY a.source_edb_info_id ASC `
- _, err = o.Raw(sql, edbInfoIdList).QueryRows(&list)
- return
- }
|