作者 唐旭辉

日常提交保存

... ... @@ -59,3 +59,12 @@ func CreateUsersRepository(options map[string]interface{}) (domain.UsersReposito
}
return repository.NewUsersRepository(transactionContext)
}
// CreateUsersRepository 公司企业信息
func CreateCompanyRepository(options map[string]interface{}) (domain.CompanyRepository, error) {
var transactionContext *transaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*transaction.TransactionContext)
}
return repository.NewCompanyRepository(transactionContext)
}
... ...
package service
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/lib"
)
type SyncCompanyService struct{}
func (service SyncCompanyService) DoAction(action string, data []byte) error {
//企业平台的公司基础数据
type CompanyBase struct {
Id int64 `json:"id"` //id
Name string `json:"name"` //公司名称名称
AdminCompanyId int `json:"admin_company_id"` //总后台的公司id
Logo string `json:"logo"` //公司图标
Remarks string `json:"remarks"` //备注
Status int8 `json:"status"`
}
// CompanytData 企业平台发送过来的公司数据数据
type CompanytData struct {
Company CompanyBase `json:"company"`
User EmployeeData `json:"user"`
}
//CompanyCharge 企业平台发送过来的变更公司主管数据
type CompanyCharge struct {
Id int64 `json:"id"`
Charge []int64 `json:"charge"`
}
func (service SyncCompanyService) DoAction(action string, byteData []byte) error {
switch action {
case "add":
//添加公司
var (
data CompanytData
err error
)
err = json.Unmarshal(byteData, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
return service.addCompany(data)
case "edit":
//编辑
var (
data CompanytData
err error
)
err = json.Unmarshal(byteData, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
return service.updateCompany(data)
case "setCompanyCharge":
//设置公司主管
var (
data CompanyCharge
err error
)
err = json.Unmarshal(byteData, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
return service.updateCompanyCharge(data)
default:
return errors.New("action not found")
}
return nil
}
func (service SyncCompanyService) addCompany(data CompanytData) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
companyRespository domain.CompanyRepository
userRespository domain.UsersRepository
)
if companyRespository, err = factory.CreateCompanyRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if userRespository, err = factory.CreateUsersRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
comany := domain.Company{
Id: data.Company.Id,
Name: data.Company.Name,
Phone: "",
Logo: data.Company.Logo,
Remarks: data.Company.Remarks,
AdminCompanyId: data.Company.AdminCompanyId,
Status: data.Company.Status,
Enable: domain.CompanyEnableNo, //默认初始化值
}
err = companyRespository.Add(&comany)
if err != nil {
return fmt.Errorf("添加公司数据失败,%s", err)
}
user := domain.Users{
Id: data.User.Id,
CompanyId: data.User.CompanyId,
OpenId: data.User.OpenId,
Name: data.User.Name,
Sex: data.User.Sex,
JobNum: data.User.JobNum,
Phone: data.User.Phone,
PrivatePhone: data.User.PrivatePhone,
Email: data.User.Email,
ExtensionNum: data.User.ExtensionNum,
Workspace: data.User.WorkSpace,
Status: data.User.Status,
Avatar: data.User.Avatar,
Remarks: data.User.Remarks,
ChargeStatus: data.User.ChargeStatus,
Permission: []domain.AdminPermissionBase{}, //初始化权限
AccessPartners: []domain.Partner{}, //默认初始化
}
err = userRespository.Add(&user)
if err != nil {
return fmt.Errorf("添加用户数据失败,%s", err)
}
err = transactionContext.CommitTransaction()
return nil
}
func (service SyncCompanyService) updateCompany(data CompanytData) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
companyRespository domain.CompanyRepository
userRespository domain.UsersRepository
oldCompany domain.Company
oldUser domain.Users
)
if companyRespository, err = factory.CreateCompanyRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if userRespository, err = factory.CreateUsersRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
oldCompany, err = companyRespository.FindOne(map[string]interface{}{
"Id": data.Company.Id,
})
oldUser, err = userRespository.FindOne(map[string]interface{}{
"Id": data.User.Id,
})
oldCompany.Update(map[string]interface{}{
"Name": data.Company.Name,
"Logo": data.Company.Logo,
"Remarks": data.Company.Remarks,
"AdminCompanyId": data.Company.AdminCompanyId,
"Status": data.User.Status,
})
entryTime, _ := time.Parse("2006-01-02", data.User.EntryTime)
oldUser.Update(map[string]interface{}{
"CompanyId": data.User.CompanyId,
"OpenId": data.User.OpenId,
"Name": data.User.Name,
"Sex": data.User.Sex,
"JobNum": data.User.JobNum,
"Phone": data.User.Phone,
"PrivatePhone": data.User.PrivatePhone,
"Email": data.User.Email,
"ExtensionNum": data.User.ExtensionNum,
"Workspace": data.User.WorkSpace,
"Status": data.User.Status,
"Avatar": data.User.Avatar,
"Remarks": data.User.Remarks,
"ChargeStatus": data.User.ChargeStatus,
"EntryTime": entryTime,
})
err = companyRespository.Edit(&oldCompany)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = userRespository.Edit(&oldUser)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = transactionContext.CommitTransaction()
return nil
}
func (service SyncCompanyService) updateCompanyCharge(data CompanyCharge) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
userRespository domain.UsersRepository
)
if userRespository, err = factory.CreateUsersRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
var oldUsers []domain.Users
_, oldUsers, err = userRespository.Find(map[string]interface{}{
"CompanyId": data.Id,
"charge_status": domain.UserIsCompanyCharge,
"Limit": -1,
})
for i := range oldUsers {
_ = oldUsers[i].Update(map[string]interface{}{
"charge_status": domain.UserIsNotCompanyCharge,
})
err = userRespository.Edit(&oldUsers[i])
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "更新用户的charge_status失败:"+err.Error())
}
}
for _, v := range data.Charge {
var userToUpdate domain.Users
userToUpdate, err := userRespository.FindOne(map[string]interface{}{
"Id": v,
})
if err != nil {
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()))
}
}
err = transactionContext.CommitTransaction()
return nil
}
... ...
... ... @@ -87,7 +87,7 @@ func (service SyncEmployeeService) DoAction(action string, byteData []byte) erro
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
err = service.addEmployeeData(data)
err = service.addEmployeeData([]EmployeeData{data})
return err
case "edit":
//编辑更新
... ... @@ -99,7 +99,7 @@ func (service SyncEmployeeService) DoAction(action string, byteData []byte) erro
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
err = service.updateEmployeeData(data)
err = service.updateEmployeeData([]EmployeeData{data})
return err
case "batchDelete":
//批量删除
... ... @@ -123,10 +123,9 @@ func (service SyncEmployeeService) DoAction(action string, byteData []byte) erro
return fmt.Errorf("数据解析失败:%s", err)
}
return service.updateUsersStatus(data)
case "batchRemove":
//移动通讯录用户部门 暂时不需要
return nil
// case "batchRemove":
// //移动通讯录用户部门 暂时不需要
// return nil
case "import":
//批量导入
var (
... ... @@ -137,19 +136,18 @@ func (service SyncEmployeeService) DoAction(action string, byteData []byte) erro
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
// service.addEmployeeData(data.Add)
// for i := range data.Edit {
// UpdateEmployeeData(data.Edit[i])
// }
err1 := service.addEmployeeData(data.Add)
err2 := service.updateEmployeeData(data.Edit)
if err1 != nil || err2 != nil {
return fmt.Errorf("添加用户错误:%s,更新用户发生错误:%s", err1, err2)
}
return nil
default:
return errors.New("nothing todo")
return errors.New("action not found")
}
return nil
}
func (service SyncEmployeeService) addEmployeeData(data EmployeeData) error {
func (service SyncEmployeeService) addEmployeeData(datas []EmployeeData) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
... ... @@ -166,34 +164,37 @@ func (service SyncEmployeeService) addEmployeeData(data EmployeeData) error {
}); 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())
for i := range datas {
data := datas[i]
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
}
func (service SyncEmployeeService) updateEmployeeData(data EmployeeData) error {
func (service SyncEmployeeService) updateEmployeeData(datas []EmployeeData) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
... ... @@ -210,39 +211,42 @@ func (service SyncEmployeeService) updateEmployeeData(data EmployeeData) error {
}); 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())
for i := range datas {
data := datas[i]
var oldUser domain.Users
oldUser, err = usersRepository.FindOne(map[string]interface{}{
"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{}{
"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
}
//deleteEmployeeData 删除用户
func (service SyncEmployeeService) deleteEmployeeData(data DeleteUserData) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
... ... @@ -291,3 +295,8 @@ func (service SyncEmployeeService) updateUsersStatus(data ForbidAllowUserData) e
err = transactionContext.CommitTransaction()
return err
}
//ChangeAdmin 变更公司负责人,超级管理员
func (service SyncEmployeeService) ChangeAdmin() error {
return nil
}
... ...
package domain
import "time"
import (
"fmt"
"time"
)
// 公司的状态 1正常 2禁用
const (
... ... @@ -48,6 +51,34 @@ func (c Company) EnableIsOk() bool {
return c.Enable == CompanyEnableYes
}
func (c *Company) Update(m map[string]interface{}) error {
if v, ok := m["Name"]; ok {
c.Name = fmt.Sprint(v)
}
if v, ok := m["Phone"]; ok {
c.Phone = fmt.Sprint(v)
}
if v, ok := m["Logo"]; ok {
c.Logo = fmt.Sprint(v)
}
if v, ok := m["Remarks"]; ok {
c.Remarks = fmt.Sprint(v)
}
if v, ok := m["AdminCompanyId"]; ok {
c.AdminCompanyId = v.(int)
}
if v, ok := m["Remarks"]; ok {
c.Remarks = fmt.Sprint(v)
}
if v, ok := m["Status"]; ok {
c.Status = v.(int8)
}
if v, ok := m["Enable"]; ok {
c.Enable = v.(int8)
}
return nil
}
type CompanyRepository interface {
Add(*Company) error
Edit(*Company) error
... ...
... ... @@ -10,8 +10,8 @@ const (
//用户是否是主管 :【1:是主管】【 2:不是主管】
const (
userIsCharge int8 = 1
userIsNotCharge int8 = 2
UserIsCompanyCharge int8 = 1
UserIsNotCompanyCharge int8 = 2
)
//Users 企业平台的用户
... ... @@ -36,6 +36,7 @@ type Users struct {
UpdateAt time.Time
Permission []AdminPermissionBase //权限
AccessPartners []Partner
AdminType int8 //是否是公司负责人,即超级管理员
}
//IsUsable 用户是否可用
... ... @@ -45,7 +46,7 @@ func (u Users) IsUsable() bool {
//IsCharge 用户是否是主管
func (u Users) IsCharge() bool {
return u.ChargeStatus == userIsCharge
return u.ChargeStatus == UserIsCompanyCharge
}
func (u Users) InCompany(companyid int64) bool {
... ... @@ -104,23 +105,26 @@ func (u *Users) Update(m map[string]interface{}) error {
if v, ok := m["AccessPartners"]; ok {
u.AccessPartners = v.([]Partner)
}
if v, ok := m["AdminType"]; ok {
u.AdminType = v.(int8)
}
return nil
}
type UsersFindOneQuery struct {
Id int64
Phone string
}
// type UsersFindOneQuery struct {
// Id int64
// Phone string
// }
type UsersFindQuery struct {
Ids []int64
Offset int
Limit int
}
// type UsersFindQuery struct {
// Ids []int64
// Offset int
// Limit int
// }
type UsersRepository interface {
Add(*Users) error
Edit(*Users) error
Remove([]int64) error
FindOne(UsersFindOneQuery) (Users, error)
Find(UsersFindQuery) (int, []Users, error)
FindOne(queryOptions map[string]interface{}) (Users, error)
Find(queryOptions map[string]interface{}) (int, []Users, error)
}
... ...
... ... @@ -6,7 +6,7 @@ import "time"
type Company struct {
tableName struct{} `pg:"company"`
// 唯一标识
Id int64
Id int64 `pg:",pk"`
// 名称
Name string
// 手机号码
... ...
... ... @@ -8,7 +8,7 @@ import (
type Users struct {
tableName struct{} `pg:"users"`
Id int64 //用户id
Id int64 `pg:",pk"` //用户id
CompanyId int64 //公司id
OpenId int64 //统一用户中心
Name string //用户名称
... ... @@ -23,6 +23,7 @@ type Users struct {
Status int8 //状态:【1:正常】【 2:禁用】
Avatar string ///头像
Remarks string //备注
AdminType int8 //是否为当前公司负责人 【1:是】【2:否】
ChargeStatus int8 //是否为当前公司主管 【1:是】【2:否】
Permission []domain.AdminPermissionBase //权限
AccessPartners []domain.Partner //可查看的合伙人
... ...
package transaction
import "github.com/go-pg/pg/v10"
import (
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
)
type TransactionContext struct {
PgDd *pg.DB
... ... @@ -26,6 +29,13 @@ func (transactionContext *TransactionContext) RollbackTransaction() error {
return err
}
func (transactionContext *TransactionContext) GetDB() orm.DB {
if transactionContext.PgTx != nil {
return transactionContext.PgTx
}
return transactionContext.PgDd
}
func NewPGTransactionContext(pgDd *pg.DB) *TransactionContext {
return &TransactionContext{
PgDd: pgDd,
... ...
package repository
import (
"errors"
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
... ... @@ -82,7 +83,25 @@ func (reponsitory CompanyRepository) Edit(m *domain.Company) error {
}
func (reponsitory CompanyRepository) FindOne(queryOptions map[string]interface{}) (domain.Company, error) {
return domain.Company{}, nil
if len(queryOptions) == 0 {
return domain.Company{}, errors.New("queryOptions empty")
}
var (
err error
tx = reponsitory.transactionContext.PgTx
m models.Company
)
query := tx.Model(&m)
if v, ok := queryOptions["Id"]; ok {
query = query.Where("id=?", v)
}
err = query.First()
if err != nil {
return domain.Company{}, err
}
return reponsitory.transformPgModelToDomainModel(&m)
}
func (reponsitory CompanyRepository) Find(queryOptions map[string]interface{}) (int64, []domain.Company, error) {
... ...
package repository
import (
"errors"
"fmt"
"time"
... ... @@ -110,24 +111,19 @@ func (reponsitory UsersRepository) Edit(u *domain.Users) error {
return err
}
func (reponsitory UsersRepository) FindOne(queryOptions domain.UsersFindOneQuery) (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")
}
var (
err error
tx = reponsitory.transactionContext.PgTx
m models.Users
hasWhere bool
err error
tx = reponsitory.transactionContext.PgTx
m models.Users
)
query := tx.Model(&m)
if queryOptions.Id > 0 {
query = query.Where("id=?", queryOptions.Id)
hasWhere = true
}
if len(queryOptions.Phone) > 0 {
query = query.Where("phone=?", queryOptions.Id)
hasWhere = true
}
if !hasWhere {
return domain.Users{}, fmt.Errorf("queryOptions 中没有搜索条件")
if v, ok := queryOptions["Id"]; ok {
query = query.Where("id=?", v)
}
err = query.First()
if err != nil {
... ... @@ -136,15 +132,25 @@ func (reponsitory UsersRepository) FindOne(queryOptions domain.UsersFindOneQuery
return reponsitory.transformPgModelToDomainModel(&m)
}
func (reponsitory UsersRepository) Find(queryOption domain.UsersFindQuery) (int, []domain.Users, error) {
func (reponsitory UsersRepository) Find(queryOption map[string]interface{}) (int, []domain.Users, error) {
db := reponsitory.transactionContext.PgTx
usersModels := []models.Users{}
query := db.Model(&usersModels)
if queryOption.Offset > -1 {
query = query.Offset(queryOption.Offset)
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 queryOption.Limit > 0 {
query = query.Limit(queryOption.Limit)
if v, ok := queryOption["Offset"]; ok {
offset := v.(int)
query = query.Offset(offset)
}
if v, ok := queryOption["Limit"]; ok {
limit := v.(int)
if limit > 0 {
query = query.Limit(limit)
}
} else {
query = query.Limit(20)
}
... ...