作者 Administrator

合并分支 'test' 到 'master'

Test



查看合并请求 !2
... ... @@ -21,7 +21,7 @@ _testmain.go
*.exe
*.test
.log
*.log
.idea
*.tmp
... ...
... ... @@ -21,6 +21,13 @@ func main() {
w, _ := logrus.NewKafkaWriter(constant.KAFKA_HOST, constant.TOPIC_LOG_STASH, false)
log.Logger.AddHook(w)
}
bw := log.NewBeegoLogWriter(log.LoggerConfig{
Filename: constant.LOG_FILE,
Level: 7,
MaxSize: 1024 * 1024 * 2,
})
log.Logger.AddHook(bw)
log.Logger.Info("server start!")
web.Run()
}
... ...
... ... @@ -228,6 +228,7 @@ func (roleService *RoleService) GetRoleRelatedUsers(getRoleRelatedUsersQuery *qu
queryOptions["organizationId"] = getRoleRelatedUsersQuery.OrgId
}
queryOptions["userType"] = domain.UserTypeEmployee
queryOptions["limit"] = domain.MaxQueryRow
_, users, err := userRepository.Find(queryOptions)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
... ...
... ... @@ -81,5 +81,10 @@ func (dto *UserDto) LoadDto(user *domain.User, company *domain.Company) error {
dto.Organization = user.Organization
dto.Department = user.Department
dto.CreatedAt = user.CreatedAt
if user.UserType == domain.UserTypeCooperation {
dto.Department = &domain.Department{
DepartmentName: "共创用户",
}
}
return nil
}
... ...
... ... @@ -18,35 +18,35 @@ type UserService struct {
}
// 批量添加用户
func (userService *UserService) BatchAdd(batchAddCommand *command.BatchAddCommand) (interface{}, error) {
if err := batchAddCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
batchAddUserService, _ := factory.CreateBatchAddUserService(map[string]interface{}{
"transactionContext": transactionContext,
})
if err = batchAddUserService.BatchAddUser(batchAddCommand.OperateInfo, batchAddCommand.Users, batchAddCommand.Password); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}
// 批量添加用户
//func (userService *UserService) BatchAdd(batchAddCommand *command.BatchAddCommand) (interface{}, error) {
// if err := batchAddCommand.ValidateCommand(); err != nil {
// return nil, application.ThrowError(application.ARG_ERROR, err.Error())
// }
// transactionContext, err := factory.CreateTransactionContext(nil)
// if err != nil {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
// if err := transactionContext.StartTransaction(); err != nil {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
// defer func() {
// transactionContext.RollbackTransaction()
// }()
//
// batchAddUserService, _ := factory.CreateBatchAddUserService(map[string]interface{}{
// "transactionContext": transactionContext,
// })
// if err = batchAddUserService.BatchAddUser(batchAddCommand.OperateInfo, batchAddCommand.Users, batchAddCommand.Password); err != nil {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
//
// if err := transactionContext.CommitTransaction(); err != nil {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
// return nil, nil
//}
// BatchAdd2 批量添加用户
func (userService *UserService) BatchAdd2(batchAddCommand *command.BatchAdd2Command) (interface{}, error) {
if err := batchAddCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
... ...
... ... @@ -12,6 +12,7 @@ var SERVICE_ENV = "dev"
var HTTP_PORT int = 8081
var CACHE_PREFIX = "allied-creation-user-dev"
var LOG_LEVEL = "debug"
var LOG_FILE = "app.log"
var CUSTOMER_ACCOUNT = []int64{3129687560814592, 3129687690100739, 3492238958608384}
... ...
... ... @@ -2,6 +2,8 @@ package domain
import "fmt"
const MaxQueryRow = 10000
var (
ErrorNotFound = fmt.Errorf("没有此资源")
)
... ...
... ... @@ -13,7 +13,7 @@ type PgBatchAddUserService struct {
transactionContext *pgTransaction.TransactionContext
}
// CreateUser 批量添加用户服务
// BatchAddUser 批量添加用户服务
//
// optUser 操作用户
// users 待添加用户列表数据
... ... @@ -107,7 +107,6 @@ func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, use
user.FailReason = err.Error()
failRows = append(failRows, user)
continue
//return err
}
var org, dep *domain.Org
var ok bool
... ... @@ -115,26 +114,22 @@ func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, use
user.FailReason = "导入的组织机构不存在:" + user.Org
failRows = append(failRows, user)
continue
//return fmt.Errorf("导入的组织机构不存在:" + user.Org)
}
if dep, ok = mapOrg[user.Department]; !ok && user.UserType != domain.UserTypeCooperation {
user.FailReason = "导入的所属部门不存在:" + user.Department
failRows = append(failRows, user)
continue
//return fmt.Errorf("导入的所属部门不存在:" + user.Department)
}
newUser := &domain.User{
CompanyId: user.CompanyId,
UserType: user.UserType,
UserCode: user.UserCode,
OrganizationId: org.OrgId,
//DepartmentId: dep.OrgId,
UserOrg: []*domain.Org{},
UserRole: []*domain.Role{},
FavoriteMenus: []string{},
CooperationInfo: &domain.CooperationInfo{
CooperationCompany: user.CooperationCompany,
//CooperationDeadline: user.CooperationDeadline,
},
UserInfo: &domain.UserInfo{
UserName: user.UserName,
... ... @@ -147,7 +142,6 @@ func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, use
Phone: user.Phone,
UserName: user.UserName,
OrgName: org.OrgName,
//DepName: dep.OrgName,
},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
... ... @@ -164,11 +158,13 @@ func (ptr *PgBatchAddUserService) BatchAddUser2(optUser *domain.OperateInfo, use
continue
}
}
if user.UserType == domain.UserTypeEmployee {
newUser.UserOrg = append(newUser.UserOrg, org.CloneSample())
}
if newUser, err = createUserService.CreateUser(nil, newUser, password); err != nil {
user.FailReason = err.Error()
failRows = append(failRows, user)
continue
//return fmt.Errorf("%v %v", user.UserName, err.Error())
}
}
return failRows, nil
... ... @@ -181,7 +177,7 @@ func (ptr *PgBatchAddUserService) preCheck(user *domain.User) error {
if len(user.UserInfo.UserName) == 0 {
return fmt.Errorf("导入的用户姓名为空值")
}
if len(user.UserInfo.Phone) == 0 {
if len(user.UserInfo.Phone) == 0 || len(user.UserInfo.Phone) != 11 {
return fmt.Errorf("导入的手机号不是有效手机号")
}
if user.OrganizationId == 0 {
... ... @@ -203,15 +199,9 @@ func (ptr *PgBatchAddUserService) preCheck2(user *domain.BatchAddUserItem) error
if len(user.UserName) == 0 {
return fmt.Errorf("导入的用户姓名为空值")
}
if len(user.Phone) == 0 {
if len(user.Phone) == 0 || len(user.Phone) != 11 {
return fmt.Errorf("导入的手机号不是有效手机号")
}
//if len(user.Org) == 0 {
// return fmt.Errorf("导入的组织机构不存在")
//}
//if len(user.Department) == 0 && user.UserType == domain.UserTypeEmployee {
// return fmt.Errorf("导入的所属部门不存在")
//}
return nil
}
... ...
... ... @@ -3,6 +3,7 @@ package pg
import (
"context"
"fmt"
"github.com/beego/beego/v2/core/logs"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/models"
"log"
"reflect"
... ... @@ -63,6 +64,7 @@ func (hook SqlGeneratePrintHook) AfterQuery(c context.Context, q *pg.QueryEvent)
}
//log.Logger.Debug(string(sqlStr))
log.Println(string(sqlStr))
logs.Debug(string(sqlStr))
return nil
}
... ...
package log
import (
"encoding/json"
"errors"
"fmt"
"github.com/beego/beego/v2/core/logs"
"io"
"sync/atomic"
)
var errOutOfMaxSize = errors.New("msg size is out of limit ")
type BaseHook struct {
syncWrite bool
msgChan chan []byte
maxSize int32
currentSize int32
closeChan chan struct{}
w io.Writer
}
func (b *BaseHook) Write(p []byte) (n int, err error) {
if b.syncWrite {
return b.w.Write(p)
}
if b.currentSize >= b.maxSize {
fmt.Println(errOutOfMaxSize.Error(), b.currentSize)
return 0, errOutOfMaxSize
}
b.msgChan <- p
atomic.AddInt32(&b.currentSize, 1)
return len(p), nil
}
// NewBaseHook
// syncWriteFlag 同步写标识 true:同步写 false:异步写
func NewBaseHook(syncWriteFlag bool, internalIo io.WriteCloser) (*BaseHook, error) {
writer := &BaseHook{
syncWrite: syncWriteFlag,
maxSize: 10000,
msgChan: make(chan []byte, 10000),
closeChan: make(chan struct{}),
w: internalIo,
}
go writer.ConsumeMsg()
return writer, nil
}
func (b *BaseHook) ConsumeMsg() {
for {
select {
case <-b.closeChan:
return
case m, ok := <-b.msgChan:
if ok {
atomic.AddInt32(&b.currentSize, -1)
if _, err := b.w.Write(m); err != nil {
fmt.Println(err)
}
}
}
}
}
func (b *BaseHook) Close() {
close(b.msgChan)
b.closeChan <- struct{}{}
//b.wc.Close()
}
type LoggerConfig struct {
Level int `json:"level,omitempty"`
Filename string `json:"filename,omitempty"`
MaxSize int `json:"maxsize,omitempty"`
//MaxBackups int `json:"max_backups,omitempty"`
//MaxAge int `json:"max_age,omitempty"`
//Compress bool `json:"compress,omitempty"`
}
type internalLog struct {
*logs.BeeLogger
}
func NewBeegoLogWriter(conf LoggerConfig) io.Writer {
logger := logs.GetBeeLogger()
logger.SetLevel(conf.Level)
logger.EnableFuncCallDepth(true)
logger.SetLogFuncCallDepth(2)
confByte, _ := json.Marshal(conf)
err := logger.SetLogger(logs.AdapterFile, string(confByte))
if err != nil {
fmt.Println(err.Error())
}
return &internalLog{logger}
}
func (l *internalLog) Write(p []byte) (n int, err error) {
l.Debug(string(p))
return len(p), nil
}
... ...
... ... @@ -84,14 +84,14 @@ func (controller *UserController) GetUserProfile() {
controller.Response(data, err)
}
func (controller *UserController) BatchAdd() {
userService := service.NewUserService(nil)
batchAddCommand := &command.BatchAddCommand{}
Must(controller.Unmarshal(batchAddCommand))
batchAddCommand.OperateInfo = ParseOperateInfo(controller.BaseController)
data, err := userService.BatchAdd(batchAddCommand)
controller.Response(data, err)
}
//func (controller *UserController) BatchAdd() {
// userService := service.NewUserService(nil)
// batchAddCommand := &command.BatchAddCommand{}
// Must(controller.Unmarshal(batchAddCommand))
// batchAddCommand.OperateInfo = ParseOperateInfo(controller.BaseController)
// data, err := userService.BatchAdd(batchAddCommand)
// controller.Response(data, err)
//}
func (controller *UserController) BatchAdd2() {
userService := service.NewUserService(nil)
... ...
package routers
import (
"github.com/beego/beego/v2/server/web"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/constant"
)
func init() {
web.SetStaticPath("/log", constant.LOG_FILE)
}
... ...
... ... @@ -13,7 +13,7 @@ func init() {
web.Router("/user/search", &controllers.UserController{}, "Post:SearchUser")
web.Router("/user/:userId/access-menus", &controllers.UserController{}, "Get:GetUserAccessMenus")
web.Router("/user/:userId/profile", &controllers.UserController{}, "Get:GetUserProfile")
web.Router("/user/batch-add", &controllers.UserController{}, "Post:BatchAdd")
//web.Router("/user/batch-add", &controllers.UserController{}, "Post:BatchAdd")
web.Router("/user/batch-add2", &controllers.UserController{}, "Post:BatchAdd2")
web.Router("/user/batch-enable", &controllers.UserController{}, "Post:BatchEnable")
web.Router("/user/batch-reset-password", &controllers.UserController{}, "Post:BatchResetPassword")
... ...