yinbentan
2024-09-26 2030ec81f18f4ec9ea1800f13046acafff6d50f7
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
package crm_aps
 
import (
    "aps_crm/constvar"
    "aps_crm/model"
    "aps_crm/model/grpc_init"
    "aps_crm/pkg/ecode"
    "aps_crm/service"
    "context"
    "errors"
    "fmt"
    "github.com/shopspring/decimal"
    "gorm.io/gorm"
    "strconv"
)
 
type Server struct {
    UnimplementedCrmAndApsGrpcServiceServer
}
 
func (s *Server) GetCrmSalesDetailsList(ctx context.Context, req *GetCrmSalesDetailsListRequest) (*GetCrmSalesDetailsListResponse, error) {
    search := model.NewSalesDetailsSearch().Orm.Model(model.SalesDetails{})
    if req.StartTime != "" && req.EndTime != "" {
        if req.TimeType == "签约日期" {
            search = search.Where("sign_time between ? and ?", req.StartTime, req.EndTime)
        } else if req.TimeType == "交付日期" {
            search = search.Where("delivery_date between ? and ?", req.StartTime, req.EndTime)
        }
    }
    if req.ClientName != "" {
        search = search.Joins("Client", "LEFT").Where("Client.name like ?", "%"+req.ClientName+"%")
    }
    search = search.Preload("Member").Preload("Client").Where("status = ?", constvar.WaitConfirmed).Where("project_id = ?", "")
 
    var records = make([]*model.SalesDetails, 0)
    var total int64
    err := search.Count(&total).Error
    if err != nil {
        return nil, err
    }
    if req.Page*req.PageSize > 0 {
        search = search.Limit(int(req.PageSize)).Offset(int((req.Page - 1) * req.PageSize))
    }
    err = search.Order("id desc").Find(&records).Error
    if err != nil {
        return nil, err
    }
    sds := make([]*CrmSalesDetails, 0)
    for _, record := range records {
        var sd CrmSalesDetails
        sd.Number = record.Number
        sd.ClientName = record.Client.Name
        sd.MemberName = record.Member.Username
        sd.SignTime = record.SignTime
        sd.DeliveryDate = record.DeliveryDate
        sds = append(sds, &sd)
    }
    resp := new(GetCrmSalesDetailsListResponse)
    resp.SalesDetails = sds
    resp.Total = total
    return resp, nil
}
 
