system_user_info_logic.go
10.5 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package user
import (
"context"
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
"strconv"
"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/cmd/discuss/interanl/pkg/gateway/authlib"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/tool"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"github.com/zeromicro/go-zero/core/logx"
)
type SystemUserInfoLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSystemUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemUserInfoLogic {
return &SystemUserInfoLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SystemUserInfoLogic) SystemUserInfo(req *types.SystemUserInfoRequest) (resp *types.SystemUserInfoResponse, err error) {
var (
conn = l.svcCtx.DefaultDBConn()
userToken = contextdata.GetUserTokenFromCtx(l.ctx)
response *authlib.DataUserMe
company *domain.Company
companyId int64
code = tool.Krand(6, tool.KC_RAND_KIND_ALL)
)
if response, err = l.svcCtx.ApiAuthService.MeInfo(l.ctx, authlib.RequestUserMeQuery{
Token: req.Token,
}); err != nil {
return nil, xerr.NewErrMsgErr("获取用户资料失败", err)
}
companyId, _ = strconv.ParseInt(response.CurrentCompany.ID, 10, 64)
resp = &types.SystemUserInfoResponse{}
resp.UserName = response.User.NickName
resp.UserId, _ = strconv.ParseInt(response.User.ID, 10, 64)
resp.Avatar = response.User.Avatar
resp.CompanyName = response.CurrentCompany.Name
resp.CompanyId = companyId
resp.Code = code
if companyId != userToken.CompanyId {
return nil, xerr.NewErrMsgErr("获取用户资料失败", fmt.Errorf("当前登录公司信息不匹配"))
}
company, err = l.svcCtx.CompanyRepository.FindOne(l.ctx, conn, userToken.CompanyId)
// 新建公司
if err == domain.ErrNotFound {
company = &domain.Company{
Id: companyId,
Name: response.CurrentCompany.Name,
Logo: response.CurrentCompany.Logo,
Code: code,
}
if company, err = l.svcCtx.CompanyRepository.Insert(l.ctx, conn, company); err != nil {
return nil, xerr.NewErrMsgErr("获取用户资料失败", err)
}
err = nil
return
}
if err != nil {
return nil, xerr.NewErrMsgErr("获取用户资料失败", err)
}
resp.Code = company.Code
// 更新公司
if response.CurrentCompany != nil {
var changed bool
if response.CurrentCompany.Name != "" && response.CurrentCompany.Name != company.Name {
company.Name = response.CurrentCompany.Name
changed = true
}
if response.CurrentCompany.Logo != "" && response.CurrentCompany.Logo != company.Logo {
company.Logo = response.CurrentCompany.Logo
changed = true
}
if company.Logo == "" {
company.Logo = domain.DefaultCompanyLog
changed = true
}
if changed {
if company, err = l.svcCtx.CompanyRepository.Update(l.ctx, conn, company); err != nil {
return nil, xerr.NewErrMsgErr("获取用户资料失败", err)
}
}
}
resp.CompanyVisible = company.Visible == 1
err = l.initSystemData(companyId)
if err != nil {
return nil, err
}
if err = l.initCompanyTemplate(companyId, resp.UserId, resp.UserName); err != nil {
return nil, err
}
return
}
// 后台登录时检查/设置公司的初始数据
func (l *SystemUserInfoLogic) initSystemData(companyId int64) error {
// 初始设置文章标签
queryOption := domain.NewQueryOptions().WithCountOnly()
conn := l.svcCtx.DefaultDBConn()
categoryList := []*domain.ArticleCategory{
{
Name: "机会风险",
SortBy: 1,
Enable: 1,
Other: "",
CompanyId: companyId,
},
{
Name: "紧急重要",
SortBy: 2,
Enable: 1,
Other: "",
CompanyId: companyId,
},
}
articleTags := []*domain.ArticleTag{
{
Id: 0, CompanyId: companyId, Image: domain.Image{Url: "https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/test/20231115/object/index_imp_01.png", Width: 0, Height: 0},
Name: "紧急重要", Category: "紧急重要", Remark: "优先解决", SortBy: 1,
},
{
Id: 0, CompanyId: companyId, Image: domain.Image{Url: "https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/test/20231115/object/index_imp_04.png", Width: 0, Height: 0},
Name: "不紧急不重要", Category: "紧急重要", Remark: "给别人做", SortBy: 2,
},
{
Id: 0, CompanyId: companyId, Image: domain.Image{Url: "https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/test/20231115/object/index_imp_03.png", Width: 0, Height: 0},
Name: "紧急不重要", Category: "紧急重要", Remark: "有空再做", SortBy: 3,
},
{
Id: 0, CompanyId: companyId, Image: domain.Image{Url: "https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/test/20231115/object/index_imp_02.png", Width: 0, Height: 0},
Name: "不紧急重要", Category: "紧急重要", Remark: "制定计划去做", SortBy: 4,
},
{
Id: 0, CompanyId: companyId, Image: domain.Image{Url: "https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/test/20231115/object/index_chance_03.png", Width: 0, Height: 0},
Name: "大机会高风险", Category: "机会风险", Remark: "谨慎考虑专项讨论", SortBy: 5,
},
{
Id: 0, CompanyId: companyId, Image: domain.Image{Url: "https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/test/20231115/object/index_chance_02.png", Width: 0, Height: 0},
Name: "大机会中风险", Category: "机会风险", Remark: "加大关注值得尝试", SortBy: 6,
},
{
Id: 0, CompanyId: companyId, Image: domain.Image{Url: "https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/test/20231115/object/index_chance_01.png", Width: 0, Height: 0},
Name: "大机会低风险", Category: "机会风险", Remark: "全员投入抓紧落实", SortBy: 7,
},
{
Id: 0, CompanyId: companyId, Image: domain.Image{Url: "https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/test/20231115/object/index_chance_04.png", Width: 0, Height: 0},
Name: "中机会高风险", Category: "机会风险", Remark: "专人跟踪成立项目", SortBy: 8,
},
{
Id: 0, CompanyId: companyId, Image: domain.Image{Url: "https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/test/20231115/object/index_chance_05.png", Width: 0, Height: 0},
Name: "中机会中风险", Category: "机会风险", Remark: "讨论落实", SortBy: 9,
},
{
Id: 0, CompanyId: companyId, Image: domain.Image{Url: "https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/test/20231115/object/index_chance_06.png", Width: 0, Height: 0},
Name: "中机会低风险", Category: "机会风险", Remark: "解决问题多手准备", SortBy: 10,
},
{
Id: 0, CompanyId: companyId, Image: domain.Image{Url: "https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/test/20231115/object/index_chance_07.png", Width: 0, Height: 0},
Name: "小机会高风险", Category: "机会风险", Remark: "有空看看", SortBy: 11,
},
{
Id: 0, CompanyId: companyId, Image: domain.Image{Url: "https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/test/20231115/object/index_chance_08.png", Width: 0, Height: 0},
Name: "小机会中风险", Category: "机会风险", Remark: "持续监控做好控制", SortBy: 12,
},
{
Id: 0, CompanyId: companyId, Image: domain.Image{Url: "https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/test/20231115/object/index_chance_09.png", Width: 0, Height: 0},
Name: "小机会低风险", Category: "机会风险", Remark: "全员警戒马上解决", SortBy: 13,
},
}
cnt, _, err := l.svcCtx.ArticleCategoryRepository.Find(l.ctx, conn, companyId, queryOption)
if cnt == 0 {
if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
for _, category := range categoryList {
category, err = l.svcCtx.ArticleCategoryRepository.Insert(l.ctx, conn, category)
if err != nil {
return err
}
}
cnt, _, err = l.svcCtx.ArticleTagRepository.Find(l.ctx, conn, companyId, queryOption)
if err != nil {
return xerr.NewErrMsgErr("初始话公司数据失败", err)
}
for _, tag := range articleTags {
for _, category := range categoryList {
if category.Name == tag.Category {
tag.CategoryId = category.Id
}
}
}
if cnt == 0 {
err = l.svcCtx.ArticleTagRepository.CreateInBatches(l.ctx, conn, articleTags)
if err != nil {
return xerr.NewErrMsgErr("初始话公司数据失败", err)
}
}
return nil
}, true); err != nil {
return xerr.NewErrMsgErr("初始话公司数据失败", err)
}
}
return nil
}
// 初始化公司模板数据
func (l *SystemUserInfoLogic) initCompanyTemplate(companyId int64, uid int64, uname string) error {
var conn = l.svcCtx.DefaultDBConn()
if companyId <= 0 {
return xerr.NewErrMsgErr("公司id不能为0", nil)
}
total, _, err := l.svcCtx.ArticleTemplateRepository.FindCompanyUnscoped(l.ctx, conn, companyId, domain.NewQueryOptions().WithCountOnly().WithKV("templateClass", domain.DefaultTemplateClass))
if err != nil {
return err
}
if total == 0 {
templateList := []*domain.ArticleTemplate{
NewDefaultArticleTemplate(companyId, "演绎式", uid, uname, 1),
NewDefaultArticleTemplate(companyId, "归纳式", uid, uname, 2),
}
if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
for _, tp := range templateList {
tp, err = l.svcCtx.ArticleTemplateRepository.Insert(l.ctx, conn, tp)
if err != nil {
return err
}
}
return nil
}, true); err != nil {
return xerr.NewErrMsgErr("初始话公司数据失败", err)
}
}
return nil
}
func NewDefaultArticleTemplate(companyId int64, name string, operatorId int64, operatorName string, sort int) *domain.ArticleTemplate {
return &domain.ArticleTemplate{
CompanyId: companyId,
Name: name,
TemplateClass: domain.DefaultTemplateClass,
BelongTo: domain.BelongToCompany,
Paragraphs: []domain.Paragraph{},
Icon: "",
TargetWhoRead: domain.ArticleTemplateAll,
TargetWhoReview: domain.ArticleTemplateAll,
WhoRead: []int64{},
WhoReview: []int64{},
Operator: domain.Operator{
Id: operatorId,
Name: operatorName,
Type: domain.OperatorTypeAdmin,
},
Sort: sort,
}
}