审查视图

pkg/application/user/sync_data_service.go 11.6 KB
tangxvhui authored
1 2
package user
tangxvhui authored
3
import (
tangxvhui authored
4
	"encoding/json"
tangxvhui authored
5 6
	"time"
tangxvhui authored
7 8
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/log"
tangxvhui authored
9 10 11 12 13 14
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user/command"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)
tangxvhui authored
15
type SyncDataUserService struct{}
tangxvhui authored
16
tangxvhui authored
17 18 19 20 21 22
// type BusinessAdminCommand struct {
// 	// employee:员工
// 	Module string `json:"module"`
// 	// add:添加,edit:编辑,batchDelete:批量删除,batchForbid:批量禁用用户,batchRemove:批量更改用户部门,import:导入用户
// 	Action string `json:"action"`
// 	// 具体的对象JSON数据
庄敏学 authored
23
// 	Data json.RawMessage `json:"data"`
tangxvhui authored
24
// }
tangxvhui authored
25
tangxvhui authored
26
func (srv SyncDataUserService) FromBusinessAdmin(param *domain.MessageBody) error {
tangxvhui authored
27 28 29 30 31
	action := param.Module + "/" + param.Action
	var err error
	switch action {
	case "employee/add":
		var param1 command.SaveUserCommand
庄敏学 authored
32
		err = json.Unmarshal(param.Data, &param1)
tangxvhui authored
33 34 35 36 37 38
		if err != nil {
			return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
		err = srv.AddUser(&param1)
	case "employee/edit":
		var param2 command.SaveUserCommand
庄敏学 authored
39
		err = json.Unmarshal(param.Data, &param2)
tangxvhui authored
40 41 42 43 44 45
		if err != nil {
			return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
		err = srv.UpdateUser(&param2)
	case "employee/batchDelete":
		var param3 command.BatchDeleteCommand
庄敏学 authored
46
		err = json.Unmarshal(param.Data, &param3)
tangxvhui authored
47 48 49 50
		if err != nil {
			return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
		err = srv.batchDelete(&param3)
庄敏学 authored
51
	case "employee/batchForbid":
tangxvhui authored
52
		var param4 command.BatchForbidCommand
庄敏学 authored
53
		err = json.Unmarshal(param.Data, &param4)
tangxvhui authored
54 55 56 57
		if err != nil {
			return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
		err = srv.batchForbid(&param4)
庄敏学 authored
58
	case "employee/import":
tangxvhui authored
59
		var param4 command.ImportUserCommand
庄敏学 authored
60
		err = json.Unmarshal(param.Data, &param4)
tangxvhui authored
61 62 63 64
		if err != nil {
			return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
		err = srv.importUser(&param4)
庄敏学 authored
65 66 67 68 69 70 71
	case "employee/batchRemove":
		batchRemove := &command.BatchRemove{}
		err = json.Unmarshal(param.Data, batchRemove)
		if err != nil {
			return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
		err = srv.BatchRemove(batchRemove)
tangxvhui authored
72 73 74 75 76 77 78
	default:
		log.Logger.Error("action err:" + action)
	}

	return err
}
庄敏学 authored
79 80 81
// AddUser
// 从BusinessAdmins 接收消息 添加用户
// module="employee" action="add"
tangxvhui authored
82
func (srv SyncDataUserService) AddUser(param *command.SaveUserCommand) error {
tangxvhui authored
83 84 85 86 87 88 89 90 91 92 93 94
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
	nowTime := time.Now()
	newUser := domain.User{
tangxvhui authored
95 96 97 98 99
		Id:           param.Id,
		Account:      param.Phone,
		AvatarUrl:    param.Avatar,
		CompanyId:    param.CompanyId,
		AdminType:    param.AdminType,
庄敏学 authored
100 101
		DepartmentId: param.DepartmentIds(),
		PositionId:   param.PositionIds(),
tangxvhui authored
102
		Name:         param.Name,
庄敏学 authored
103
		Email:        param.Email,
tangxvhui authored
104
		Status:       param.Status,
105
		EntryTime:    param.EntryTime,
庄敏学 authored
106 107
		UpdatedAt:    nowTime,
		CreatedAt:    nowTime,
tangxvhui authored
108 109 110 111 112 113
	}
	userRepo := factory.CreateUserRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	_, err = userRepo.Insert(&newUser)
	if err != nil {
tangxvhui authored
114
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
tangxvhui authored
115 116
	}
	if err := transactionContext.CommitTransaction(); err != nil {
tangxvhui authored
117
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
tangxvhui authored
118 119 120 121
	}
	return nil
}
庄敏学 authored
122 123 124
// UpdateUser
// 从BusinessAdmins 接收消息 更新用户
// module="employee" action="edit"
tangxvhui authored
125
func (srv SyncDataUserService) UpdateUser(param *command.SaveUserCommand) error {
tangxvhui authored
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
	userRepo := factory.CreateUserRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	_, userList, err := userRepo.Find(map[string]interface{}{
		"limit": 1,
		"id":    param.Id,
	})
	if err != nil {
tangxvhui authored
144
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
tangxvhui authored
145 146 147 148 149 150 151 152 153
	}
	var (
		newUser *domain.User
	)
	nowTime := time.Now()
	if len(userList) > 0 {
		newUser = userList[0]
	} else {
		newUser = &domain.User{
庄敏学 authored
154
			CreatedAt: nowTime,
tangxvhui authored
155 156 157 158 159 160 161 162 163
		}
	}
	newUser.Id = param.Id
	newUser.Account = param.Phone
	newUser.AvatarUrl = param.Avatar
	newUser.CompanyId = param.CompanyId
	newUser.AdminType = param.AdminType
	newUser.Name = param.Name
	newUser.Status = param.Status
164
	newUser.EntryTime = param.EntryTime
庄敏学 authored
165 166
	newUser.PositionId = param.PositionIds()
	newUser.DepartmentId = param.DepartmentIds()
庄敏学 authored
167
庄敏学 authored
168
	newUser.UpdatedAt = nowTime
tangxvhui authored
169 170 171
	if len(userList) > 0 {
		_, err = userRepo.Update(newUser)
		if err != nil {
tangxvhui authored
172
			return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
tangxvhui authored
173 174 175 176
		}
	} else {
		_, err = userRepo.Insert(newUser)
		if err != nil {
tangxvhui authored
177
			return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
tangxvhui authored
178 179 180
		}
	}
	if err := transactionContext.CommitTransaction(); err != nil {
tangxvhui authored
181
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
tangxvhui authored
182 183 184 185
	}
	return nil
}
庄敏学 authored
186 187 188
// batchDelete
// 从BusinessAdmins 接收消息 删除用户
// module="employee" action="batchDelete"
tangxvhui authored
189
func (srv SyncDataUserService) batchDelete(param *command.BatchDeleteCommand) error {
tangxvhui authored
190 191 192
	if len(param.Uids) == 0 {
		return nil
	}
tangxvhui authored
193 194 195 196 197 198 199 200 201 202
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
tangxvhui authored
203 204 205
	userRepo := factory.CreateUserRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
tangxvhui authored
206
tangxvhui authored
207 208
	err = userRepo.Remove(param.Uids)
	if err != nil {
tangxvhui authored
209
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
tangxvhui authored
210
	}
tangxvhui authored
211
	if err := transactionContext.CommitTransaction(); err != nil {
tangxvhui authored
212
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
tangxvhui authored
213 214 215 216
	}
	return nil
}
庄敏学 authored
217 218 219
// batchForbid
// 从BusinessAdmins 接收消息 禁用,启用用户
// module="employee" action="batchForbid"
tangxvhui authored
220
func (srv SyncDataUserService) batchForbid(param *command.BatchForbidCommand) error {
tangxvhui authored
221 222 223 224 225
	if len(param.Uids) == 0 {
		return nil
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
tangxvhui authored
226 227
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
tangxvhui authored
228 229 230 231 232 233 234 235 236 237 238 239 240 241
	if err := transactionContext.StartTransaction(); err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
	userRepo := factory.CreateUserRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	_, userList, err := userRepo.Find(map[string]interface{}{
		"ids":   param.Uids,
		"limit": len(param.Uids),
	})
	if err != nil {
tangxvhui authored
242
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
tangxvhui authored
243 244 245 246 247
	}
	for i := range userList {
		userList[i].Status = param.Status
		_, err = userRepo.Update(userList[i])
		if err != nil {
tangxvhui authored
248
			return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
tangxvhui authored
249 250 251
		}
	}
	if err := transactionContext.CommitTransaction(); err != nil {
tangxvhui authored
252
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
tangxvhui authored
253
	}
tangxvhui authored
254 255 256
	return nil
}
庄敏学 authored
257 258 259
// importUser
// 从BusinessAdmins 接收消息 导入用户数据
// module="employee" action="import"
tangxvhui authored
260
func (srv SyncDataUserService) importUser(param *command.ImportUserCommand) error {
tangxvhui authored
261 262 263 264 265 266 267 268 269 270
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
tangxvhui authored
271 272 273
	userRepo := factory.CreateUserRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
tangxvhui authored
274
tangxvhui authored
275 276 277 278 279 280 281
	editUserMap := map[int64]command.SaveUserCommand{}
	var editUserIds []int64
	for i := range param.EditUsers {
		editUserIds = append(editUserIds, param.EditUsers[i].Id)
		editUserMap[param.EditUsers[i].Id] = param.EditUsers[i]
	}
	nowTime := time.Now()
庄敏学 authored
282 283 284 285
	if len(editUserIds) > 0 {
		_, editUserList, err := userRepo.Find(map[string]interface{}{
			"ids": editUserIds,
		})
tangxvhui authored
286
		if err != nil {
庄敏学 authored
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
			return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
		for i := range editUserList {
			mVal, ok := editUserMap[editUserList[i].Id]
			if !ok {
				continue
			}
			editUserList[i].Account = mVal.Phone
			editUserList[i].AdminType = mVal.AdminType
			editUserList[i].AvatarUrl = mVal.Avatar
			editUserList[i].Name = mVal.Name
			editUserList[i].Status = mVal.Status
			editUserList[i].CompanyId = mVal.CompanyId
			editUserList[i].EntryTime = mVal.EntryTime
			editUserList[i].UpdatedAt = nowTime
			_, err = userRepo.Update(editUserList[i])
			if err != nil {
				return err
			}
tangxvhui authored
306 307 308 309 310
		}
	}
	var (
		tempUser domain.User
	)
庄敏学 authored
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
	if len(param.AddUsers) > 0 {
		for i := range param.AddUsers {
			tempUser = domain.User{
				Id:        param.AddUsers[i].Id,
				Account:   param.AddUsers[i].Phone,
				AvatarUrl: param.AddUsers[i].Avatar,
				CompanyId: param.AddUsers[i].CompanyId,
				AdminType: param.AddUsers[i].AdminType,
				Name:      param.AddUsers[i].Name,
				Status:    param.AddUsers[i].Status,
				EntryTime: param.AddUsers[i].EntryTime,
				UpdatedAt: nowTime,
				DeletedAt: nil,
				CreatedAt: nowTime,
			}
			_, err := userRepo.Insert(&tempUser)
			if err != nil {
				return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
			}
tangxvhui authored
330 331
		}
	}
tangxvhui authored
332
	if err := transactionContext.CommitTransaction(); err != nil {
tangxvhui authored
333
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
tangxvhui authored
334
	}
tangxvhui authored
335
tangxvhui authored
336 337
	return nil
}
庄敏学 authored
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372

// BatchRemove 调整部门
func (srv SyncDataUserService) BatchRemove(batchRemove *command.BatchRemove) error {
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
	userRepo := factory.CreateUserRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	if len(batchRemove.UserIds) > 0 {
		for _, item := range batchRemove.UserIds {
			user, err := userRepo.FindOne(map[string]interface{}{"id": item, "companyId": batchRemove.CompanyId})
			if err != nil {
				return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
			}
			user.DepartmentId = batchRemove.DepartmentIds
			user.UpdatedAt = time.Now()
			_, err = userRepo.Update(user)
			if err != nil {
				return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
			}
		}
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	return nil
}