| | |
| | | "aps_crm/constvar" |
| | | "aps_crm/model" |
| | | "aps_crm/model/grpc_init" |
| | | "aps_crm/proto/product_inventory" |
| | | "aps_crm/pkg/ecode" |
| | | "aps_crm/service" |
| | | "context" |
| | | "errors" |
| | | "fmt" |
| | | "github.com/shopspring/decimal" |
| | | "gorm.io/gorm" |
| | | "strconv" |
| | | ) |
| | | |
| | | type Server struct { |
| | |
| | | } |
| | | |
| | | //推送到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 |
| | | } |
| | | //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) |
| | |
| | | MemberName: detail.Member.Username, |
| | | SignTime: detail.SignTime, |
| | | DeliveryDate: detail.DeliveryDate, |
| | | Source: detail.Source, |
| | | Source: "CRM", |
| | | ProductTotal: total.IntPart(), |
| | | ProjectId: req.ProjectId, |
| | | Products: products, |
| | | DeliverType: int64(detail.DeliverType), |
| | | }) |
| | | if err != nil { |
| | | //状态还原 |
| | |
| | | } |
| | | 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 |
| | | } |