hutongqing
2024-12-14 e7cf443b37f8f4d8a1bc4fe4cd6f058f39e5c7f5
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
using AutoMapper;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.OrderEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_Core.Utilities;
using WIDESEA_DTO.ERP;
using WIDESEA_DTO.Inbound;
using WIDESEA_DTO.Outbound;
using WIDESEA_IBasicService;
using WIDESEA_IOutboundRepository;
using WIDESEA_IOutboundService;
using WIDESEA_IStockService;
using WIDESEA_Model.Models;
 
namespace WIDESEA_OutboundService
{
    public class OutboundOrderService : ServiceBase<Dt_OutboundOrder, IOutboundOrderRepository>, IOutboundOrderService
    {
        private readonly IMapper _mapper;
        private readonly IBasicService _basicService;
        private readonly IOutboundOrderDetailRepository _outboundOrderDetailRepository;
        private readonly IStockInfoService _stockInfoService;
 
        public IOutboundOrderRepository Repository => BaseDal;
 
        public OutboundOrderService(IOutboundOrderRepository BaseDal, IMapper mapper, IBasicService basicService, IOutboundOrderDetailRepository outboundOrderDetailRepository, IStockInfoService stockInfoService) : base(BaseDal)
        {
            _mapper = mapper;
            _basicService = basicService;
            _outboundOrderDetailRepository = outboundOrderDetailRepository;
            _stockInfoService = stockInfoService;
        }
 
        public WebResponseContent ReceiveOutOrder(ErpOutOrderDTO model)
        {
            try
            {
                Dt_MaterielInfo materielInfo = _basicService.MaterielInfoService.Repository.QueryFirst(x => x.MaterielCode == model.MCode);
                if (materielInfo == null)
                {
                    return WebResponseContent.Instance.Error($"未找到该物料信息");
                }
 
                Dt_Warehouse warehouse = _basicService.WarehouseService.Repository.QueryFirst(x => x.WarehouseCode == model.WaId);
                if (warehouse == null)
                {
                    return WebResponseContent.Instance.Error($"未找到该仓库信息");
                }
 
                Dt_OutboundOrder oldOutboundOrder = BaseDal.Db.Queryable<Dt_OutboundOrder>().Where(x => x.UpperOrderNo == model.OrderNo).Includes(x => x.Details).First();
 
                if (model.Way == 1)
                {
                    if (oldOutboundOrder != null)
                    {
                        if (oldOutboundOrder.Details.FirstOrDefault(x => x.RowNo == Convert.ToInt32(model.RowNo)) != null)
                        {
                            return WebResponseContent.Instance.Error($"该明细行号已存在");
                        }
                        if (oldOutboundOrder.WarehouseId != warehouse.WarehouseId)
                        {
                            return WebResponseContent.Instance.Error($"仓库不一致");
                        }
                        else
                        {
                            Dt_OutboundOrderDetail outboundOrderDetail = new Dt_OutboundOrderDetail()
                            {
                                RowNo = Convert.ToInt32(model.RowNo),
                                BatchNo = model.MLot,
                                MaterielCode = model.MCode,
                                MaterielName = materielInfo.MaterielName,
                                OrderDetailStatus = OrderDetailStatusEnum.New.ObjToInt(),
                                OrderQuantity = model.Qty,
                                OrderId = oldOutboundOrder.Id
                            };
 
                            _outboundOrderDetailRepository.AddData(outboundOrderDetail);
                        }
                    }
                    else
                    {
                        Dt_OutboundOrderDetail outboundOrderDetail = new Dt_OutboundOrderDetail()
                        {
                            RowNo = Convert.ToInt32(model.RowNo),
                            BatchNo = model.MLot,
                            MaterielCode = model.MCode,
                            MaterielName = materielInfo.MaterielName,
                            OrderDetailStatus = OrderDetailStatusEnum.New.ObjToInt(),
                            OrderQuantity = model.Qty,
                        };
 
                        Dt_OutboundOrder outboundOrder = new Dt_OutboundOrder()
                        {
                            UpperOrderNo = model.OrderNo,
                            OrderStatus = OutOrderStatusEnum.未开始.ObjToInt(),
                            OrderType = OutOrderTypeEnum.Issue.ObjToInt(),
                            CreateType = OrderCreateTypeEnum.UpperSystemPush.ObjToInt(),
                            WarehouseId = warehouse.WarehouseId,
                            Details = new List<Dt_OutboundOrderDetail> { outboundOrderDetail }
                        };
 
                        Db.InsertNav(outboundOrder).Include(x => x.Details).ExecuteCommand();
                    }
                }
                else if (model.Way == 2)
                {
                    if (oldOutboundOrder == null)
                    {
                        return WebResponseContent.Instance.Error($"未找到该出库单");
                    }
                    Dt_OutboundOrderDetail? outboundOrderDetail = oldOutboundOrder.Details.FirstOrDefault(x => x.RowNo == Convert.ToInt32(model.RowNo));
                    if (outboundOrderDetail == null)
                    {
                        return WebResponseContent.Instance.Error($"未找到该明细行号信息");
                    }
                    if (outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.New.ObjToInt())
                    {
                        return WebResponseContent.Instance.Error($"该明细不可修改");
                    }
                    outboundOrderDetail = new Dt_OutboundOrderDetail()
                    {
                        RowNo = Convert.ToInt32(model.RowNo),
                        BatchNo = model.MLot,
                        MaterielCode = model.MCode,
                        MaterielName = materielInfo.MaterielName,
                        OrderDetailStatus = OrderDetailStatusEnum.New.ObjToInt(),
                        OrderQuantity = model.Qty,
                    };
 
                    _outboundOrderDetailRepository.UpdateData(outboundOrderDetail);
                }
                else if (model.Way == 3)
                {
                    if (oldOutboundOrder == null)
                    {
                        return WebResponseContent.Instance.Error($"未找到该出库单");
                    }
                }
 
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
        public WebResponseContent ReleaseOutOrder(int id)
        {
            try
            {
                Dt_OutboundOrder outboundOrder = Db.Queryable<Dt_OutboundOrder>().Where(x => x.Id == id).Includes(x => x.Details).First();
                if (outboundOrder == null)
                {
                    return WebResponseContent.Instance.Error($"未找到该出库单");
                }
                if(outboundOrder.Details == null || outboundOrder.Details.Count == 0)
                {
                    return WebResponseContent.Instance.Error($"未找到明细信息");
                }
 
                foreach (var item in outboundOrder.Details)
                {
                }
 
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
    }
}