作者 唐旭辉

完成与企业平台以及统一用户中心数据同步相关

... ... @@ -39,10 +39,7 @@ func (adminPermissionSrv AdminPermissionService) ListAdminPermission(queryOption
} else {
permissionRepository = value
}
permissions, err = permissionRepository.Find(domain.AdminPermissionFindQuery{
NotCode: queryOption.NotCode,
ParentId: queryOption.ParentId,
})
permissions, err = permissionRepository.Find(map[string]interface{}{})
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
... ...
... ... @@ -87,8 +87,8 @@ func (adminUserSrv AdminUserService) SaveAdminUser(saveUserCmd *command.SaveAdmi
} else {
permissionRepository = value
}
permissions, err = permissionRepository.Find(domain.AdminPermissionFindQuery{
IdsIn: saveUserCmd.PermissionId,
permissions, err = permissionRepository.Find(map[string]interface{}{
"IdsIn": saveUserCmd.PermissionId,
})
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
... ...
package service
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)
type CompanyService struct{}
func NewCompanyService(options map[string]interface{}) *CompanyService {
newCompanyService := &CompanyService{}
return newCompanyService
}
//AllowCompany 公司启用合伙人功能
func (service CompanyService) AllowCompany(companyId int64) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
companyRespository domain.CompanyRepository
oldCompanyData domain.Company
)
if companyRespository, err = factory.CreateCompanyRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
oldCompanyData, err = companyRespository.FindOne(map[string]interface{}{
"Id": companyId,
})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("获取公司(id=%d)的数据失败:%s", companyId, err.Error()))
}
err = oldCompanyData.Update(map[string]interface{}{
"Enable": domain.CompanyEnableYes,
})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
//更新公司数据
err = companyRespository.Edit(&oldCompanyData)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("更新公司(id=%d)的数据失败:%s", companyId, err.Error()))
}
err = transactionContext.CommitTransaction()
return nil
}
//ForbidCompany 公司禁用合伙人功能
func (service CompanyService) ForbidCompany(companyId int64) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
companyRespository domain.CompanyRepository
oldCompanyData domain.Company
)
if companyRespository, err = factory.CreateCompanyRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
oldCompanyData, err = companyRespository.FindOne(map[string]interface{}{
"Id": companyId,
})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("获取公司(id=%d)的数据失败:%s", companyId, err.Error()))
}
err = oldCompanyData.Update(map[string]interface{}{
"Enable": domain.CompanyEnableNo,
})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
//更新公司数据
err = companyRespository.Edit(&oldCompanyData)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("更新公司(id=%d)的数据失败:%s", companyId, err.Error()))
}
err = transactionContext.CommitTransaction()
return nil
}
//GetCompanyData 获取公司数据
func (service CompanyService) GetCompanyData(companyId int64) (*domain.Company, error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
companyRespository domain.CompanyRepository
companyData domain.Company
)
if companyRespository, err = factory.CreateCompanyRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
companyData, err = companyRespository.FindOne(map[string]interface{}{
"Id": companyId,
})
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("获取公司(id=%d)的数据失败:%s", companyId, err.Error()))
}
err = transactionContext.CommitTransaction()
return &companyData, nil
}
... ...
package command
type ChanceSuperAdminCommand struct {
CompanyId int64 `json:"company_id"` //公司id
Phone string `json:"phone"` //新的超级管理员手机号(账号)
}
... ...
... ... @@ -13,6 +13,8 @@ import (
type SyncCompanyService struct{}
var _ SyncAction = (*SyncCompanyService)(nil)
//企业平台的公司基础数据
type CompanyBase struct {
Id int64 `json:"id"` //id
... ... @@ -73,7 +75,6 @@ func (service SyncCompanyService) DoAction(action string, byteData []byte) error
default:
return errors.New("action not found")
}
return nil
}
func (service SyncCompanyService) addCompany(data CompanytData) error {
... ... @@ -252,14 +253,14 @@ func (service SyncCompanyService) updateCompanyCharge(data CompanyCharge) error
"Id": v,
})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("获取用户id=%d的失败:%s", v, err.Error()))
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("获取用户(id=%d)的数据失败:%s", v, err.Error()))
}
_ = userToUpdate.Update(map[string]interface{}{
"charge_status": domain.UserIsCompanyCharge,
})
err = userRespository.Edit(&userToUpdate)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("更新用户id=%d的失败:%s", v, err.Error()))
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("更新用户(id=%d)的数据失败:%s", v, err.Error()))
}
}
err = transactionContext.CommitTransaction()
... ...
... ... @@ -7,6 +7,7 @@ import (
"time"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/unifiedUserCenter/command"
"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"
... ... @@ -75,6 +76,13 @@ type ImportEmployeeData struct {
//SyncEmployeeService 同步用户数据
type SyncEmployeeService struct{}
func NewSyncEmployeeService(option map[string]interface{}) *SyncEmployeeService {
syncEmployee := new(SyncEmployeeService)
return syncEmployee
}
var _ SyncAction = (*SyncEmployeeService)(nil)
func (service SyncEmployeeService) DoAction(action string, byteData []byte) error {
switch action {
case "add":
... ... @@ -297,6 +305,82 @@ func (service SyncEmployeeService) updateUsersStatus(data ForbidAllowUserData) e
}
//ChangeAdmin 变更公司负责人,超级管理员
func (service SyncEmployeeService) ChangeAdmin() error {
func (service SyncEmployeeService) ChangeSuperAdmin(cmd command.ChanceSuperAdminCommand) 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 (
oldSuperUser domain.Users
newSuperUser domain.Users
userList []domain.Users
)
_, userList, err = usersRepository.Find(map[string]interface{}{
"CompanyId": cmd.CompanyId,
"AdminType": domain.UserIsAdmin,
})
if err != nil {
e := fmt.Sprintf("获取用户(admin_type=%d;company_id=%d)数据失败:%s",
domain.UserIsAdmin, cmd.CompanyId, err.Error())
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
if len(userList) == 0 {
e := fmt.Sprintf("没有获得公司主管数据(admin_type=%d;company_id=%d)",
domain.UserIsAdmin, cmd.CompanyId)
return lib.ThrowError(lib.BUSINESS_ERROR, e)
}
if len(userList) > 1 {
e := fmt.Sprintf("存在复数公司主管数据(admin_type=%d;company_id=%d)",
domain.UserIsAdmin, cmd.CompanyId)
return lib.ThrowError(lib.BUSINESS_ERROR, e)
}
oldSuperUser = userList[0]
newSuperUser, err = usersRepository.FindOne(map[string]interface{}{
"CompanyId": cmd.CompanyId,
"Phone": cmd.Phone,
})
if err != nil {
e := fmt.Sprintf("获取公司用户数据(phone=%s;company_id=%d)",
cmd.Phone, cmd.CompanyId)
return lib.ThrowError(lib.BUSINESS_ERROR, e)
}
err = oldSuperUser.Update(map[string]interface{}{
"AdminType": domain.UserIsNotAdmin,
})
if err != nil {
return lib.ThrowError(lib.BUSINESS_ERROR, err.Error())
}
//提取到domain???
err = newSuperUser.Update(map[string]interface{}{
"AdminType": domain.UserIsAdmin,
})
if err != nil {
return lib.ThrowError(lib.BUSINESS_ERROR, err.Error())
}
err = usersRepository.Edit(&oldSuperUser)
if err != nil {
e := fmt.Sprintf("更新公司主管user数据(id=%d)",
oldSuperUser.Id)
return lib.ThrowError(lib.BUSINESS_ERROR, e)
}
err = usersRepository.Edit(&newSuperUser)
if err != nil {
e := fmt.Sprintf("更新公司主管user数据(id=%d)",
newSuperUser.Id)
return lib.ThrowError(lib.BUSINESS_ERROR, e)
}
err = transactionContext.CommitTransaction()
return nil
}
... ...
package service
import "errors"
import (
"errors"
//PlatformAction 企业平台数据同步服务 接口设定
type PlatformAction interface {
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/unifiedUserCenter/command"
)
//PlatformAction 企业平台数据同步服务 动作接口设定
type SyncAction interface {
DoAction(string, []byte) error
}
var actionmap = map[string]PlatformAction{
//actionMap 数据同步需要的动作集合,静态不要在运行时变更这里的数据
var actionMap = map[string]SyncAction{
// "department":
// "position":
"employee": SyncEmployeeService{},
"company": SyncCompanyService{},
}
func NewPlatformAction(module string) (PlatformAction, error) {
if v, ok := actionmap[module]; ok {
return v, nil
func NewSyncAction(cmd command.SyncCallbackCommand) error {
err := cmd.ValidateCommand()
if err != nil {
return err
}
var (
action SyncAction
ok bool
)
if action, ok = actionMap[cmd.Module]; !ok {
return errors.New("module cannot found")
}
return nil, errors.New("module cannot found")
return action.DoAction(cmd.Action, cmd.Data)
}
... ...
... ... @@ -26,12 +26,6 @@ type AdminPermission struct {
Icon string `json:"icon"`
}
type AdminPermissionFindQuery struct {
IdsIn []int
NotCode []string
ParentId interface{}
}
type AdminPermissionRepository interface {
Find(queryOptions AdminPermissionFindQuery) ([]AdminPermission, error)
Find(map[string]interface{}) ([]AdminPermission, error)
}
... ...
... ... @@ -14,6 +14,12 @@ const (
UserIsNotCompanyCharge int8 = 2
)
//用户类型 1普通用户 2主管理员
const (
UserIsNotAdmin int8 = 1
UserIsAdmin int8 = 2
)
//Users 企业平台的用户
type Users struct {
Id int64 //用户id
... ... @@ -36,7 +42,7 @@ type Users struct {
UpdateAt time.Time
Permission []AdminPermissionBase //权限
AccessPartners []Partner
AdminType int8 //是否是公司负责人,即超级管理员
AdminType int8 //是否是公司负责人,即超级管理员 1普通用户 2主管理员
}
//IsUsable 用户是否可用
... ...
... ... @@ -33,18 +33,13 @@ func (reponsitory AdminPermissionRepository) transformPgModelToDomainModel(permi
return result, nil
}
func (reponsitory AdminPermissionRepository) Find(queryOptions domain.AdminPermissionFindQuery) ([]domain.AdminPermission, error) {
func (reponsitory AdminPermissionRepository) Find(queryOptions map[string]interface{}) ([]domain.AdminPermission, error) {
db := reponsitory.transactionContext.PgDd
permissionModels := make([]models.AdminPermission, 0)
query := db.Model(&permissionModels)
if queryOptions.ParentId != nil {
query = query.Where("parent_id=?", queryOptions.ParentId)
}
if len(queryOptions.NotCode) > 0 {
query = query.WhereIn("code not in (?) ", queryOptions.NotCode)
}
if len(queryOptions.IdsIn) > 0 {
query = query.WhereIn(" id in (?) ", queryOptions.IdsIn)
if v, ok := queryOptions["IdsIn"]; ok {
in := v.([]int)
query = query.WhereIn("id in (?) ", in)
}
if err := query.Select(); err != nil {
return nil, err
... ...
... ... @@ -113,18 +113,21 @@ func (reponsitory UsersRepository) Edit(u *domain.Users) error {
func (reponsitory UsersRepository) FindOne(queryOptions map[string]interface{}) (domain.Users, error) {
if len(queryOptions) == 0 {
return domain.Users{}, errors.New("queryOptions empty")
return domain.Users{}, errors.New("queryOptions is empty")
}
var (
err error
tx = reponsitory.transactionContext.PgTx
m models.Users
)
query := tx.Model(&m)
query.Where("deleted_at ISNULL")
if v, ok := queryOptions["Id"]; ok {
query = query.Where("id=?", v)
}
if v, ok := queryOptions["phone"]; ok {
query = query.Where("phone=?", v)
}
err = query.First()
if err != nil {
return domain.Users{}, err
... ... @@ -136,12 +139,16 @@ func (reponsitory UsersRepository) Find(queryOption map[string]interface{}) (int
db := reponsitory.transactionContext.PgTx
usersModels := []models.Users{}
query := db.Model(&usersModels)
query.Where("deleted_at ISNULL")
if v, ok := queryOption["CompanyId"]; ok {
query = query.Where("company_id=?", v)
}
if v, ok := queryOption["ChargeStatus"]; ok {
query = query.Where("charge_status=?", v)
}
if v, ok := queryOption["AdminType"]; ok {
query = query.Where("admin_type=?", v)
}
if v, ok := queryOption["Offset"]; ok {
offset := v.(int)
query = query.Offset(offset)
... ...
... ... @@ -230,6 +230,9 @@ func (c *AdminUserController) BeforeEditAdminUser() {
}
var rspData []map[string]interface{}
for i := range allPermission {
if allPermission[i].Code == domain.PERMINSSION_ADMIN_USER {
continue
}
m := map[string]interface{}{
"id": allPermission[i].Id,
"permissionName": allPermission[i].Name,
... ...
package controllers
//用于和企业平台和总后台进行数据通讯
import (
"encoding/json"
"errors"
"github.com/astaxie/beego/logs"
companyService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/company/service"
syncCmd "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/unifiedUserCenter/command"
syncService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/unifiedUserCenter/service"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
)
type SyncDataController struct {
BaseController
}
////Prepare 重写 BaseController 的Prepare方法
func (c *SyncDataController) Prepare() {
c.BaseController.Prepare()
}
//SyncData 和企业后台同步数据
func (c *SyncDataController) SyncData() {
var (
cmd syncCmd.SyncCallbackCommand
err error
)
if err = c.BindJsonData(&cmd); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
err = syncService.NewSyncAction(cmd)
if err != nil {
logs.Error("同步数据异常;%s", err)
}
//不论子系统如何处理以及处理结果成功失败 必须返回 0
c.ResponseData(nil)
return
}
//CompanyAdminChance 变更公司主管(超级管理)
func (c *SyncDataController) CompanyAdminChance() {
var (
param syncCmd.ChanceSuperAdminCommand
err error
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
syncEmployeeSrv := syncService.NewSyncEmployeeService(nil)
syncEmployeeSrv.ChangeSuperAdmin(param)
if err != nil {
logs.Error("变更公司的主管失败;%s", err)
}
c.ResponseData(nil)
return
}
//AllowForidCompany 总后台调用
func (c *SyncDataController) AllowForidCompany() {
type Paremeter struct {
Companyid int64 `json:"company_id"`
Status int `json:"status"`
}
var param Paremeter
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
logs.Error("json 解析失败 err:%s", err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
var (
err error
)
comanySrv := companyService.NewCompanyService(nil)
switch param.Status {
case 1:
err = comanySrv.AllowCompany(param.Companyid)
case 2:
err = comanySrv.ForbidCompany(param.Companyid)
default:
c.ResponseError(errors.New("参数错误"))
return
}
if err != nil {
c.ResponseError(err)
}
c.ResponseData(nil)
return
}
//GetCompanyInfo 总后台调用
func (c *SyncDataController) GetCompanyInfo() {
type Paremeter struct {
Companyid int64 `json:"company_id"`
}
var param Paremeter
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
logs.Error("json 解析失败 err:%s", err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
var (
err error
companyData *domain.Company
)
comanySrv := companyService.NewCompanyService(nil)
companyData, err = comanySrv.GetCompanyData(param.Companyid)
rspData := map[string]interface{}{
"comopany_id": 0,
"exist": -1, //公司【1:存在;【-1:不存在】
"status": -1, //公司的启用状态【1:启用,-1:禁用】
}
if err != nil {
logs.Error(err)
c.ResponseData(rspData)
return
}
if companyData.EnableIsOk() {
rspData["status"] = 1
}
rspData["exist"] = 1
c.ResponseData(rspData)
return
}
... ...
package routers
import (
"github.com/astaxie/beego"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/port/beego/controllers"
)
//外部调用,企业平台,总后台调用
func init() {
nsPlatform := beego.NewNamespace("/platform",
beego.NSRouter("/action", &controllers.SyncDataController{}, "post:SyncData"),
beego.NSRouter("/admins_change", &controllers.SyncDataController{}, "post:CompanyAdminChance"),
)
nsUcenter := beego.NewNamespace("/ucenter",
beego.NSRouter("/company/allow_forbid", &controllers.SyncDataController{}, "post:AllowForidCompany"),
beego.NSRouter("/company/info", &controllers.SyncDataController{}, "post:GetCompanyInfo"),
)
beego.AddNamespace(nsPlatform) //企业平台调用
beego.AddNamespace(nsUcenter) //统一用户中心调用
}
... ...