123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614 |
- // Copyright 2019 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package v1
- import (
- "github.com/gin-gonic/gin"
- "github.com/jaryhe/gopkgs/logger"
- "github.com/jaryhe/gopkgs/tasker/httptasker"
- "github.com/jaryhe/gopkgs/util"
- "go.uber.org/zap"
- "net/http"
- "smart-enterprise-management-gateway/errors"
- param_v1 "smart-enterprise-management-gateway/param/v1"
- "smart-enterprise-management-gateway/pb"
- "smart-enterprise-management-gateway/pb/v1"
- "smart-enterprise-management-gateway/utils"
- "strconv"
- "strings"
- )
- // 项目列表
- // @Summary 项目列表
- // @Description 项目列表
- // @Tags project
- // @Accept json
- // @Produce json
- // @Param token header string true " "
- // @Param page query int true " "
- // @Param filter query string false " "
- // @Param page_size query int false "小于0时代表获取所有"
- // @Success 200 {object} v1.ProjectListResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/project/list [get]
- func (c *Controller) ProjectList(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.ProjectListRequest{}
- var loginUid int64
- var userName string
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, &req.ProjectListQuery, nil)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- resp := param_v1.ProjectListResponse{}
- rpcReq := &v1.ProjectListRequest{
- Page:req.Page,
- Filter:req.Filter,
- Cid:loginUid,
- PageSize:req.PageSize,
- }
- if req.FilterStatus != "" {
- array := strings.Split(req.FilterStatus, ",")
- for _, v := range array {
- value, _ := strconv.Atoi(v)
- rpcReq.FilterStatus = append(rpcReq.FilterStatus, int32(value))
- }
- }
- reply, err := pb.Enterprise.ProjectList(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "ProjectList"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- resp.Data = *reply
- if resp.Data.List == nil {
- resp.Data.List = make([]*v1.ProjectItem, 0)
- }
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- // 添加项目
- // @Summary 添加项目
- // @Description 添加项目
- // @Tags project
- // @Accept json
- // @Produce json
- // @Param token header string true " "
- // @Param body body v1.ProjectAddBody true " "
- // @Success 200 {object} v1.ProjectListResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/project [post]
- func (c *Controller) ProjectAdd(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.ProjectAddRequest{}
- var loginUid int64
- var userName string
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.ProjectAddBody)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- loginUid,userName, err = utils.GetJwtIdFromCtx(ctx)
- return nil
- }
- // 业务处理
- var projectId int64
- handleServiceTask := func() error {
- // 响应数据
- resp := param_v1.ProjectAddResponse{}
- rpcReq := &v1.ProjectAddRequest{}
- utils.StructCopy(rpcReq, &req.ProjectAddBody, "")
- rpcReq.CompanyId = loginUid
- reply, err := pb.Enterprise.ProjectAdd(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "ProjectAdd"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- projectId = reply.Id
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- err := httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- s, _ := json.MarshalToString(req)
- utils.LogWrite("项目申请", loginUid, userName, s, err, projectId)
- }
- // 项目账号列表
- // @Summary 项目账号列表
- // @Description 项目账号列表
- // @Tags project
- // @Accept json
- // @Produce json
- // @Param token header string true " "
- // @Param page query int true " "
- // @Param filter query string false " "
- // @Success 200 {object} v1.ProjectUserListResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/project/user_list [get]
- func (c *Controller) ProjectUserList(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.ProjectUserListRequest{}
- var loginUid int64
- var userName string
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, &req.ProjectUserListQuery, nil)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- resp := param_v1.ProjectUserListResponse{}
- rpcReq := &v1.ProjectUserListRequest{
- Page:req.Page,
- Filter:req.Filter,
- Cid:loginUid,
- }
- reply, err := pb.Enterprise.ProjectUserList(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "ProjectUserList"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- resp.Data = *reply
- if resp.Data.List == nil {
- resp.Data.List = make([]*v1.ProjectUserItem, 0)
- }
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- // 起停用项目账号
- // @Summary 起停用项目账号
- // @Description 起停用项目账号
- // @Tags project
- // @Accept json
- // @Produce json
- // @Param token header string true " "
- // @Param body body v1.EnableProjectUserBody true " "
- // @Success 200 {object} v1.EnableProjectUserResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/project/user [put]
- func (c *Controller) EnableProjectUser(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.EnableProjectUserRequest{}
- var loginUid int64
- var userName string
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.EnableProjectUserBody)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
- return nil
- }
- // 业务处理
- var projectId int64
- handleServiceTask := func() error {
- // 响应数据
- resp := param_v1.EnableProjectUserResponse{}
- rpcReq := &v1.EnableProjectUserRequest{
- Id:req.Id,
- Enable:req.Enable,
- }
- reply, err := pb.Enterprise.EnableProjectUser(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "EnableProjectUser"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- projectId = reply.ProjectId
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- err := httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- s, _ := json.MarshalToString(req)
- utils.LogWrite("起停用项目账号", loginUid, userName, s, err, projectId)
- }
- // 重置项目账号密码
- // @Summary 重置项目账号密码
- // @Description 重置项目账号密码
- // @Tags project
- // @Accept json
- // @Produce json
- // @Param token header string true " "
- // @Param body body v1.ProjectUserPasswordResetBody true " "
- // @Success 200 {object} v1.ProjectUserPasswordResetResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/project/password [put]
- func (c *Controller) ProjectUserPasswordReset(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.ProjectUserPasswordResetRequest{}
- var loginUid int64
- var userName string
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.ProjectUserPasswordResetBody)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- loginUid,userName, err = utils.GetJwtIdFromCtx(ctx)
- return nil
- }
- // 业务处理
- var projectId int64
- handleServiceTask := func() error {
- // 响应数据
- resp := param_v1.ProjectUserPasswordResetResponse{}
- rpcReq := &v1.ProjectUserPasswordResetRequest{}
- rpcReq.Password = req.Password
- rpcReq.Id = req.Id
- _, err := pb.Enterprise.ProjectUserPasswordReset(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "ProjectUserPasswordReset"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- err := httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- s, _ := json.MarshalToString(req)
- utils.LogWrite("重置项目账号密码", loginUid, userName, s, err, projectId)
- }
- // 变更项目
- // @Summary变更项目
- // @Description 变更项目
- // @Tags project
- // @Accept json
- // @Produce json
- // @Param token header string true " "
- // @Param body body v1.ProjectUpdateBody true " "
- // @Success 200 {object} v1.ProjectUpdateResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/project [put]
- func (c *Controller) ProjectUpdate(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.ProjectUpdateRequest{}
- var loginUid int64
- var userName string
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.ProjectUpdateBody)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- loginUid,userName, err = utils.GetJwtIdFromCtx(ctx)
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- resp := param_v1.ProjectUpdateResponse{}
- rpcReq := &v1.ProjectUpdateRequest{}
- utils.StructCopy(rpcReq, &req.ProjectUpdateBody, "")
- _, err := pb.Enterprise.ProjectUpdate(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "ProjectUpdate"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- err := httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- s, _ := json.MarshalToString(req)
- utils.LogWrite("项目变更", loginUid, userName, s, err, req.Id)
- }
- // 获取省市区
- // @Summary获取省市区
- // @Description 获取省市区
- // @Tags project
- // @Accept json
- // @Produce json
- // @Success 200 {object} v1.ProjectDistrictResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/project/district [get]
- func (c *Controller) ProjectDistrict(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.ProjectDistrictRequest{}
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- resp := param_v1.ProjectDistrictResponse{}
- rpcReq := &v1.ProjectDistrictRequest{}
- rpcResp, err := pb.Enterprise.ProjectDistrict(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "ProjectDistrict"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- if rpcResp.List == nil {
- rpcResp.List = []*v1.ProjectDistrictProvince{}
- }
- for i, v := range rpcResp.List {
- if v.List == nil {
- rpcResp.List[i].List = []*v1.ProjectDistrictCity{}
- continue
- }
- for j, z := range rpcResp.List[i].List {
- if z.List == nil {
- rpcResp.List[i].List[j].List = []*v1.ProjectDistrictZone{}
- }
- }
- }
- resp.Data = *rpcResp
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, handleServiceTask)
- }
- // 项目详情
- // @Summary 项目详情
- // @Description 项目详情
- // @Tags project
- // @Accept json
- // @Produce json
- // @Param token header string true " "
- // @Param id path int64 true " "
- // @Success 200 {object} v1.ProjectInfoResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/project/info/{id} [get]
- func (c *Controller) ProjectInfo(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.ProjectInfoRequest{}
- var loginUid int64
- var userName string
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, &req.ProjectInfoPath, nil, nil)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- resp := param_v1.ProjectInfoResponse{}
- rpcReq := &v1.ProjectInfoRequest{
- Id:req.Id,
- }
- reply, err := pb.Enterprise.ProjectInfo(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "ProjectInfo"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- resp.Data = *reply
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- }
- // 项目删除
- // @Summary 项目删除
- // @Description 项目删除
- // @Tags project
- // @Accept json
- // @Produce json
- // @Param token header string true " "
- // @Param id path int64 true " "
- // @Success 200 {object} v1.ProjectDelResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/project/{id} [delete]
- func (c *Controller) ProjectDel(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.ProjectDelRequest{}
- var loginUid int64
- var userName string
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, &req.ProjectDelPath, nil, nil)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
- return nil
- }
- origin := ""
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- resp := param_v1.ProjectDelResponse{}
- rpcReq := &v1.ProjectDelRequest{
- Id:req.Id,
- }
- reply, err := pb.Enterprise.ProjectDel(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "ProjectDel"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- origin = reply.Origin
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- err := httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- utils.LogWrite("项目删除", loginUid, userName, origin, err, req.Id)
- }
- //
- // @Summary 项目完工
- // @Description 项目完工
- // @Tags project
- // @Accept json
- // @Produce json
- // @Param token header string true " "
- // @Param id path int64 true " "
- // @Success 200 {object} v1.ProjectFinishResponse
- // @Failure 500 {object} base.HTTPError
- // @Router /api/v1/project/finish/{id} [put]
- func (c *Controller) ProjectFinish(ctx *gin.Context) {
- // 解析参数
- req := ¶m_v1.ProjectFinishRequest{}
- var loginUid int64
- var userName string
- parseParamTask := func() error {
- err := util.ShouldBind(ctx, &req.Header, &req.ProjectFinishPath, nil, nil)
- if err != nil {
- logger.Error("func",
- zap.String("call", "util.ShouldBind"),
- zap.String("error", err.Error()))
- return errors.ParamsError
- }
- loginUid, userName, err = utils.GetJwtIdFromCtx(ctx)
- return nil
- }
- // 业务处理
- handleServiceTask := func() error {
- // 响应数据
- resp := param_v1.ProjectFinishResponse{}
- rpcReq := &v1.ProjectFinishRequest{
- Id:req.Id,
- }
- _, err := pb.Enterprise.ProjectFinish(ctx, rpcReq)
- if err != nil {
- s, _ := json.MarshalToString(req)
- logger.Error("func",
- zap.String("call", "ProjectFinish"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- return errors.ErrorTransForm(err)
- }
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- err := httptasker.Exec(ctx, parseParamTask, handleServiceTask)
- utils.LogWrite("项目完工", loginUid, userName, "", err, req.Id)
- }
|