123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- package data_manage
- import (
- "encoding/json"
- "fmt"
- "hongze/hz_eta_api/models"
- "hongze/hz_eta_api/models/data_manage"
- "hongze/hz_eta_api/services/data"
- "hongze/hz_eta_api/utils"
- "strings"
- "time"
- )
- // CalculateSave
- // @Title 生成预测指标运算
- // @Description 生成预测指标运算接口
- // @Param request body data_manage.PredictEdbInfoCalculateSaveReq true "type json string"
- // @Success Ret=200 返回指标id
- // @router /predict_edb_info/calculate/save [post]
- func (this *PredictEdbInfoController) CalculateSave() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req data_manage.PredictEdbInfoCalculateSaveReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- //传入操作人
- req.AdminId = sysUser.AdminId
- req.AdminName = sysUser.RealName
- // 基础的一些数据校验
- req.EdbName = strings.Trim(req.EdbName, " ")
- if req.EdbName == "" {
- br.Msg = "指标名称不能为空"
- return
- }
- if req.Frequency == "" {
- br.Msg = "频率不能为空"
- return
- }
- if req.Unit == "" {
- br.Msg = "单位不能为空"
- return
- }
- if req.ClassifyId <= 0 {
- br.Msg = "请选择分类"
- return
- }
- if len(req.EdbInfoIdArr) <= 0 {
- br.Msg = "请选择指标"
- return
- }
- if req.CalculateFormula == "" {
- br.Msg = "请填写指标"
- return
- }
- calculateFormula := req.CalculateFormula
- calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
- calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
- calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
- calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
- calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
- req.CalculateFormula = calculateFormula
- edbInfoMap := make(map[int]string)
- //移除研究员选择指标中的未使用的指标
- {
- //转大写的计算公式
- upperCalculateFormulaStr := strings.ToUpper(req.CalculateFormula)
- //用到的指标
- newEdbInfoIdArr := make([]data_manage.EdbInfoFromTag, 0)
- for _, tmpEdbInfo := range req.EdbInfoIdArr {
- _, ok := edbInfoMap[tmpEdbInfo.EdbInfoId]
- if ok {
- br.Msg = "选择指标失败,请勿选择重复指标!"
- return
- }
- edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo.FromTag
- upperFromTag := strings.ToUpper(tmpEdbInfo.FromTag)
- if strings.Contains(upperCalculateFormulaStr, upperFromTag) {
- newEdbInfoIdArr = append(newEdbInfoIdArr, tmpEdbInfo)
- }
- }
- req.EdbInfoIdArr = newEdbInfoIdArr
- }
- // 添加计算指标
- reqJson, err := json.Marshal(req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- respItem, err := data.SavePredictEdbData(string(reqJson))
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- if respItem.Ret != 200 {
- br.Msg = respItem.Msg
- br.ErrMsg = respItem.ErrMsg
- return
- }
- resp := respItem.Data
- //添加es
- data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
- br.Ret = 200
- br.Success = true
- br.Msg = "保存成功"
- br.Data = resp
- br.IsAddLog = true
- }
- // CalculateBatchSave
- // @Title 生成预测指标运算(同比、同差)
- // @Description 生成预测指标运算(同比、同差)接口
- // @Param request body data_manage.PredictEdbInfoCalculateBatchSaveReq true "type json string"
- // @Success Ret=200 返回指标id
- // @router /predict_edb_info/calculate/batch/save [post]
- func (this *PredictEdbInfoController) CalculateBatchSave() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req data_manage.PredictEdbInfoCalculateBatchSaveReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- //传入操作人
- req.AdminId = sysUser.AdminId
- req.AdminName = sysUser.RealName
- req.EdbName = strings.Trim(req.EdbName, " ")
- if req.EdbName == "" {
- br.Msg = "指标名称不能为空"
- return
- }
- if req.Frequency == "" {
- br.Msg = "频率不能为空"
- return
- }
- if req.Unit == "" {
- br.Msg = "单位不能为空"
- return
- }
- if req.ClassifyId <= 0 {
- br.Msg = "请选择分类"
- return
- }
- // 添加计算指标
- reqJson, err := json.Marshal(req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- respItem, err := data.BatchSavePredictEdbData(string(reqJson))
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- if respItem.Ret != 200 {
- br.Msg = respItem.Msg
- br.ErrMsg = respItem.ErrMsg
- return
- }
- resp := respItem.Data
- //添加es
- data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
- br.Ret = 200
- br.Success = true
- br.Msg = "保存成功"
- br.Data = resp
- br.IsAddLog = true
- }
- // BatchCalculateBatchSave
- // @Title 批量添加 生成预测指标运算(同比、同差)
- // @Description 批量添加 生成预测指标运算(同比、同差)接口
- // @Param request body data_manage.PredictEdbInfoCalculateBatchSaveReq true "type json string"
- // @Success Ret=200 返回指标id
- // @router /predict_edb_info/calculate/batch/save/batch [post]
- func (this *PredictEdbInfoController) BatchCalculateBatchSave() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var reqList []data_manage.BatchPredictEdbInfoCalculateBatchSaveReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &reqList)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- resp := data_manage.BatchEdbInfoCalculateBatchSaveResp{
- Fail: make([]data_manage.BatchEdbInfoCalculateBatchSaveFailResp, 0),
- Success: make([]data_manage.BatchEdbInfoCalculateBatchSaveSuccessResp, 0),
- }
- redisKeyList := make([]string, 0) //需要清理的缓存key列表
- defer func() {
- for _, redisKey := range redisKeyList {
- utils.Rc.Delete(redisKey)
- }
- }()
- for _, v := range reqList {
- req := v.CalculateInfo
- req.EdbName = strings.Trim(req.EdbName, " ")
- if req.EdbName == "" {
- resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
- CalculateId: v.CalculateId,
- Msg: "指标名称不能为空",
- })
- continue
- }
- if req.Frequency == "" {
- resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
- CalculateId: v.CalculateId,
- Msg: "频率不能为空",
- })
- continue
- }
- if req.Unit == "" {
- resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
- CalculateId: v.CalculateId,
- Msg: "单位不能为空",
- })
- continue
- }
- if req.ClassifyId <= 0 {
- resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
- CalculateId: v.CalculateId,
- Msg: "请选择分类",
- })
- continue
- }
- //fromEdbInfoId := req.FromEdbInfoId
- //
- //if fromEdbInfoId <= 0 {
- // resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
- // CalculateId: v.CalculateId,
- // Msg: "请选择指标",
- // })
- // continue
- //}
- req.AdminId = sysUser.AdminId
- req.AdminName = sysUser.RealName
- //加入缓存机制,避免创建同一个名称的指标 start
- redisKey := fmt.Sprint("predict_edb_info:calculate:batch:save:", sysUser.AdminId, ":", req.Source, ":", v.CalculateId)
- isExist := utils.Rc.IsExist(redisKey)
- if isExist {
- resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
- CalculateId: v.CalculateId,
- Msg: "指标正在处理,请勿重复提交",
- })
- continue
- } else {
- //设置3分钟缓存
- utils.Rc.SetNX(redisKey, 1, time.Second*300)
- redisKeyList = append(redisKeyList, redisKey)
- }
- //加入缓存机制,避免创建同一个名称的指标 end
- reqJson, err := json.Marshal(req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- respItem, err := data.BatchSavePredictEdbData(string(reqJson))
- if err != nil {
- resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
- CalculateId: v.CalculateId,
- Msg: "新增失败!",
- ErrMsg: "新增失败,Err:" + err.Error(),
- })
- continue
- }
- if respItem.Ret != 200 {
- resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
- CalculateId: v.CalculateId,
- Msg: respItem.Msg,
- ErrMsg: respItem.ErrMsg,
- })
- continue
- }
- result := respItem.Data
- //添加es
- data.AddOrEditEdbInfoToEs(result.EdbInfoId)
- resp.Success = append(resp.Success, data_manage.BatchEdbInfoCalculateBatchSaveSuccessResp{
- CalculateId: v.CalculateId,
- EdbInfoId: result.EdbInfoId,
- UniqueCode: result.UniqueCode,
- })
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "批量保存成功"
- br.Data = resp
- br.IsAddLog = true
- }
|