package trade_analysis import ( "encoding/json" "eta/eta_api/controllers" "eta/eta_api/models" "eta/eta_api/models/data_manage" "eta/eta_api/models/system" "eta/eta_api/services/data" "eta/eta_api/services/data/data_manage_permission" "eta/eta_api/utils" "fmt" "sort" "time" ) // WarehouseClassifyController 建仓过程-分类 type WarehouseClassifyController struct { controllers.BaseAuthController } //func (c *WarehouseClassifyController) ClassifyMove() { // br := new(models.BaseResponse).Init() // defer func() { // if br.ErrMsg == "" { // br.IsSendEmail = false // } // c.Data["json"] = br // c.ServeJSON() // }() // var pars models.CommonClassifyMoveReq // // classifyOb := new(trade_analysis.WareHouseProcessClassify) // ctx := data.NewCommonClassifyCtx(classifyOb) // // tips, e := data.CommonClassifyMove(pars, ctx) // if tips != "" { // br.Msg = tips // return // } // if e != nil { // br.Msg = "操作失败" // br.ErrMsg = fmt.Sprintf("移动失败, %v", e) // return // } // // br.Ret = 200 // br.Success = true // br.Msg = "操作成功" //} // ChartClassifyList // @Title 分类列表 // @Description 分类列表接口 // @Param IsShowMe query bool false "是否只看我的,true、false" // @Param ParentId query bool false "父级ID" // @Param Source query int false "图表类型,3:相关性,4:滚动相关性" // @Success 200 {object} data_manage.ChartClassifyListResp // @router /warehouse/classify/list [get] func (this *WarehouseClassifyController) ChartClassifyList() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() resp := new(data_manage.ChartClassifyListResp) isShowMe, _ := this.GetBool("IsShowMe") parentId, _ := this.GetInt("ParentId") source, _ := this.GetInt("Source", utils.CHART_SOURCE_TRADE_ANALYSIS_PROCESS) nodeAll := make([]*data_manage.ChartClassifyItems, 0) // 查询分类节点 rootList, err := data_manage.GetChartClassifyByParentId(parentId, utils.CHART_SOURCE_TRADE_ANALYSIS_PROCESS) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "获取失败" br.ErrMsg = "获取数据失败,Err:" + err.Error() return } if len(rootList) > 0 { permissionClassifyIdList, e := data_manage_permission.GetUserChartClassifyPermissionList(this.SysUser.AdminId, 0) if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取已授权分类id数据失败,Err:" + e.Error() return } for _, v := range rootList { // 操作按钮权限 v.HaveOperaAuth = data_manage_permission.CheckChartClassifyPermissionByPermissionIdList(v.IsJoinPermission, v.ChartClassifyId, permissionClassifyIdList) button := data.GetChartClassifyOpButton(this.SysUser, v.SysUserId, v.HaveOperaAuth) v.Button = button v.ParentId = parentId v.Children = make([]*data_manage.ChartClassifyItems, 0) nodeAll = append(nodeAll, v) } } // 查询图表节点, ParentId=0时说明仅查询一级目录节点 if parentId > 0 { // 查询当前分类信息 currClassify, e := data_manage.GetChartClassifyById(parentId) if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取当前分类信息失败,Err:" + e.Error() return } // 获取所有有权限的指标和分类 permissionEdbIdList, permissionClassifyIdList, e := data_manage_permission.GetUserChartAndClassifyPermissionList(this.SysUser.AdminId, 0, 0) if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + e.Error() return } var adminId int if isShowMe { adminId = this.SysUser.AdminId } charts, e := data_manage.GetChartInfoBySourceAndParentId(source, parentId, adminId) if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取图表信息失败, Err: %v", e) return } for _, v := range charts { // 操作按钮权限 v.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(v.IsJoinPermission, currClassify.IsJoinPermission, v.ChartInfoId, v.ChartClassifyId, permissionEdbIdList, permissionClassifyIdList) button := data.GetChartOpButton(this.SysUser, v.SysUserId, v.HaveOperaAuth) button.AddButton = false //不管有没有权限,图表都是没有添加按钮的 v.Button = button v.ParentId = parentId v.Children = make([]*data_manage.ChartClassifyItems, 0) nodeAll = append(nodeAll, v) } } // 整体排序 if len(nodeAll) > 0 { sort.Slice(nodeAll, func(i, j int) bool { return nodeAll[i].Sort < nodeAll[j].Sort }) } resp.AllNodes = nodeAll br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // ChartClassifyItems // @Title 获取所有分类接口-不包含图表 // @Description 获取所有分类接口-不包含图表 // @Success 200 {object} data_manage.ChartClassifyListResp // @router /warehouse/classify/items [get] func (this *WarehouseClassifyController) ChartClassifyItems() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() rootList, err := data_manage.GetChartClassifyByParentId(0, utils.CHART_SOURCE_TRADE_ANALYSIS_PROCESS) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取数据失败,Err:" + err.Error() return } nodeAll := make([]*data_manage.ChartClassifyItems, 0) for k := range rootList { rootNode := rootList[k] nodeAll = append(nodeAll, rootNode) } resp := new(data_manage.ChartClassifyListResp) resp.AllNodes = nodeAll br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // AddChartClassify // @Title 新增分类 // @Description 新增分类接口 // @Param request body data_manage.AddChartClassifyReq true "type json string" // @Success 200 Ret=200 保存成功 // @router /warehouse/classify/add [post] func (this *WarehouseClassifyController) AddChartClassify() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req data_manage.AddChartClassifyReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ChartClassifyName == "" { br.Msg = "请输入分类名称" br.IsSendEmail = false return } if req.ParentId < 0 { br.Msg = "参数错误" br.IsSendEmail = false return } // 新增图表分类 _, err, errMsg, isSendEmail := data.AddChartClassify(req.ChartClassifyName, req.ParentId, req.Level, utils.CHART_SOURCE_TRADE_ANALYSIS_PROCESS, this.Lang, this.SysUser) if err != nil { br.Msg = errMsg br.ErrMsg = "添加分类失败,Err:" + err.Error() br.IsSendEmail = isSendEmail return } br.Ret = 200 br.Msg = "添加成功" br.Success = true } // EditChartClassify // @Title 修改分类 // @Description 修改分类接口 // @Param request body data_manage.EditChartClassifyReq true "type json string" // @Success 200 Ret=200 修改成功 // @router /warehouse/classify/edit [post] func (this *WarehouseClassifyController) EditChartClassify() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req data_manage.EditChartClassifyReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ChartClassifyName == "" { br.Msg = "请输入分类名称" br.IsSendEmail = false return } if req.ChartClassifyId <= 0 { br.Msg = "参数错误" br.IsSendEmail = false return } // 编辑图表分类 _, err, errMsg, isSendEmail := data.EditChartClassify(req.ChartClassifyId, utils.CHART_SOURCE_TRADE_ANALYSIS_PROCESS, req.ChartClassifyName, this.Lang, this.SysUser) if err != nil { br.Msg = errMsg br.ErrMsg = "保存分类失败,Err:" + err.Error() br.IsSendEmail = isSendEmail return } br.Ret = 200 br.Msg = "修改成功" br.Success = true br.IsAddLog = true } // DeleteChartClassifyCheck // @Title 删除图表检测接口 // @Description 删除图表检测接口 // @Param request body data_manage.ChartClassifyDeleteCheckResp true "type json string" // @Success 200 Ret=200 检测成功 // @router /warehouse/classify/delete/check [post] func (this *WarehouseClassifyController) DeleteChartClassifyCheck() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req data_manage.ChartClassifyDeleteCheckReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ChartClassifyId < 0 && req.ChartInfoId <= 0 { br.Msg = "参数错误" br.IsSendEmail = false return } var deleteStatus int var tipsMsg string //删除分类 if req.ChartClassifyId > 0 && req.ChartInfoId == 0 { //判断分类下,是否含有图表 count, err := data_manage.GetChartInfoCountByClassifyId(req.ChartClassifyId) if err != nil { br.Msg = "删除失败" br.ErrMsg = "分类下是否含有图表失败,Err:" + err.Error() return } if count > 0 { deleteStatus = 1 tipsMsg = "该分类下关联图表不可删除" } } if deleteStatus != 1 && req.ChartInfoId == 0 { classifyCount, err := data_manage.GetChartClassifyCountByClassifyId(req.ChartClassifyId) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "删除失败" br.ErrMsg = "分类下是否含有图表失败,Err:" + err.Error() return } if classifyCount > 0 { deleteStatus = 2 tipsMsg = "确认删除当前目录及包含的子目录吗" } } if deleteStatus == 0 { tipsMsg = "可删除,进行删除操作" } resp := new(data_manage.ChartClassifyDeleteCheckResp) resp.DeleteStatus = deleteStatus resp.TipsMsg = tipsMsg br.Ret = 200 br.Msg = "检测成功" br.Success = true br.Data = resp } // DeleteChartClassify // @Title 删除分类/图表 // @Description 删除分类/图表接口 // @Param request body data_manage.DeleteChartClassifyReq true "type json string" // @Success 200 Ret=200 删除成功 // @router /warehouse/classify/delete [post] func (this *WarehouseClassifyController) DeleteChartClassify() { 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.DeleteChartClassifyReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ChartClassifyId < 0 && req.ChartInfoId <= 0 { br.Msg = "参数错误" br.IsSendEmail = false return } //删除分类 if req.ChartClassifyId > 0 && req.ChartInfoId == 0 { //判断是否含有指标 count, err := data_manage.GetChartInfoCountByClassifyId(req.ChartClassifyId) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "删除失败" br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error() return } if count > 0 { br.Msg = "该目录下存在关联指标,不可删除" br.IsSendEmail = false return } err = data_manage.DeleteChartClassify(req.ChartClassifyId) if err != nil { br.Msg = "删除失败" br.ErrMsg = "删除失败,Err:" + err.Error() return } } resp := new(data_manage.AddChartInfoResp) //删除图表 if req.ChartInfoId > 0 { chartInfo, err := data_manage.GetChartInfoById(req.ChartInfoId) if err != nil { if err.Error() == utils.ErrNoRow() { br.Msg = "图表已删除,请刷新页面" br.ErrMsg = "指标不存在,Err:" + err.Error() return } else { br.Msg = "删除失败" br.ErrMsg = "删除失败,获取指标信息失败,Err:" + err.Error() return } } if chartInfo == nil { br.Msg = "图表已删除,请刷新页面" return } //图表操作权限 ok := data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, true) if !ok { br.Msg = "没有该图表的操作权限" br.ErrMsg = "没有该图表的操作权限" return } // 获取引用该图表的MyCharts, 用于ES删除 var myCond string var myPars []interface{} myCond += ` AND a.chart_info_id = ? ` myPars = append(myPars, chartInfo.ChartInfoId) myCharts, e := data_manage.GetMyChartListGroupByCharyInfoIdAndAdminIdByCondition(myCond, myPars) if e != nil { br.Msg = "删除失败" br.ErrMsg = "获取引用图表的MyChats失败, Err: " + e.Error() return } myIds := make([]int, 0) for _, m := range myCharts { myIds = append(myIds, m.MyChartId) } source := chartInfo.Source //删除图表及关联指标 err = data_manage.DeleteChartInfoAndData(chartInfo.ChartInfoId) if err != nil { br.Msg = "删除失败" br.ErrMsg = "删除失败,Err:" + err.Error() return } //删除ES { go data.EsDeleteChartInfo(chartInfo.ChartInfoId) // 删除MY ETA 图表 es数据 //go data.EsDeleteMyChartInfoByChartInfoId(chartInfo.ChartInfoId) go data.EsDeleteMyChartInfoByMyChartIds(myIds) } // 删除图例和多图关联 seriesOb := new(data_manage.ChartSeries) { cond := fmt.Sprintf("chart_info_id = ?") pars := make([]interface{}, 0) pars = append(pars, chartInfo.ChartInfoId) if e = seriesOb.RemoveByCondition(cond, pars); e != nil { br.Msg = "删除失败" br.ErrMsg = fmt.Sprintf("删除图表关联图例失败, %v", e) return } } if e = data_manage.RemoveMultiConfigChartMappingByChartInfoId(chartInfo.ChartInfoId); e != nil { br.Msg = "删除失败" br.ErrMsg = fmt.Sprintf("删除图表多图配置关联失败, %v", e) return } var condition string var pars []interface{} condition += " AND chart_classify_id=? AND source = ? " pars = append(pars, chartInfo.ChartClassifyId, source) condition += " AND chart_info_id>? ORDER BY create_time ASC LIMIT 1 " pars = append(pars, req.ChartInfoId) nextItem, err := data_manage.GetChartInfoByCondition(condition, pars) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "删除失败" br.ErrMsg = "获取下一级图库信息失败,Err:" + err.Error() return } if nextItem != nil { resp.UniqueCode = nextItem.UniqueCode resp.ChartInfoId = nextItem.ChartInfoId } else { var condition string var pars []interface{} condition += " AND level=1 " //pars = append(pars, chartInfo.ChartClassifyId) condition += " AND chart_classify_id>? ORDER BY chart_classify_id ASC LIMIT 1 " pars = append(pars, chartInfo.ChartClassifyId) classifyItem, err := data_manage.GetChartClassifyByCondition(condition, pars) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "删除失败" br.ErrMsg = "获取下一级图库分类信息失败,Err:" + err.Error() return } if classifyItem != nil { nextItem, err = data_manage.GetNextChartInfo(chartInfo.ChartClassifyId) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "删除失败" br.ErrMsg = "获取下一级图库信息失败,Err:" + err.Error() return } if nextItem != nil { resp.UniqueCode = nextItem.UniqueCode resp.ChartInfoId = nextItem.ChartInfoId } } } //新增操作日志 { chartLog := new(data_manage.ChartInfoLog) chartLog.ChartName = chartInfo.ChartName chartLog.ChartInfoId = req.ChartInfoId chartLog.ChartClassifyId = chartInfo.ChartClassifyId chartLog.SysUserId = sysUser.AdminId chartLog.SysUserRealName = sysUser.RealName chartLog.UniqueCode = chartInfo.UniqueCode chartLog.CreateTime = time.Now() chartLog.Content = string(this.Ctx.Input.RequestBody) chartLog.Status = "删除图表" chartLog.Method = this.Ctx.Input.URI() go data_manage.AddChartInfoLog(chartLog) } } br.Ret = 200 br.Msg = "删除成功" br.Success = true br.Data = resp br.IsAddLog = true } // ChartClassifyMove // @Title 分类移动接口 // @Description 分类移动接口 // @Success 200 {object} data_manage.MoveChartClassifyReq // @router /warehouse/classify/move [post] func (this *WarehouseClassifyController) ChartClassifyMove() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } 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.MoveChartClassifyReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ClassifyId <= 0 && req.ChartInfoId <= 0 { br.Msg = "参数错误" br.ErrMsg = "请选择拖动目标,分类目录或者指标" return } err, errMsg := data.MoveChartClassify(req, sysUser, utils.CHART_SOURCE_TRADE_ANALYSIS_PROCESS) if errMsg != `` { br.Msg = errMsg br.ErrMsg = errMsg if err != nil { br.ErrMsg = err.Error() } else { br.IsSendEmail = false } return } br.Ret = 200 br.Success = true br.Msg = "移动成功" } // ClassifyTree // @Title 多层分类列表树 // @Description 多层分类列表树 // @Success 200 {object} data_manage.ChartClassifyListResp // @router /warehouse/classify/tree [get] func (this *WarehouseClassifyController) ClassifyTree() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() allList, err := data_manage.GetChartClassifyAllBySource(utils.CHART_SOURCE_TRADE_ANALYSIS_PROCESS) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取所有分类失败, Err:" + err.Error() return } nodeAll := make([]*data_manage.ChartClassifyItems, 0) if len(allList) > 0 { // 已授权分类id permissionClassifyIdList, e := data_manage_permission.GetUserChartClassifyPermissionList(this.SysUser.AdminId, 0) if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取已授权分类id数据失败,Err:" + e.Error() return } for k, v := range allList { // 数据权限 v.HaveOperaAuth = data_manage_permission.CheckChartClassifyPermissionByPermissionIdList(v.IsJoinPermission, v.ChartClassifyId, permissionClassifyIdList) // 按钮权限 button := data.GetChartClassifyOpButton(this.SysUser, v.SysUserId, v.HaveOperaAuth) allList[k].Button = button } nodeAll = data.GetChartClassifyTreeRecursive(allList, 0) //根据sort值排序 sort.Slice(nodeAll, func(i, j int) bool { return nodeAll[i].Sort < nodeAll[j].Sort }) } language := `CN` // 显示的语言 { configDetail, _ := system.GetConfigDetailByCode(this.SysUser.AdminId, system.ChartLanguageVar) if configDetail != nil { language = configDetail.ConfigValue } else { configDetail, _ = system.GetDefaultConfigDetailByCode(system.ChartLanguageVar) if configDetail != nil { language = configDetail.ConfigValue } } } // 是否允许添加一级分类 canOpClassify := true button := data.GetChartClassifyOpButton(this.SysUser, 0, true) if !button.AddButton { canOpClassify = false } resp := new(data_manage.ChartClassifyListResp) resp.AllNodes = nodeAll resp.Language = language resp.CanOpClassify = canOpClassify br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp }