package data_manage import ( "encoding/json" "eta/eta_api/controllers" "eta/eta_api/models" "eta/eta_api/models/data_manage" "eta/eta_api/models/data_manage/request" "eta/eta_api/models/data_manage/response" "eta/eta_api/services/data" "eta/eta_api/utils" "github.com/rdlucklib/rdluck_tools/paging" ) type SciHqDataController struct { controllers.BaseAuthController } // @Title 卓创红期数据分类 // @Description 卓创红期数据分类接口 // @Success 200 {object} data_manage.SciClassify // @router /sci_hq/classify [get] func (this *SciHqDataController) Classify() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() classifyList, err := data_manage.GetSciHqClassifyAll() if err != nil { br.Msg = "查询失败" br.ErrMsg = "查询失败, Err:" + err.Error() return } initClassify := &data_manage.BaseFromSciHqClassifyItem{ ClassifyId: 0, ClassifyName: "未分类", ClassifyNameEn: "Unclassified", ParentId: 0, Level: 1, Sort: 0, Children: nil, } finalList := make([]*data_manage.BaseFromSciHqClassifyItem, 0) classifyTree := getSciHqClassifyTree(classifyList, 0) finalList = append(finalList, initClassify) finalList = append(finalList, classifyTree...) br.Msg = "查询成功" br.Data = finalList br.Success = true br.Ret = 200 } // AddSciClassify // @Title 新增分类 // @Description 新增分类接口 // @Param request body data_manage.AddBaseFromSciClassifyReq true "type json string" // @Success 200 Ret=200 保存成功 // @router /sci_hq/classify/add [post] func (this *SciHqDataController) AddClassify() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req request.AddBaseFromSciHqClassifyReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ClassifyName == "" { br.Msg = "请输入分类名称" return } if req.ParentId < 0 { br.Msg = "操作异常" return } ok, err := data.AddSciHqClassify(req.ClassifyName, this.Lang, req.ParentId) if err != nil { br.Msg = "添加失败" br.ErrMsg = "添加失败,Err:" + err.Error() return } if !ok { br.Msg = "添加失败" return } br.Msg = "添加成功" br.Success = true br.Ret = 200 } // DelClassify // @Title 新增分类 // @Description 新增分类接口 // @Param request body data_manage.AddBaseFromSciClassifyReq true "type json string" // @Success 200 Ret=200 保存成功 // @router /sci_hq/classify/del [post] func (this *SciHqDataController) DelClassify() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req request.DelBaseFromSciHqClassifyReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ClassifyId < 0 { br.Msg = "参数错误" br.IsSendEmail = false return } err = data.DelSciHqClassify(req.ClassifyId) if err != nil { br.Msg = "删除失败" br.ErrMsg = "删除失败,Err:" + err.Error() return } br.Ret = 200 br.Msg = "删除成功" br.Success = true br.IsAddLog = true } // EditClassify // @Title 修改分类 // @Description 修改分类接口 // @Param request body data_manage.EditBaseFromMysteelChemicalClassifyReq true "type json string" // @Success 200 Ret=200 修改成功 // @router /sci_hq/classify/edit [post] func (this *SciHqDataController) EditClassify() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req request.EditBaseFromSciHqClassifyReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ClassifyId <= 0 { br.Msg = "参数错误" return } if req.ClassifyName == "" { br.Msg = "请输入分类名称" return } classify, err := data_manage.GetSciHqClassifyById(req.ClassifyId) if err != nil { if err.Error() == utils.ErrNoRow() { br.Msg = "分类不存在" return } br.Msg = "编辑失败" br.ErrMsg = "获取分类失败,Err:" + err.Error() return } switch this.Lang { case utils.EnLangVersion: if classify.ClassifyNameEn != req.ClassifyName { count, err := data_manage.GetSciHqClassifyCountByName(req.ClassifyName, this.Lang) if err != nil { br.Msg = "编辑失败" br.ErrMsg = "获取分类失败,Err:" + err.Error() return } if count > 0 { br.Msg = "分类名称已存在" return } err = classify.Update([]string{"classify_name_en", "modify_time"}) if err != nil { br.Msg = "编辑失败" br.ErrMsg = "编辑失败,Err:" + err.Error() return } } default: if classify.ClassifyName != req.ClassifyName { count, err := data_manage.GetSciHqClassifyCountByName(req.ClassifyName, this.Lang) if err != nil { br.Msg = "编辑失败" br.ErrMsg = "获取分类失败,Err:" + err.Error() return } if count > 0 { br.Msg = "分类名称已存在" return } err = classify.Update([]string{"classify_name", "modify_time"}) if err != nil { br.Msg = "编辑失败" br.ErrMsg = "编辑失败,Err:" + err.Error() return } } } br.Msg = "编辑成功" br.Success = true br.Ret = 200 br.IsAddLog = true } // Move // @Title 分类移动接口 // @Description 分类移动接口 // @Success 200 {object} data_manage.MoveBaseFromMysteelChemicalClassifyReq // @router /sci/classify/move [post] func (this *SciHqDataController) Move() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req request.MoveBaseFromSciHqClassifyReq if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } } // SciHqIndexList // @Title 卓创红期指标列表 // @Description 卓创红期数据指标列表接口 // @Param ClassifyId query int true "分类id" // @Success 200 {object} data_manage.BaseFromMysteelChemicalIndexResp // @router /sci_hq/index/list [get] func (this *SciHqDataController) SciHqIndexList() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() classifyId, _ := this.GetInt("ClassifyId", 0) indexList, err := data_manage.GetSciHqIndexBaseInfoByClassifyId(classifyId) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "获取失败" br.ErrMsg = "获取指标信息失败,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = indexList } // SciHqIndexPageList // @Title 卓创红期指标列表 // @Description 卓创红期数据指标列表接口 // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Param ClassifyId query int true "分类id" // @Success 200 {object} data_manage.BaseFromMysteelChemicalIndexResp // @router /sci_hq/index/page/list [get] func (this *SciHqDataController) SciHqIndexPageList() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() classifyId, _ := this.GetInt("ClassifyId", 0) pageSize, _ := this.GetInt("PageSize") currrentIndex, _ := this.GetInt("CurrentIndex") var startSize int if pageSize <= 0 { pageSize = utils.PageSize20 } if currrentIndex <= 0 { currrentIndex = 1 } startSize = utils.StartIndex(currrentIndex, pageSize) total, err := data_manage.GetSciHqIndexCountByClassifyId(classifyId) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取指标信息失败,Err:" + err.Error() return } indexList, err := data_manage.GetSciHqIndexByClassifyId(classifyId, startSize, pageSize) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "获取失败" br.ErrMsg = "获取指标信息失败,Err:" + err.Error() return } page := paging.GetPaging(currrentIndex, pageSize, total) resp := new(response.SciHqIndexPageListResp) resp.List = indexList resp.Paging = page br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // SciHqSearchList // @Title 卓创红期模糊搜索 // @Description 卓创红期模糊搜索 // @Param Keyword query string ture "关键字搜索" // @Success 200 {object} models.BaseResponse // @router /sci/search_list [get] func (this *SciHqDataController) SciHqSearchList() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() //关键字 keyword := this.GetString("Keyword") var condition string var pars []interface{} if keyword != "" { condition += ` AND (index_code LIKE ? OR index_name LIKE ?)` pars = utils.GetLikeKeywordPars(pars, keyword, 2) } else { br.Msg = "请输入指标ID/指标名称" return } list, err := data_manage.GetSciHqIndexBaseInfoByCondition(condition, pars) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取失败,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = list } // SciHqSingleData // @Title 获取卓创红期据 // @Description 获取卓创红期单条数据接口 // @Param IndexCode query string true "指标唯一编码" // @Success 200 {object} models.BaseResponse // @router /sci_hq/single_data [get] func (this *SciHqDataController) SciHqSingleData() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() indexCode := this.GetString("IndexCode") indexInfo, err := data_manage.GetBaseFromSciHqIndexByIndexCode(indexCode) if err != nil { br.Msg = "获取指标信息失败" br.ErrMsg = "获取指标信息失败,Err:" + err.Error() return } dataList, err := data_manage.GetSciHqIndexDataByCode(indexCode) if err != nil { br.Msg = "获取数据失败" br.ErrMsg = "获取数据失败,Err:" + err.Error() return } var ret response.SciHqSingleDataResp ret.ClassifyId = indexInfo.ClassifyId ret.BaseFromSciHqIndexId = indexInfo.BaseFromSciHqIndexId ret.IndexCode = indexInfo.IndexCode ret.IndexName = indexInfo.IndexName ret.Frequency = indexInfo.Frequency ret.CreateTime = indexInfo.CreateTime.Format(utils.FormatDateTime) ret.ModifyTime = indexInfo.ModifyTime.Format(utils.FormatDateTime) ret.Unit = indexInfo.Unit ret.Data = dataList br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = ret } // ResetSciHqIndex // @Title 指标数据清除分类 // @Description 指标数据清除分类 // @Param request body data_manage.DelBaseFromSciReq true "type json string" // @Success 200 Ret=200 操作成功 // @router /sci_hq/reset [post] func (this *SciHqDataController) ResetSciHqIndex() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req request.ResetBaseFromSciHqReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.BaseFromSciHqIndexId < 0 { br.Msg = "参数错误" br.IsSendEmail = false return } err = data.ResetSciHqIndex(req.BaseFromSciHqIndexId) if err != nil { br.Msg = "移动失败" br.ErrMsg = "移动失败,Err:" + err.Error() return } br.Ret = 200 br.Msg = "操作成功" br.Success = true br.IsAddLog = true } // EditSciHq // @Title 编辑卓创红期指标 // @Description 编辑卓创红期指标接口 // @Param request body data_manage.AddEdbClassifyReq true "type json string" // @Success 200 Ret=200 保存成功 // @router /sci_hq/edit [post] func (this *SciHqDataController) EditSciHq() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req request.EditBaseFromSciHqReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.BaseFromSciHqIndexId <= 0 { br.Msg = "请选择指标" br.IsSendEmail = false return } if req.ClassifyId <= 0 { br.Msg = "请选择分类" br.IsSendEmail = false return } //编辑指标 sciIndexInfo, err := data.EditSciHqIndex(req.BaseFromSciHqIndexId, req.ClassifyId) if err != nil { if err.Error() == utils.ErrNoRow() { br.Msg = "指标不存在或已删除" return } br.Msg = "编辑失败" br.ErrMsg = "编辑指标失败,Err:" + err.Error() return } resp := response.EditSciHqIndexInfoResp{ BaseFromSciHqIndexId: sciIndexInfo.BaseFromSciHqIndexId, IndexCode: sciIndexInfo.IndexCode, } br.Data = resp br.Ret = 200 br.Msg = "保存成功" br.Success = true br.IsAddLog = true } // DeleteSciHqData // @Title 删除指标 // @Description 删除指标接口 // @Param request body data_manage.DelBaseFromSciReq true "type json string" // @Success 200 Ret=200 删除成功 // @router /sci_hq/del [post] func (this *SciHqDataController) DeleteSciHqData() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req request.DelBaseFromSciHqReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.BaseFromSciHqIndexId < 0 { br.Msg = "参数错误" br.IsSendEmail = false return } err, errMsg := data.DelSciHqData(req.BaseFromSciHqIndexId) if errMsg != `` { br.Msg = errMsg br.ErrMsg = errMsg if err != nil { br.ErrMsg = errMsg + ";Err:" + err.Error() } else { br.IsSendEmail = false } return } br.Ret = 200 br.Msg = "删除成功" br.Success = true br.IsAddLog = true } // getSciHqClassifyTree 返回卓创红旗的树形结构 func getSciHqClassifyTree(items []*data_manage.BaseFromSciHqClassifyItem, parentId int) []*data_manage.BaseFromSciHqClassifyItem { res := make([]*data_manage.BaseFromSciHqClassifyItem, 0) for _, item := range items { if item.ParentId == parentId { t := new(data_manage.BaseFromSciHqClassifyItem) t.ClassifyId = item.ClassifyId t.ClassifyName = item.ClassifyName t.ParentId = item.ParentId t.Level = item.Level t.Sort = item.Sort t.ModifyTime = item.ModifyTime t.CreateTime = item.CreateTime t.ClassifyNameEn = item.ClassifyNameEn t.Children = getSciHqClassifyTree(items, item.ClassifyId) res = append(res, t) } } return res }