作者 唐旭辉

更新

... ... @@ -36,8 +36,8 @@ func (service CompanyService) AllowCompany(companyId int64) error {
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
oldCompanyData, err = companyRespository.FindOne(map[string]interface{}{
"Id": companyId,
oldCompanyData, err = companyRespository.FindOne(domain.CompanyFindOneOptions{
Id: companyId,
})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("获取公司(id=%d)的数据失败:%s", companyId, err.Error()))
... ... @@ -78,8 +78,8 @@ func (service CompanyService) ForbidCompany(companyId int64) error {
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
oldCompanyData, err = companyRespository.FindOne(map[string]interface{}{
"Id": companyId,
oldCompanyData, err = companyRespository.FindOne(domain.CompanyFindOneOptions{
Id: companyId,
})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("获取公司(id=%d)的数据失败:%s", companyId, err.Error()))
... ... @@ -120,8 +120,8 @@ func (service CompanyService) GetCompanyData(companyId int64) (*domain.Company,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
companyData, err = companyRespository.FindOne(map[string]interface{}{
"Id": companyId,
companyData, err = companyRespository.FindOne(domain.CompanyFindOneOptions{
Id: companyId,
})
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("获取公司(id=%d)的数据失败:%s", companyId, err.Error()))
... ...
... ... @@ -170,11 +170,11 @@ func (service SyncCompanyService) updateCompany(data CompanytData) error {
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
oldCompany, err = companyRespository.FindOne(map[string]interface{}{
"Id": data.Company.Id,
oldCompany, err = companyRespository.FindOne(domain.CompanyFindOneOptions{
Id: data.Company.Id,
})
oldUser, err = userRespository.FindOne(map[string]interface{}{
"Id": data.User.Id,
oldUser, err = userRespository.FindOne(domain.UsersFindOneQuery{
Id: data.User.Id,
})
oldCompany.Update(map[string]interface{}{
"Name": data.Company.Name,
... ... @@ -233,10 +233,10 @@ func (service SyncCompanyService) updateCompanyCharge(data CompanyCharge) error
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,
_, oldUsers, err = userRespository.Find(domain.UsersFindQuery{
CompanyId: data.Id,
ChargeStatus: domain.UserIsCompanyCharge,
Limit: -1,
})
for i := range oldUsers {
_ = oldUsers[i].Update(map[string]interface{}{
... ... @@ -249,8 +249,8 @@ func (service SyncCompanyService) updateCompanyCharge(data CompanyCharge) error
}
for _, v := range data.Charge {
var userToUpdate domain.Users
userToUpdate, err := userRespository.FindOne(map[string]interface{}{
"Id": v,
userToUpdate, err := userRespository.FindOne(domain.UsersFindOneQuery{
Id: v,
})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("获取用户(id=%d)的数据失败:%s", v, err.Error()))
... ...
... ... @@ -222,8 +222,8 @@ func (service SyncEmployeeService) updateEmployeeData(datas []EmployeeData) erro
for i := range datas {
data := datas[i]
var oldUser domain.Users
oldUser, err = usersRepository.FindOne(map[string]interface{}{
"Id": data.Id,
oldUser, err = usersRepository.FindOne(domain.UsersFindOneQuery{
Id: data.Id,
})
if err != nil {
return lib.ThrowError(lib.BUSINESS_ERROR, err.Error())
... ... @@ -327,9 +327,9 @@ func (service SyncEmployeeService) ChangeSuperAdmin(cmd command.ChanceSuperAdmin
newSuperUser domain.Users
userList []domain.Users
)
_, userList, err = usersRepository.Find(map[string]interface{}{
"CompanyId": cmd.CompanyId,
"AdminType": domain.UserIsAdmin,
_, userList, err = usersRepository.Find(domain.UsersFindQuery{
CompanyId: cmd.CompanyId,
AdminType: domain.UserIsAdmin,
})
if err != nil {
e := fmt.Sprintf("获取用户(admin_type=%d;company_id=%d)数据失败:%s",
... ... @@ -347,9 +347,9 @@ func (service SyncEmployeeService) ChangeSuperAdmin(cmd command.ChanceSuperAdmin
return lib.ThrowError(lib.BUSINESS_ERROR, e)
}
oldSuperUser = userList[0]
newSuperUser, err = usersRepository.FindOne(map[string]interface{}{
"CompanyId": cmd.CompanyId,
"Phone": cmd.Phone,
newSuperUser, err = usersRepository.FindOne(domain.UsersFindOneQuery{
CompanyId: cmd.CompanyId,
Phone: cmd.Phone,
})
if err != nil {
e := fmt.Sprintf("获取公司用户数据(phone=%s;company_id=%d)",
... ...
... ... @@ -79,9 +79,16 @@ func (c *Company) Update(m map[string]interface{}) error {
return nil
}
type CompanyFindOneOptions struct {
Id int64
}
type CompanyFindOptions struct {
}
type CompanyRepository interface {
Add(*Company) error
Edit(*Company) error
FindOne(queryOptions map[string]interface{}) (Company, error)
Find(queryOptions map[string]interface{}) (int64, []Company, error)
FindOne(queryOptions CompanyFindOneOptions) (Company, error)
Find(queryOptions CompanyFindOptions) (int64, []Company, error)
}
... ...
... ... @@ -117,20 +117,25 @@ func (u *Users) Update(m map[string]interface{}) error {
return nil
}
// type UsersFindOneQuery struct {
// Id int64
// Phone string
// }
type UsersFindOneQuery struct {
Id int64
Phone string
CompanyId int64
OpenId int64
}
// type UsersFindQuery struct {
// Ids []int64
// Offset int
// Limit int
// }
type UsersFindQuery struct {
AdminType int8
ChargeStatus int8
CompanyId int64
Ids []int64
Offset int
Limit int
}
type UsersRepository interface {
Add(*Users) error
Edit(*Users) error
Remove([]int64) error
FindOne(queryOptions map[string]interface{}) (Users, error)
Find(queryOptions map[string]interface{}) (int, []Users, error)
FindOne(queryOptions UsersFindOneQuery) (Users, error)
Find(queryOptions UsersFindQuery) (int, []Users, error)
}
... ...
package repository
import (
"errors"
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
... ... @@ -82,11 +81,7 @@ func (reponsitory CompanyRepository) Edit(m *domain.Company) error {
return err
}
func (reponsitory CompanyRepository) FindOne(queryOptions map[string]interface{}) (domain.Company, error) {
if len(queryOptions) == 0 {
return domain.Company{}, errors.New("queryOptions empty")
}
func (reponsitory CompanyRepository) FindOne(queryOptions domain.CompanyFindOneOptions) (domain.Company, error) {
var (
err error
tx = reponsitory.transactionContext.PgTx
... ... @@ -94,8 +89,8 @@ func (reponsitory CompanyRepository) FindOne(queryOptions map[string]interface{}
)
query := tx.Model(&m)
if v, ok := queryOptions["Id"]; ok {
query = query.Where("id=?", v)
if queryOptions.Id > 0 {
query = query.Where("id=?", queryOptions.Id)
}
err = query.First()
if err != nil {
... ... @@ -104,6 +99,6 @@ func (reponsitory CompanyRepository) FindOne(queryOptions map[string]interface{}
return reponsitory.transformPgModelToDomainModel(&m)
}
func (reponsitory CompanyRepository) Find(queryOptions map[string]interface{}) (int64, []domain.Company, error) {
func (reponsitory CompanyRepository) Find(queryOptions domain.CompanyFindOptions) (int64, []domain.Company, error) {
return 0, nil, nil
}
... ...
package repository
import (
"errors"
"fmt"
"time"
... ... @@ -111,10 +110,7 @@ func (reponsitory UsersRepository) Edit(u *domain.Users) error {
return err
}
func (reponsitory UsersRepository) FindOne(queryOptions map[string]interface{}) (domain.Users, error) {
if len(queryOptions) == 0 {
return domain.Users{}, errors.New("queryOptions is empty")
}
func (reponsitory UsersRepository) FindOne(queryOptions domain.UsersFindOneQuery) (domain.Users, error) {
var (
err error
tx = reponsitory.transactionContext.PgTx
... ... @@ -122,17 +118,17 @@ func (reponsitory UsersRepository) FindOne(queryOptions map[string]interface{})
)
query := tx.Model(&m)
query.Where("deleted_at ISNULL")
if v, ok := queryOptions["Id"]; ok {
query = query.Where("id=?", v)
if queryOptions.Id > 0 {
query = query.Where("id=?", queryOptions.Id)
}
if v, ok := queryOptions["Phone"]; ok {
query = query.Where("phone=?", v)
if len(queryOptions.Phone) > 0 {
query = query.Where("phone=?", queryOptions.Phone)
}
if v, ok := queryOptions["CompanyId"]; ok {
query = query.Where("company_id=?", v)
if queryOptions.CompanyId > 0 {
query = query.Where("company_id=?", queryOptions.CompanyId)
}
if v, ok := queryOptions["OpenId"]; ok {
query = query.Where("open_id=?", v)
if queryOptions.OpenId > 0 {
query = query.Where("open_id=?", queryOptions.OpenId)
}
err = query.First()
if err != nil {
... ... @@ -141,31 +137,27 @@ func (reponsitory UsersRepository) FindOne(queryOptions map[string]interface{})
return reponsitory.transformPgModelToDomainModel(&m)
}
func (reponsitory UsersRepository) Find(queryOption map[string]interface{}) (int, []domain.Users, error) {
func (reponsitory UsersRepository) Find(queryOption domain.UsersFindQuery) (int, []domain.Users, error) {
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 queryOption.CompanyId > 0 {
query = query.Where("company_id=?", queryOption.CompanyId)
}
if v, ok := queryOption["ChargeStatus"]; ok {
query = query.Where("charge_status=?", v)
if queryOption.ChargeStatus > 0 {
query = query.Where("charge_status=?", queryOption.ChargeStatus)
}
if v, ok := queryOption["AdminType"]; ok {
query = query.Where("admin_type=?", v)
if queryOption.AdminType > 0 {
query = query.Where("admin_type=?", queryOption.AdminType)
}
if v, ok := queryOption["Offset"]; ok {
offset := v.(int)
query = query.Offset(offset)
if queryOption.Offset >= 0 {
query = query.Offset(queryOption.Offset)
}
if v, ok := queryOption["Limit"]; ok {
limit := v.(int)
if limit > 0 {
query = query.Limit(limit)
}
if queryOption.Limit > 0 {
query = query.Limit(queryOption.Limit)
} else {
query = query.Limit(20)
query = query.Limit(1000)
}
var (
err error
... ...