| | |
| | | "aps_crm/constvar" |
| | | "aps_crm/model" |
| | | "aps_crm/model/grpc_init" |
| | | "aps_crm/pkg/ecode" |
| | | "aps_crm/proto/product_inventory" |
| | | "aps_crm/service" |
| | | "context" |
| | | "errors" |
| | | "fmt" |
| | | "github.com/shopspring/decimal" |
| | | "gorm.io/gorm" |
| | | ) |
| | | |
| | | type Server struct { |
| | |
| | | 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 { |
| | |
| | | MemberName: detail.Member.Username, |
| | | SignTime: detail.SignTime, |
| | | DeliveryDate: detail.DeliveryDate, |
| | | Source: detail.Source, |
| | | Source: "CRM", |
| | | ProductTotal: total.IntPart(), |
| | | ProjectId: req.ProjectId, |
| | | Products: products, |
| | | }) |
| | | 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, |
| | | }) |
| | | } |
| | | return resp, 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().Update(salesDetail) |
| | | if err != nil { |
| | | return nil, errors.New("更改CRM销售明细失败") |
| | | } |
| | | |
| | | return &UpdateSalesDetailResponse{}, nil |
| | | } |
| | | |
| | | func (s *Server) RemoveSalesDetail(ctx context.Context, req *RemoveSalesDetailRequest) (*RemoveSalesDetailRequest, 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 &RemoveSalesDetailRequest{}, nil |
| | | } |