正在显示
19 个修改的文件
包含
658 行增加
和
6 行删除
@@ -50,3 +50,12 @@ func CreateOrderGoodRepository(options map[string]interface{}) (domain.OrderGood | @@ -50,3 +50,12 @@ func CreateOrderGoodRepository(options map[string]interface{}) (domain.OrderGood | ||
50 | } | 50 | } |
51 | return repository.NewOrderGoodRepository(transactionContext) | 51 | return repository.NewOrderGoodRepository(transactionContext) |
52 | } | 52 | } |
53 | + | ||
54 | +//CreateOrderGoodRepository 订单信息 | ||
55 | +func CreateUsersRepository(options map[string]interface{}) (domain.UsersRepository, error) { | ||
56 | + var transactionContext *transaction.TransactionContext | ||
57 | + if value, ok := options["transactionContext"]; ok { | ||
58 | + transactionContext = value.(*transaction.TransactionContext) | ||
59 | + } | ||
60 | + return repository.NewUsersRepository(transactionContext) | ||
61 | +} |
1 | +package command | ||
2 | + | ||
3 | +import ( | ||
4 | + "encoding/json" | ||
5 | + "fmt" | ||
6 | +) | ||
7 | + | ||
8 | +type SyncCallbackCommand struct { | ||
9 | + //position:职位,department:部门,employee:员工,company:公司 | ||
10 | + Module string `json:"module"` | ||
11 | + //add:添加,edit:编辑,delete删除,batchDelete:批量删除, | ||
12 | + //setCompanyCharge:更改公司主管,batchForbid:批量禁用用户, | ||
13 | + //batchRemove:批量更改用户部门,changeAdmin换管理员 | ||
14 | + Action string `json:"action"` | ||
15 | + //具体的对象JSON数据 | ||
16 | + Data json.RawMessage `json:"data" ` | ||
17 | +} | ||
18 | + | ||
19 | +func (command *SyncCallbackCommand) ValidateCommand() error { | ||
20 | + if len(command.Module) == 0 { | ||
21 | + return fmt.Errorf("module 必填") | ||
22 | + } | ||
23 | + if len(command.Action) == 0 { | ||
24 | + return fmt.Errorf("action 必填") | ||
25 | + } | ||
26 | + return nil | ||
27 | +} |
1 | +package service | ||
2 | + | ||
3 | +import ( | ||
4 | + "encoding/json" | ||
5 | + "errors" | ||
6 | + "fmt" | ||
7 | +) | ||
8 | + | ||
9 | +// UserDepartData 用户的部门数据 | ||
10 | +type UserDepartData struct { | ||
11 | + Id int64 `json:"id"` | ||
12 | + CompanyId int64 `json:"company_id"` | ||
13 | + DepartmentId int64 `json:"department_id"` | ||
14 | + UserId int64 `json:"user_id"` | ||
15 | +} | ||
16 | + | ||
17 | +//UserPositionData 用户的职位数据 | ||
18 | +type UserPositionData struct { | ||
19 | + Id int64 `json:"id"` | ||
20 | + CompanyId int64 `json:"company_id"` | ||
21 | + PositionId int64 `json:"position_id"` | ||
22 | + UserId int64 `json:"user_id"` | ||
23 | +} | ||
24 | + | ||
25 | +//EmployeeData 从平台平台接收的数据 | ||
26 | +type EmployeeData struct { | ||
27 | + Id int64 `json:"id"` //用户的id,对应本地的user_company_id | ||
28 | + OpenId int64 `json:"open_id"` //同一用户中心的用户id | ||
29 | + Phone string `json:"phone"` | ||
30 | + Name string `json:"name"` | ||
31 | + Sex int8 `json:"sex"` | ||
32 | + JobNum string `json:"job_num"` | ||
33 | + PrivatePhone string `json:"private_phone"` //私人手机 | ||
34 | + CompanyId int64 `json:"company_id"` //总后台的company_id | ||
35 | + Email string `json:"email"` | ||
36 | + ExtensionNum string `json:"extension_num"` //分机号 | ||
37 | + EntryTime string `json:"entry_time"` //入职时间 | ||
38 | + WorkSpace string `json:"workspace"` | ||
39 | + IsBusiness int8 `json:"is_business"` // | ||
40 | + Status int8 `json:"status"` | ||
41 | + Avatar string `json:"avatar"` | ||
42 | + ExtraText string `json:"extra_text"` | ||
43 | + Remarks string `json:"remarks"` | ||
44 | + AdminType int8 `json:"admin_type"` | ||
45 | + ChargeStatus int8 `json:"charge_status"` | ||
46 | + UserDepartments []UserDepartData `json:"user_departments"` | ||
47 | + UserPositions []UserPositionData `json:"user_positions"` | ||
48 | +} | ||
49 | + | ||
50 | +//SyncEmployeeService 同步用户数据 | ||
51 | +type SyncEmployeeService struct{} | ||
52 | + | ||
53 | +func (service SyncEmployeeService) DoAction(action string, byteData []byte) error { | ||
54 | + switch action { | ||
55 | + case "add": | ||
56 | + var ( | ||
57 | + data EmployeeData | ||
58 | + err error | ||
59 | + ) | ||
60 | + err = json.Unmarshal(byteData, &data) | ||
61 | + if err != nil { | ||
62 | + return fmt.Errorf("数据解析失败:%s", err) | ||
63 | + } | ||
64 | + err = service.AddEmployeeData(data) | ||
65 | + return err | ||
66 | + case "edit": | ||
67 | + var ( | ||
68 | + data EmployeeData | ||
69 | + err error | ||
70 | + ) | ||
71 | + err = json.Unmarshal(byteData, &data) | ||
72 | + if err != nil { | ||
73 | + return fmt.Errorf("数据解析失败:%s", err) | ||
74 | + } | ||
75 | + err = service.UpdateEmployeeData(data) | ||
76 | + return err | ||
77 | + case "batchDelete": | ||
78 | + case "batchForbid": | ||
79 | + case "batchRemove": | ||
80 | + case "import": | ||
81 | + default: | ||
82 | + return errors.New("action not found") | ||
83 | + } | ||
84 | + return nil | ||
85 | +} | ||
86 | + | ||
87 | +func (service SyncEmployeeService) AddEmployeeData(data EmployeeData) error { | ||
88 | + | ||
89 | + return nil | ||
90 | +} | ||
91 | + | ||
92 | +func (service SyncEmployeeService) UpdateEmployeeData(data EmployeeData) error { | ||
93 | + return nil | ||
94 | +} |
1 | +package service | ||
2 | + | ||
3 | +import "errors" | ||
4 | + | ||
5 | +//PlatformAction 企业平台数据同步服务 接口设定 | ||
6 | +type PlatformAction interface { | ||
7 | + DoAction(string, []byte) error | ||
8 | +} | ||
9 | + | ||
10 | +var actionmap = map[string]PlatformAction{ | ||
11 | + // "department": | ||
12 | + // "position": | ||
13 | + "employee": SyncEmployeeService{}, | ||
14 | + "company": SyncCompanyService{}, | ||
15 | +} | ||
16 | + | ||
17 | +func NewPlatformAction(module string) (PlatformAction, error) { | ||
18 | + if v, ok := actionmap[module]; ok { | ||
19 | + return v, nil | ||
20 | + } | ||
21 | + return nil, errors.New("module cannot found") | ||
22 | +} |
pkg/domain/company.go
0 → 100644
1 | +package domain | ||
2 | + | ||
3 | +import "time" | ||
4 | + | ||
5 | +// 公司的状态 1正常 2禁用 | ||
6 | +const ( | ||
7 | + companyStatusUsable int8 = 1 | ||
8 | + companyStatusUnusable int8 = 2 | ||
9 | +) | ||
10 | + | ||
11 | +//是否开启机会模块,是否有效【1:有效】【2:无效】 | ||
12 | +const ( | ||
13 | + CompanyEnableYes int8 = 1 | ||
14 | + CompanyEnableNo int8 = 2 | ||
15 | +) | ||
16 | + | ||
17 | +// 公司信息 | ||
18 | +type Company struct { | ||
19 | + // 唯一标识 | ||
20 | + Id int64 `json:"id"` | ||
21 | + // 名称 | ||
22 | + Name string `json:"name"` | ||
23 | + // 手机号码 | ||
24 | + Phone string `json:"phone"` | ||
25 | + // 公司logo | ||
26 | + Logo string `json:"logo"` | ||
27 | + // 备注 | ||
28 | + Remarks string `json:"remarks"` | ||
29 | + // 总后台的公司id | ||
30 | + AdminCompanyId int `json:"adminCompanyId"` | ||
31 | + // 状态 1正常 2禁用 | ||
32 | + Status int8 `json:"status"` | ||
33 | + //是否开启机会模块,是否有效【1:有效】【2:无效】 | ||
34 | + Enable int8 `json:"enable"` | ||
35 | + // 创建时间 | ||
36 | + CreateAt time.Time `json:"createAt"` | ||
37 | + // 更新时间 | ||
38 | + UpdateAt time.Time `json:"updateAt"` | ||
39 | + // 删除时间 | ||
40 | + DeleteAt time.Time `json:"deleteAt"` | ||
41 | +} | ||
42 | + | ||
43 | +func (c Company) StatusIsOk() bool { | ||
44 | + return c.Status == companyStatusUsable | ||
45 | +} | ||
46 | + | ||
47 | +func (c Company) EnableIsOk() bool { | ||
48 | + return c.Enable == CompanyEnableYes | ||
49 | +} | ||
50 | + | ||
51 | +type CompanyRepository interface { | ||
52 | + Add(*Company) error | ||
53 | + Edit(*Company) error | ||
54 | + FindOne(queryOptions map[string]interface{}) (Company, error) | ||
55 | + Find(queryOptions map[string]interface{}) (int64, []Company, error) | ||
56 | +} |
pkg/domain/partner_category_info.go
0 → 100644
1 | +package domain | ||
2 | + | ||
3 | +// 合伙人分类信息 | ||
4 | +type PartnerCategoryInfo struct { | ||
5 | + // 唯一标识 | ||
6 | + Id int64 `json:"id"` | ||
7 | + // 名称 | ||
8 | + Name string `json:"name"` | ||
9 | +} | ||
10 | + | ||
11 | +type PartnerCategoryInfoRepository interface { | ||
12 | + Find(queryOptions map[string]interface{}) (int64, []*PartnerCategoryInfo, error) | ||
13 | +} |
@@ -28,8 +28,6 @@ var partnerCategoryMap = map[int]string{ | @@ -28,8 +28,6 @@ var partnerCategoryMap = map[int]string{ | ||
28 | 28 | ||
29 | type PartnerInfo struct { | 29 | type PartnerInfo struct { |
30 | Partner Partner `json:"partner"` | 30 | Partner Partner `json:"partner"` |
31 | - //合伙类别 | ||
32 | - PartnerCategory int `json:"partnerCategory"` | ||
33 | // 登录密码 | 31 | // 登录密码 |
34 | Password string `json:"password"` | 32 | Password string `json:"password"` |
35 | // 状态(1:启用或者0:禁用) | 33 | // 状态(1:启用或者0:禁用) |
@@ -40,10 +38,14 @@ type PartnerInfo struct { | @@ -40,10 +38,14 @@ type PartnerInfo struct { | ||
40 | UpdateAt time.Time `json:"updateAt"` | 38 | UpdateAt time.Time `json:"updateAt"` |
41 | //合作时间 | 39 | //合作时间 |
42 | CooperateTime time.Time `json:"cooperateTime"` | 40 | CooperateTime time.Time `json:"cooperateTime"` |
43 | - //关联业务员//所属区域信息 | 41 | + //所属区域信息 |
44 | RegionInfo *RegionInfo `json:"regionInfo"` | 42 | RegionInfo *RegionInfo `json:"regionInfo"` |
45 | - | 43 | + //关联业务员 |
46 | Salesman []Salesman `json:"salesman"` | 44 | Salesman []Salesman `json:"salesman"` |
45 | + //合伙人分类 | ||
46 | + PartnerCategoryInfos []PartnerCategoryInfo `json:"partnerCategoryInfos"` | ||
47 | + //合伙类别 | ||
48 | + PartnerCategory int `json:"partnerCategory"` | ||
47 | } | 49 | } |
48 | 50 | ||
49 | func (p *PartnerInfo) GetPartnerCategory() map[int]string { | 51 | func (p *PartnerInfo) GetPartnerCategory() map[int]string { |
@@ -35,6 +35,7 @@ type Users struct { | @@ -35,6 +35,7 @@ type Users struct { | ||
35 | CreateAt time.Time | 35 | CreateAt time.Time |
36 | UpdateAt time.Time | 36 | UpdateAt time.Time |
37 | Permission []AdminPermissionBase //权限 | 37 | Permission []AdminPermissionBase //权限 |
38 | + AccessPartners []Partner | ||
38 | } | 39 | } |
39 | 40 | ||
40 | //IsUsable 用户是否可用 | 41 | //IsUsable 用户是否可用 |
@@ -46,3 +47,25 @@ func (u Users) IsUsable() bool { | @@ -46,3 +47,25 @@ func (u Users) IsUsable() bool { | ||
46 | func (u Users) IsCharge() bool { | 47 | func (u Users) IsCharge() bool { |
47 | return u.ChargeStatus == userIsCharge | 48 | return u.ChargeStatus == userIsCharge |
48 | } | 49 | } |
50 | + | ||
51 | +func (u Users) InCompany(companyid int64) bool { | ||
52 | + return u.CompanyId == companyid | ||
53 | +} | ||
54 | + | ||
55 | +type UsersFindOneQuery struct { | ||
56 | + Id int64 | ||
57 | + Phone string | ||
58 | +} | ||
59 | + | ||
60 | +type UsersFindQuery struct { | ||
61 | + Ids []int64 | ||
62 | + Offset int | ||
63 | + Limit int | ||
64 | +} | ||
65 | +type UsersRepository interface { | ||
66 | + Add(*Users) error | ||
67 | + Edit(*Users) error | ||
68 | + Remove([]int64) error | ||
69 | + FindOne(UsersFindOneQuery) (Users, error) | ||
70 | + Find(UsersFindQuery) (int, []Users, error) | ||
71 | +} |
1 | +package models | ||
2 | + | ||
3 | +import ( | ||
4 | + "context" | ||
5 | + "time" | ||
6 | + | ||
7 | + "github.com/go-pg/pg/v10" | ||
8 | +) | ||
9 | + | ||
10 | +// 业务分红信息 | ||
11 | +type BusinessBonus struct { | ||
12 | + tableName struct{} `pg:"business_bonus"` | ||
13 | + // 唯一标识 | ||
14 | + Id int64 | ||
15 | + // 公司编号 | ||
16 | + CompanyId int64 | ||
17 | + // 合伙人信息Id | ||
18 | + PartnerInfoId string | ||
19 | + // 应收分红 | ||
20 | + Bonus float64 | ||
21 | + // 未收分红 | ||
22 | + BonusNot float64 | ||
23 | + // 分红支出 | ||
24 | + BonusExpense float64 | ||
25 | + // 是否关闭【0;否】【1:是】 | ||
26 | + IsDisable int8 | ||
27 | + // 分红状态 1:待支付分红 2:已支付分红 | ||
28 | + BonusStatus int8 | ||
29 | + // 创建时间 | ||
30 | + CreateAt time.Time | ||
31 | + // 更新时间 | ||
32 | + UpdateAt time.Time | ||
33 | + // 删除时间 | ||
34 | + DeleteAt time.Time | ||
35 | +} | ||
36 | + | ||
37 | +var _ pg.BeforeUpdateHook = (*BusinessBonus)(nil) | ||
38 | + | ||
39 | +func (bonus *BusinessBonus) BeforeUpdate(ctx context.Context) (context.Context, error) { | ||
40 | + bonus.UpdateAt = time.Now() | ||
41 | + return ctx, nil | ||
42 | +} | ||
43 | + | ||
44 | +var _ pg.BeforeInsertHook = (*BusinessBonus)(nil) | ||
45 | + | ||
46 | +func (bonus *BusinessBonus) BeforeInsert(ctx context.Context) (context.Context, error) { | ||
47 | + bonus.CreateAt = time.Now() | ||
48 | + bonus.UpdateAt = time.Now() | ||
49 | + return ctx, nil | ||
50 | +} |
pkg/infrastructure/pg/models/company.go
0 → 100644
1 | +package models | ||
2 | + | ||
3 | +import "time" | ||
4 | + | ||
5 | +// 公司信息 | ||
6 | +type Company struct { | ||
7 | + tableName struct{} `pg:"company"` | ||
8 | + // 唯一标识 | ||
9 | + Id int64 | ||
10 | + // 名称 | ||
11 | + Name string | ||
12 | + // 手机号码 | ||
13 | + Phone string | ||
14 | + // 公司logo | ||
15 | + Logo string | ||
16 | + // 备注 | ||
17 | + Remarks string | ||
18 | + // 总后台的公司id | ||
19 | + AdminCompanyId int | ||
20 | + // 状态 1正常 2禁用 | ||
21 | + Status int8 | ||
22 | + //是否开启机会模块,是否有效【1:有效】【2:无效】 | ||
23 | + Enable int8 | ||
24 | + // 创建时间 | ||
25 | + CreateAt time.Time | ||
26 | + // 更新时间 | ||
27 | + UpdateAt time.Time | ||
28 | + // 删除时间 | ||
29 | + DeleteAt time.Time | ||
30 | +} |
@@ -20,8 +20,7 @@ type PartnerInfo struct { | @@ -20,8 +20,7 @@ type PartnerInfo struct { | ||
20 | Password string | 20 | Password string |
21 | // 状态(1:启用或者0:禁用) | 21 | // 状态(1:启用或者0:禁用) |
22 | Status int `pg:",use_zero"` | 22 | Status int `pg:",use_zero"` |
23 | - // 合伙类别 (1.研发合伙人 2.业务合伙人 3.事业) | ||
24 | - PartnerCategory int `pg:",default:1"` //partner_category | 23 | + |
25 | //所属区域信息 | 24 | //所属区域信息 |
26 | RegionInfo *domain.RegionInfo | 25 | RegionInfo *domain.RegionInfo |
27 | //创建时间 | 26 | //创建时间 |
@@ -32,6 +31,10 @@ type PartnerInfo struct { | @@ -32,6 +31,10 @@ type PartnerInfo struct { | ||
32 | CooperateTime time.Time | 31 | CooperateTime time.Time |
33 | //关联业务员 | 32 | //关联业务员 |
34 | Salesman []domain.Salesman | 33 | Salesman []domain.Salesman |
34 | + // 合伙类别 (1.研发合伙人 2.业务合伙人 3.事业) | ||
35 | + PartnerCategory int `pg:",default:1"` //partner_category | ||
36 | + //合伙类别 | ||
37 | + PartnerCategoryInfos []PartnerCategoryInfo | ||
35 | } | 38 | } |
36 | 39 | ||
37 | var _ pg.BeforeUpdateHook = (*PartnerInfo)(nil) | 40 | var _ pg.BeforeUpdateHook = (*PartnerInfo)(nil) |
pkg/infrastructure/pg/models/users.go
0 → 100644
1 | +package models | ||
2 | + | ||
3 | +import ( | ||
4 | + "time" | ||
5 | + | ||
6 | + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain" | ||
7 | +) | ||
8 | + | ||
9 | +type Users struct { | ||
10 | + tableName struct{} `pg:"users"` | ||
11 | + Id int64 //用户id | ||
12 | + CompanyId int64 //公司id | ||
13 | + OpenId int64 //统一用户中心 | ||
14 | + Name string //用户名称 | ||
15 | + Sex int8 //性别:【0:未知】【1:男】【2:女】 | ||
16 | + JobNum string //工号 | ||
17 | + Phone string //手机号,同账号 | ||
18 | + PrivatePhone string //私人手机号 | ||
19 | + Email string //邮件 | ||
20 | + ExtensionNum string //分机号 | ||
21 | + EntryTime time.Time //入职时间 | ||
22 | + Workspace string //工作地 | ||
23 | + Status int8 //状态:【1:正常】【 2:禁用】 | ||
24 | + Avatar string ///头像 | ||
25 | + Remarks string //备注 | ||
26 | + ChargeStatus int8 //是否为当前公司主管 【1:是】【2:否】 | ||
27 | + Permission []domain.AdminPermissionBase //权限 | ||
28 | + AccessPartners []domain.Partner //可查看的合伙人 | ||
29 | + CreateAt time.Time | ||
30 | + UpdateAt time.Time | ||
31 | + DeleteAt time.Time | ||
32 | +} |
1 | +package repository | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + | ||
6 | + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain" | ||
7 | + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models" | ||
8 | + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction" | ||
9 | +) | ||
10 | + | ||
11 | +type CompanyRepository struct { | ||
12 | + transactionContext *transaction.TransactionContext | ||
13 | +} | ||
14 | + | ||
15 | +var ( | ||
16 | + _ domain.CompanyRepository = (*CompanyRepository)(nil) | ||
17 | +) | ||
18 | + | ||
19 | +func NewCompanyRepository(transactionContext *transaction.TransactionContext) (*CompanyRepository, error) { | ||
20 | + if transactionContext == nil { | ||
21 | + return nil, fmt.Errorf("transactionContext参数不能为nil") | ||
22 | + } | ||
23 | + return &CompanyRepository{transactionContext: transactionContext}, nil | ||
24 | +} | ||
25 | + | ||
26 | +func (repository CompanyRepository) transformPgModelToDomainModel(m *models.Company) (domain.Company, error) { | ||
27 | + return domain.Company{ | ||
28 | + Id: m.Id, | ||
29 | + Name: m.Name, | ||
30 | + Phone: m.Phone, | ||
31 | + Logo: m.Logo, | ||
32 | + Remarks: m.Remarks, | ||
33 | + AdminCompanyId: m.AdminCompanyId, | ||
34 | + Status: m.Status, | ||
35 | + Enable: m.Enable, | ||
36 | + CreateAt: m.CreateAt, | ||
37 | + UpdateAt: m.UpdateAt, | ||
38 | + }, nil | ||
39 | +} | ||
40 | + | ||
41 | +func (reponsitory CompanyRepository) Add(m *domain.Company) error { | ||
42 | + var ( | ||
43 | + err error | ||
44 | + tx = reponsitory.transactionContext.PgTx | ||
45 | + ) | ||
46 | + companyModel := models.Company{ | ||
47 | + Id: m.Id, | ||
48 | + Name: m.Name, | ||
49 | + Phone: m.Phone, | ||
50 | + Logo: m.Logo, | ||
51 | + Remarks: m.Remarks, | ||
52 | + AdminCompanyId: m.AdminCompanyId, | ||
53 | + Status: m.Status, | ||
54 | + Enable: m.Enable, | ||
55 | + CreateAt: m.CreateAt, | ||
56 | + UpdateAt: m.UpdateAt, | ||
57 | + } | ||
58 | + _, err = tx.Model(&companyModel).Insert() | ||
59 | + return err | ||
60 | +} | ||
61 | + | ||
62 | +func (reponsitory CompanyRepository) Edit(m *domain.Company) error { | ||
63 | + var ( | ||
64 | + err error | ||
65 | + tx = reponsitory.transactionContext.PgTx | ||
66 | + ) | ||
67 | + companyModel := models.Company{ | ||
68 | + Id: m.Id, | ||
69 | + Name: m.Name, | ||
70 | + Phone: m.Phone, | ||
71 | + Logo: m.Logo, | ||
72 | + Remarks: m.Remarks, | ||
73 | + AdminCompanyId: m.AdminCompanyId, | ||
74 | + Status: m.Status, | ||
75 | + Enable: m.Enable, | ||
76 | + CreateAt: m.CreateAt, | ||
77 | + DeleteAt: m.DeleteAt, | ||
78 | + UpdateAt: m.UpdateAt, | ||
79 | + } | ||
80 | + _, err = tx.Model(&companyModel).Update() | ||
81 | + return err | ||
82 | +} | ||
83 | + | ||
84 | +func (reponsitory CompanyRepository) FindOne(queryOptions map[string]interface{}) (domain.Company, error) { | ||
85 | + return domain.Company{}, nil | ||
86 | +} | ||
87 | + | ||
88 | +func (reponsitory CompanyRepository) Find(queryOptions map[string]interface{}) (int64, []domain.Company, error) { | ||
89 | + return 0, nil, nil | ||
90 | +} |
1 | +package repository | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "time" | ||
6 | + | ||
7 | + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain" | ||
8 | + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models" | ||
9 | + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction" | ||
10 | +) | ||
11 | + | ||
12 | +type UsersRepository struct { | ||
13 | + transactionContext *transaction.TransactionContext | ||
14 | +} | ||
15 | + | ||
16 | +var ( | ||
17 | + _ domain.UsersRepository = (*UsersRepository)(nil) | ||
18 | +) | ||
19 | + | ||
20 | +func NewUsersRepository(transactionContext *transaction.TransactionContext) (*UsersRepository, error) { | ||
21 | + if transactionContext == nil { | ||
22 | + return nil, fmt.Errorf("transactionContext参数不能为nil") | ||
23 | + } | ||
24 | + return &UsersRepository{transactionContext: transactionContext}, nil | ||
25 | +} | ||
26 | + | ||
27 | +func (repository UsersRepository) transformPgModelToDomainModel(m *models.Users) (domain.Users, error) { | ||
28 | + return domain.Users{ | ||
29 | + Id: m.Id, | ||
30 | + CompanyId: m.CompanyId, | ||
31 | + OpenId: m.OpenId, | ||
32 | + Name: m.Name, | ||
33 | + Sex: m.Sex, | ||
34 | + JobNum: m.JobNum, | ||
35 | + Phone: m.Phone, | ||
36 | + PrivatePhone: m.PrivatePhone, | ||
37 | + Email: m.Email, | ||
38 | + ExtensionNum: m.ExtensionNum, | ||
39 | + EntryTime: m.EntryTime, | ||
40 | + Workspace: m.Workspace, | ||
41 | + Status: m.Status, | ||
42 | + Avatar: m.Avatar, | ||
43 | + Remarks: m.Remarks, | ||
44 | + ChargeStatus: m.ChargeStatus, | ||
45 | + CreateAt: m.CreateAt, | ||
46 | + UpdateAt: m.UpdateAt, | ||
47 | + Permission: m.Permission, | ||
48 | + }, nil | ||
49 | +} | ||
50 | + | ||
51 | +func (reponsitory UsersRepository) Add(u *domain.Users) error { | ||
52 | + var ( | ||
53 | + err error | ||
54 | + tx = reponsitory.transactionContext.PgTx | ||
55 | + ) | ||
56 | + m := &models.Users{ | ||
57 | + Id: u.Id, | ||
58 | + CompanyId: u.CompanyId, | ||
59 | + OpenId: u.OpenId, | ||
60 | + Name: u.Name, | ||
61 | + Sex: u.Sex, | ||
62 | + JobNum: u.JobNum, | ||
63 | + Phone: u.Phone, | ||
64 | + PrivatePhone: u.PrivatePhone, | ||
65 | + Email: u.Email, | ||
66 | + ExtensionNum: u.ExtensionNum, | ||
67 | + EntryTime: u.EntryTime, | ||
68 | + Workspace: u.Workspace, | ||
69 | + Status: u.Status, | ||
70 | + Avatar: u.Avatar, | ||
71 | + Remarks: u.Remarks, | ||
72 | + ChargeStatus: u.ChargeStatus, | ||
73 | + CreateAt: u.CreateAt, | ||
74 | + UpdateAt: u.UpdateAt, | ||
75 | + Permission: u.Permission, | ||
76 | + AccessPartners: u.AccessPartners, | ||
77 | + } | ||
78 | + _, err = tx.Model(m).Insert() | ||
79 | + return err | ||
80 | +} | ||
81 | + | ||
82 | +func (reponsitory UsersRepository) Edit(u *domain.Users) error { | ||
83 | + var ( | ||
84 | + err error | ||
85 | + tx = reponsitory.transactionContext.PgTx | ||
86 | + ) | ||
87 | + m := &models.Users{ | ||
88 | + Id: u.Id, | ||
89 | + CompanyId: u.CompanyId, | ||
90 | + OpenId: u.OpenId, | ||
91 | + Name: u.Name, | ||
92 | + Sex: u.Sex, | ||
93 | + JobNum: u.JobNum, | ||
94 | + Phone: u.Phone, | ||
95 | + PrivatePhone: u.PrivatePhone, | ||
96 | + Email: u.Email, | ||
97 | + ExtensionNum: u.ExtensionNum, | ||
98 | + EntryTime: u.EntryTime, | ||
99 | + Workspace: u.Workspace, | ||
100 | + Status: u.Status, | ||
101 | + Avatar: u.Avatar, | ||
102 | + Remarks: u.Remarks, | ||
103 | + ChargeStatus: u.ChargeStatus, | ||
104 | + CreateAt: u.CreateAt, | ||
105 | + UpdateAt: u.UpdateAt, | ||
106 | + Permission: u.Permission, | ||
107 | + AccessPartners: u.AccessPartners, | ||
108 | + } | ||
109 | + _, err = tx.Model(m).WherePK().Update() | ||
110 | + return err | ||
111 | +} | ||
112 | + | ||
113 | +func (reponsitory UsersRepository) FindOne(queryOptions domain.UsersFindOneQuery) (domain.Users, error) { | ||
114 | + var ( | ||
115 | + err error | ||
116 | + tx = reponsitory.transactionContext.PgTx | ||
117 | + m models.Users | ||
118 | + hasWhere bool | ||
119 | + ) | ||
120 | + query := tx.Model(&m) | ||
121 | + if queryOptions.Id > 0 { | ||
122 | + query = query.Where("id=?", queryOptions.Id) | ||
123 | + hasWhere = true | ||
124 | + } | ||
125 | + if len(queryOptions.Phone) > 0 { | ||
126 | + query = query.Where("phone=?", queryOptions.Id) | ||
127 | + hasWhere = true | ||
128 | + } | ||
129 | + if !hasWhere { | ||
130 | + return domain.Users{}, fmt.Errorf("queryOptions 中没有搜索条件") | ||
131 | + } | ||
132 | + err = query.First() | ||
133 | + if err != nil { | ||
134 | + return domain.Users{}, err | ||
135 | + } | ||
136 | + return reponsitory.transformPgModelToDomainModel(&m) | ||
137 | +} | ||
138 | + | ||
139 | +func (reponsitory UsersRepository) Find(queryOption domain.UsersFindQuery) (int, []domain.Users, error) { | ||
140 | + db := reponsitory.transactionContext.PgTx | ||
141 | + usersModels := []models.Users{} | ||
142 | + query := db.Model(&usersModels) | ||
143 | + if queryOption.Offset > -1 { | ||
144 | + query = query.Offset(queryOption.Offset) | ||
145 | + } | ||
146 | + if queryOption.Limit > 0 { | ||
147 | + query = query.Limit(queryOption.Limit) | ||
148 | + } else { | ||
149 | + query = query.Limit(20) | ||
150 | + } | ||
151 | + var ( | ||
152 | + err error | ||
153 | + usersReturn = make([]domain.Users, 0) | ||
154 | + cnt int | ||
155 | + ) | ||
156 | + query = query.Order("id DESC") | ||
157 | + cnt, err = query.SelectAndCount() | ||
158 | + if err != nil { | ||
159 | + return 0, usersReturn, err | ||
160 | + } | ||
161 | + for i := range usersModels { | ||
162 | + domainUsers, err := reponsitory.transformPgModelToDomainModel(&usersModels[i]) | ||
163 | + if err != nil { | ||
164 | + return 0, usersReturn, err | ||
165 | + } | ||
166 | + usersReturn = append(usersReturn, domainUsers) | ||
167 | + } | ||
168 | + return cnt, usersReturn, nil | ||
169 | +} | ||
170 | + | ||
171 | +func (reponsitory UsersRepository) Remove(ids []int64) error { | ||
172 | + if len(ids) == 0 { | ||
173 | + return nil | ||
174 | + } | ||
175 | + var ( | ||
176 | + err error | ||
177 | + tx = reponsitory.transactionContext.PgTx | ||
178 | + ) | ||
179 | + _, err = tx.Model(&models.Users{}). | ||
180 | + Set("delete_at=?", time.Now()). | ||
181 | + WhereIn("id in(?)", ids). | ||
182 | + Update() | ||
183 | + return err | ||
184 | +} |
-
请 注册 或 登录 后发表评论