zhangqian
2023-12-18 ed3d45b189f102249eabd0cb5a0299bc66b0dea6
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
package inventory_order
 
import (
    "context"
    "github.com/shopspring/decimal"
    "strconv"
    "time"
    "wms/constvar"
    "wms/models"
    "wms/pkg/timex"
)
 
type Server struct {
    UnimplementedInventoryOrderServiceServer
}
 
func (s *Server) CreateOperationList(ctx context.Context, req *CreateOperationListRequest) (*CreateOperationListResponse, error) {
    var operations []*models.Operation
    warehouse, err := models.NewWarehouseSearch().First()
    if err != nil {
        return nil, err
    }
    fromLocation, err := models.NewLocationSearch().SetID(warehouse.LocationId).First()
    if err != nil {
        return nil, err
    }
    toLocation, err := models.NewLocationSearch().SetType(int(constvar.LocationTypeCustomer)).First()
    if err != nil {
        return nil, err
    }
    tp := constvar.BaseOperationTypeOutgoing
    if req.OperationType == 1 {
        tp = constvar.BaseOperationTypeIncoming
        fromLocation, err = models.NewLocationSearch().SetType(int(constvar.LocationTypeVendor)).First()
        if err != nil {
            return nil, err
        }
        toLocation, err = models.NewLocationSearch().SetID(warehouse.LocationId).First()
        if err != nil {
            return nil, err
        }
    }
    operationType, err := models.NewOperationTypeSearch().SetWarehouseId(warehouse.Id).SetBaseOperationType(tp).First()
    if err != nil {
        return nil, err
    }
    operationResp := make([]*OperationResponse, 0)
    for _, list := range req.List {
        var operation models.Operation
        var details []*models.OperationDetails
        var or OperationResponse
        operation.SourceNumber = list.SourceNumber
        operation.OperationDate = timex.TimeToString2(time.Now())
        operation.Number = strconv.FormatInt(time.Now().Unix(), 10)
        operation.Status = constvar.OperationStatus_Ready
        operation.OperationTypeName = operationType.Name
        operation.OperationTypeId = operationType.Id
        operation.FromLocationID = fromLocation.Id
        operation.ToLocationID = toLocation.Id
        operation.BaseOperationType = constvar.BaseOperationTypeOutgoing
        operation.Source = req.Source
        or.WorkOrderId = operation.SourceNumber
        or.Number = operation.Number
        operationResp = append(operationResp, &or)
        if req.OperationType == 1 {
            operation.BaseOperationType = constvar.BaseOperationTypeIncoming
        }
        for _, product := range list.Products {
            var detail models.OperationDetails
            detail.ProductId = product.ProductNumber
            detail.Amount = decimal.NewFromInt(product.Amount)
            details = append(details, &detail)
        }
        operation.Details = details
        operations = append(operations, &operation)
    }
    err = models.NewOperationSearch().CreateBatch(operations)
    resp := new(CreateOperationListResponse)
    resp.List = operationResp
    return resp, err
}
 
func (s *Server) GetWarehouseInfo(ctx context.Context, req *GetWarehouseInfoRequest) (*GetWarehouseInfoResponse, error) {
    warehouses, err := models.NewWarehouseSearch().FindNotTotal()
    if err != nil {
        return nil, err
    }
    list := make([]*WarehouseInfo, 0)
    for _, warehouse := range warehouses {
        var wi WarehouseInfo
        wi.Id = strconv.Itoa(warehouse.Id)
        wi.Name = warehouse.Name
        list = append(list, &wi)
    }
    resp := new(GetWarehouseInfoResponse)
    resp.List = list
    return resp, nil
}