// Copyright 2019 github.com. All rights reserved. // Use of this source code is governed by github.com. package v1 import ( "cp-organization-management-gateway/consts" "cp-organization-management-gateway/errors" param_v1 "cp-organization-management-gateway/param/v1" "cp-organization-management-gateway/pb" "cp-organization-management-gateway/pb/v1" "cp-organization-management-gateway/utils" "github.com/jaryhe/gopkgs/logger" "github.com/jaryhe/gopkgs/tasker/httptasker" "github.com/jaryhe/gopkgs/util" "net/http" "github.com/gin-gonic/gin" "go.uber.org/zap" ) func handleZoneList(list []*v1.ZoneItem) ([]*v1.ZoneItem) { if len(list) == 0 { list = make([]*v1.ZoneItem, 0) return list } for i, v := range list { list[i].Childs = handleZoneList(v.Childs) } return list } // // @Summary 区域列表 // @Description 区域列表 // @Tags 区域 // @Accept json // @Produce json // @Param token header string true "token" // @Success 200 {object} v1.ZoneListResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/zone/list [get] func (c *Controller) ZoneList(ctx *gin.Context) { // 解析参数 req := ¶m_v1.ZoneListRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, nil, 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.GetTokeInfo(ctx) if err != nil { return err } resp := param_v1.ZoneListResponse{} rpcReq := &v1.ZoneListRequest{ OrganizationCode:tokenInfo.OrganizationCode, Uid:tokenInfo.Uid, } rpcRsp, err := pb.Organization.ZoneList(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "pb.Organization.ZoneList"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } rpcRsp.List = handleZoneList(rpcRsp.List) resp.Data = *rpcRsp ctx.JSON(http.StatusOK, resp) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) } // // @Summary 删除区域 // @Description 删除区域 // @Tags 区域 // @Accept json // @Produce json // @Param token header string true "token" // @Param zone_code path string true " " // @Success 200 {object} v1.ZoneDelResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/zone/{zone_code} [delete] func (c *Controller) ZoneDel(ctx *gin.Context) { // 解析参数 req := ¶m_v1.ZoneDelRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, &req.ZoneDelPath, nil, 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.GetTokeInfo(ctx) if err != nil { return err } resp := param_v1.ZoneDelResponse{} rpcReq := &v1.ZoneDelRequest{ OrganizationCode:tokenInfo.OrganizationCode, Uid:tokenInfo.Uid, ZoneCode:req.ZoneCode, } rpcRsp, err := pb.Organization.ZoneDel(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "pb.Organization.ZoneDel"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } ctx.JSON(http.StatusOK, resp) logReq := OperationLogRequest{ Module:consts.OperationModuleZone, Action:consts.OperationActionZoneDel, Origin:rpcRsp.Zones, Target:req.ZoneDelPath, UserName:tokenInfo.Username, Uid:tokenInfo.Uid, OrganizationCode:tokenInfo.OrganizationCode, } 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.ZoneAddBody true " " // @Success 200 {object} v1.ZoneAddResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/zone [post] func (c *Controller) ZoneAdd(ctx *gin.Context) { // 解析参数 req := ¶m_v1.ZoneAddRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.ZoneAddBody) 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.GetTokeInfo(ctx) if err != nil { return err } resp := param_v1.ZoneAddResponse{} rpcReq := &v1.ZoneAddRequest{ OrganizationCode:tokenInfo.OrganizationCode, Uid:tokenInfo.Uid, ParentZoneCode:req.ParentZoneCode, ZoneName:req.ZoneName, } rpcRsp, err := pb.Organization.ZoneAdd(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "pb.Organization.ZoneAdd"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } resp.Data = *rpcRsp ctx.JSON(http.StatusOK, resp) logReq := OperationLogRequest{ Module:consts.OperationModuleZone, Action:consts.OperationActionZoneAdd, Origin:nil, Target:req.ZoneAddBody, UserName:tokenInfo.Username, Uid:tokenInfo.Uid, OrganizationCode:tokenInfo.OrganizationCode, } 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.ZoneUpdateBody true " " // @Success 200 {object} v1.ZoneUpdateResponse // @Failure 500 {object} base.HTTPError // @Router /api/v1/zone [put] func (c *Controller) ZoneUpdate(ctx *gin.Context) { // 解析参数 req := ¶m_v1.ZoneUpdateRequest{} parseParamTask := func() error { err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.ZoneUpdateBody) 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.GetTokeInfo(ctx) if err != nil { return err } resp := param_v1.ZoneUpdateResponse{} rpcReq := &v1.ZoneUpdateRequest{ OrganizationCode:tokenInfo.OrganizationCode, Uid:tokenInfo.Uid, ZoneName:req.ZoneName, ZoneCode:req.ZoneCode, ParentZoneCode:req.ParentZoneCode, } rpcRsp, err := pb.Organization.ZoneUpdate(ctx, rpcReq) if err != nil { s, _ := json.MarshalToString(req) logger.Error("func", zap.String("call", "pb.Organization.ZoneUpdate"), zap.String("params", s), zap.String("error", err.Error())) return errors.ErrorTransForm(err) } ctx.JSON(http.StatusOK, resp) logReq := OperationLogRequest{ Module:consts.OperationModuleZone, Action:consts.OperationActionZoneUpdate, Origin:rpcRsp.Origin, Target:rpcRsp.Target, UserName:tokenInfo.Username, Uid:tokenInfo.Uid, OrganizationCode:tokenInfo.OrganizationCode, } OperationLogAdd(&logReq) return nil } // 执行任务 httptasker.Exec(ctx, parseParamTask, handleServiceTask) }