123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- // 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)
- }
|