func (s *Server) SendSalesDetailsAndProjectToCrm(ctx context.Context, req *SendSalesDetailsAndProjectToCrmRequest) (*SendSalesDetailsAndProjectToCrmResponse, error) {
    details, err := model.NewSalesDetailsSearch().SetNumbers(req.SalesDetailsNumbers).SetPreload(true).FindNotTotal()
    if err != nil {
        return nil, err
    }
    for _, detail := range details {
        m := make(map[string]interface{})
        m["status"] = constvar.WaitOutbound
        m["project_id"] = req.ProjectId
 
        err := model.NewSalesDetailsSearch().SetNumber(detail.Number).UpdateByMap(m)
        if err != nil {
            return nil, err
        }
 
        //推送到wms
        //clientName := ""
        //if detail.ClientId > 0 {
        //    first, err := model.NewClientSearch(nil).SetId(detail.ClientId).First()
        //    if err == nil {
        //        clientName = first.Name
        //    }
        //}
        //wmsProducts := make([]*product_inventory.InventoryProduct, 0)
        //for _, product := range detail.Products {
        //    var p product_inventory.InventoryProduct
        //    p.Id = product.Number
        //    p.Amount = product.Amount.String()
        //    wmsProducts = append(wmsProducts, &p)
        //}
        //clientWms := product_inventory.NewProductInventoryServiceClient(grpc_init.ProductInventoryServiceConn)
        //_, err = clientWms.CreateOperation(ctx, &product_inventory.CreateOperationRequest{
        //    Number:      detail.Number,
        //    Addressee:   detail.Addressee,
        //    Address:     detail.Address,
        //    Phone:       detail.Phone,
        //    DeliverType: int32(detail.DeliverType),
        //    Source:      "CRM",
        //    ClientId:    int64(detail.ClientId),
        //    ClientName:  clientName,
        //    ProductList: wmsProducts,
        //})
        //if err != nil {
        //    //状态还原
        //    m["status"] = constvar.WaitConfirmed
        //    _ = model.NewSalesDetailsSearch().SetNumber(detail.Number).UpdateByMap(m)
        //    return nil, err
        //}
 
        //推送到aps
        products := make([]*SalesDetailsProduct, 0)
        var total decimal.Decimal
        for _, product := range detail.Products {
            var sp SalesDetailsProduct
            sp.ProductId = product.Number
            sp.Amount = product.Amount.IntPart()
            products = append(products, &sp)
            total = total.Add(product.Amount)
        }
 
        client := NewCrmAndApsGrpcServiceClient(grpc_init.CrmApsGrpcServiceConn)
        _, err = client.SendSalesDetailsToApsProject(ctx, &SendSalesDetailsToApsProjectRequest{
            Number:       detail.Number,
            ClientName:   detail.Client.Name,
            MemberName:   detail.Member.Username,
            SignTime:     detail.SignTime,
            DeliveryDate: detail.DeliveryDate,
            Source:       "CRM",
            ProductTotal: total.IntPart(),
            ProjectId:    req.ProjectId,
            Products:     products,
            DeliverType:  int64(detail.DeliverType),
        })
        if err != nil {
            //状态还原
            m["status"] = constvar.WaitConfirmed
            _ = model.NewSalesDetailsSearch().SetNumber(detail.Number).UpdateByMap(m)
            return nil, err
        }
    }
    return new(SendSalesDetailsAndProjectToCrmResponse), nil
}
 
func (s *Server) GetClientList(ctx context.Context, req *GetClientListRequest) (*GetClientListResponse, error) {
    if req.Page <= 0 || req.PageSize == 0 {
        return nil, errors.New("参数错误")
    }
    clientService := new(service.ClientService)
    params := map[string]interface{}{}
    if req.Keyword != "" {
        params["name"] = req.Keyword
    }
    clients, total, errCode := clientService.GetClientList(int(req.Page), int(req.PageSize), params)
    if errCode != ecode.OK {
        return nil, errors.New(fmt.Sprintf("内部错误, code:%v", errCode))
    }
    resp := new(GetClientListResponse)
    resp.Total = total
    resp.List = make([]*Client, 0, len(clients))
    for _, client := range clients {
        resp.List = append(resp.List, &Client{
            Number: client.Number,
            Name:   client.Name,
            Id:     int64(client.Id),
        })
    }
    return resp, nil
}
 
func (s *Server) GetClientMaxId(ctx context.Context, req *ClientMaxIdRequest) (*ClientMaxIdResponse, error) {
    clientService := new(service.ClientService)
    id, err := clientService.GetClientMaxId()
    return &ClientMaxIdResponse{Id: int64(id)}, err
}
func (s *Server) AddClient(ctx context.Context, req *AddClientRequest) (*ClientMsgResponse, error) {
    clientService := new(service.ClientService)
    client := &model.Client{Id: int(req.Id), Number: req.Number, Name: req.Name, DetailAddress: req.DetailAddress, Remark: req.Remark, CreatorId: int(req.CreatorId)}
    code := clientService.AddClient(client, 0)
    if code != ecode.OK {
        return &ClientMsgResponse{Code: int64(code), Msg: "添加失败", Id: strconv.Itoa(client.Id)}, nil
    }
    return &ClientMsgResponse{Code: int64(code), Msg: "添加成功", Id: strconv.Itoa(client.Id)}, nil
}
func (s *Server) EditClient(ctx context.Context, req *EditClientRequest) (*ClientMsgResponse, error) {
    clientService := new(service.ClientService)
    client := &model.Client{Id: int(req.Id), Number: req.Number, Name: req.Name, DetailAddress: req.DetailAddress, Remark: req.Remark, CreatorId: int(req.CreatorId)}
    code := clientService.UpdateClient(client, 0)
    if code != ecode.OK {
        return &ClientMsgResponse{Code: int64(code), Msg: "添加失败", Id: strconv.Itoa(client.Id)}, nil
    }
    return &ClientMsgResponse{Code: int64(code), Msg: "添加成功", Id: strconv.Itoa(client.Id)}, nil
}
func (s *Server) DelClient(ctx context.Context, req *DelClientRequest) (*ClientMsgResponse, error) {
    clientService := new(service.ClientService)
 
    code := clientService.DeleteClient([]int{int(req.Id)})
    if code != ecode.OK {
        return &ClientMsgResponse{Code: int64(code), Msg: "删除失败", Id: strconv.FormatInt(req.Id, 10)}, nil
    }
    return &ClientMsgResponse{Code: int64(code), Msg: "删除成功", Id: strconv.FormatInt(req.Id, 10)}, nil
}
 
