作者 tangxvhui

更新

@@ -2,7 +2,7 @@ package command @@ -2,7 +2,7 @@ package command
2 2
3 //AddCompanyCommand 数据来源 企业平台 推送的消息 3 //AddCompanyCommand 数据来源 企业平台 推送的消息
4 //新建公司 4 //新建公司
5 -type AddCompanyCommand struct { 5 +type SaveCompanyCommand struct {
6 //新添加的公司 6 //新添加的公司
7 Comapany struct { 7 Comapany struct {
8 Id int64 `json:"id" ` 8 Id int64 `json:"id" `
@@ -10,7 +10,7 @@ type AddCompanyCommand struct { @@ -10,7 +10,7 @@ type AddCompanyCommand struct {
10 Name string `json:"name"` // 公司名称 10 Name string `json:"name"` // 公司名称
11 Status int `json:"status"` // 公司状态,1正常 2禁用 11 Status int `json:"status"` // 公司状态,1正常 2禁用
12 } `json:"company"` 12 } `json:"company"`
13 - //新添加公司时, 新建的公司主管理员 13 + //新添加公司时,第一個用户就是主管理员
14 User struct { 14 User struct {
15 Id int64 ` json:"id"` // 用户Id 15 Id int64 ` json:"id"` // 用户Id
16 Phone string `json:"phone"` // 用户账号 16 Phone string `json:"phone"` // 用户账号
1 package company 1 package company
2 2
  3 +import (
  4 + "time"
  5 +
  6 + "github.com/linmadan/egglib-go/core/application"
  7 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/company/command"
  8 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
  9 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
  10 +)
  11 +
3 type CompanyServices struct { 12 type CompanyServices struct {
4 } 13 }
5 14
6 //从BusinessAdmins 接收消息,变更公司数据 15 //从BusinessAdmins 接收消息,变更公司数据
  16 +//
7 func (c CompanyServices) BusinessAdminCompany() error { 17 func (c CompanyServices) BusinessAdminCompany() error {
8 return nil 18 return nil
9 } 19 }
10 20
11 -func (c CompanyServices) AddCompany() error {  
12 - return nil  
13 -} 21 +//addCompany
  22 +//从BusinessAdmins 接收消息 添加公司
  23 +func (c CompanyServices) addCompany(param *command.SaveCompanyCommand) error {
  24 + transactionContext, err := factory.CreateTransactionContext(nil)
  25 + if err != nil {
  26 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  27 + }
  28 + if err := transactionContext.StartTransaction(); err != nil {
  29 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  30 + }
  31 + defer func() {
  32 + _ = transactionContext.RollbackTransaction()
  33 + }()
  34 + nowTime := time.Now()
  35 + newCompany := domain.Company{
  36 + Id: param.Comapany.Id,
  37 + Logo: param.Comapany.Logo,
  38 + Name: param.Comapany.Name,
  39 + Status: param.Comapany.Status,
  40 + UpdateAt: nowTime,
  41 + CreateAt: nowTime,
  42 + DeleteAt: nil,
  43 + }
14 44
15 -func (c CompanyServices) EditCompany() error { 45 + newUser := domain.User{
  46 + Id: param.User.Id,
  47 + Account: param.User.Phone,
  48 + AvatarUrl: param.User.Avatar,
  49 + CompanyId: param.User.CompanyId,
  50 + AdminType: param.User.AdminType,
  51 + Name: param.User.Name,
  52 + Status: param.User.Status,
  53 + UpdateAt: nowTime,
  54 + DeleteAt: nil,
  55 + CreateAt: nowTime,
  56 + }
  57 + companyRepo := factory.CreateCompanyRepository(map[string]interface{}{
  58 + "transactionContext": transactionContext,
  59 + })
  60 + userRepo := factory.CreateUserRepository(map[string]interface{}{
  61 + "transactionContext": transactionContext,
  62 + })
  63 + _, err = companyRepo.Insert(&newCompany)
  64 + if err != nil {
  65 + return err
  66 + }
  67 + _, err = userRepo.Insert(&newUser)
  68 + if err != nil {
  69 + return err
  70 + }
  71 + if err := transactionContext.CommitTransaction(); err != nil {
  72 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  73 + }
16 return nil 74 return nil
17 } 75 }
18 76
19 -func (c CompanyServices) ChangeAdmin() error {  
20 - return nil  
21 -} 77 +//editCompany
  78 +//从BusinessAdmins 接收消息 更新公司
  79 +func (c CompanyServices) editCompany(param *command.SaveCompanyCommand) error {
  80 + transactionContext, err := factory.CreateTransactionContext(nil)
  81 + if err != nil {
  82 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  83 + }
  84 + if err := transactionContext.StartTransaction(); err != nil {
  85 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  86 + }
  87 + defer func() {
  88 + _ = transactionContext.RollbackTransaction()
  89 + }()
  90 + companyRepo := factory.CreateCompanyRepository(map[string]interface{}{
  91 + "transactionContext": transactionContext,
  92 + })
  93 + userRepo := factory.CreateUserRepository(map[string]interface{}{
  94 + "transactionContext": transactionContext,
  95 + })
  96 +
  97 + _, companyList, err := companyRepo.Find(map[string]interface{}{
  98 + "limit": 1,
  99 + "id": param.Comapany.Id,
  100 + })
  101 + if err != nil {
  102 + return err
  103 + }
  104 + _, userList, err := userRepo.Find(map[string]interface{}{
  105 + "limit": 1,
  106 + "id": param.User.Id,
  107 + })
  108 + if err != nil {
  109 + return err
  110 + }
  111 + var (
  112 + newCompany *domain.Company
  113 + newUser *domain.User
  114 + )
  115 + nowTime := time.Now()
  116 + if len(companyList) > 0 {
  117 + newCompany = companyList[0]
  118 + } else {
  119 + newCompany = &domain.Company{
  120 + CreateAt: nowTime,
  121 + }
  122 + }
  123 + if len(userList) > 0 {
  124 + newUser = userList[0]
  125 + } else {
  126 + newUser = &domain.User{
  127 + CreateAt: nowTime,
  128 + }
  129 + }
  130 +
  131 + newCompany.Id = param.Comapany.Id
  132 + newCompany.Logo = param.Comapany.Logo
  133 + newCompany.Name = param.Comapany.Name
  134 + newCompany.Status = param.Comapany.Status
  135 + newCompany.UpdateAt = nowTime
22 136
23 -func (c CompanyServices) SetCompanyCharge() error { 137 + newUser.Id = param.User.Id
  138 + newUser.Account = param.User.Phone
  139 + newUser.AvatarUrl = param.User.Avatar
  140 + newUser.CompanyId = param.User.CompanyId
  141 + newUser.AdminType = param.User.AdminType
  142 + newUser.Name = param.User.Name
  143 + newUser.Status = param.User.Status
  144 + newUser.UpdateAt = nowTime
  145 + if len(companyList) > 0 {
  146 + _, err = companyRepo.Update(newCompany)
  147 + if err != nil {
  148 + return err
  149 + }
  150 + } else {
  151 + _, err = companyRepo.Insert(newCompany)
  152 + if err != nil {
  153 + return err
  154 + }
  155 + }
  156 + if len(userList) > 0 {
  157 + _, err = userRepo.Update(newUser)
  158 + if err != nil {
  159 + return err
  160 + }
  161 + } else {
  162 + _, err = userRepo.Insert(newUser)
  163 + if err != nil {
  164 + return err
  165 + }
  166 + }
  167 + if err := transactionContext.CommitTransaction(); err != nil {
  168 + return err
  169 + }
24 return nil 170 return nil
25 } 171 }
  1 +package factory
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/core/application"
  5 + "github.com/linmadan/egglib-go/transaction/pg"
  6 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
  7 + pgDB "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg"
  8 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/repository"
  9 +)
  10 +
  11 +func CreateTransactionContext(options map[string]interface{}) (application.TransactionContext, error) {
  12 + return pg.NewPGTransactionContext(pgDB.DB), nil
  13 +}
  14 +
  15 +func CreateCompanyRepository(options map[string]interface{}) domain.CompanyRepository {
  16 + var transactionContext *pg.TransactionContext
  17 + if value, ok := options["transactionContext"]; ok {
  18 + transactionContext = value.(*pg.TransactionContext)
  19 + }
  20 + return repository.NewCompanyRepository(transactionContext)
  21 +}
  22 +
  23 +func CreateUserRepository(options map[string]interface{}) domain.UserRepository {
  24 + var transactionContext *pg.TransactionContext
  25 + if value, ok := options["transactionContext"]; ok {
  26 + transactionContext = value.(*pg.TransactionContext)
  27 + }
  28 + return repository.NewUserRepository(transactionContext)
  29 +}
1 package command 1 package command
2 2
3 -//UpdateCompanyCommand 数据来源 企业平台 推送的消息  
4 -type UpdateCompanyCommand struct {  
5 - Id int64 `json:"id,string"` // 公司编号  
6 - Logo string `json:"logo"` // 公司logo  
7 - Name string `json:"name"` // 公司名称  
8 - Status int `json:"status"` // 公司状态,1正常 2禁用  
9 - // 公司主管理员  
10 - User struct {  
11 - Id int64 ` json:"id"` // 用户Id  
12 - Phone string `json:"phone"` // 用户账号  
13 - Avatar string `json:"avatar"` // 用户头像URL  
14 - CompanyId int64 `json:"company_id"` // 公司编号  
15 - AdminType int `json:"admin_type"` // 1普通员工 2 主管理员  
16 - Name string `json:"name"` // 用户姓名  
17 - Status int `json:"status"` // 用户状态(1正常 2禁用)  
18 - } `json:"user"` 3 +type SaveUserCommand struct {
  4 + Id int64 ` json:"id"` // 用户Id
  5 + Phone string `json:"phone"` // 用户账号
  6 + Avatar string `json:"avatar"` // 用户头像URL
  7 + CompanyId int64 `json:"company_id"` // 公司编号
  8 + AdminType int `json:"admin_type"` // 1普通员工 2 主管理员
  9 + Name string `json:"name"` // 用户姓名
  10 + Status int `json:"status"` // 用户状态(1正常 2禁用)
19 } 11 }
1 package user 1 package user
2 2
  3 +import (
  4 + "time"
  5 +
  6 + "github.com/linmadan/egglib-go/core/application"
  7 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
  8 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user/command"
  9 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
  10 +)
  11 +
3 type UserService struct{} 12 type UserService struct{}
4 13
5 -func (srv UserService) CreateUser() {} 14 +//AddUser
  15 +//从BusinessAdmins 接收消息 添加用户
  16 +func (srv UserService) addUser(param command.SaveUserCommand) error {
  17 + transactionContext, err := factory.CreateTransactionContext(nil)
  18 + if err != nil {
  19 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  20 + }
  21 + if err := transactionContext.StartTransaction(); err != nil {
  22 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  23 + }
  24 + defer func() {
  25 + _ = transactionContext.RollbackTransaction()
  26 + }()
  27 + nowTime := time.Now()
  28 + newUser := domain.User{
  29 + Id: param.Id,
  30 + Account: param.Phone,
  31 + AvatarUrl: param.Avatar,
  32 + CompanyId: param.CompanyId,
  33 + AdminType: param.AdminType,
  34 + Name: param.Name,
  35 + Status: param.Status,
  36 + UpdateAt: nowTime,
  37 + DeleteAt: nil,
  38 + CreateAt: nowTime,
  39 + }
  40 + userRepo := factory.CreateUserRepository(map[string]interface{}{
  41 + "transactionContext": transactionContext,
  42 + })
  43 + _, err = userRepo.Insert(&newUser)
  44 + if err != nil {
  45 + return err
  46 + }
  47 + if err := transactionContext.CommitTransaction(); err != nil {
  48 + return err
  49 + }
  50 + return nil
  51 +}
  52 +
  53 +//UpdateUser
  54 +//从BusinessAdmins 接收消息 更新用户
  55 +func (srv UserService) updateUser(param command.SaveUserCommand) error {
  56 + transactionContext, err := factory.CreateTransactionContext(nil)
  57 + if err != nil {
  58 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  59 + }
  60 + if err := transactionContext.StartTransaction(); err != nil {
  61 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  62 + }
  63 + defer func() {
  64 + _ = transactionContext.RollbackTransaction()
  65 + }()
  66 + userRepo := factory.CreateUserRepository(map[string]interface{}{
  67 + "transactionContext": transactionContext,
  68 + })
  69 + _, userList, err := userRepo.Find(map[string]interface{}{
  70 + "limit": 1,
  71 + "id": param.Id,
  72 + })
  73 + if err != nil {
  74 + return err
  75 + }
  76 + var (
  77 + newUser *domain.User
  78 + )
  79 + nowTime := time.Now()
  80 + if len(userList) > 0 {
  81 + newUser = userList[0]
  82 + } else {
  83 + newUser = &domain.User{
  84 + CreateAt: nowTime,
  85 + }
  86 + }
  87 + newUser.Id = param.Id
  88 + newUser.Account = param.Phone
  89 + newUser.AvatarUrl = param.Avatar
  90 + newUser.CompanyId = param.CompanyId
  91 + newUser.AdminType = param.AdminType
  92 + newUser.Name = param.Name
  93 + newUser.Status = param.Status
  94 + newUser.UpdateAt = nowTime
  95 + if len(userList) > 0 {
  96 + _, err = userRepo.Update(newUser)
  97 + if err != nil {
  98 + return err
  99 + }
  100 + } else {
  101 + _, err = userRepo.Insert(newUser)
  102 + if err != nil {
  103 + return err
  104 + }
  105 + }
  106 + if err := transactionContext.CommitTransaction(); err != nil {
  107 + return err
  108 + }
  109 + return nil
  110 +}
  111 +
  112 +//changeAdmin
  113 +//从BusinessAdmins 接收消息 变更主管
  114 +func (srv UserService) changeAdmin(param *command.ChangeAdminCommand) error {
  115 + transactionContext, err := factory.CreateTransactionContext(nil)
  116 + if err != nil {
  117 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  118 + }
  119 + if err := transactionContext.StartTransaction(); err != nil {
  120 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  121 + }
  122 + defer func() {
  123 + _ = transactionContext.RollbackTransaction()
  124 + }()
  125 +
  126 + if err := transactionContext.CommitTransaction(); err != nil {
  127 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  128 + }
  129 + return nil
  130 +}
  131 +
  132 +func (srv UserService) setCompanyCharge(param *command.ChangeAdminCommand) error {
  133 + transactionContext, err := factory.CreateTransactionContext(nil)
  134 + if err != nil {
  135 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  136 + }
  137 + if err := transactionContext.StartTransaction(); err != nil {
  138 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  139 + }
  140 + defer func() {
  141 + _ = transactionContext.RollbackTransaction()
  142 + }()
6 143
7 -func (srv UserService) UpdateUser() {} 144 + if err := transactionContext.CommitTransaction(); err != nil {
  145 + return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  146 + }
  147 + return nil
  148 +}
@@ -3,17 +3,18 @@ package domain @@ -3,17 +3,18 @@ package domain
3 import "time" 3 import "time"
4 4
5 type Company struct { 5 type Company struct {
6 - Id int64 //公司编号  
7 - Logo string //公司logo  
8 - Name string //公司名称  
9 - Status int //公司状态,1正常 2禁用  
10 - UpdateAt time.Time //更新时间  
11 - CreatedAt time.Time //创建时间  
12 - DeleteAt *time.Time 6 + Id int64 //公司编号
  7 + Logo string //公司logo
  8 + Name string //公司名称
  9 + Status int //公司状态,1正常 2禁用
  10 + UpdateAt time.Time //更新时间
  11 + CreateAt time.Time //创建时间
  12 + DeleteAt *time.Time
13 } 13 }
14 14
15 type CompanyRepository interface { 15 type CompanyRepository interface {
16 - Save(company *Company) (*Company, error) 16 + Insert(company *Company) (*Company, error)
  17 + Update(Company *Company) (*Company, error)
17 Remove(company *Company) (*Company, error) 18 Remove(company *Company) (*Company, error)
18 FindOne(queryOptions map[string]interface{}) (*Company, error) 19 FindOne(queryOptions map[string]interface{}) (*Company, error)
19 Find(queryOptions map[string]interface{}) (int, []*Company, error) 20 Find(queryOptions map[string]interface{}) (int, []*Company, error)
@@ -3,22 +3,21 @@ package domain @@ -3,22 +3,21 @@ package domain
3 import "time" 3 import "time"
4 4
5 type User struct { 5 type User struct {
6 - Id int64 // 用户Id  
7 - Account string // 用户账号  
8 - AvatarUrl string // 用户头像URL  
9 - CompanyId int64 // 公司编号  
10 - IsPrincipal bool // 是否公司负责人  
11 - AdminType int // 1普通员工 2 主管理员  
12 - Name string // 用户姓名  
13 - RoleId int64 // 用户角色id  
14 - Status int // 用户状态(1正常 2禁用)  
15 - UpdateAt time.Time // 更新时间  
16 - DeleteAt *time.Time  
17 - CreateAt time.Time 6 + Id int64 // 用户Id
  7 + Account string // 用户账号
  8 + AvatarUrl string // 用户头像URL
  9 + CompanyId int64 // 公司编号
  10 + AdminType int // 1普通员工 2 主管理员
  11 + Name string // 用户姓名
  12 + Status int // 用户状态(1正常 2禁用)
  13 + UpdateAt time.Time // 更新时间
  14 + DeleteAt *time.Time
  15 + CreateAt time.Time
18 } 16 }
19 17
20 type UserRepository interface { 18 type UserRepository interface {
21 - Save(user *User) (*User, error) 19 + Insert(user *User) (*User, error)
  20 + Update(user *User) (*User, error)
22 Remove(user *User) (*User, error) 21 Remove(user *User) (*User, error)
23 FindOne(queryOptions map[string]interface{}) (*User, error) 22 FindOne(queryOptions map[string]interface{}) (*User, error)
24 Find(queryOptions map[string]interface{}) (int, []*User, error) 23 Find(queryOptions map[string]interface{}) (int, []*User, error)
@@ -3,17 +3,15 @@ package models @@ -3,17 +3,15 @@ package models
3 import "time" 3 import "time"
4 4
5 type User struct { 5 type User struct {
6 - tableName struct{} `pg:"user"`  
7 - Id int64 `pg:"pk:id"` // 用户Id  
8 - Account string // 用户账号  
9 - AvatarUrl string // 用户头像URL  
10 - CompanyId int64 // 公司编号  
11 - IsPrincipal bool // 是否公司负责人  
12 - AdminType int // 1普通员工 2 主管理员  
13 - Name string // 用户姓名  
14 - RoleId int64 // 用户角色id  
15 - Status int // 用户状态(1正常 2禁用)  
16 - UpdateAt time.Time // 更新时间  
17 - CreateAt time.Time // 创建时间  
18 - DeleteAt *time.Time // 删除时间 6 + tableName struct{} `pg:"user"`
  7 + Id int64 `pg:"pk:id"` // 用户Id
  8 + Account string // 用户账号
  9 + AvatarUrl string // 用户头像URL
  10 + CompanyId int64 // 公司编号
  11 + AdminType int // 1普通员工 2 主管理员
  12 + Name string // 用户姓名
  13 + Status int // 用户状态(1正常 2禁用)
  14 + UpdateAt time.Time // 更新时间
  15 + CreateAt time.Time // 创建时间
  16 + DeleteAt *time.Time // 删除时间
19 } 17 }
1 package repository 1 package repository
2 2
3 import ( 3 import (
4 - "errors"  
5 "time" 4 "time"
6 5
7 "github.com/go-pg/pg/v10" 6 "github.com/go-pg/pg/v10"
@@ -16,28 +15,45 @@ type CompanyRepository struct { @@ -16,28 +15,45 @@ type CompanyRepository struct {
16 15
17 var _ domain.CompanyRepository = (*CompanyRepository)(nil) 16 var _ domain.CompanyRepository = (*CompanyRepository)(nil)
18 17
19 -func (repo *CompanyRepository) Save(u *domain.Company) (*domain.Company, error) { 18 +func NewCompanyRepository(tx *pgTransaction.TransactionContext) *CompanyRepository {
  19 + return &CompanyRepository{
  20 + transactionContext: tx,
  21 + }
  22 +}
  23 +
  24 +func (repo *CompanyRepository) Insert(u *domain.Company) (*domain.Company, error) {
20 companyModel := models.Company{ 25 companyModel := models.Company{
21 Id: u.Id, 26 Id: u.Id,
22 Logo: u.Logo, 27 Logo: u.Logo,
23 Name: u.Name, 28 Name: u.Name,
24 Status: u.Status, 29 Status: u.Status,
25 UpdateAt: u.UpdateAt, 30 UpdateAt: u.UpdateAt,
26 - CreateAt: u.CreatedAt, 31 + CreateAt: u.CreateAt,
27 DeleteAt: u.DeleteAt, 32 DeleteAt: u.DeleteAt,
28 } 33 }
29 tx := repo.transactionContext.PgTx 34 tx := repo.transactionContext.PgTx
30 - if companyModel.Id == 0 {  
31 - _, err := tx.Model(&companyModel).Insert()  
32 - if err != nil {  
33 - return nil, err  
34 - }  
35 - u.Id = companyModel.Id  
36 - } else {  
37 - _, err := tx.Model(&companyModel).WherePK().Update()  
38 - if err != nil {  
39 - return nil, err  
40 - } 35 + _, err := tx.Model(&companyModel).Insert()
  36 + if err != nil {
  37 + return nil, err
  38 + }
  39 + u.Id = companyModel.Id
  40 + return u, nil
  41 +}
  42 +
  43 +func (repo *CompanyRepository) Update(u *domain.Company) (*domain.Company, error) {
  44 + companyModel := models.Company{
  45 + Id: u.Id,
  46 + Logo: u.Logo,
  47 + Name: u.Name,
  48 + Status: u.Status,
  49 + UpdateAt: u.UpdateAt,
  50 + CreateAt: u.CreateAt,
  51 + DeleteAt: u.DeleteAt,
  52 + }
  53 + tx := repo.transactionContext.PgTx
  54 + _, err := tx.Model(&companyModel).WherePK().Update()
  55 + if err != nil {
  56 + return nil, err
41 } 57 }
42 return u, nil 58 return u, nil
43 } 59 }
@@ -45,7 +61,7 @@ func (repo *CompanyRepository) Save(u *domain.Company) (*domain.Company, error) @@ -45,7 +61,7 @@ func (repo *CompanyRepository) Save(u *domain.Company) (*domain.Company, error)
45 func (repo *CompanyRepository) Remove(u *domain.Company) (*domain.Company, error) { 61 func (repo *CompanyRepository) Remove(u *domain.Company) (*domain.Company, error) {
46 nowTime := time.Now() 62 nowTime := time.Now()
47 u.DeleteAt = &nowTime 63 u.DeleteAt = &nowTime
48 - _, err := repo.Save(u) 64 + _, err := repo.Update(u)
49 return u, err 65 return u, err
50 } 66 }
51 67
@@ -58,7 +74,7 @@ func (repo *CompanyRepository) FindOne(queryOptions map[string]interface{}) (*do @@ -58,7 +74,7 @@ func (repo *CompanyRepository) FindOne(queryOptions map[string]interface{}) (*do
58 } 74 }
59 err := query.First() 75 err := query.First()
60 if err == pg.ErrNoRows { 76 if err == pg.ErrNoRows {
61 - return nil, errors.New("company 不存在") 77 + return nil, ErrNoRows
62 } 78 }
63 if err != nil { 79 if err != nil {
64 return nil, err 80 return nil, err
@@ -72,6 +88,9 @@ func (repo *CompanyRepository) Find(queryOptions map[string]interface{}) (int, [ @@ -72,6 +88,9 @@ func (repo *CompanyRepository) Find(queryOptions map[string]interface{}) (int, [
72 companyModel := []models.Company{} 88 companyModel := []models.Company{}
73 query := tx.Model(&companyModel). 89 query := tx.Model(&companyModel).
74 Limit(20) 90 Limit(20)
  91 + if v, ok := queryOptions["id"]; ok {
  92 + query.Where("id=?", v)
  93 + }
75 if v, ok := queryOptions["limit"]; ok { 94 if v, ok := queryOptions["limit"]; ok {
76 query.Limit(v.(int)) 95 query.Limit(v.(int))
77 } 96 }
@@ -92,12 +111,12 @@ func (repo *CompanyRepository) Find(queryOptions map[string]interface{}) (int, [ @@ -92,12 +111,12 @@ func (repo *CompanyRepository) Find(queryOptions map[string]interface{}) (int, [
92 111
93 func (repo *CompanyRepository) TransformToCompanyDomain(m *models.Company) *domain.Company { 112 func (repo *CompanyRepository) TransformToCompanyDomain(m *models.Company) *domain.Company {
94 return &domain.Company{ 113 return &domain.Company{
95 - Id: m.Id,  
96 - Logo: m.Logo,  
97 - Name: m.Name,  
98 - Status: m.Status,  
99 - UpdateAt: m.UpdateAt,  
100 - CreatedAt: m.CreateAt,  
101 - DeleteAt: m.DeleteAt, 114 + Id: m.Id,
  115 + Logo: m.Logo,
  116 + Name: m.Name,
  117 + Status: m.Status,
  118 + UpdateAt: m.UpdateAt,
  119 + CreateAt: m.CreateAt,
  120 + DeleteAt: m.DeleteAt,
102 } 121 }
103 } 122 }
1 package repository 1 package repository
2 2
3 import ( 3 import (
4 - "errors"  
5 "time" 4 "time"
6 5
7 "github.com/go-pg/pg/v10" 6 "github.com/go-pg/pg/v10"
@@ -16,33 +15,51 @@ type UserRepository struct { @@ -16,33 +15,51 @@ type UserRepository struct {
16 15
17 var _ domain.UserRepository = (*UserRepository)(nil) 16 var _ domain.UserRepository = (*UserRepository)(nil)
18 17
19 -func (repo *UserRepository) Save(user *domain.User) (*domain.User, error) { 18 +func NewUserRepository(tx *pgTransaction.TransactionContext) *UserRepository {
  19 + return &UserRepository{
  20 + transactionContext: tx,
  21 + }
  22 +}
  23 +
  24 +func (repo *UserRepository) Insert(user *domain.User) (*domain.User, error) {
20 userModel := models.User{ 25 userModel := models.User{
21 - Id: user.Id,  
22 - Account: user.Account,  
23 - AvatarUrl: user.AvatarUrl,  
24 - CompanyId: user.CompanyId,  
25 - IsPrincipal: user.IsPrincipal,  
26 - AdminType: user.AdminType,  
27 - Name: user.Name,  
28 - RoleId: user.RoleId,  
29 - Status: user.Status,  
30 - UpdateAt: user.UpdateAt,  
31 - CreateAt: user.CreateAt,  
32 - DeleteAt: user.DeleteAt, 26 + Id: user.Id,
  27 + Account: user.Account,
  28 + AvatarUrl: user.AvatarUrl,
  29 + CompanyId: user.CompanyId,
  30 + AdminType: user.AdminType,
  31 + Name: user.Name,
  32 + Status: user.Status,
  33 + UpdateAt: user.UpdateAt,
  34 + CreateAt: user.CreateAt,
  35 + DeleteAt: user.DeleteAt,
33 } 36 }
34 tx := repo.transactionContext.PgTx 37 tx := repo.transactionContext.PgTx
35 - if userModel.Id == 0 {  
36 - _, err := tx.Model(&userModel).Insert()  
37 - if err != nil {  
38 - return nil, err  
39 - }  
40 - user.Id = userModel.Id  
41 - } else {  
42 - _, err := tx.Model(&userModel).WherePK().Update()  
43 - if err != nil {  
44 - return nil, err  
45 - } 38 + _, err := tx.Model(&userModel).Insert()
  39 + if err != nil {
  40 + return nil, err
  41 + }
  42 + user.Id = userModel.Id
  43 + return user, nil
  44 +}
  45 +
  46 +func (repo *UserRepository) Update(user *domain.User) (*domain.User, error) {
  47 + userModel := models.User{
  48 + Id: user.Id,
  49 + Account: user.Account,
  50 + AvatarUrl: user.AvatarUrl,
  51 + CompanyId: user.CompanyId,
  52 + AdminType: user.AdminType,
  53 + Name: user.Name,
  54 + Status: user.Status,
  55 + UpdateAt: user.UpdateAt,
  56 + CreateAt: user.CreateAt,
  57 + DeleteAt: user.DeleteAt,
  58 + }
  59 + tx := repo.transactionContext.PgTx
  60 + _, err := tx.Model(&userModel).WherePK().Update()
  61 + if err != nil {
  62 + return nil, err
46 } 63 }
47 return user, nil 64 return user, nil
48 } 65 }
@@ -50,7 +67,7 @@ func (repo *UserRepository) Save(user *domain.User) (*domain.User, error) { @@ -50,7 +67,7 @@ func (repo *UserRepository) Save(user *domain.User) (*domain.User, error) {
50 func (repo *UserRepository) Remove(user *domain.User) (*domain.User, error) { 67 func (repo *UserRepository) Remove(user *domain.User) (*domain.User, error) {
51 nowTime := time.Now() 68 nowTime := time.Now()
52 user.DeleteAt = &nowTime 69 user.DeleteAt = &nowTime
53 - _, err := repo.Save(user) 70 + _, err := repo.Update(user)
54 return user, err 71 return user, err
55 } 72 }
56 73
@@ -63,7 +80,7 @@ func (repo *UserRepository) FindOne(queryOptions map[string]interface{}) (*domai @@ -63,7 +80,7 @@ func (repo *UserRepository) FindOne(queryOptions map[string]interface{}) (*domai
63 } 80 }
64 err := query.First() 81 err := query.First()
65 if err == pg.ErrNoRows { 82 if err == pg.ErrNoRows {
66 - return nil, errors.New("user 不存在") 83 + return nil, ErrNoRows
67 } 84 }
68 if err != nil { 85 if err != nil {
69 return nil, err 86 return nil, err
@@ -77,6 +94,9 @@ func (repo *UserRepository) Find(queryOptions map[string]interface{}) (int, []*d @@ -77,6 +94,9 @@ func (repo *UserRepository) Find(queryOptions map[string]interface{}) (int, []*d
77 userModel := []models.User{} 94 userModel := []models.User{}
78 query := tx.Model(&userModel). 95 query := tx.Model(&userModel).
79 Limit(20) 96 Limit(20)
  97 + if v, ok := queryOptions["id"]; ok {
  98 + query.Where("id=?", v)
  99 + }
80 if v, ok := queryOptions["limit"]; ok { 100 if v, ok := queryOptions["limit"]; ok {
81 query.Limit(v.(int)) 101 query.Limit(v.(int))
82 } 102 }
@@ -97,17 +117,15 @@ func (repo *UserRepository) Find(queryOptions map[string]interface{}) (int, []*d @@ -97,17 +117,15 @@ func (repo *UserRepository) Find(queryOptions map[string]interface{}) (int, []*d
97 117
98 func (repo *UserRepository) TransformToUserDomain(user *models.User) *domain.User { 118 func (repo *UserRepository) TransformToUserDomain(user *models.User) *domain.User {
99 return &domain.User{ 119 return &domain.User{
100 - Id: user.Id,  
101 - Account: user.Account,  
102 - AvatarUrl: user.AvatarUrl,  
103 - CompanyId: user.CompanyId,  
104 - IsPrincipal: user.IsPrincipal,  
105 - AdminType: user.AdminType,  
106 - Name: user.Name,  
107 - RoleId: user.RoleId,  
108 - Status: user.Status,  
109 - UpdateAt: user.UpdateAt,  
110 - CreateAt: user.CreateAt,  
111 - DeleteAt: user.DeleteAt, 120 + Id: user.Id,
  121 + Account: user.Account,
  122 + AvatarUrl: user.AvatarUrl,
  123 + CompanyId: user.CompanyId,
  124 + AdminType: user.AdminType,
  125 + Name: user.Name,
  126 + Status: user.Status,
  127 + UpdateAt: user.UpdateAt,
  128 + CreateAt: user.CreateAt,
  129 + DeleteAt: user.DeleteAt,
112 } 130 }
113 } 131 }
  1 +package repository
  2 +
  3 +import "github.com/go-pg/pg/v10"
  4 +
  5 +var ErrNoRows = pg.ErrNoRows