|
|
package company
|
|
|
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
"time"
|
|
|
|
|
|
"github.com/linmadan/egglib-go/core/application"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/company/command"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/log"
|
|
|
)
|
|
|
|
|
|
type CompanyServices struct {
|
|
|
type SyncDataCompanyService struct {
|
|
|
}
|
|
|
|
|
|
// type BusinessAdminCommand struct {
|
|
|
// //company:公司
|
|
|
// Module string `json:"module"`
|
|
|
// // add:添加,edit:编辑,setCompanyCharge:更改公司主管,changeAdmin换管理员
|
|
|
// Action string `json:"action"`
|
|
|
// // 具体的对象JSON数据
|
|
|
// Datas json.RawMessage `json:"data"`
|
|
|
// }
|
|
|
|
|
|
//从BusinessAdmins 接收消息,变更公司数据
|
|
|
//
|
|
|
func (c CompanyServices) BusinessAdminCompany() error {
|
|
|
return nil
|
|
|
func (c SyncDataCompanyService) FromBusinessAdmin(param *domain.MessageBody) error {
|
|
|
action := param.Module + "/" + param.Action
|
|
|
var err error
|
|
|
switch action {
|
|
|
case "company/add":
|
|
|
var param1 command.SaveCompanyCommand
|
|
|
err = json.Unmarshal(param.Datas, ¶m1)
|
|
|
if err != nil {
|
|
|
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
err = c.addCompany(¶m1)
|
|
|
case "company/edit":
|
|
|
var param2 command.SaveCompanyCommand
|
|
|
err = json.Unmarshal(param.Datas, ¶m2)
|
|
|
if err != nil {
|
|
|
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
err = c.editCompany(¶m2)
|
|
|
case "company/setCompanyCharge":
|
|
|
var param3 command.SetCompanyCharge
|
|
|
err = json.Unmarshal(param.Datas, ¶m3)
|
|
|
if err != nil {
|
|
|
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
err = c.setCompanyCharge(¶m3)
|
|
|
case "company/changeAdmin":
|
|
|
var param3 command.ChangeAdminCommand
|
|
|
err = json.Unmarshal(param.Datas, ¶m3)
|
|
|
if err != nil {
|
|
|
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
err = c.changeAdmin(¶m3)
|
|
|
default:
|
|
|
log.Logger.Error("action err:" + action)
|
|
|
}
|
|
|
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
//addCompany
|
|
|
//从BusinessAdmins 接收消息 添加公司
|
|
|
func (c CompanyServices) addCompany(param *command.SaveCompanyCommand) error {
|
|
|
//module="company" action="add"
|
|
|
func (c SyncDataCompanyService) addCompany(param *command.SaveCompanyCommand) error {
|
|
|
transactionContext, err := factory.CreateTransactionContext(nil)
|
|
|
if err != nil {
|
|
|
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
...
|
...
|
@@ -63,11 +109,11 @@ func (c CompanyServices) addCompany(param *command.SaveCompanyCommand) error { |
|
|
})
|
|
|
_, err = companyRepo.Insert(&newCompany)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
_, err = userRepo.Insert(&newUser)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
...
|
...
|
@@ -77,7 +123,8 @@ func (c CompanyServices) addCompany(param *command.SaveCompanyCommand) error { |
|
|
|
|
|
//editCompany
|
|
|
//从BusinessAdmins 接收消息 更新公司
|
|
|
func (c CompanyServices) editCompany(param *command.SaveCompanyCommand) error {
|
|
|
//module="company" action="edit"
|
|
|
func (c SyncDataCompanyService) editCompany(param *command.SaveCompanyCommand) error {
|
|
|
transactionContext, err := factory.CreateTransactionContext(nil)
|
|
|
if err != nil {
|
|
|
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
...
|
...
|
@@ -100,14 +147,14 @@ func (c CompanyServices) editCompany(param *command.SaveCompanyCommand) error { |
|
|
"id": param.Comapany.Id,
|
|
|
})
|
|
|
if err != nil {
|
|
|
return err
|
|
|
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
_, userList, err := userRepo.Find(map[string]interface{}{
|
|
|
"limit": 1,
|
|
|
"id": param.User.Id,
|
|
|
})
|
|
|
if err != nil {
|
|
|
return err
|
|
|
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
var (
|
|
|
newCompany *domain.Company
|
...
|
...
|
@@ -146,32 +193,33 @@ func (c CompanyServices) editCompany(param *command.SaveCompanyCommand) error { |
|
|
if len(companyList) > 0 {
|
|
|
_, err = companyRepo.Update(newCompany)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
} else {
|
|
|
_, err = companyRepo.Insert(newCompany)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
}
|
|
|
if len(userList) > 0 {
|
|
|
_, err = userRepo.Update(newUser)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
} else {
|
|
|
_, err = userRepo.Insert(newUser)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
}
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
return err
|
|
|
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func (srv CompanyServices) setCompanyCharge(param *command.SetCompanyCharge) error {
|
|
|
//module="company" action="setCompanyCharge"
|
|
|
func (srv SyncDataCompanyService) setCompanyCharge(param *command.SetCompanyCharge) error {
|
|
|
transactionContext, err := factory.CreateTransactionContext(nil)
|
|
|
if err != nil {
|
|
|
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
...
|
...
|
@@ -193,14 +241,14 @@ func (srv CompanyServices) setCompanyCharge(param *command.SetCompanyCharge) err |
|
|
})
|
|
|
|
|
|
if err != nil {
|
|
|
return err
|
|
|
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
for i := range companyList {
|
|
|
companyList[i].ChargeUserIds = param.ChargeUserIds
|
|
|
companyList[i].UpdateAt = time.Now()
|
|
|
_, err = companyRepo.Update(companyList[i])
|
|
|
if err != nil {
|
|
|
return err
|
|
|
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
}
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
...
|
...
|
@@ -211,7 +259,8 @@ func (srv CompanyServices) setCompanyCharge(param *command.SetCompanyCharge) err |
|
|
|
|
|
//changeAdmin
|
|
|
//从BusinessAdmins 接收消息 变更主管
|
|
|
func (srv CompanyServices) changeAdmin(param *command.ChangeAdminCommand) error {
|
|
|
//module="company" action="changeAdmin"
|
|
|
func (srv SyncDataCompanyService) changeAdmin(param *command.ChangeAdminCommand) error {
|
|
|
transactionContext, err := factory.CreateTransactionContext(nil)
|
|
|
if err != nil {
|
|
|
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
...
|
...
|
@@ -233,7 +282,7 @@ func (srv CompanyServices) changeAdmin(param *command.ChangeAdminCommand) error |
|
|
"adminType": domain.UserTypeManager,
|
|
|
})
|
|
|
if err != nil {
|
|
|
return err
|
|
|
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
//修改旧管理员 为普通用户
|
|
|
for i := range userList {
|
...
|
...
|
@@ -241,7 +290,7 @@ func (srv CompanyServices) changeAdmin(param *command.ChangeAdminCommand) error |
|
|
userList[i].UpdateAt = time.Now()
|
|
|
_, err := userRepo.Update(userList[i])
|
|
|
if err != nil {
|
|
|
return err
|
|
|
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
}
|
|
|
//获取新管理员
|
...
|
...
|
@@ -251,7 +300,7 @@ func (srv CompanyServices) changeAdmin(param *command.ChangeAdminCommand) error |
|
|
"account": param.UserAccount,
|
|
|
})
|
|
|
if err != nil {
|
|
|
return err
|
|
|
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
//修改为管理员用户
|
|
|
for i := range userList2 {
|
...
|
...
|
@@ -259,7 +308,7 @@ func (srv CompanyServices) changeAdmin(param *command.ChangeAdminCommand) error |
|
|
userList[i].UpdateAt = time.Now()
|
|
|
_, err := userRepo.Update(userList[i])
|
|
|
if err != nil {
|
|
|
return err
|
|
|
return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
}
|
|
|
if err := transactionContext.CommitTransaction(); err != nil {
|
...
|
...
|
|