package v1 import ( "git.getensh.com/common/gopkgs/logger" "git.getensh.com/common/gopkgs/tasker/httptasker" "git.getensh.com/common/gopkgs/util" "github.com/gin-gonic/gin" "go.uber.org/zap" "net/http" "property-system-gateway/errors" param_v1 "property-system-gateway/param/v1" "property-system-gateway/pb" "property-system-gateway/pb/v1" "property-system-gateway/utils" ) // // @Summary 添加部门 // @Description 添加部门 // @Tags 部门 // @Accept json // @Produce json // @Param token header string true "token" // @Param body body v1.DepartmentAddBody true "信息" // @Success 200 {object} v1.DepartmentAddResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/department [post] func (c *Controller) DepartmentAdd(ctx *gin.Context) { // 解析参数 req := ¶m_v1.DepartmentAddRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.DepartmentAddBody) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { tokenInfo, err := utils.GetSubjectValue(ctx) if err != nil { return err } // 响应数据 resp := param_v1.DepartmentAddResponse{} rpcReq := &v1.DepartmentAddRequest{ GardenId:tokenInfo.GardenId, DepartmentName:req.DepartmentName, Desc:req.Desc, Cid:tokenInfo.Cid, } rpcRsp, err := pb.System.DepartmentAdd(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "pb.System.DepartmentAdd"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } resp.Data = *rpcRsp ctx.JSON(http.StatusOK, resp) logReq := OperationLogRequest{ Module:ModuleDepartment, Action:ActionDepartmentAdd, Origin:nil, Target:req.DepartmentAddBody, UserName:tokenInfo.UserName, Uid:tokenInfo.Uid, Cid:tokenInfo.Cid, GardenId:tokenInfo.GardenId, } go OperationLogAdd(&logReq) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) } // // @Summary 更改部门 // @Description 更改部门 // @Tags 部门 // @Accept json // @Produce json // @Param token header string true "token" // @Param body body v1.DepartmentUpdateBody true "信息" // @Success 200 {object} v1.DepartmentUpdateResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/department [put] func (c *Controller) DepartmentUpdate(ctx *gin.Context) { // 解析参数 req := ¶m_v1.DepartmentUpdateRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.DepartmentUpdateBody) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { tokenInfo, err := utils.GetSubjectValue(ctx) if err != nil { return err } // 响应数据 resp := param_v1.DepartmentUpdateResponse{} rpcReq := &v1.DepartmentUpdateRequest{ GardenId:tokenInfo.GardenId, Id:req.Id, DepartmentName:req.DepartmentName, Desc:req.Desc, } rpcRsp, err := pb.System.DepartmentUpdate(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "pb.System.DepartmentUpdate"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } ctx.JSON(http.StatusOK, resp) logReq := OperationLogRequest{ Module:ModuleDepartment, Action:ActionDepartmentUpdate, Origin:rpcRsp.Origin, Target:req.DepartmentUpdateBody, UserName:tokenInfo.UserName, Uid:tokenInfo.Uid, Cid:tokenInfo.Cid, GardenId:tokenInfo.GardenId, } go OperationLogAdd(&logReq) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) } // // @Summary 删除部门 // @Description 删除部门 // @Tags 部门 // @Accept json // @Produce json // @Param token header string true "token" // @Param id query int true "id" // @Success 200 {object} v1.DepartmentDelResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/department [delete] func (c *Controller) DepartmentDel(ctx *gin.Context) { // 解析参数 req := ¶m_v1.DepartmentDelRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, &req.DepartmentDelQuery, nil) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { tokenInfo, err := utils.GetSubjectValue(ctx) if err != nil { return err } // 响应数据 resp := param_v1.DepartmentDelResponse{} rpcReq := &v1.DepartmentDelRequest{ GardenId:tokenInfo.GardenId, Id:req.Id, } rpcRsp, err := pb.System.DepartmentDel(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "pb.System.DepartmentDel"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } ctx.JSON(http.StatusOK, resp) logReq := OperationLogRequest{ Module:ModuleDepartment, Action:ActionDepartmentDel, Origin:rpcRsp.Origin, Target:nil, UserName:tokenInfo.UserName, Uid:tokenInfo.Uid, Cid:tokenInfo.Cid, GardenId:tokenInfo.GardenId, } go OperationLogAdd(&logReq) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) } // // @Summary 部门列表 // @Description 部门列表 // @Tags 部门 // @Accept json // @Produce json // @Param page query int false " " // @Param page_size query int false " " // @Param token header string true "token" // @Success 200 {object} v1.DepartmentListResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/department [get] func (c *Controller) DepartmentList(ctx *gin.Context) { // 解析参数 req := ¶m_v1.DepartmentListRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, &req.DepartmentListQuery, nil) if err != nil { logger.Error("func", zap.String("call", "util.ShouldBind"), zap.String("error", err.Error())) return errors.ParamsError } return nil } // 业务处理 handleServiceTask := func() error { tokenInfo, err := utils.GetSubjectValue(ctx) if err != nil { return err } // 响应数据 resp := param_v1.DepartmentListResponse{} rpcReq := &v1.DepartmentListRequest{ GardenId:tokenInfo.GardenId, } rpcRsp, err := pb.System.DepartmentList(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "pb.System.DepartmentList"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } if rpcRsp.List == nil { rpcRsp.List = make([]*v1.DepartmentItem, 0) } resp.Data = *rpcRsp ctx.JSON(http.StatusOK, resp) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) }