func (s *Server) UpdateSalesDetail(ctx context.Context, req *UpdateSalesDetailRequest) (*UpdateSalesDetailResponse, error) {
    if req.Number == "" {
        return nil, errors.New("销售明细编号为空")
    }
    if req.Status == 0 {
        return nil, errors.New("销售明细状态为空")
    }
 
    status := constvar.SalesDetailsStatus(int(req.Status))
    if !status.Valid() {
        return nil, errors.New("销售明细状态不正确")
    }
 
    salesDetail, err := model.NewSalesDetailsSearch().SetNumber(req.Number).First()
    if err == gorm.ErrRecordNotFound {
        return nil, errors.New("销售明细不存在")
    }
 
    salesDetail.Status = status
    err = model.NewSalesDetailsSearch().SetId(salesDetail.Id).Update(salesDetail)
    if err != nil {
        return nil, errors.New("更改CRM销售明细失败")
    }
 
    return &UpdateSalesDetailResponse{}, nil
}
 
func (s *Server) RemoveSalesDetail(ctx context.Context, req *RemoveSalesDetailRequest) (*RemoveSalesDetailResponse, error) {
    if req.Number == "" {
        return nil, errors.New("销售明细编号为空")
    }
 
    _, err := model.NewSalesDetailsSearch().SetNumber(req.Number).First()
    if err == gorm.ErrRecordNotFound {
        return nil, errors.New("销售明细不存在")
    }
 
    err = model.NewSalesDetailsSearch().SetNumber(req.Number).Delete()
    if err == gorm.ErrRecordNotFound {
        return nil, errors.New("销售明细删除失败")
    }
 
    return &RemoveSalesDetailResponse{}, nil
}
 
func (s *Server) GetCrmSalesDetailsProductInfo(ctx context.Context, req *GetCrmSalesDetailsProductInfoRequest) (*GetCrmSalesDetailsProductInfoResponse, error) {
    if req.SalesDetailsNumber == "" {
        return nil, errors.New("销售明细编号为空")
    }
    first, err := model.NewSalesDetailsSearch().SetPreload(true).SetNumber(req.SalesDetailsNumber).First()
    if err != nil {
        return nil, err
    }
    list := make([]*CrmSalesDetailsProductInfo, 0)
    for _, product := range first.Products {
        var csp CrmSalesDetailsProductInfo
        csp.ProductId = product.Number
        csp.ProductName = product.Name
        csp.Specs = product.Specs
        csp.Unit = product.Unit
        csp.Amount = product.Amount.IntPart()
        csp.Cost = product.Cost
        csp.Price = product.Price.IntPart()
        csp.Total = product.Total.IntPart()
        csp.Profit = product.Profit
        csp.Margin = product.Margin
        list = append(list, &csp)
    }
    resp := new(GetCrmSalesDetailsProductInfoResponse)
    resp.List = list
    return resp, nil
}