切换导航条
此项目
正在载入...
登录
allied-creation
/
performance
·
提交
转到一个项目
GitLab
转到群组
项目
活动
文件
提交
管道
0
构建
0
图表
里程碑
问题
0
合并请求
0
成员
标记
维基
派生
网络
创建新的问题
下载为
邮件补丁
差异文件
浏览文件
作者
tangxvhui
2 years ago
提交
ce9e76b8d8e75d0af4e53c6481d1be6faf5dac5e
1 个父辈
8adddff6
更新
隐藏空白字符变更
内嵌
并排对比
正在显示
13 个修改的文件
包含
472 行增加
和
124 行删除
pkg/application/company/command/add_company.go → pkg/application/company/command/save_company.go
pkg/application/company/service.go
pkg/application/factory/reposetory.go
pkg/application/company/command/change_admin.go → pkg/application/user/command/change_admin.go
pkg/application/company/command/update_company.go → pkg/application/user/command/save_user.go
pkg/application/company/command/set_company_charge.go → pkg/application/user/command/set_company_charge.go
pkg/application/user/service.go
pkg/domain/company.go
pkg/domain/user.go
pkg/infrastructure/pg/models/user.go
pkg/infrastructure/repository/pg_company_repository.go
pkg/infrastructure/repository/pg_user_repository.go
pkg/infrastructure/repository/var.go
pkg/application/company/command/
add
_company.go → pkg/application/company/command/
save
_company.go
查看文件 @
ce9e76b
...
...
@@ -2,7 +2,7 @@ package command
//AddCompanyCommand 数据来源 企业平台 推送的消息
//新建公司
type
Add
CompanyCommand
struct
{
type
Save
CompanyCommand
struct
{
//新添加的公司
Comapany
struct
{
Id
int64
`json:"id" `
...
...
@@ -10,7 +10,7 @@ type AddCompanyCommand struct {
Name
string
`json:"name"`
// 公司名称
Status
int
`json:"status"`
// 公司状态,1正常 2禁用
}
`json:"company"`
//新添加公司时,
新建的公司
主管理员
//新添加公司时,
第一個用户就是
主管理员
User
struct
{
Id
int64
` json:"id"`
// 用户Id
Phone
string
`json:"phone"`
// 用户账号
...
...
pkg/application/company/service.go
查看文件 @
ce9e76b
package
company
import
(
"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"
)
type
CompanyServices
struct
{
}
//从BusinessAdmins 接收消息,变更公司数据
//
func
(
c
CompanyServices
)
BusinessAdminCompany
()
error
{
return
nil
}
func
(
c
CompanyServices
)
AddCompany
()
error
{
return
nil
}
//addCompany
//从BusinessAdmins 接收消息 添加公司
func
(
c
CompanyServices
)
addCompany
(
param
*
command
.
SaveCompanyCommand
)
error
{
transactionContext
,
err
:=
factory
.
CreateTransactionContext
(
nil
)
if
err
!=
nil
{
return
application
.
ThrowError
(
application
.
TRANSACTION_ERROR
,
err
.
Error
())
}
if
err
:=
transactionContext
.
StartTransaction
();
err
!=
nil
{
return
application
.
ThrowError
(
application
.
TRANSACTION_ERROR
,
err
.
Error
())
}
defer
func
()
{
_
=
transactionContext
.
RollbackTransaction
()
}()
nowTime
:=
time
.
Now
()
newCompany
:=
domain
.
Company
{
Id
:
param
.
Comapany
.
Id
,
Logo
:
param
.
Comapany
.
Logo
,
Name
:
param
.
Comapany
.
Name
,
Status
:
param
.
Comapany
.
Status
,
UpdateAt
:
nowTime
,
CreateAt
:
nowTime
,
DeleteAt
:
nil
,
}
func
(
c
CompanyServices
)
EditCompany
()
error
{
newUser
:=
domain
.
User
{
Id
:
param
.
User
.
Id
,
Account
:
param
.
User
.
Phone
,
AvatarUrl
:
param
.
User
.
Avatar
,
CompanyId
:
param
.
User
.
CompanyId
,
AdminType
:
param
.
User
.
AdminType
,
Name
:
param
.
User
.
Name
,
Status
:
param
.
User
.
Status
,
UpdateAt
:
nowTime
,
DeleteAt
:
nil
,
CreateAt
:
nowTime
,
}
companyRepo
:=
factory
.
CreateCompanyRepository
(
map
[
string
]
interface
{}{
"transactionContext"
:
transactionContext
,
})
userRepo
:=
factory
.
CreateUserRepository
(
map
[
string
]
interface
{}{
"transactionContext"
:
transactionContext
,
})
_
,
err
=
companyRepo
.
Insert
(
&
newCompany
)
if
err
!=
nil
{
return
err
}
_
,
err
=
userRepo
.
Insert
(
&
newUser
)
if
err
!=
nil
{
return
err
}
if
err
:=
transactionContext
.
CommitTransaction
();
err
!=
nil
{
return
application
.
ThrowError
(
application
.
TRANSACTION_ERROR
,
err
.
Error
())
}
return
nil
}
func
(
c
CompanyServices
)
ChangeAdmin
()
error
{
return
nil
}
//editCompany
//从BusinessAdmins 接收消息 更新公司
func
(
c
CompanyServices
)
editCompany
(
param
*
command
.
SaveCompanyCommand
)
error
{
transactionContext
,
err
:=
factory
.
CreateTransactionContext
(
nil
)
if
err
!=
nil
{
return
application
.
ThrowError
(
application
.
TRANSACTION_ERROR
,
err
.
Error
())
}
if
err
:=
transactionContext
.
StartTransaction
();
err
!=
nil
{
return
application
.
ThrowError
(
application
.
TRANSACTION_ERROR
,
err
.
Error
())
}
defer
func
()
{
_
=
transactionContext
.
RollbackTransaction
()
}()
companyRepo
:=
factory
.
CreateCompanyRepository
(
map
[
string
]
interface
{}{
"transactionContext"
:
transactionContext
,
})
userRepo
:=
factory
.
CreateUserRepository
(
map
[
string
]
interface
{}{
"transactionContext"
:
transactionContext
,
})
_
,
companyList
,
err
:=
companyRepo
.
Find
(
map
[
string
]
interface
{}{
"limit"
:
1
,
"id"
:
param
.
Comapany
.
Id
,
})
if
err
!=
nil
{
return
err
}
_
,
userList
,
err
:=
userRepo
.
Find
(
map
[
string
]
interface
{}{
"limit"
:
1
,
"id"
:
param
.
User
.
Id
,
})
if
err
!=
nil
{
return
err
}
var
(
newCompany
*
domain
.
Company
newUser
*
domain
.
User
)
nowTime
:=
time
.
Now
()
if
len
(
companyList
)
>
0
{
newCompany
=
companyList
[
0
]
}
else
{
newCompany
=
&
domain
.
Company
{
CreateAt
:
nowTime
,
}
}
if
len
(
userList
)
>
0
{
newUser
=
userList
[
0
]
}
else
{
newUser
=
&
domain
.
User
{
CreateAt
:
nowTime
,
}
}
newCompany
.
Id
=
param
.
Comapany
.
Id
newCompany
.
Logo
=
param
.
Comapany
.
Logo
newCompany
.
Name
=
param
.
Comapany
.
Name
newCompany
.
Status
=
param
.
Comapany
.
Status
newCompany
.
UpdateAt
=
nowTime
func
(
c
CompanyServices
)
SetCompanyCharge
()
error
{
newUser
.
Id
=
param
.
User
.
Id
newUser
.
Account
=
param
.
User
.
Phone
newUser
.
AvatarUrl
=
param
.
User
.
Avatar
newUser
.
CompanyId
=
param
.
User
.
CompanyId
newUser
.
AdminType
=
param
.
User
.
AdminType
newUser
.
Name
=
param
.
User
.
Name
newUser
.
Status
=
param
.
User
.
Status
newUser
.
UpdateAt
=
nowTime
if
len
(
companyList
)
>
0
{
_
,
err
=
companyRepo
.
Update
(
newCompany
)
if
err
!=
nil
{
return
err
}
}
else
{
_
,
err
=
companyRepo
.
Insert
(
newCompany
)
if
err
!=
nil
{
return
err
}
}
if
len
(
userList
)
>
0
{
_
,
err
=
userRepo
.
Update
(
newUser
)
if
err
!=
nil
{
return
err
}
}
else
{
_
,
err
=
userRepo
.
Insert
(
newUser
)
if
err
!=
nil
{
return
err
}
}
if
err
:=
transactionContext
.
CommitTransaction
();
err
!=
nil
{
return
err
}
return
nil
}
...
...
pkg/application/factory/reposetory.go
0 → 100644
查看文件 @
ce9e76b
package
factory
import
(
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
pgDB
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/repository"
)
func
CreateTransactionContext
(
options
map
[
string
]
interface
{})
(
application
.
TransactionContext
,
error
)
{
return
pg
.
NewPGTransactionContext
(
pgDB
.
DB
),
nil
}
func
CreateCompanyRepository
(
options
map
[
string
]
interface
{})
domain
.
CompanyRepository
{
var
transactionContext
*
pg
.
TransactionContext
if
value
,
ok
:=
options
[
"transactionContext"
];
ok
{
transactionContext
=
value
.
(
*
pg
.
TransactionContext
)
}
return
repository
.
NewCompanyRepository
(
transactionContext
)
}
func
CreateUserRepository
(
options
map
[
string
]
interface
{})
domain
.
UserRepository
{
var
transactionContext
*
pg
.
TransactionContext
if
value
,
ok
:=
options
[
"transactionContext"
];
ok
{
transactionContext
=
value
.
(
*
pg
.
TransactionContext
)
}
return
repository
.
NewUserRepository
(
transactionContext
)
}
...
...
pkg/application/
company
/command/change_admin.go → pkg/application/
user
/command/change_admin.go
查看文件 @
ce9e76b
pkg/application/
company/command/update_company
.go → pkg/application/
user/command/save_user
.go
查看文件 @
ce9e76b
package
command
//UpdateCompanyCommand 数据来源 企业平台 推送的消息
type
UpdateCompanyCommand
struct
{
Id
int64
`json:"id,string"`
// 公司编号
Logo
string
`json:"logo"`
// 公司logo
Name
string
`json:"name"`
// 公司名称
Status
int
`json:"status"`
// 公司状态,1正常 2禁用
// 公司主管理员
User
struct
{
Id
int64
` json:"id"`
// 用户Id
Phone
string
`json:"phone"`
// 用户账号
Avatar
string
`json:"avatar"`
// 用户头像URL
CompanyId
int64
`json:"company_id"`
// 公司编号
AdminType
int
`json:"admin_type"`
// 1普通员工 2 主管理员
Name
string
`json:"name"`
// 用户姓名
Status
int
`json:"status"`
// 用户状态(1正常 2禁用)
}
`json:"user"`
type
SaveUserCommand
struct
{
Id
int64
` json:"id"`
// 用户Id
Phone
string
`json:"phone"`
// 用户账号
Avatar
string
`json:"avatar"`
// 用户头像URL
CompanyId
int64
`json:"company_id"`
// 公司编号
AdminType
int
`json:"admin_type"`
// 1普通员工 2 主管理员
Name
string
`json:"name"`
// 用户姓名
Status
int
`json:"status"`
// 用户状态(1正常 2禁用)
}
...
...
pkg/application/
company
/command/set_company_charge.go → pkg/application/
user
/command/set_company_charge.go
查看文件 @
ce9e76b
pkg/application/user/service.go
查看文件 @
ce9e76b
package
user
import
(
"time"
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user/command"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)
type
UserService
struct
{}
func
(
srv
UserService
)
CreateUser
()
{}
//AddUser
//从BusinessAdmins 接收消息 添加用户
func
(
srv
UserService
)
addUser
(
param
command
.
SaveUserCommand
)
error
{
transactionContext
,
err
:=
factory
.
CreateTransactionContext
(
nil
)
if
err
!=
nil
{
return
application
.
ThrowError
(
application
.
TRANSACTION_ERROR
,
err
.
Error
())
}
if
err
:=
transactionContext
.
StartTransaction
();
err
!=
nil
{
return
application
.
ThrowError
(
application
.
TRANSACTION_ERROR
,
err
.
Error
())
}
defer
func
()
{
_
=
transactionContext
.
RollbackTransaction
()
}()
nowTime
:=
time
.
Now
()
newUser
:=
domain
.
User
{
Id
:
param
.
Id
,
Account
:
param
.
Phone
,
AvatarUrl
:
param
.
Avatar
,
CompanyId
:
param
.
CompanyId
,
AdminType
:
param
.
AdminType
,
Name
:
param
.
Name
,
Status
:
param
.
Status
,
UpdateAt
:
nowTime
,
DeleteAt
:
nil
,
CreateAt
:
nowTime
,
}
userRepo
:=
factory
.
CreateUserRepository
(
map
[
string
]
interface
{}{
"transactionContext"
:
transactionContext
,
})
_
,
err
=
userRepo
.
Insert
(
&
newUser
)
if
err
!=
nil
{
return
err
}
if
err
:=
transactionContext
.
CommitTransaction
();
err
!=
nil
{
return
err
}
return
nil
}
//UpdateUser
//从BusinessAdmins 接收消息 更新用户
func
(
srv
UserService
)
updateUser
(
param
command
.
SaveUserCommand
)
error
{
transactionContext
,
err
:=
factory
.
CreateTransactionContext
(
nil
)
if
err
!=
nil
{
return
application
.
ThrowError
(
application
.
TRANSACTION_ERROR
,
err
.
Error
())
}
if
err
:=
transactionContext
.
StartTransaction
();
err
!=
nil
{
return
application
.
ThrowError
(
application
.
TRANSACTION_ERROR
,
err
.
Error
())
}
defer
func
()
{
_
=
transactionContext
.
RollbackTransaction
()
}()
userRepo
:=
factory
.
CreateUserRepository
(
map
[
string
]
interface
{}{
"transactionContext"
:
transactionContext
,
})
_
,
userList
,
err
:=
userRepo
.
Find
(
map
[
string
]
interface
{}{
"limit"
:
1
,
"id"
:
param
.
Id
,
})
if
err
!=
nil
{
return
err
}
var
(
newUser
*
domain
.
User
)
nowTime
:=
time
.
Now
()
if
len
(
userList
)
>
0
{
newUser
=
userList
[
0
]
}
else
{
newUser
=
&
domain
.
User
{
CreateAt
:
nowTime
,
}
}
newUser
.
Id
=
param
.
Id
newUser
.
Account
=
param
.
Phone
newUser
.
AvatarUrl
=
param
.
Avatar
newUser
.
CompanyId
=
param
.
CompanyId
newUser
.
AdminType
=
param
.
AdminType
newUser
.
Name
=
param
.
Name
newUser
.
Status
=
param
.
Status
newUser
.
UpdateAt
=
nowTime
if
len
(
userList
)
>
0
{
_
,
err
=
userRepo
.
Update
(
newUser
)
if
err
!=
nil
{
return
err
}
}
else
{
_
,
err
=
userRepo
.
Insert
(
newUser
)
if
err
!=
nil
{
return
err
}
}
if
err
:=
transactionContext
.
CommitTransaction
();
err
!=
nil
{
return
err
}
return
nil
}
//changeAdmin
//从BusinessAdmins 接收消息 变更主管
func
(
srv
UserService
)
changeAdmin
(
param
*
command
.
ChangeAdminCommand
)
error
{
transactionContext
,
err
:=
factory
.
CreateTransactionContext
(
nil
)
if
err
!=
nil
{
return
application
.
ThrowError
(
application
.
TRANSACTION_ERROR
,
err
.
Error
())
}
if
err
:=
transactionContext
.
StartTransaction
();
err
!=
nil
{
return
application
.
ThrowError
(
application
.
TRANSACTION_ERROR
,
err
.
Error
())
}
defer
func
()
{
_
=
transactionContext
.
RollbackTransaction
()
}()
if
err
:=
transactionContext
.
CommitTransaction
();
err
!=
nil
{
return
application
.
ThrowError
(
application
.
TRANSACTION_ERROR
,
err
.
Error
())
}
return
nil
}
func
(
srv
UserService
)
setCompanyCharge
(
param
*
command
.
ChangeAdminCommand
)
error
{
transactionContext
,
err
:=
factory
.
CreateTransactionContext
(
nil
)
if
err
!=
nil
{
return
application
.
ThrowError
(
application
.
TRANSACTION_ERROR
,
err
.
Error
())
}
if
err
:=
transactionContext
.
StartTransaction
();
err
!=
nil
{
return
application
.
ThrowError
(
application
.
TRANSACTION_ERROR
,
err
.
Error
())
}
defer
func
()
{
_
=
transactionContext
.
RollbackTransaction
()
}()
func
(
srv
UserService
)
UpdateUser
()
{}
if
err
:=
transactionContext
.
CommitTransaction
();
err
!=
nil
{
return
application
.
ThrowError
(
application
.
TRANSACTION_ERROR
,
err
.
Error
())
}
return
nil
}
...
...
pkg/domain/company.go
查看文件 @
ce9e76b
...
...
@@ -3,17 +3,18 @@ package domain
import
"time"
type
Company
struct
{
Id
int64
//公司编号
Logo
string
//公司logo
Name
string
//公司名称
Status
int
//公司状态,1正常 2禁用
UpdateAt
time
.
Time
//更新时间
CreatedAt
time
.
Time
//创建时间
DeleteAt
*
time
.
Time
Id
int64
//公司编号
Logo
string
//公司logo
Name
string
//公司名称
Status
int
//公司状态,1正常 2禁用
UpdateAt
time
.
Time
//更新时间
CreateAt
time
.
Time
//创建时间
DeleteAt
*
time
.
Time
}
type
CompanyRepository
interface
{
Save
(
company
*
Company
)
(
*
Company
,
error
)
Insert
(
company
*
Company
)
(
*
Company
,
error
)
Update
(
Company
*
Company
)
(
*
Company
,
error
)
Remove
(
company
*
Company
)
(
*
Company
,
error
)
FindOne
(
queryOptions
map
[
string
]
interface
{})
(
*
Company
,
error
)
Find
(
queryOptions
map
[
string
]
interface
{})
(
int
,
[]
*
Company
,
error
)
...
...
pkg/domain/user.go
查看文件 @
ce9e76b
...
...
@@ -3,22 +3,21 @@ package domain
import
"time"
type
User
struct
{
Id
int64
// 用户Id
Account
string
// 用户账号
AvatarUrl
string
// 用户头像URL
CompanyId
int64
// 公司编号
IsPrincipal
bool
// 是否公司负责人
AdminType
int
// 1普通员工 2 主管理员
Name
string
// 用户姓名
RoleId
int64
// 用户角色id
Status
int
// 用户状态(1正常 2禁用)
UpdateAt
time
.
Time
// 更新时间
DeleteAt
*
time
.
Time
CreateAt
time
.
Time
Id
int64
// 用户Id
Account
string
// 用户账号
AvatarUrl
string
// 用户头像URL
CompanyId
int64
// 公司编号
AdminType
int
// 1普通员工 2 主管理员
Name
string
// 用户姓名
Status
int
// 用户状态(1正常 2禁用)
UpdateAt
time
.
Time
// 更新时间
DeleteAt
*
time
.
Time
CreateAt
time
.
Time
}
type
UserRepository
interface
{
Save
(
user
*
User
)
(
*
User
,
error
)
Insert
(
user
*
User
)
(
*
User
,
error
)
Update
(
user
*
User
)
(
*
User
,
error
)
Remove
(
user
*
User
)
(
*
User
,
error
)
FindOne
(
queryOptions
map
[
string
]
interface
{})
(
*
User
,
error
)
Find
(
queryOptions
map
[
string
]
interface
{})
(
int
,
[]
*
User
,
error
)
...
...
pkg/infrastructure/pg/models/user.go
查看文件 @
ce9e76b
...
...
@@ -3,17 +3,15 @@ package models
import
"time"
type
User
struct
{
tableName
struct
{}
`pg:"user"`
Id
int64
`pg:"pk:id"`
// 用户Id
Account
string
// 用户账号
AvatarUrl
string
// 用户头像URL
CompanyId
int64
// 公司编号
IsPrincipal
bool
// 是否公司负责人
AdminType
int
// 1普通员工 2 主管理员
Name
string
// 用户姓名
RoleId
int64
// 用户角色id
Status
int
// 用户状态(1正常 2禁用)
UpdateAt
time
.
Time
// 更新时间
CreateAt
time
.
Time
// 创建时间
DeleteAt
*
time
.
Time
// 删除时间
tableName
struct
{}
`pg:"user"`
Id
int64
`pg:"pk:id"`
// 用户Id
Account
string
// 用户账号
AvatarUrl
string
// 用户头像URL
CompanyId
int64
// 公司编号
AdminType
int
// 1普通员工 2 主管理员
Name
string
// 用户姓名
Status
int
// 用户状态(1正常 2禁用)
UpdateAt
time
.
Time
// 更新时间
CreateAt
time
.
Time
// 创建时间
DeleteAt
*
time
.
Time
// 删除时间
}
...
...
pkg/infrastructure/repository/pg_company_repository.go
查看文件 @
ce9e76b
package
repository
import
(
"errors"
"time"
"github.com/go-pg/pg/v10"
...
...
@@ -16,28 +15,45 @@ type CompanyRepository struct {
var
_
domain
.
CompanyRepository
=
(
*
CompanyRepository
)(
nil
)
func
(
repo
*
CompanyRepository
)
Save
(
u
*
domain
.
Company
)
(
*
domain
.
Company
,
error
)
{
func
NewCompanyRepository
(
tx
*
pgTransaction
.
TransactionContext
)
*
CompanyRepository
{
return
&
CompanyRepository
{
transactionContext
:
tx
,
}
}
func
(
repo
*
CompanyRepository
)
Insert
(
u
*
domain
.
Company
)
(
*
domain
.
Company
,
error
)
{
companyModel
:=
models
.
Company
{
Id
:
u
.
Id
,
Logo
:
u
.
Logo
,
Name
:
u
.
Name
,
Status
:
u
.
Status
,
UpdateAt
:
u
.
UpdateAt
,
CreateAt
:
u
.
Create
d
At
,
CreateAt
:
u
.
CreateAt
,
DeleteAt
:
u
.
DeleteAt
,
}
tx
:=
repo
.
transactionContext
.
PgTx
if
companyModel
.
Id
==
0
{
_
,
err
:=
tx
.
Model
(
&
companyModel
)
.
Insert
()
if
err
!=
nil
{
return
nil
,
err
}
u
.
Id
=
companyModel
.
Id
}
else
{
_
,
err
:=
tx
.
Model
(
&
companyModel
)
.
WherePK
()
.
Update
()
if
err
!=
nil
{
return
nil
,
err
}
_
,
err
:=
tx
.
Model
(
&
companyModel
)
.
Insert
()
if
err
!=
nil
{
return
nil
,
err
}
u
.
Id
=
companyModel
.
Id
return
u
,
nil
}
func
(
repo
*
CompanyRepository
)
Update
(
u
*
domain
.
Company
)
(
*
domain
.
Company
,
error
)
{
companyModel
:=
models
.
Company
{
Id
:
u
.
Id
,
Logo
:
u
.
Logo
,
Name
:
u
.
Name
,
Status
:
u
.
Status
,
UpdateAt
:
u
.
UpdateAt
,
CreateAt
:
u
.
CreateAt
,
DeleteAt
:
u
.
DeleteAt
,
}
tx
:=
repo
.
transactionContext
.
PgTx
_
,
err
:=
tx
.
Model
(
&
companyModel
)
.
WherePK
()
.
Update
()
if
err
!=
nil
{
return
nil
,
err
}
return
u
,
nil
}
...
...
@@ -45,7 +61,7 @@ func (repo *CompanyRepository) Save(u *domain.Company) (*domain.Company, error)
func
(
repo
*
CompanyRepository
)
Remove
(
u
*
domain
.
Company
)
(
*
domain
.
Company
,
error
)
{
nowTime
:=
time
.
Now
()
u
.
DeleteAt
=
&
nowTime
_
,
err
:=
repo
.
Sav
e
(
u
)
_
,
err
:=
repo
.
Updat
e
(
u
)
return
u
,
err
}
...
...
@@ -58,7 +74,7 @@ func (repo *CompanyRepository) FindOne(queryOptions map[string]interface{}) (*do
}
err
:=
query
.
First
()
if
err
==
pg
.
ErrNoRows
{
return
nil
,
errors
.
New
(
"company 不存在"
)
return
nil
,
ErrNoRows
}
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -72,6 +88,9 @@ func (repo *CompanyRepository) Find(queryOptions map[string]interface{}) (int, [
companyModel
:=
[]
models
.
Company
{}
query
:=
tx
.
Model
(
&
companyModel
)
.
Limit
(
20
)
if
v
,
ok
:=
queryOptions
[
"id"
];
ok
{
query
.
Where
(
"id=?"
,
v
)
}
if
v
,
ok
:=
queryOptions
[
"limit"
];
ok
{
query
.
Limit
(
v
.
(
int
))
}
...
...
@@ -92,12 +111,12 @@ func (repo *CompanyRepository) Find(queryOptions map[string]interface{}) (int, [
func
(
repo
*
CompanyRepository
)
TransformToCompanyDomain
(
m
*
models
.
Company
)
*
domain
.
Company
{
return
&
domain
.
Company
{
Id
:
m
.
Id
,
Logo
:
m
.
Logo
,
Name
:
m
.
Name
,
Status
:
m
.
Status
,
UpdateAt
:
m
.
UpdateAt
,
CreatedAt
:
m
.
CreateAt
,
DeleteAt
:
m
.
DeleteAt
,
Id
:
m
.
Id
,
Logo
:
m
.
Logo
,
Name
:
m
.
Name
,
Status
:
m
.
Status
,
UpdateAt
:
m
.
UpdateAt
,
CreateAt
:
m
.
CreateAt
,
DeleteAt
:
m
.
DeleteAt
,
}
}
...
...
pkg/infrastructure/repository/pg_user_repository.go
查看文件 @
ce9e76b
package
repository
import
(
"errors"
"time"
"github.com/go-pg/pg/v10"
...
...
@@ -16,33 +15,51 @@ type UserRepository struct {
var
_
domain
.
UserRepository
=
(
*
UserRepository
)(
nil
)
func
(
repo
*
UserRepository
)
Save
(
user
*
domain
.
User
)
(
*
domain
.
User
,
error
)
{
func
NewUserRepository
(
tx
*
pgTransaction
.
TransactionContext
)
*
UserRepository
{
return
&
UserRepository
{
transactionContext
:
tx
,
}
}
func
(
repo
*
UserRepository
)
Insert
(
user
*
domain
.
User
)
(
*
domain
.
User
,
error
)
{
userModel
:=
models
.
User
{
Id
:
user
.
Id
,
Account
:
user
.
Account
,
AvatarUrl
:
user
.
AvatarUrl
,
CompanyId
:
user
.
CompanyId
,
IsPrincipal
:
user
.
IsPrincipal
,
AdminType
:
user
.
AdminType
,
Name
:
user
.
Name
,
RoleId
:
user
.
RoleId
,
Status
:
user
.
Status
,
UpdateAt
:
user
.
UpdateAt
,
CreateAt
:
user
.
CreateAt
,
DeleteAt
:
user
.
DeleteAt
,
Id
:
user
.
Id
,
Account
:
user
.
Account
,
AvatarUrl
:
user
.
AvatarUrl
,
CompanyId
:
user
.
CompanyId
,
AdminType
:
user
.
AdminType
,
Name
:
user
.
Name
,
Status
:
user
.
Status
,
UpdateAt
:
user
.
UpdateAt
,
CreateAt
:
user
.
CreateAt
,
DeleteAt
:
user
.
DeleteAt
,
}
tx
:=
repo
.
transactionContext
.
PgTx
if
userModel
.
Id
==
0
{
_
,
err
:=
tx
.
Model
(
&
userModel
)
.
Insert
()
if
err
!=
nil
{
return
nil
,
err
}
user
.
Id
=
userModel
.
Id
}
else
{
_
,
err
:=
tx
.
Model
(
&
userModel
)
.
WherePK
()
.
Update
()
if
err
!=
nil
{
return
nil
,
err
}
_
,
err
:=
tx
.
Model
(
&
userModel
)
.
Insert
()
if
err
!=
nil
{
return
nil
,
err
}
user
.
Id
=
userModel
.
Id
return
user
,
nil
}
func
(
repo
*
UserRepository
)
Update
(
user
*
domain
.
User
)
(
*
domain
.
User
,
error
)
{
userModel
:=
models
.
User
{
Id
:
user
.
Id
,
Account
:
user
.
Account
,
AvatarUrl
:
user
.
AvatarUrl
,
CompanyId
:
user
.
CompanyId
,
AdminType
:
user
.
AdminType
,
Name
:
user
.
Name
,
Status
:
user
.
Status
,
UpdateAt
:
user
.
UpdateAt
,
CreateAt
:
user
.
CreateAt
,
DeleteAt
:
user
.
DeleteAt
,
}
tx
:=
repo
.
transactionContext
.
PgTx
_
,
err
:=
tx
.
Model
(
&
userModel
)
.
WherePK
()
.
Update
()
if
err
!=
nil
{
return
nil
,
err
}
return
user
,
nil
}
...
...
@@ -50,7 +67,7 @@ func (repo *UserRepository) Save(user *domain.User) (*domain.User, error) {
func
(
repo
*
UserRepository
)
Remove
(
user
*
domain
.
User
)
(
*
domain
.
User
,
error
)
{
nowTime
:=
time
.
Now
()
user
.
DeleteAt
=
&
nowTime
_
,
err
:=
repo
.
Sav
e
(
user
)
_
,
err
:=
repo
.
Updat
e
(
user
)
return
user
,
err
}
...
...
@@ -63,7 +80,7 @@ func (repo *UserRepository) FindOne(queryOptions map[string]interface{}) (*domai
}
err
:=
query
.
First
()
if
err
==
pg
.
ErrNoRows
{
return
nil
,
errors
.
New
(
"user 不存在"
)
return
nil
,
ErrNoRows
}
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -77,6 +94,9 @@ func (repo *UserRepository) Find(queryOptions map[string]interface{}) (int, []*d
userModel
:=
[]
models
.
User
{}
query
:=
tx
.
Model
(
&
userModel
)
.
Limit
(
20
)
if
v
,
ok
:=
queryOptions
[
"id"
];
ok
{
query
.
Where
(
"id=?"
,
v
)
}
if
v
,
ok
:=
queryOptions
[
"limit"
];
ok
{
query
.
Limit
(
v
.
(
int
))
}
...
...
@@ -97,17 +117,15 @@ func (repo *UserRepository) Find(queryOptions map[string]interface{}) (int, []*d
func
(
repo
*
UserRepository
)
TransformToUserDomain
(
user
*
models
.
User
)
*
domain
.
User
{
return
&
domain
.
User
{
Id
:
user
.
Id
,
Account
:
user
.
Account
,
AvatarUrl
:
user
.
AvatarUrl
,
CompanyId
:
user
.
CompanyId
,
IsPrincipal
:
user
.
IsPrincipal
,
AdminType
:
user
.
AdminType
,
Name
:
user
.
Name
,
RoleId
:
user
.
RoleId
,
Status
:
user
.
Status
,
UpdateAt
:
user
.
UpdateAt
,
CreateAt
:
user
.
CreateAt
,
DeleteAt
:
user
.
DeleteAt
,
Id
:
user
.
Id
,
Account
:
user
.
Account
,
AvatarUrl
:
user
.
AvatarUrl
,
CompanyId
:
user
.
CompanyId
,
AdminType
:
user
.
AdminType
,
Name
:
user
.
Name
,
Status
:
user
.
Status
,
UpdateAt
:
user
.
UpdateAt
,
CreateAt
:
user
.
CreateAt
,
DeleteAt
:
user
.
DeleteAt
,
}
}
...
...
pkg/infrastructure/repository/var.go
0 → 100644
查看文件 @
ce9e76b
package
repository
import
"github.com/go-pg/pg/v10"
var
ErrNoRows
=
pg
.
ErrNoRows
...
...
请
注册
或
登录
后发表评论