system_update_logic.go
3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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"
"github.com/zeromicro/go-zero/core/logx"
)
type SystemUpdateLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSystemUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemUpdateLogic {
return &SystemUpdateLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SystemUpdateLogic) SystemUpdate(req *types.DepartmentUpdateRequest) (resp *types.DepartmentGetResponse, err error) {
var userToken = contextdata.GetUserTokenFromCtx(l.ctx)
var conn = l.svcCtx.DefaultDBConn()
total, _, err := l.svcCtx.DepartmentRepository.Find(l.ctx, conn, domain.NewQueryOptions().
WithCountOnly().
WithKV("companyId", userToken.CompanyId).
WithKV("notId", req.Id). // 排除自己
WithKV("name", req.Name))
if err != nil {
return nil, err
}
if total > 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
// 更新分组中的用户Id
var newIdMap = map[int64]int{}
for i := range req.Ids {
newIdMap[req.Ids[i]] = 0
}
// 获取公司下的所有用户
_, users, err := l.svcCtx.UserRepository.Find(l.ctx, conn, domain.NewQueryOptions().
WithFindOnly().
WithKV("companyId", one.CompanyId))
if err != nil {
return nil, err
}
// 更新
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, conn transaction.Conn) error {
_, err = l.svcCtx.DepartmentRepository.UpdateWithVersion(l.ctx, conn, one)
if err != nil {
return err
}
var findIndex = func(ids []int64, id int64) int {
for i, _ := range ids {
if ids[i] == id {
return i
}
}
return -1
}
for i := range users {
var user = users[i]
if _, ok := newIdMap[user.Id]; ok {
var targetIndex = findIndex(user.Departments, req.Id)
if targetIndex == -1 { // 归属分组不存在,则新增
user.Departments = append(user.Departments, req.Id)
_, err = l.svcCtx.UserRepository.UpdateWithVersion(l.ctx, conn, user)
if err != nil {
return err
}
}
} else {
var targetIndex = findIndex(user.Departments, req.Id)
if targetIndex != -1 { // 归属分组存在,则移除
user.Departments = append(user.Departments[:targetIndex], user.Departments[targetIndex+1:]...) // 移除分组ID
_, err = l.svcCtx.UserRepository.UpdateWithVersion(l.ctx, conn, user)
if err != nil {
return err
}
}
}
}
return nil
}, 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
}