作者 郑周

1 部门逻辑-平台增删改查

1 package department 1 package department
2 2
3 import ( 3 import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
4 "net/http" 5 "net/http"
5 6
6 "github.com/zeromicro/go-zero/rest/httpx" 7 "github.com/zeromicro/go-zero/rest/httpx"
@@ -19,10 +20,6 @@ func SystemAddHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { @@ -19,10 +20,6 @@ func SystemAddHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
19 20
20 l := department.NewSystemAddLogic(r.Context(), svcCtx) 21 l := department.NewSystemAddLogic(r.Context(), svcCtx)
21 resp, err := l.SystemAdd(&req) 22 resp, err := l.SystemAdd(&req)
22 - if err != nil {  
23 - httpx.ErrorCtx(r.Context(), w, err)  
24 - } else {  
25 - httpx.OkJsonCtx(r.Context(), w, resp)  
26 - } 23 + result.HttpResult(r, w, resp, err)
27 } 24 }
28 } 25 }
1 package department 1 package department
2 2
3 import ( 3 import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
4 "net/http" 5 "net/http"
5 6
6 "github.com/zeromicro/go-zero/rest/httpx" 7 "github.com/zeromicro/go-zero/rest/httpx"
@@ -19,10 +20,6 @@ func SystemGetHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { @@ -19,10 +20,6 @@ func SystemGetHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
19 20
20 l := department.NewSystemGetLogic(r.Context(), svcCtx) 21 l := department.NewSystemGetLogic(r.Context(), svcCtx)
21 resp, err := l.SystemGet(&req) 22 resp, err := l.SystemGet(&req)
22 - if err != nil {  
23 - httpx.ErrorCtx(r.Context(), w, err)  
24 - } else {  
25 - httpx.OkJsonCtx(r.Context(), w, resp)  
26 - } 23 + result.HttpResult(r, w, resp, err)
27 } 24 }
28 } 25 }
1 package department 1 package department
2 2
3 import ( 3 import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
4 "net/http" 5 "net/http"
5 6
6 "github.com/zeromicro/go-zero/rest/httpx" 7 "github.com/zeromicro/go-zero/rest/httpx"
@@ -19,10 +20,6 @@ func SystemListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { @@ -19,10 +20,6 @@ func SystemListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
19 20
20 l := department.NewSystemListLogic(r.Context(), svcCtx) 21 l := department.NewSystemListLogic(r.Context(), svcCtx)
21 resp, err := l.SystemList(&req) 22 resp, err := l.SystemList(&req)
22 - if err != nil {  
23 - httpx.ErrorCtx(r.Context(), w, err)  
24 - } else {  
25 - httpx.OkJsonCtx(r.Context(), w, resp)  
26 - } 23 + result.HttpResult(r, w, resp, err)
27 } 24 }
28 } 25 }
1 package department 1 package department
2 2
3 import ( 3 import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
4 "net/http" 5 "net/http"
5 6
6 "github.com/zeromicro/go-zero/rest/httpx" 7 "github.com/zeromicro/go-zero/rest/httpx"
@@ -19,10 +20,6 @@ func SystemUpdateHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { @@ -19,10 +20,6 @@ func SystemUpdateHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
19 20
20 l := department.NewSystemUpdateLogic(r.Context(), svcCtx) 21 l := department.NewSystemUpdateLogic(r.Context(), svcCtx)
21 resp, err := l.SystemUpdate(&req) 22 resp, err := l.SystemUpdate(&req)
22 - if err != nil {  
23 - httpx.ErrorCtx(r.Context(), w, err)  
24 - } else {  
25 - httpx.OkJsonCtx(r.Context(), w, resp)  
26 - } 23 + result.HttpResult(r, w, resp, err)
27 } 24 }
28 } 25 }
@@ -2,6 +2,10 @@ package department @@ -2,6 +2,10 @@ package department
2 2
3 import ( 3 import (
4 "context" 4 "context"
  5 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
  6 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
  7 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
  8 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
5 9
6 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc" 10 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types" 11 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
@@ -24,7 +28,41 @@ func NewSystemAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemA @@ -24,7 +28,41 @@ func NewSystemAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemA
24 } 28 }
25 29
26 func (l *SystemAddLogic) SystemAdd(req *types.DepartmentAddRequest) (resp *types.DepartmentGetResponse, err error) { 30 func (l *SystemAddLogic) SystemAdd(req *types.DepartmentAddRequest) (resp *types.DepartmentGetResponse, err error) {
27 - // todo: add your logic here and delete this line 31 + var userToken = contextdata.GetUserTokenFromCtx(l.ctx)
  32 + var conn = l.svcCtx.DefaultDBConn()
28 33
29 - return 34 + _, list, err := l.svcCtx.DepartmentRepository.Find(l.ctx, conn, domain.NewQueryOptions().
  35 + WithOffsetLimit(1, 1).WithCountOnly().
  36 + WithKV("companyId", userToken.CompanyId).
  37 + WithKV("name", req.Name))
  38 + if err != nil {
  39 + return nil, err
  40 + }
  41 + if len(list) > 0 {
  42 + return nil, xerr.NewErrMsg("该分组名称已存在(不能重复)")
  43 + }
  44 +
  45 + insert := &domain.Department{
  46 + Id: 0,
  47 + ParentId: 0,
  48 + CompanyId: userToken.CompanyId,
  49 + Name: req.Name,
  50 + }
  51 + err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error {
  52 + _, err = l.svcCtx.DepartmentRepository.Insert(l.ctx, conn, insert)
  53 + return err
  54 + }, true)
  55 + if err != nil {
  56 + return nil, xerr.NewErrMsgErr("新增分组失败", err)
  57 + }
  58 +
  59 + resp = &types.DepartmentGetResponse{
  60 + Department: types.Department{
  61 + Id: insert.Id,
  62 + CompanyId: insert.CompanyId,
  63 + ParentId: insert.ParentId,
  64 + Name: insert.Name,
  65 + },
  66 + }
  67 + return resp, nil
30 } 68 }
@@ -2,7 +2,6 @@ package department @@ -2,7 +2,6 @@ package department
2 2
3 import ( 3 import (
4 "context" 4 "context"
5 -  
6 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc" 5 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types" 6 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
8 7
@@ -24,7 +23,18 @@ func NewSystemGetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemG @@ -24,7 +23,18 @@ func NewSystemGetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemG
24 } 23 }
25 24
26 func (l *SystemGetLogic) SystemGet(req *types.DepartmentGetRequest) (resp *types.DepartmentGetResponse, err error) { 25 func (l *SystemGetLogic) SystemGet(req *types.DepartmentGetRequest) (resp *types.DepartmentGetResponse, err error) {
27 - // todo: add your logic here and delete this line  
28 -  
29 - return 26 + var conn = l.svcCtx.DefaultDBConn()
  27 + department, err := l.svcCtx.DepartmentRepository.FindOne(l.ctx, conn, req.Id)
  28 + if err != nil {
  29 + return nil, err
  30 + }
  31 + resp = &types.DepartmentGetResponse{
  32 + Department: types.Department{
  33 + Id: department.Id,
  34 + CompanyId: department.CompanyId,
  35 + ParentId: department.ParentId,
  36 + Name: department.Name,
  37 + },
  38 + }
  39 + return resp, nil
30 } 40 }
@@ -2,9 +2,10 @@ package department @@ -2,9 +2,10 @@ package department
2 2
3 import ( 3 import (
4 "context" 4 "context"
5 -  
6 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc" 5 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types" 6 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
  7 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
  8 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
8 9
9 "github.com/zeromicro/go-zero/core/logx" 10 "github.com/zeromicro/go-zero/core/logx"
10 ) 11 )
@@ -24,7 +25,29 @@ func NewSystemListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *System @@ -24,7 +25,29 @@ func NewSystemListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *System
24 } 25 }
25 26
26 func (l *SystemListLogic) SystemList(req *types.DepartmentListRequest) (resp *types.DepartmentListResponse, err error) { 27 func (l *SystemListLogic) SystemList(req *types.DepartmentListRequest) (resp *types.DepartmentListResponse, err error) {
27 - // todo: add your logic here and delete this line 28 + var userToken = contextdata.GetUserTokenFromCtx(l.ctx)
  29 + var conn = l.svcCtx.DefaultDBConn()
  30 +
  31 + total, list, err := l.svcCtx.DepartmentRepository.Find(l.ctx, conn, domain.NewQueryOptions().
  32 + WithOffsetLimit(req.Page, req.Size).
  33 + WithKV("companyId", userToken.CompanyId))
  34 + if err != nil {
  35 + return nil, err
  36 + }
  37 +
  38 + resp = &types.DepartmentListResponse{
  39 + Total: total,
  40 + List: make([]types.Department, 0),
  41 + }
  42 + for _, item := range list {
  43 + to := types.Department{
  44 + Id: item.Id,
  45 + CompanyId: item.CompanyId,
  46 + ParentId: item.ParentId,
  47 + Name: item.Name,
  48 + }
  49 + resp.List = append(resp.List, to)
  50 + }
  51 + return resp, nil
28 52
29 - return  
30 } 53 }
@@ -2,6 +2,10 @@ package department @@ -2,6 +2,10 @@ package department
2 2
3 import ( 3 import (
4 "context" 4 "context"
  5 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
  6 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
  7 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
  8 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
5 9
6 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc" 10 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types" 11 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
@@ -24,7 +28,42 @@ func NewSystemUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Syst @@ -24,7 +28,42 @@ func NewSystemUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Syst
24 } 28 }
25 29
26 func (l *SystemUpdateLogic) SystemUpdate(req *types.DepartmentUpdateRequest) (resp *types.DepartmentGetResponse, err error) { 30 func (l *SystemUpdateLogic) SystemUpdate(req *types.DepartmentUpdateRequest) (resp *types.DepartmentGetResponse, err error) {
27 - // todo: add your logic here and delete this line 31 + var userToken = contextdata.GetUserTokenFromCtx(l.ctx)
  32 + var conn = l.svcCtx.DefaultDBConn()
28 33
29 - return 34 + _, list, err := l.svcCtx.DepartmentRepository.Find(l.ctx, conn, domain.NewQueryOptions().
  35 + WithOffsetLimit(1, 1).WithCountOnly().
  36 + WithKV("companyId", userToken.CompanyId).
  37 + WithKV("notId", req.Id). // 排除自己
  38 + WithKV("name", req.Name))
  39 + if err != nil {
  40 + return nil, err
  41 + }
  42 + if len(list) > 0 {
  43 + return nil, xerr.NewErrMsg("该分组名称已存在(不能重复)")
  44 + }
  45 +
  46 + one, err := l.svcCtx.DepartmentRepository.FindOne(l.ctx, conn, req.Id)
  47 + if err != nil {
  48 + return nil, xerr.NewErrMsgErr("分组修改失败", err)
  49 + }
  50 + one.Name = req.Name
  51 +
  52 + // 更新
  53 + err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error {
  54 + _, err = l.svcCtx.DepartmentRepository.UpdateWithVersion(l.ctx, conn, one)
  55 + return err
  56 + }, true)
  57 + if err != nil {
  58 + return nil, xerr.NewErrMsgErr("分组修改失败", err)
  59 + }
  60 + resp = &types.DepartmentGetResponse{
  61 + Department: types.Department{
  62 + Id: one.Id,
  63 + CompanyId: one.CompanyId,
  64 + ParentId: one.ParentId,
  65 + Name: one.Name,
  66 + },
  67 + }
  68 + return resp, nil
30 } 69 }
@@ -34,9 +34,10 @@ func (l *MiniSystemLogic) MiniSystem(req *types.MessageSystemRequest) (resp *typ @@ -34,9 +34,10 @@ func (l *MiniSystemLogic) MiniSystem(req *types.MessageSystemRequest) (resp *typ
34 if err != nil { 34 if err != nil {
35 return nil, err 35 return nil, err
36 } 36 }
37 - resp = &types.MessageSystemResponse{}  
38 - resp.Total = total  
39 - resp.List = make([]types.MessageSystemItem, 0) 37 + resp = &types.MessageSystemResponse{
  38 + Total: total,
  39 + List: make([]types.MessageSystemItem, 0),
  40 + }
40 for _, item := range list { 41 for _, item := range list {
41 to := types.MessageSystemItem{ 42 to := types.MessageSystemItem{
42 Id: item.Id, 43 Id: item.Id,
@@ -120,9 +120,15 @@ func (repository *DepartmentRepository) Find(ctx context.Context, conn transacti @@ -120,9 +120,15 @@ func (repository *DepartmentRepository) Find(ctx context.Context, conn transacti
120 ) 120 )
121 queryFunc := func() (interface{}, error) { 121 queryFunc := func() (interface{}, error) {
122 tx = tx.Model(&ms).Order("id desc") 122 tx = tx.Model(&ms).Order("id desc")
  123 + if v, ok := queryOptions["notId"]; ok {
  124 + tx.Where("id != ?", v)
  125 + }
123 if v, ok := queryOptions["companyId"]; ok { 126 if v, ok := queryOptions["companyId"]; ok {
124 tx.Where("company_id = ?", v) 127 tx.Where("company_id = ?", v)
125 } 128 }
  129 + if v, ok := queryOptions["name"]; ok {
  130 + tx.Where("name = ?", v)
  131 + }
126 if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil { 132 if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
127 return dms, tx.Error 133 return dms, tx.Error
128 } 134 }
@@ -9,8 +9,7 @@ type Department struct { @@ -9,8 +9,7 @@ type Department struct {
9 Id int64 `json:"id,omitempty"` // 部门ID 9 Id int64 `json:"id,omitempty"` // 部门ID
10 CompanyId int64 `json:"companyId,omitempty"` // 公司ID 10 CompanyId int64 `json:"companyId,omitempty"` // 公司ID
11 ParentId int64 `json:"parentId,omitempty"` // 父级ID 11 ParentId int64 `json:"parentId,omitempty"` // 父级ID
12 - Name string `json:"name,omitempty"` // 部门名称  
13 - 12 + Name string `json:"name,omitempty"` // 部门名称\
14 CreatedAt int64 `json:"createdAt,omitempty"` 13 CreatedAt int64 `json:"createdAt,omitempty"`
15 UpdatedAt int64 `json:"updatedAt,omitempty"` 14 UpdatedAt int64 `json:"updatedAt,omitempty"`
16 DeletedAt int64 `json:"deletedAt,omitempty"` 15 DeletedAt int64 `json:"deletedAt,omitempty"`