partner_info_controller.go
9.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package controllers
import (
"crypto/sha1"
"errors"
"fmt"
"time"
"github.com/astaxie/beego/logs"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
partnerQuery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/partnerInfo/query"
partnerInfoCmd "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/partnerInfo/command"
partnerInfoService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/partnerInfo/service"
)
type PartnerInfoController struct {
BaseController
}
////Prepare 重写 BaseController 的Prepare方法
func (c *PartnerInfoController) Prepare() {
c.BaseController.Prepare()
if ok := c.ValidJWTToken(); !ok {
return
}
if ok := c.ValidAdminPermission(domain.PERMINSSION_PARTNER); !ok {
return
}
}
//CreatePartnerInfo 创建合伙人
func (c *PartnerInfoController) CreatePartnerInfo() {
//用与适配前端定义的数据结构
type Parameter struct {
PartnerName string `json:"partnerName"`
PartnerType []*domain.PartnerCategory `json:"partnerType"`
Area string `json:"area"`
Account string `json:"account"`
State int `json:"state"`
CooperationTime string `json:"cooperationTime"`
SalesmanName string `json:"salesmanName"`
Phone string `json:"phone"`
Remark string `json:"remark"`
}
var (
param Parameter
err error
)
if err = c.BindJsonData(¶m); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
defaultPwd := fmt.Sprintf("%x", sha1.Sum([]byte("123456")))
cooperateTime, err := time.ParseInLocation("2006-01-02", param.CooperationTime, time.Local)
if err != nil {
e := lib.ThrowError(lib.ARG_ERROR, err.Error())
c.ResponseError(e)
return
}
companyId := c.GetUserCompany()
cmd := partnerInfoCmd.CreatePartnerInfoCommand{
PartnerName: param.PartnerName,
Account: param.Account,
Password: defaultPwd,
Status: param.State,
PartnerCategory: param.PartnerType,
CooperateTime: cooperateTime,
CompanyId: companyId,
Remark: param.Remark,
}
if len(param.SalesmanName) > 0 || len(param.Phone) > 0 {
cmd.Salesman = []domain.Salesman{
domain.Salesman{
Name: param.SalesmanName,
Telephone: param.Phone,
},
}
}
if len(param.Area) > 0 {
cmd.RegionInfo = &domain.RegionInfo{
RegionName: param.Area,
}
}
serve := partnerInfoService.NewPartnerInfoService(nil)
newPartner, err := serve.CreatePartnerInfo(&cmd)
if err != nil {
c.ResponseError(err)
return
}
if newPartner != nil {
c.ResponseData(map[string]interface{}{
"id": newPartner.Partner.Id,
})
return
}
c.ResponseData(nil)
return
}
//UpdatePartnerInfo 更新合伙人
func (c *PartnerInfoController) UpdatePartnerInfo() {
//用与适配前端定义的数据结构
type Parameter struct {
ID int64 `json:"id"`
PartnerType []*domain.PartnerCategory `json:"partnerType"`
Area string `json:"area"`
State int `json:"state"`
CooperationTime string `json:"cooperationTime"`
SalesmanName string `json:"salesmanName"`
Phone string `json:"phone"`
Remark string `json:"remark"`
}
var (
param Parameter
err error
)
if err = c.BindJsonData(¶m); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
cooperateTime, err := time.ParseInLocation("2006-01-02", param.CooperationTime, time.Local)
if err != nil {
e := lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
c.ResponseError(e)
return
}
companyId := c.GetUserCompany()
cmd := partnerInfoCmd.UpdatePartnerInfoCommand{
Id: param.ID,
Status: param.State,
PartnerCategory: param.PartnerType,
CooperateTime: cooperateTime,
RegionInfo: &domain.RegionInfo{
RegionName: param.Area,
},
Salesman: []domain.Salesman{
domain.Salesman{
Name: param.SalesmanName,
Telephone: param.Phone,
},
},
CompanyId: companyId,
Remark: param.Remark,
}
serve := partnerInfoService.NewPartnerInfoService(nil)
err = serve.UpdatePartnerInfo(&cmd)
if err != nil {
c.ResponseError(err)
return
}
c.ResponseData(nil)
return
}
//GetPartnerInfo 获取合伙人
func (c *PartnerInfoController) GetPartnerInfo() {
//用与适配前端定义的数据结构
type Parameter struct {
Id int64 `json:"id"`
}
var (
param Parameter
err error
)
if err = c.BindJsonData(¶m); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
companyId := c.GetUserCompany()
newPartnerService := partnerInfoService.NewPartnerInfoService(nil)
partnerInfo, err := newPartnerService.GetPartnerInfo(partnerQuery.GetPartnerInfoQuery{
Id: param.Id,
CompanyId: companyId,
})
if err != nil {
c.ResponseError(err)
return
}
//数据适配
rspResult := map[string]interface{}{
"account": partnerInfo.Partner.Account,
"partnerName": partnerInfo.Partner.PartnerName,
// "partnerType": []map[string]interface{}{},
"area": partnerInfo.RegionInfo.RegionName,
"salesmanName": "",
"phone": "",
"createTime": partnerInfo.CreateAt.Local().Format("2006-01-02 15:04:05"),
"cooperationTime": partnerInfo.CooperateTime.Local().Format("2006-01-02"),
"state": partnerInfo.Status,
"id": partnerInfo.Partner.Id,
"remark": partnerInfo.Remark,
}
if len(partnerInfo.Salesman) > 0 {
rspResult["salesmanName"] = partnerInfo.Salesman[0].Name
rspResult["phone"] = partnerInfo.Salesman[0].Telephone
}
var partnerTypes []map[string]interface{}
for _, v := range partnerInfo.PartnerCategoryInfos {
m := map[string]interface{}{
"id": v.Id,
"name": v.Name,
"code": v.Code,
}
partnerTypes = append(partnerTypes, m)
}
rspResult["partnerType"] = partnerTypes
c.ResponseData(rspResult)
return
}
/**
* @Author SteveChan
* @Description //TODO 移除合伙人
* @Date 15:31 2020/12/29
* @Param
* @return
**/
func (c *PartnerInfoController) RemovePartnerInfo() {
//用与适配前端定义的数据结构
type Parameter struct {
ID int64 `json:"id"`
}
var (
param Parameter
err error
)
if err = c.BindJsonData(¶m); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
cmd := partnerInfoCmd.RemovePartnerInfoCommand{
Id: param.ID,
}
serve := partnerInfoService.NewPartnerInfoService(nil)
err = serve.RemovePartnerInfo(cmd)
if err != nil {
c.ResponseError(err)
return
}
c.ResponseData(nil)
return
}
//PartnerInfoSetState 合伙人批量禁用.启用
func (c *PartnerInfoController) PartnerInfoSetState() {
//用与适配前端定义的数据结构
type Parameter struct {
Id []int64 `json:"id"`
Status int `json:"status"`
}
var (
param Parameter
err error
)
if err = c.BindJsonData(¶m); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
companyId := c.GetUserCompany()
var cmd partnerInfoCmd.StatusPartnerInfoCommand
switch param.Status {
case 0:
cmd = partnerInfoCmd.StatusPartnerInfoCommand{
Ids: param.Id,
CompanyId: companyId,
Status: domain.PARTNER_STATUS_NO,
}
case 1:
cmd = partnerInfoCmd.StatusPartnerInfoCommand{
Ids: param.Id,
CompanyId: companyId,
Status: domain.PARTNER_STATUS_YES,
}
default:
c.ResponseError(errors.New("参数错误"))
return
}
newPartnerService := partnerInfoService.NewPartnerInfoService(nil)
err = newPartnerService.UpdateStatus(cmd)
if err != nil {
c.ResponseError(err)
return
}
c.ResponseData(nil)
return
}
//ListPartnerInfo 合伙人列表
func (c *PartnerInfoController) ListPartnerInfo() {
type Parameter struct {
PartnerType int `json:"partnerType"`
Area string `json:"area"`
PartnerName string `json:"partnerName"`
PageSize int `json:"pageSize"`
PageNumber int `json:"pageNumber"`
}
var (
param Parameter
err error
)
if err = c.BindJsonData(¶m); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
if param.PageNumber == 0 {
param.PageNumber = 1
}
if param.PageSize == 0 {
param.PageSize = 20
}
companyId := c.GetUserCompany()
query := partnerQuery.ListPartnerInfoQuery{
PartnerType: param.PartnerType,
PartnerName: param.PartnerName,
RegionInfo: param.Area,
Limit: param.PageSize,
Offset: (param.PageNumber - 1) * param.PageSize,
CompanyId: companyId,
}
newPartnerService := partnerInfoService.NewPartnerInfoService(nil)
count, partners, err := newPartnerService.ListPartnerInfo(&query)
if err != nil {
c.ResponseError(err)
return
}
var resp []map[string]interface{}
indexBegin := query.Offset
for i := range partners {
m := map[string]interface{}{
"index": indexBegin + (i + 1),
"id": partners[i].Partner.Id,
"account": partners[i].Partner.Account,
"partnerName": partners[i].Partner.PartnerName,
"area": partners[i].RegionInfo.RegionName,
"createTime": partners[i].CreateAt.Local().Format("2006-01-02 15:04:05"),
"cooperationTime": partners[i].CooperateTime.Local().Format("2006-01-02"),
"state": partners[i].Status,
"partnerType": partners[i].PartnerCategoryInfos,
"salesmanName": "",
"phone": "",
"remark": partners[i].Remark,
}
if len(partners[i].Salesman) > 0 {
m["salesmanName"] = partners[i].Salesman[0].Name
m["phone"] = partners[i].Salesman[0].Telephone
}
resp = append(resp, m)
}
c.ResponsePageList(resp, count, param.PageNumber)
return
}