1
dengjunjie
2025-05-08 092971a8ba7848f024427694c642959d0fbc8599
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
using AutoMapper;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_Core.Utilities;
using WIDESEA_DTO.Inbound;
using WIDESEA_DTO.Outbound;
using WIDESEA_IBasicRepository;
using WIDESEA_IBasicService;
using WIDESEA_IOutboundRepository;
using WIDESEA_IOutboundService;
using WIDESEA_IStockService;
using WIDESEA_ITaskInfoRepository;
using WIDESEA_Model.Models;
 
namespace WIDESEA_OutboundService
{
    public partial class OutboundOrderService : ServiceBase<Dt_OutboundOrder, IOutboundOrderRepository>, IOutboundOrderService
    {
        private readonly IMapper _mapper;
        private readonly IMaterielInfoService _materielInfoService;
        private readonly IStockInfoService _stockService;
        private readonly IStockInfoDetailService _stockDetailService;
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        private readonly IBasicRepository _basicRepository;
        private readonly ITaskRepository _taskRepository;
        private readonly IOutboundOrderDetailRepository _outboundOrderDetailRepository;
        private readonly SqlSugarClient _dbBase;
 
        public IOutboundOrderRepository Repository => BaseDal;
 
        public OutboundOrderService(IOutboundOrderRepository BaseDal, IBasicRepository basicRepository, IMapper mapper, IMaterielInfoService materielInfoService, IStockInfoDetailService stockDetailService, IStockInfoService stockInfoService, IUnitOfWorkManage unitOfWorkManage, ITaskRepository taskRepository, IOutboundOrderDetailRepository outboundOrderDetailRepository) : base(BaseDal)
        {
            _mapper = mapper;
            _materielInfoService = materielInfoService;
            _stockDetailService = stockDetailService;
            _basicRepository = basicRepository;
            _stockService = stockInfoService;
            _dbBase = unitOfWorkManage.GetDbClient();
            _taskRepository = taskRepository;
            _outboundOrderDetailRepository = outboundOrderDetailRepository;
        }
 
        public override WebResponseContent AddData(SaveModel saveModel)
        {
            OutboundOrderAddDTO outboundOrder = saveModel.MainData.DicToModel<OutboundOrderAddDTO>();
            List<OutboundOrderDetailAddDTO> orderDetailAddDTOs = saveModel.DetailData.DicToIEnumerable<OutboundOrderDetailAddDTO>();
            #region 根据物料编号合并出库数量
            //outboundOrder.Details = orderDetailAddDTOs.GroupBy(x => x.MaterielCode).Select(x => new OutboundOrderDetailAddDTO
            //{
            //    BatchNo = x.FirstOrDefault()?.BatchNo ?? "",
            //    MaterielCode = x.Key,
            //    OrderQuantity = x.Sum(x => x.OrderQuantity),
            //    Remark = x.FirstOrDefault(v => !string.IsNullOrEmpty(v.Remark))?.Remark ?? ""
            //}).ToList(); 
            #endregion
            outboundOrder.Details = new List<OutboundOrderDetailAddDTO>();
            outboundOrder.Details.AddRange(orderDetailAddDTOs);
            return AddOutboundOrder(outboundOrder);
        }
 
        public override WebResponseContent UpdateData(SaveModel saveModel)
        {
            List<Dt_OutboundOrderDetail> outboundOrderDetails = saveModel.DetailData.DicToIEnumerable<Dt_OutboundOrderDetail>();
            if (outboundOrderDetails.GroupBy(x => x.MaterielCode).Select(x => x.Count()).Any(x => x > 1))
            {
                return WebResponseContent.Instance.Error("物料重复");
            }
            outboundOrderDetails = outboundOrderDetails.Where(x => (x.Id > 0 && x.OrderDetailStatus == OrderDetailStatusEnum.New.ObjToInt()) || x.Id == 0).ToList();
 
            List<Dictionary<string, object>> dics = new List<Dictionary<string, object>>();
            JsonSerializerSettings settings = new JsonSerializerSettings();
            settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            foreach (var item in outboundOrderDetails)
            {
                string str = JsonConvert.SerializeObject(item, settings);
                Dictionary<string, object>? dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(str);
                if (dic != null)
                    dics.Add(dic);
            }
            saveModel.DetailData = dics;
            return base.UpdateData(saveModel);
        }
 
        public override WebResponseContent DeleteData(object[] keys)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                List<Dt_OutboundOrder> _OutboundOrders = BaseDal.Db.Queryable<Dt_OutboundOrder>().Includes(x => x.Details).Where(x => keys.Contains(x.Id)).ToList();
                if (_OutboundOrders.Count < 1) throw new Exception("未找到出库单");
                List<Dt_OutboundOrderDetail> orderDetails = new List<Dt_OutboundOrderDetail>();
                foreach (var item in _OutboundOrders)
                {
                    if (item.Details.Where(x => x.LockQuantity != x.OverOutQuantity).Any())
                        throw new Exception("存在未出库完成托盘");
                    orderDetails.AddRange(item.Details);
                    item.Details = null;
                }
                BaseDal.DeleteAndMoveIntoHty(_OutboundOrders, OperateType.人工删除);
                content.Status = _outboundOrderDetailRepository.DeleteAndMoveIntoHty(orderDetails, OperateType.人工删除);
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
 
        public WebResponseContent AddOutboundOrder(OutboundOrderAddDTO orderAddDTO)
        {
            WebResponseContent content = new();
            try
            {
                #region 验证数据
                (bool, string, object?) result = CheckOutboundOrderAddData(orderAddDTO);
                if (!result.Item1) return content = WebResponseContent.Instance.Error(result.Item2);
                #endregion
 
                Dt_OutboundOrder outboundOrder = _mapper.Map<Dt_OutboundOrder>(orderAddDTO);
                outboundOrder.OrderNo = DateTime.Now.ToString("yyMMddHHmmss");
                outboundOrder.OrderStatus = InboundStatusEnum.未开始.ObjToInt();
                bool a = BaseDal.Db.InsertNav(outboundOrder).Include(x => x.Details).ExecuteCommand();
                content = WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error(ex.Message);
            }
            finally
            {
 
            }
            return content;
        }
 
        private (bool, string, object?) CheckOutboundOrderAddData(OutboundOrderAddDTO outboundOrderAddDTO)
        {
            (bool, string, object?) result1 = ModelValidate.ValidateModelData(outboundOrderAddDTO);
            if (!result1.Item1) return result1;
 
            (bool, string, object?) result2 = ModelValidate.ValidateModelData(outboundOrderAddDTO.Details);
            if (!result2.Item1) return result2;
 
            IEnumerable<int> inOrderTypes = Enum.GetValues<OutOrderTypeEnum>().Cast<int>();
            if (!inOrderTypes.Contains(outboundOrderAddDTO.OrderType))
            {
                return (false, "未找到该单据类型", outboundOrderAddDTO);
            }
 
            List<string> materielCodes = outboundOrderAddDTO.Details.Select(x => x.MaterielCode).ToList();
            if (!_materielInfoService.ExsitMateriels(materielCodes))
            {
                return (false, "有物料信息未录入,请录入物料信息", outboundOrderAddDTO);
            }
 
            if (BaseDal.QueryFirst(x => x.UpperOrderNo == outboundOrderAddDTO.UpperOrderNo && !string.IsNullOrEmpty(x.UpperOrderNo)) != null)
            {
                return (false, "单据已存在", outboundOrderAddDTO);
            }
            return (true, "成功", outboundOrderAddDTO);
        }
    }
}