作者 唐旭辉

日常提交保存

... ... @@ -28,3 +28,11 @@ func CreateOrderBaseDao(options map[string]interface{}) (*dao.OrderBaseDao, erro
}
return dao.NewOrderBaseDao(transactionContext)
}
func CreateUsersDao(options map[string]interface{}) (*dao.UsersDao, error) {
var transactionContext *transaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*transaction.TransactionContext)
}
return dao.NewUsersDao(transactionContext)
}
... ...
... ... @@ -4,6 +4,12 @@ import (
"encoding/json"
"errors"
"fmt"
"time"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)
// UserDepartData 用户的部门数据
... ... @@ -47,12 +53,32 @@ type EmployeeData struct {
UserPositions []UserPositionData `json:"user_positions"`
}
//DeleteUserData 批量删除用户
type DeleteUserData struct {
CompanyId int64 `json:"companyId"`
Ids []int64 `json:"ids"`
}
//ForbidAllowUserData 禁用启用用户
type ForbidAllowUserData struct {
CompanyId int64 `json:"companyId"`
Ids []int64 `json:"ids"`
Status int8 `json:"status"`
}
//ImportEmployeeData 批量导入用户
type ImportEmployeeData struct {
Add []EmployeeData `json:"add"`
Edit []EmployeeData `json:"edit"`
}
//SyncEmployeeService 同步用户数据
type SyncEmployeeService struct{}
func (service SyncEmployeeService) DoAction(action string, byteData []byte) error {
switch action {
case "add":
//添加
var (
data EmployeeData
err error
... ... @@ -61,9 +87,10 @@ func (service SyncEmployeeService) DoAction(action string, byteData []byte) erro
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
err = service.AddEmployeeData(data)
err = service.addEmployeeData(data)
return err
case "edit":
//编辑更新
var (
data EmployeeData
err error
... ... @@ -72,23 +99,195 @@ func (service SyncEmployeeService) DoAction(action string, byteData []byte) erro
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
err = service.UpdateEmployeeData(data)
err = service.updateEmployeeData(data)
return err
case "batchDelete":
//批量删除
var (
err error
data DeleteUserData
)
err = json.Unmarshal(byteData, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
return service.deleteEmployeeData(data)
case "batchForbid":
//启用禁用
var (
err error
data ForbidAllowUserData
)
err = json.Unmarshal(byteData, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
return service.updateUsersStatus(data)
case "batchRemove":
//移动通讯录用户部门 暂时不需要
return nil
case "import":
//批量导入
var (
data ImportEmployeeData
err error
)
err = json.Unmarshal(byteData, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
// service.addEmployeeData(data.Add)
// for i := range data.Edit {
// UpdateEmployeeData(data.Edit[i])
// }
return nil
default:
return errors.New("action not found")
return errors.New("nothing todo")
}
return nil
}
func (service SyncEmployeeService) AddEmployeeData(data EmployeeData) error {
func (service SyncEmployeeService) addEmployeeData(data EmployeeData) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var usersRepository domain.UsersRepository
if usersRepository, err = factory.CreateUsersRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
newUser := domain.Users{
Id: data.Id,
CompanyId: data.CompanyId,
OpenId: data.OpenId,
Name: data.Name,
Sex: data.Sex,
JobNum: data.JobNum,
Phone: data.Phone,
PrivatePhone: data.PrivatePhone,
Email: data.Email,
ExtensionNum: data.ExtensionNum,
Workspace: data.WorkSpace,
Status: data.Status,
Avatar: data.Avatar,
Remarks: data.Remarks,
ChargeStatus: data.ChargeStatus,
Permission: []domain.AdminPermissionBase{}, //权限
AccessPartners: []domain.Partner{},
}
newUser.EntryTime, _ = time.Parse("2006-01-02", data.EntryTime)
if err = usersRepository.Add(&newUser); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = transactionContext.CommitTransaction()
return err
}
return nil
func (service SyncEmployeeService) updateEmployeeData(data EmployeeData) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var usersRepository domain.UsersRepository
if usersRepository, err = factory.CreateUsersRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
var oldUser domain.Users
oldUser, err = usersRepository.FindOne(domain.UsersFindOneQuery{
Id: data.Id,
})
if err != nil {
return lib.ThrowError(lib.BUSINESS_ERROR, err.Error())
}
entryTime, _ := time.Parse("2006-01-02", data.EntryTime)
_ = oldUser.Update(map[string]interface{}{
"Id": data.Id,
"CompanyId": data.CompanyId,
"OpenId": data.OpenId,
"Name": data.Name,
"Sex": data.Sex,
"JobNum": data.JobNum,
"Phone": data.Phone,
"PrivatePhone": data.PrivatePhone,
"Email": data.Email,
"ExtensionNum": data.ExtensionNum,
"Workspace": data.WorkSpace,
"Status": data.Status,
"Avatar": data.Avatar,
"Remarks": data.Remarks,
"ChargeStatus": data.ChargeStatus,
"EntryTime": entryTime,
})
if err = usersRepository.Edit(&oldUser); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = transactionContext.CommitTransaction()
return err
}
func (service SyncEmployeeService) UpdateEmployeeData(data EmployeeData) error {
return nil
func (service SyncEmployeeService) deleteEmployeeData(data DeleteUserData) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var usersRepository domain.UsersRepository
if usersRepository, err = factory.CreateUsersRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if err = usersRepository.Remove(data.Ids); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = transactionContext.CommitTransaction()
return err
}
//UpdateUsersStatus 批量更新用户的状态
func (service SyncEmployeeService) updateUsersStatus(data ForbidAllowUserData) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var uDao *dao.UsersDao
if uDao, err = factory.CreateUsersDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if err = uDao.UpdateUserStatus(data.Ids, data.Status); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = transactionContext.CommitTransaction()
return err
}
... ...
... ... @@ -52,6 +52,61 @@ func (u Users) InCompany(companyid int64) bool {
return u.CompanyId == companyid
}
func (u *Users) Update(m map[string]interface{}) error {
if v, ok := m["CompanyId"]; ok {
u.CompanyId = v.(int64)
}
if v, ok := m["OpenId"]; ok {
u.OpenId = v.(int64)
}
if v, ok := m["Name"]; ok {
u.Name = v.(string)
}
if v, ok := m["Sex"]; ok {
u.Sex = v.(int8)
}
if v, ok := m["JobNum"]; ok {
u.JobNum = v.(string)
}
if v, ok := m["Phone"]; ok {
u.Phone = v.(string)
}
if v, ok := m["PrivatePhone"]; ok {
u.PrivatePhone = v.(string)
}
if v, ok := m["Email"]; ok {
u.Email = v.(string)
}
if v, ok := m["ExtensionNum"]; ok {
u.ExtensionNum = v.(string)
}
if v, ok := m["EntryTime"]; ok {
u.EntryTime = v.(time.Time)
}
if v, ok := m["Workspace"]; ok {
u.Workspace = v.(string)
}
if v, ok := m["Status"]; ok {
u.Status = v.(int8)
}
if v, ok := m["Avatar"]; ok {
u.Avatar = v.(string)
}
if v, ok := m["Remarks"]; ok {
u.Remarks = v.(string)
}
if v, ok := m["ChargeStatus"]; ok {
u.ChargeStatus = v.(int8)
}
if v, ok := m["Permission"]; ok {
u.Permission = v.([]AdminPermissionBase)
}
if v, ok := m["AccessPartners"]; ok {
u.AccessPartners = v.([]Partner)
}
return nil
}
type UsersFindOneQuery struct {
Id int64
Phone string
... ...
... ... @@ -3,6 +3,7 @@ package dao
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)
... ... @@ -10,7 +11,7 @@ type UsersDao struct {
transactionContext *transaction.TransactionContext
}
func NewUserDao(transactionContext *transaction.TransactionContext) (*UsersDao, error) {
func NewUsersDao(transactionContext *transaction.TransactionContext) (*UsersDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
... ... @@ -19,3 +20,13 @@ func NewUserDao(transactionContext *transaction.TransactionContext) (*UsersDao,
}, nil
}
}
func (dao UsersDao) UpdateUserStatus(ids []int64, ststus int8) error {
tx := dao.transactionContext.PgTx
m := &models.Users{}
_, err := tx.Model(m).
Set("status=?", ststus).
Where("id in(?)", ids).
Update()
return err
}
... ...