v3_1
This commit is contained in:
@@ -3,19 +3,23 @@ package system
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"management/internal/db/model/dto"
|
||||
db "management/internal/db/sqlc"
|
||||
"management/internal/erpserver/model/form"
|
||||
"management/internal/erpserver/model/view"
|
||||
"management/internal/pkg/convertor"
|
||||
"management/internal/pkg/know"
|
||||
"management/internal/pkg/redis"
|
||||
)
|
||||
|
||||
type DepartmentBiz interface {
|
||||
Create(ctx context.Context, arg *db.CreateSysDepartmentParams) (*db.SysDepartment, error)
|
||||
Update(ctx context.Context, arg *db.UpdateSysDepartmentParams) (*db.SysDepartment, error)
|
||||
Create(ctx context.Context, req *form.Department) error
|
||||
Update(ctx context.Context, req *form.Department) error
|
||||
All(ctx context.Context) ([]*db.SysDepartment, error)
|
||||
List(ctx context.Context, q dto.SearchDto) ([]*db.SysDepartment, int64, error)
|
||||
Get(ctx context.Context, id int32) (*db.SysDepartment, error)
|
||||
@@ -96,12 +100,79 @@ func (b *departmentBiz) Get(ctx context.Context, id int32) (*db.SysDepartment, e
|
||||
return b.store.GetSysDepartment(ctx, id)
|
||||
}
|
||||
|
||||
func (b *departmentBiz) Create(ctx context.Context, arg *db.CreateSysDepartmentParams) (*db.SysDepartment, error) {
|
||||
return b.store.CreateSysDepartment(ctx, arg)
|
||||
func (b *departmentBiz) Create(ctx context.Context, req *form.Department) error {
|
||||
parent := &db.SysDepartment{
|
||||
ID: 0,
|
||||
ParentID: 0,
|
||||
ParentPath: ",0,",
|
||||
}
|
||||
if *req.ParentID > 0 {
|
||||
var err error
|
||||
parent, err = b.store.GetSysDepartment(ctx, *req.ParentID)
|
||||
if err != nil {
|
||||
return errors.New("父级节点错误")
|
||||
}
|
||||
}
|
||||
|
||||
var order int32 = 6666
|
||||
if *req.Sort > 0 {
|
||||
order = *req.Sort
|
||||
}
|
||||
|
||||
arg := &db.CreateSysDepartmentParams{
|
||||
Name: req.Name,
|
||||
ParentID: parent.ID,
|
||||
ParentPath: convertor.HandleParentPath(fmt.Sprintf("%s,%d,", parent.ParentPath, parent.ID)),
|
||||
Status: *req.Status,
|
||||
Sort: order,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
_, err := b.store.CreateSysDepartment(ctx, arg)
|
||||
if err != nil {
|
||||
if db.IsUniqueViolation(err) {
|
||||
return errors.New("部门已存在")
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *departmentBiz) Update(ctx context.Context, arg *db.UpdateSysDepartmentParams) (*db.SysDepartment, error) {
|
||||
return b.store.UpdateSysDepartment(ctx, arg)
|
||||
func (b *departmentBiz) Update(ctx context.Context, req *form.Department) error {
|
||||
parent := &db.SysDepartment{
|
||||
ID: 0,
|
||||
ParentID: 0,
|
||||
ParentPath: ",0,",
|
||||
}
|
||||
if *req.ParentID > 0 {
|
||||
var err error
|
||||
parent, err = b.store.GetSysDepartment(ctx, *req.ParentID)
|
||||
if err != nil {
|
||||
return errors.New("父级节点错误")
|
||||
}
|
||||
}
|
||||
|
||||
depart, err := b.store.GetSysDepartment(ctx, *req.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var order int32 = 6666
|
||||
if *req.Sort > 0 {
|
||||
order = *req.Sort
|
||||
}
|
||||
|
||||
arg := &db.UpdateSysDepartmentParams{
|
||||
ID: depart.ID,
|
||||
Name: req.Name,
|
||||
ParentID: parent.ID,
|
||||
ParentPath: convertor.HandleParentPath(fmt.Sprintf("%s,%d,", parent.ParentPath, parent.ID)),
|
||||
Status: *req.Status,
|
||||
Sort: order,
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
_, err = b.store.UpdateSysDepartment(ctx, arg)
|
||||
return err
|
||||
}
|
||||
|
||||
func (b *departmentBiz) Refresh(ctx context.Context) ([]*db.SysDepartment, error) {
|
||||
|
||||
@@ -3,19 +3,24 @@ package system
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"management/internal/db/model/dto"
|
||||
db "management/internal/db/sqlc"
|
||||
"management/internal/erpserver/model/form"
|
||||
"management/internal/erpserver/model/view"
|
||||
"management/internal/pkg/convertor"
|
||||
"management/internal/pkg/know"
|
||||
"management/internal/pkg/redis"
|
||||
)
|
||||
|
||||
type RoleBiz interface {
|
||||
Create(ctx context.Context, arg *db.CreateSysRoleParams) (*db.SysRole, error)
|
||||
Update(ctx context.Context, arg *db.UpdateSysRoleParams) (*db.SysRole, error)
|
||||
Create(ctx context.Context, req *form.Role) error
|
||||
Update(ctx context.Context, req *form.Role) error
|
||||
CreateOrUpdate(ctx context.Context, req *form.Role) error
|
||||
All(ctx context.Context) ([]*db.SysRole, error)
|
||||
List(ctx context.Context, q dto.SearchDto) ([]*db.SysRole, int64, error)
|
||||
Get(ctx context.Context, id int32) (*db.SysRole, error)
|
||||
@@ -44,12 +49,140 @@ func NewRole(store db.Store, redis redis.IRedis) *roleBiz {
|
||||
}
|
||||
}
|
||||
|
||||
func (b *roleBiz) Create(ctx context.Context, arg *db.CreateSysRoleParams) (*db.SysRole, error) {
|
||||
return b.store.CreateSysRole(ctx, arg)
|
||||
func (b *roleBiz) Create(ctx context.Context, req *form.Role) error {
|
||||
parent := &db.SysRole{
|
||||
ID: 0,
|
||||
ParentID: 0,
|
||||
ParentPath: ",0,",
|
||||
}
|
||||
if *req.ParentID > 0 {
|
||||
var err error
|
||||
parent, err = b.store.GetSysRole(ctx, *req.ParentID)
|
||||
if err != nil {
|
||||
return errors.New("父级节点错误")
|
||||
}
|
||||
}
|
||||
|
||||
var order int32 = 6666
|
||||
if *req.Sort > 0 {
|
||||
order = *req.Sort
|
||||
}
|
||||
|
||||
arg := &db.CreateSysRoleParams{
|
||||
Name: req.Name,
|
||||
DisplayName: req.DisplayName,
|
||||
Vip: false,
|
||||
ParentID: parent.ID,
|
||||
ParentPath: convertor.HandleParentPath(fmt.Sprintf("%s,%d,", parent.ParentPath, parent.ID)),
|
||||
Status: *req.Status,
|
||||
Sort: order,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
_, err := b.store.CreateSysRole(ctx, arg)
|
||||
if err != nil {
|
||||
if db.IsUniqueViolation(err) {
|
||||
return errors.New("角色名称已存在")
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *roleBiz) Update(ctx context.Context, arg *db.UpdateSysRoleParams) (*db.SysRole, error) {
|
||||
return b.store.UpdateSysRole(ctx, arg)
|
||||
func (b *roleBiz) Update(ctx context.Context, req *form.Role) error {
|
||||
parent := &db.SysRole{
|
||||
ID: 0,
|
||||
ParentID: 0,
|
||||
ParentPath: ",0,",
|
||||
}
|
||||
if *req.ParentID > 0 {
|
||||
var err error
|
||||
parent, err = b.store.GetSysRole(ctx, *req.ParentID)
|
||||
if err != nil {
|
||||
return errors.New("父级节点错误")
|
||||
}
|
||||
}
|
||||
|
||||
role, err := b.store.GetSysRole(ctx, *req.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var order int32 = 6666
|
||||
if *req.Sort > 0 {
|
||||
order = *req.Sort
|
||||
}
|
||||
|
||||
arg := &db.UpdateSysRoleParams{
|
||||
ID: role.ID,
|
||||
DisplayName: req.DisplayName,
|
||||
Status: *req.Status,
|
||||
ParentID: parent.ID,
|
||||
ParentPath: convertor.HandleParentPath(fmt.Sprintf("%s,%d,", parent.ParentPath, parent.ID)),
|
||||
Sort: order,
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
_, err = b.store.UpdateSysRole(ctx, arg)
|
||||
return err
|
||||
}
|
||||
|
||||
func (b *roleBiz) CreateOrUpdate(ctx context.Context, req *form.Role) error {
|
||||
parent := &db.SysRole{
|
||||
ID: 0,
|
||||
ParentID: 0,
|
||||
ParentPath: ",0,",
|
||||
}
|
||||
if *req.ParentID > 0 {
|
||||
var err error
|
||||
parent, err = b.store.GetSysRole(ctx, *req.ParentID)
|
||||
if err != nil {
|
||||
return errors.New("父级节点错误")
|
||||
}
|
||||
}
|
||||
|
||||
var order int32 = 6666
|
||||
if *req.Sort > 0 {
|
||||
order = *req.Sort
|
||||
}
|
||||
|
||||
if *req.ID > 0 {
|
||||
role, err := b.store.GetSysRole(ctx, *req.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
arg := &db.UpdateSysRoleParams{
|
||||
ID: role.ID,
|
||||
DisplayName: req.DisplayName,
|
||||
Status: *req.Status,
|
||||
ParentID: parent.ID,
|
||||
ParentPath: convertor.HandleParentPath(fmt.Sprintf("%s,%d,", parent.ParentPath, parent.ID)),
|
||||
Sort: order,
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
_, err = b.store.UpdateSysRole(ctx, arg)
|
||||
return err
|
||||
} else {
|
||||
arg := &db.CreateSysRoleParams{
|
||||
Name: req.Name,
|
||||
DisplayName: req.DisplayName,
|
||||
Vip: false,
|
||||
ParentID: parent.ID,
|
||||
ParentPath: convertor.HandleParentPath(fmt.Sprintf("%s,%d,", parent.ParentPath, parent.ID)),
|
||||
Status: *req.Status,
|
||||
Sort: order,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
_, err := b.store.CreateSysRole(ctx, arg)
|
||||
if err != nil {
|
||||
if db.IsUniqueViolation(err) {
|
||||
return errors.New("角色名称已存在")
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (b *roleBiz) All(ctx context.Context) ([]*db.SysRole, error) {
|
||||
|
||||
@@ -9,17 +9,20 @@ import (
|
||||
|
||||
"management/internal/db/model/dto"
|
||||
db "management/internal/db/sqlc"
|
||||
"management/internal/erpserver/model/req"
|
||||
"management/internal/erpserver/model/form"
|
||||
"management/internal/erpserver/model/view"
|
||||
"management/internal/pkg/crypto"
|
||||
"management/internal/pkg/know"
|
||||
"management/internal/pkg/rand"
|
||||
"management/internal/pkg/session"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// UserBiz 定义处理用户请求所需的方法.
|
||||
type UserBiz interface {
|
||||
Create(ctx context.Context, req *db.CreateSysUserParams) (*db.SysUser, error)
|
||||
Update(ctx context.Context, req *db.UpdateSysUserParams) (*db.SysUser, error)
|
||||
Create(ctx context.Context, req *form.User) error
|
||||
Update(ctx context.Context, req *form.User) error
|
||||
All(ctx context.Context) ([]*db.SysUser, error)
|
||||
List(ctx context.Context, q dto.SearchDto) ([]*db.ListSysUserConditionRow, int64, error)
|
||||
Get(ctx context.Context, id int32) (*db.SysUser, error)
|
||||
@@ -31,7 +34,7 @@ type UserBiz interface {
|
||||
|
||||
// UserExpansion 定义用户操作的扩展方法.
|
||||
type UserExpansion interface {
|
||||
Login(ctx context.Context, req *req.Login) error
|
||||
Login(ctx context.Context, req *form.Login) error
|
||||
}
|
||||
|
||||
// userBiz 是 UserBiz 接口的实现.
|
||||
@@ -50,12 +53,75 @@ func NewUser(store db.Store, session session.ISession) *userBiz {
|
||||
}
|
||||
}
|
||||
|
||||
func (b *userBiz) Create(ctx context.Context, req *db.CreateSysUserParams) (*db.SysUser, error) {
|
||||
return b.store.CreateSysUser(ctx, req)
|
||||
func (b *userBiz) Create(ctx context.Context, req *form.User) error {
|
||||
salt, err := rand.String(10)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hashedPassword, err := crypto.BcryptHashPassword(req.Password + salt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
initTime, err := time.ParseInLocation(time.DateTime, "0001-01-01 00:00:00", time.Local)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
arg := &db.CreateSysUserParams{
|
||||
Uuid: uuid.Must(uuid.NewV7()),
|
||||
Email: req.Email,
|
||||
Username: req.Username,
|
||||
HashedPassword: hashedPassword,
|
||||
Salt: salt,
|
||||
Avatar: req.Avatar,
|
||||
Gender: req.Gender,
|
||||
DepartmentID: req.DepartmentID,
|
||||
RoleID: req.RoleID,
|
||||
Status: *req.Status,
|
||||
ChangePasswordAt: initTime,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
_, err = b.store.CreateSysUser(ctx, arg)
|
||||
if err != nil {
|
||||
if db.IsUniqueViolation(err) {
|
||||
return errors.New("用户已经存在")
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *userBiz) Update(ctx context.Context, req *db.UpdateSysUserParams) (*db.SysUser, error) {
|
||||
return b.store.UpdateSysUser(ctx, req)
|
||||
func (b *userBiz) Update(ctx context.Context, req *form.User) error {
|
||||
user, err := b.store.GetSysUser(ctx, *req.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
arg := &db.UpdateSysUserParams{
|
||||
ID: user.ID,
|
||||
Username: req.Username,
|
||||
HashedPassword: user.HashedPassword,
|
||||
Avatar: req.Avatar,
|
||||
Gender: req.Gender,
|
||||
DepartmentID: req.DepartmentID,
|
||||
RoleID: req.RoleID,
|
||||
Status: *req.Status,
|
||||
ChangePasswordAt: user.ChangePasswordAt,
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
if req.ChangePassword == "on" {
|
||||
hashedPassword, err := crypto.BcryptHashPassword(req.Password + user.Salt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
arg.HashedPassword = hashedPassword
|
||||
arg.ChangePasswordAt = time.Now()
|
||||
}
|
||||
_, err = b.store.UpdateSysUser(ctx, arg)
|
||||
return err
|
||||
}
|
||||
|
||||
func (b *userBiz) All(ctx context.Context) ([]*db.SysUser, error) {
|
||||
@@ -112,7 +178,7 @@ func (b *userBiz) XmSelect(ctx context.Context) ([]*view.XmSelect, error) {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (b *userBiz) Login(ctx context.Context, req *req.Login) error {
|
||||
func (b *userBiz) Login(ctx context.Context, req *form.Login) error {
|
||||
log := &db.CreateSysUserLoginLogParams{
|
||||
CreatedAt: time.Now(),
|
||||
Email: req.Email,
|
||||
|
||||
Reference in New Issue
Block a user