作者 郑周

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

package department
import (
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
... ... @@ -19,10 +20,6 @@ func SystemAddHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
l := department.NewSystemAddLogic(r.Context(), svcCtx)
resp, err := l.SystemAdd(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
result.HttpResult(r, w, resp, err)
}
}
... ...
package department
import (
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
... ... @@ -19,10 +20,6 @@ func SystemGetHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
l := department.NewSystemGetLogic(r.Context(), svcCtx)
resp, err := l.SystemGet(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
result.HttpResult(r, w, resp, err)
}
}
... ...
package department
import (
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
... ... @@ -19,10 +20,6 @@ func SystemListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
l := department.NewSystemListLogic(r.Context(), svcCtx)
resp, err := l.SystemList(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
result.HttpResult(r, w, resp, err)
}
}
... ...
package department
import (
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
... ... @@ -19,10 +20,6 @@ func SystemUpdateHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
l := department.NewSystemUpdateLogic(r.Context(), svcCtx)
resp, err := l.SystemUpdate(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
result.HttpResult(r, w, resp, err)
}
}
... ...
... ... @@ -2,6 +2,10 @@ package department
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"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
}
func (l *SystemAddLogic) SystemAdd(req *types.DepartmentAddRequest) (resp *types.DepartmentGetResponse, err error) {
// todo: add your logic here and delete this line
var userToken = contextdata.GetUserTokenFromCtx(l.ctx)
var conn = l.svcCtx.DefaultDBConn()
return
_, list, err := l.svcCtx.DepartmentRepository.Find(l.ctx, conn, domain.NewQueryOptions().
WithOffsetLimit(1, 1).WithCountOnly().
WithKV("companyId", userToken.CompanyId).
WithKV("name", req.Name))
if err != nil {
return nil, err
}
if len(list) > 0 {
return nil, xerr.NewErrMsg("该分组名称已存在(不能重复)")
}
insert := &domain.Department{
Id: 0,
ParentId: 0,
CompanyId: userToken.CompanyId,
Name: req.Name,
}
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error {
_, err = l.svcCtx.DepartmentRepository.Insert(l.ctx, conn, insert)
return err
}, true)
if err != nil {
return nil, xerr.NewErrMsgErr("新增分组失败", err)
}
resp = &types.DepartmentGetResponse{
Department: types.Department{
Id: insert.Id,
CompanyId: insert.CompanyId,
ParentId: insert.ParentId,
Name: insert.Name,
},
}
return resp, nil
}
... ...
... ... @@ -2,7 +2,6 @@ package department
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
... ... @@ -24,7 +23,18 @@ func NewSystemGetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemG
}
func (l *SystemGetLogic) SystemGet(req *types.DepartmentGetRequest) (resp *types.DepartmentGetResponse, err error) {
// todo: add your logic here and delete this line
return
var conn = l.svcCtx.DefaultDBConn()
department, err := l.svcCtx.DepartmentRepository.FindOne(l.ctx, conn, req.Id)
if err != nil {
return nil, err
}
resp = &types.DepartmentGetResponse{
Department: types.Department{
Id: department.Id,
CompanyId: department.CompanyId,
ParentId: department.ParentId,
Name: department.Name,
},
}
return resp, nil
}
... ...
... ... @@ -2,9 +2,10 @@ package department
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
"github.com/zeromicro/go-zero/core/logx"
)
... ... @@ -24,7 +25,29 @@ func NewSystemListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *System
}
func (l *SystemListLogic) SystemList(req *types.DepartmentListRequest) (resp *types.DepartmentListResponse, err error) {
// todo: add your logic here and delete this line
var userToken = contextdata.GetUserTokenFromCtx(l.ctx)
var conn = l.svcCtx.DefaultDBConn()
total, list, err := l.svcCtx.DepartmentRepository.Find(l.ctx, conn, domain.NewQueryOptions().
WithOffsetLimit(req.Page, req.Size).
WithKV("companyId", userToken.CompanyId))
if err != nil {
return nil, err
}
resp = &types.DepartmentListResponse{
Total: total,
List: make([]types.Department, 0),
}
for _, item := range list {
to := types.Department{
Id: item.Id,
CompanyId: item.CompanyId,
ParentId: item.ParentId,
Name: item.Name,
}
resp.List = append(resp.List, to)
}
return resp, nil
return
}
... ...
... ... @@ -2,6 +2,10 @@ package department
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"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
}
func (l *SystemUpdateLogic) SystemUpdate(req *types.DepartmentUpdateRequest) (resp *types.DepartmentGetResponse, err error) {
// todo: add your logic here and delete this line
var userToken = contextdata.GetUserTokenFromCtx(l.ctx)
var conn = l.svcCtx.DefaultDBConn()
return
_, list, err := l.svcCtx.DepartmentRepository.Find(l.ctx, conn, domain.NewQueryOptions().
WithOffsetLimit(1, 1).WithCountOnly().
WithKV("companyId", userToken.CompanyId).
WithKV("notId", req.Id). // 排除自己
WithKV("name", req.Name))
if err != nil {
return nil, err
}
if len(list) > 0 {
return nil, xerr.NewErrMsg("该分组名称已存在(不能重复)")
}
one, err := l.svcCtx.DepartmentRepository.FindOne(l.ctx, conn, req.Id)
if err != nil {
return nil, xerr.NewErrMsgErr("分组修改失败", err)
}
one.Name = req.Name
// 更新
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error {
_, err = l.svcCtx.DepartmentRepository.UpdateWithVersion(l.ctx, conn, one)
return err
}, true)
if err != nil {
return nil, xerr.NewErrMsgErr("分组修改失败", err)
}
resp = &types.DepartmentGetResponse{
Department: types.Department{
Id: one.Id,
CompanyId: one.CompanyId,
ParentId: one.ParentId,
Name: one.Name,
},
}
return resp, nil
}
... ...
... ... @@ -34,9 +34,10 @@ func (l *MiniSystemLogic) MiniSystem(req *types.MessageSystemRequest) (resp *typ
if err != nil {
return nil, err
}
resp = &types.MessageSystemResponse{}
resp.Total = total
resp.List = make([]types.MessageSystemItem, 0)
resp = &types.MessageSystemResponse{
Total: total,
List: make([]types.MessageSystemItem, 0),
}
for _, item := range list {
to := types.MessageSystemItem{
Id: item.Id,
... ...
... ... @@ -120,9 +120,15 @@ func (repository *DepartmentRepository) Find(ctx context.Context, conn transacti
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).Order("id desc")
if v, ok := queryOptions["notId"]; ok {
tx.Where("id != ?", v)
}
if v, ok := queryOptions["companyId"]; ok {
tx.Where("company_id = ?", v)
}
if v, ok := queryOptions["name"]; ok {
tx.Where("name = ?", v)
}
if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
return dms, tx.Error
}
... ...
... ... @@ -9,12 +9,11 @@ type Department struct {
Id int64 `json:"id,omitempty"` // 部门ID
CompanyId int64 `json:"companyId,omitempty"` // 公司ID
ParentId int64 `json:"parentId,omitempty"` // 父级ID
Name string `json:"name,omitempty"` // 部门名称
CreatedAt int64 `json:"createdAt,omitempty"`
UpdatedAt int64 `json:"updatedAt,omitempty"`
DeletedAt int64 `json:"deletedAt,omitempty"`
Version int `json:"version,omitempty"`
Name string `json:"name,omitempty"` // 部门名称\
CreatedAt int64 `json:"createdAt,omitempty"`
UpdatedAt int64 `json:"updatedAt,omitempty"`
DeletedAt int64 `json:"deletedAt,omitempty"`
Version int `json:"version,omitempty"`
}
type DepartmentRepository interface {
... ...