wangxinhui
2025-11-21 5336bfc54525253a30f1f8238806d3a67f388e14
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
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core;
using WIDESEA_DTO.MES;
using WIDESEA_IBasicRepository;
using WIDESEA_IBasicService;
using WIDESEA_IOutboundRepository;
using WIDESEA_IOutboundService;
using WIDESEA_IRecordService;
using WIDESEA_IStockService;
using WIDESEA_Model.Models;
using WIDESEA_DTO.ERP;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Common.MaterielEnum;
using WIDESEA_Common.WareHouseEnum;
using WIDESEA_Core.Helper;
using System.Reflection;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Http;
using HslCommunication;
 
namespace WIDESEA_OutboundService
{
    public class OutSGOrderService : ServiceBase<Dt_OutSGOrder, IOutSGOrderRepository>, IOutSGOrderService
    {
        public IOutSGOrderRepository Repository => BaseDal;
        private IBasicRepository _basicRepository;
        private IStockService _stockService;
        private IBasicService _basicService;
        private ILocationStatusChangeRecordService _locationStatusChangeRecordService;
        private readonly IMapper _mapper;
        private readonly IUnitOfWorkManage _unitOfWorkManage;
 
        public OutSGOrderService(IOutSGOrderRepository BaseDal, IBasicRepository basicRepository, IStockService stockService, IBasicService basicService, ILocationStatusChangeRecordService locationStatusChangeRecordService, IMapper mapper,IUnitOfWorkManage unitOfWorkManage) : base(BaseDal)
        {
            _basicRepository = basicRepository;
            _stockService = stockService;
            _basicService = basicService;
            _locationStatusChangeRecordService = locationStatusChangeRecordService;
            _mapper = mapper;
            _unitOfWorkManage = unitOfWorkManage;
        }
        
        public WebResponseContent AddOutSGOrder(List<SGOutOrderDTO> outOrderDTOs)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                //获取所有排程单
                List<Dt_OutSGOrder> outSGOrders = BaseDal.Db.Queryable<Dt_OutSGOrder>().Includes(x => x.Details).ToList();
                List<Dt_OutSGOrderDetail> outSGOrderDetails= outSGOrders.SelectMany(x=>x.Details).ToList();
                ////判断单据
                //Dt_OutSGOrderDetail? ExistAddOutOrderDetail = outSGOrderDetails.FirstOrDefault(x => outOrderDTOs.Select(t => t.BoardMpsDetailId).Distinct().Contains(x.BoardMpsDetailId));
                //if (ExistAddOutOrderDetail != null)
                //{
                //    return content.Error($"出库排程明细{nameof(SGOutOrderDTO.BoardMpsDetailId)}:{ExistAddOutOrderDetail.BoardMpsDetailId}已存在");
                //}
                //获取所有物料
                List<Dt_MaterielInfo> materielInfos = _basicRepository.MaterielInfoRepository.QueryData(x => x.WarehouseId == WarehouseEnum.LLDYL.ObjToInt() && x.MaterielInvOrgId == MaterielInvOrgEnum.老厂.ObjToInt() && x.MaterialSourceId != 0);
                SGOutOrderDTO? sGOutOrderDTO = outOrderDTOs.FirstOrDefault(x => !materielInfos.Select(x => x.MaterielCode).Contains(x.MaterialNo));
                //SGOutOrderDTO? sGOutOrderDTO = outOrderDTOs.FirstOrDefault(x => materielInfos.FirstOrDefault(t=>t.MaterielCode.StartsWith(x.MaterialNo))==null);
                if (sGOutOrderDTO != null)
                {
                    return content.Error($"生产排程{sGOutOrderDTO.OrderId}物料:{sGOutOrderDTO.MaterialNo}不存在");
                }
                List<Dt_OutSGOrder> AddOutSGOrders = new List<Dt_OutSGOrder>();
                foreach (var item in outOrderDTOs.OrderBy(x=>x.Number))
                {
                    //获取工单
                    Dt_OutSGOrder? ExistOutSGOrder = AddOutSGOrders.FirstOrDefault(x => x.OrderId == item.OrderId);
                    
                    //明细提前转换
                    Dt_OutSGOrderDetail outSGOrderDetail = _mapper.Map<Dt_OutSGOrderDetail>(item);
                    //获取物料
                    Dt_MaterielInfo? materielInfo = materielInfos.FirstOrDefault(x => x.MaterielCode == item.MaterialNo);
                    string code = ExtractFirstPercentContent(outSGOrderDetail.Remark);
                    //处理特殊指定排程判断
                    if (!code.IsNullOrEmpty() && code.StartsWith(outSGOrderDetail.MaterialNo))
                    {
                        materielInfo = materielInfos.FirstOrDefault(x => x.MaterielCode == code);
                        if (materielInfo==null)
                        {
                            return content.Error($"生产排程{item.OrderId}指定排程物料:{code}不存在");
                        }
                        outSGOrderDetail.MaterialNo = code;
                    }
                    outSGOrderDetail.MaterialName = materielInfo.MaterielName;
                    
                    //判断工单是否已经存在
                    if (ExistOutSGOrder != null)
                    {
                        ExistOutSGOrder.Details.Add(outSGOrderDetail);
                    }
                    else
                    {
                        Dt_OutSGOrder outSGOrder = _mapper.Map<Dt_OutSGOrder>(item);
                        outSGOrder.Details = new List<Dt_OutSGOrderDetail>() { outSGOrderDetail };
                        AddOutSGOrders.Add(outSGOrder);
                    }
                }
                BaseDal.Db.InsertNav(AddOutSGOrders).Include(x => x.Details).ExecuteCommand();
                content.OK("接收排程成功");
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
        public static string ExtractFirstPercentContent(string input)
        {
            if (string.IsNullOrEmpty(input))
                return null;
 
            Regex regex = new Regex(@"%(.*?)%");
            Match match = regex.Match(input);
 
            if (match.Success && match.Groups[1].Success)
            {
                return match.Groups[1].Value.Trim();
            }
 
            return null;
        }
    }
}