123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- package controllers
- import (
- "encoding/json"
- "eta_gn/eta_index_lib/logic"
- "eta_gn/eta_index_lib/models"
- "eta_gn/eta_index_lib/utils"
- "fmt"
- "strconv"
- "strings"
- "time"
- )
- // 指标
- type EdbInfoController struct {
- BaseAuthController
- }
- // @Title 新增指标接口
- // @Description 新增指标接口
- // @Success 200 {object} models.AddEdbInfoParams
- // @router /add [post]
- func (this *EdbInfoController) Add() {
- br := new(models.BaseResponse).Init()
- var cacheKey string
- defer func() {
- utils.Rc.Delete(cacheKey)
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.AddEdbInfoParams
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.EdbCode == "" {
- br.Msg = "请输入指标编码!"
- br.ErrMsg = "请输入指标编码,指标编码为空"
- return
- }
- deleteCache := true
- cacheKey = "CACHE_EDB_INFO_ADD_" + req.EdbCode
- defer func() {
- if deleteCache {
- utils.Rc.Delete(cacheKey)
- }
- }()
- if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
- deleteCache = false
- br.Msg = "系统处理中,请稍后重试!"
- br.ErrMsg = "系统处理中,请稍后重试!"
- return
- }
- if req.ClassifyId <= 0 {
- br.Msg = "请输入指标分类!"
- br.ErrMsg = "请输入指标分类"
- return
- }
- if req.Source <= 0 {
- br.Msg = "请输入指标来源!"
- br.ErrMsg = "请输入指标来源"
- return
- }
- if req.EdbName == "" {
- br.Msg = "请输入指标名称!"
- br.ErrMsg = "请输入指标名称"
- return
- }
- if req.Source != utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
- if req.Frequency == "" {
- br.Msg = "请输入指标频度!"
- br.ErrMsg = "请输入指标频度"
- return
- }
- if req.Unit == "" {
- br.Msg = "请输入指标单位!"
- br.ErrMsg = "请输入指标单位"
- return
- }
- }
- var isAdd bool
- item, err := models.GetEdbInfoByEdbCode(req.Source, req.EdbCode)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- isAdd = true
- } else {
- br.Msg = "判断指标是否存在失败!"
- br.ErrMsg = "判断指标是否存在失败"
- return
- }
- }
- if item != nil && item.EdbInfoId <= 0 {
- isAdd = true
- }
- var windUrl string
- if isAdd {
- if req.Source == utils.DATA_SOURCE_WIND {
- windUrl, err = GetServerUrl()
- if err != nil {
- br.Msg = "判断失败!"
- br.ErrMsg = "判断失败,Err:" + err.Error()
- return
- }
- }
- }
- adminId, _ := strconv.Atoi(utils.InitAdminId)
- if req.AdminId > 0 {
- adminId = req.AdminId
- }
- adminName := utils.InitAdminName
- if req.AdminName != "" {
- adminName = strings.TrimSpace(req.AdminName)
- }
- edbInfo, err := models.EdbInfoAdd(&req, windUrl, adminId, adminName)
- if err != nil {
- br.Msg = "新增指标失败!"
- br.ErrMsg = "新增指标失败,Err:" + err.Error()
- return
- }
- // 更新ES
- go logic.UpdateEs(edbInfo.EdbInfoId)
- br.Ret = 200
- br.Msg = "新增成功"
- br.Data = edbInfo
- }
- // @Title 获取基础指标详情
- // @Description 获取基础指标详情
- // @Param request body models.EdbInfoDetailReq true "type json string"
- // @Success 200 {object} models.EdbInfo
- // @router /detail [post]
- func (this *EdbInfoController) EdbIndexDetail() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.EdbInfoDetailReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.EdbCode == "" {
- br.Msg = "请输入指标编码!"
- return
- }
- detail, err := models.GetEdbInfoOnlyByEdbCode(req.EdbCode)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "指标数据不存在!"
- br.ErrMsg = "指标数据不存在,Err:" + err.Error()
- br.ErrCode = "400"
- return
- }
- br.Msg = "获取数据失败!"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = detail
- }
- // @Title 复制新增来自弘则的指标接口
- // @Description 复制新增来自弘则的指标接口
- // @Success 200 {object} models.EdbInfo
- // @router /copy/from/hz [post]
- func (this *EdbInfoController) CopyFromHz() {
- br := new(models.BaseResponse).Init()
- var cacheKey string
- defer func() {
- utils.Rc.Delete(cacheKey)
- this.Data["json"] = br
- this.ServeJSON()
- }()
- req := new(models.EdbInfo)
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.EdbCode == "" {
- br.Msg = "请输入指标编码!"
- br.ErrMsg = "请输入指标编码,指标编码为空"
- return
- }
- deleteCache := true
- cacheKey = "CACHE_EDB_INFO_COPY_FROM_HZ_" + req.EdbCode
- defer func() {
- if deleteCache {
- utils.Rc.Delete(cacheKey)
- }
- }()
- if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
- deleteCache = false
- br.Msg = "系统处理中,请稍后重试!"
- br.ErrMsg = "系统处理中,请稍后重试!"
- return
- }
- if req.ClassifyId <= 0 {
- br.Msg = "请输入指标分类!"
- br.ErrMsg = "请输入指标分类"
- return
- }
- if req.Source <= 0 {
- br.Msg = "请输入指标来源!"
- br.ErrMsg = "请输入指标来源"
- return
- }
- if req.EdbName == "" {
- br.Msg = "请输入指标名称!"
- br.ErrMsg = "请输入指标名称"
- return
- }
- if req.Frequency == "" {
- br.Msg = "请输入指标频度!"
- br.ErrMsg = "请输入指标频度"
- return
- }
- if req.Unit == "" {
- br.Msg = "请输入指标单位!"
- br.ErrMsg = "请输入指标单位"
- return
- }
- var isAdd bool
- item, err := models.GetEdbInfoByEdbCode(req.Source, req.EdbCode)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- isAdd = true
- } else {
- br.Msg = "判断指标是否存在失败!"
- br.ErrMsg = "判断指标是否存在失败"
- return
- }
- }
- if item != nil && item.EdbInfoId <= 0 {
- isAdd = true
- }
- if isAdd {
- req.EdbInfoId = 0
- req.StartDate = "1990-01-01"
- err = req.Add()
- if err != nil {
- br.Msg = "新增指标失败!"
- br.ErrMsg = "新增指标失败"
- return
- }
- }
- edbInfo, err := models.GetEdbInfoOnlyByEdbCode(req.EdbCode)
- // 更新ES
- go logic.UpdateEs(edbInfo.EdbInfoId)
- br.Ret = 200
- br.Msg = "新增成功"
- br.Data = edbInfo
- }
- // RefreshCheck @Title 指标数据更新检查
- // @Description 指标数据更新检查
- // @Param request body models.EdbInfoRefreshCheckReq true "type json string"
- // @Success 200 {object} models.EdbInfo
- // @router /refresh_check [post]
- func (this *EdbInfoController) RefreshCheck() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.EdbInfoRefreshCheckReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.Source == 0 {
- br.Msg = "请输入指标来源"
- return
- }
- if req.LatestDate == "" {
- br.Msg = "请输入查询日期"
- return
- }
- _, err = time.Parse(utils.FormatDate, req.LatestDate)
- if err != nil {
- br.Msg = "查询日期格式错误"
- return
- }
- condition := ` and source=? and no_update=0 `
- pars := make([]interface{}, 0)
- pars = append(pars, req.Source)
- if req.FrequencyBatch != "" {
- frequency := strings.Split(req.FrequencyBatch, ",")
- condition += ` and frequency in (` + utils.GetOrmInReplace(len(frequency)) + `)`
- for _, v := range frequency {
- pars = append(pars, v)
- }
- }
- updateCondition := condition + ` and latest_date = ?`
- pars = append(pars, req.LatestDate)
- unUpdateCondition := condition + ` and latest_date < ?`
- updateNum, err := models.GetEdbInfoCountByCondition(updateCondition, pars)
- if err != nil {
- br.Msg = "获取数据失败!"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
- unUpdateNum, err := models.GetEdbInfoCountByCondition(unUpdateCondition, pars)
- if err != nil {
- br.Msg = "获取数据失败!"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
- resp := models.EdbInfoRefreshCheckResp{
- UpdateNum: updateNum,
- UnUpdateNum: unUpdateNum,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // ResetEdbClassify
- // @Title 重置指标分类
- // @Description 重置指标分类
- // @Param request body models.ResetEdbClassifyReq true "type json string"
- // @Success 200 {object} models.EdbInfo
- // @router /classify_reset [post]
- func (this *EdbInfoController) ResetEdbClassify() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.ResetEdbClassifyReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- req.IndexCode = strings.TrimSpace(req.IndexCode)
- if req.IndexCode == "" {
- br.Msg = "指标编码不可为空"
- return
- }
- req.ClassifyFirst = strings.TrimSpace(req.ClassifyFirst)
- if req.ClassifyFirst == "" {
- br.Msg = "一级分类名称不可为空"
- return
- }
- req.ClassifySecond = strings.TrimSpace(req.ClassifySecond)
- if req.ClassifySecond == "" {
- br.Msg = "二级分类名称不可为空"
- return
- }
- req.ClassifyThird = strings.TrimSpace(req.ClassifyThird)
- if req.ClassifyThird == "" {
- br.Msg = "三级分类名称不可为空"
- return
- }
- // 获取指标信息
- edbInfo, e := models.GetEdbInfoOnlyByEdbCode(req.IndexCode)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Msg = "指标不存在"
- return
- }
- br.Msg = "获取指标信息失败"
- br.ErrMsg = "获取指标信息失败, Err: " + e.Error()
- return
- }
- edbInfoType := uint8(edbInfo.EdbInfoType)
- // 新增分类
- firstClassify, e, msg := models.SaveEdbClassify(req.ClassifyFirst, 0, 0, edbInfoType, utils.InitAdminId, utils.InitAdminName)
- if msg != "" {
- br.Msg = "新增一级分类失败"
- br.ErrMsg = "新增一级分类失败, ErrMsg: " + e.Error()
- if e != nil {
- br.ErrMsg = fmt.Sprintf("新增一级分类失败, Err: %s, ErrMsg: %s", e.Error(), msg)
- } else {
- br.IsSendEmail = false
- }
- return
- }
- secondClassify, e, msg := models.SaveEdbClassify(req.ClassifySecond, firstClassify.ClassifyId, 1, edbInfoType, utils.InitAdminId, utils.InitAdminName)
- if msg != "" {
- br.Msg = "新增二级分类失败"
- br.ErrMsg = "新增二级分类失败, ErrMsg: " + e.Error()
- if e != nil {
- br.ErrMsg = fmt.Sprintf("新增二级分类失败, Err: %s, ErrMsg: %s", e.Error(), msg)
- } else {
- br.IsSendEmail = false
- }
- return
- }
- thirdClassify, e, msg := models.SaveEdbClassify(req.ClassifyThird, secondClassify.ClassifyId, 2, edbInfoType, utils.InitAdminId, utils.InitAdminName)
- if msg != "" {
- br.Msg = "新增三级分类失败"
- br.ErrMsg = "新增三级分类失败, ErrMsg: " + e.Error()
- if e != nil {
- br.ErrMsg = fmt.Sprintf("新增三级分类失败, Err: %s, ErrMsg: %s", e.Error(), msg)
- } else {
- br.IsSendEmail = false
- }
- return
- }
- // 更新指标分类
- cols := []string{"ClassifyId", "ModifyTime"}
- edbInfo.ClassifyId = thirdClassify.ClassifyId
- edbInfo.ModifyTime = time.Now().Local()
- if e := edbInfo.Update(cols); e != nil {
- br.Msg = "更新指标失败"
- br.ErrMsg = "更新指标失败, Err: " + e.Error()
- return
- }
- // 更新ES
- go logic.UpdateEs(edbInfo.EdbInfoId)
- br.Data = edbInfo
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
|