ApiInterfaceLogAction.cs 20.5 KB
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Xml;
using Infrastructure;
using Quartz;
using ServiceReference4;
using ServiceReference5;
using WebApp;
using WebRepository;

namespace WebMvc
{
    /// <summary>
    ///  接口回传
    /// </summary>
    [PersistJobDataAfterExecution]
    [DisallowConcurrentExecution]
    public class ApiInterfaceLogAction
    {
        private string ConnString { set; get; }
        IJobExecutionContext Context { set; get; }

        public ApiInterfaceLogAction(string _ConnString, IJobExecutionContext _Context)
        {
            ConnString = _ConnString;
            Context = _Context;
        }

        public void Execute(JobContainer jobContainer)
        {
            UnitWork _unitWork = new UnitWork(AppSettingsJson.JobContext(ConnString));
            var SourceCode = "";
            try
            {
                List<ApiInterfaceLog> ApiinterfaceLogs = _unitWork.Find<ApiInterfaceLog>(n => (n.AllNum == n.ComNum && n.AllNum > 0 && n.Status == 0 && n.Type == "ApiLK_In") || (n.ComNum > 0 && n.AllNum != 0 && n.Type == "ApiLK_Out" && n.Status == 0)).ToList();
                foreach (ApiInterfaceLog ApiinterfaceLog in ApiinterfaceLogs)
                {
                    SourceCode = ApiinterfaceLog.TaskNo;
                    //入库回传
                    if (ApiinterfaceLog.Type == "ApiLK_In")
                    {
                        //通过入库单据定位物料
                        List<ReceiptDetail> receiptDetails = _unitWork.Find<ReceiptDetail>(n => n.SourceCode == ApiinterfaceLog.TaskNo).ToList();
                        //回传单头
                        InAndBackModel inAndBackModel = new InAndBackModel();
                        inAndBackModel.State = "S";
                        inAndBackModel.MO = receiptDetails.First().Lot;
                        inAndBackModel.PN = receiptDetails.First().MaterialCode;
                        inAndBackModel.TaskNumber = ApiinterfaceLog.TaskNo;

                        //回传单身
                        List<InAndBackDetailModel> inAndBackDetails = new List<InAndBackDetailModel>();

                        foreach (var receipt in receiptDetails)
                        {
                            InAndBackDetailModel inAndBackDetail = new InAndBackDetailModel();
                            Inventory inventory = _unitWork.Find<Inventory>(n => n.QrCode == receipt.Project).FirstOrDefault();
                            if (inventory != null)
                            {
                                inAndBackDetail.LWH = inventory.LWH;
                                inAndBackDetail.LWHSL = inventory.LWHSL;
                                inAndBackDetail.Quantity = (decimal)inventory.Qty;
                                inAndBackDetail.Lot = inventory.BLot;
                                inAndBackDetail.Supllier = inventory.Supplier;
                                inAndBackDetail.Barcode = inventory.QrCode;
                                inAndBackDetail.Uow = inventory.Unit;
                            }
                            else
                            {
                                ApiinterfaceLog.Message = "单据已经完成,但是没有找到对应库存";
                                ApiinterfaceLog.Status = 4;
                                _unitWork.Update(ApiinterfaceLog);
                                continue;
                            }
                            inAndBackDetails.Add(inAndBackDetail);
                        }
                        inAndBackModel.DeliveryResults = inAndBackDetails;
                        //铜箔入库MES单据完成后向ERP接口表中填入数据,等待向ERP回传
                        ReceiptHeader receiptHeader1 = _unitWork.Find<ReceiptHeader>(n => n.SourceCode == ApiinterfaceLog.TaskNo).FirstOrDefault();
                        if (receiptHeader1 != null && receiptHeader1.Type == "ReturnCopperFoil")
                        {
                            InterfaceLog inter = new InterfaceLog
                            {
                                Type = "ErpSourceCode",
                                AllNum = 1,
                                ComNum = 1,
                                Request = "",
                                Status = 0,
                                TaskNo = receiptHeader1.ErpSourceCode,
                                CreateTime = DateTime.Now,
                                CreateBy = "WMS"
                            };
                            _unitWork.Add(inter);
                        }
                        ApiRequest apiRequest = new ApiRequest("MES");
                        //MesResponse MESesponse = new MesResponse
                        //{
                        //    Status = "S"
                        //};
                        MesResponse MESesponse = apiRequest.TokenPost<MesResponse>(JsonHelper.Instance.Serialize(inAndBackModel), "WmsService/ReportMaterialDistribution", "入库单据回传");

                        //回传状态成功
                        if (MESesponse.Status == "S")
                        {
                           List <ReceiptDetail> receipts = _unitWork.Find<ReceiptDetail>(n => n.SourceCode == ApiinterfaceLog.TaskNo && n.Status == ReceiptHeaderStatus.过账).ToList();
                            foreach (ReceiptDetail receipt in receipts)
                            {
                                if (receipt != null)
                                {
                                    receipt.Status = ReceiptHeaderStatus.回传;
                                    receipt.UpdateBy = "wms";
                                    receipt.UpdateTime = DateTime.Now;
                                    _unitWork.Update(receipt);
                                };
                            };

                            ApiInterfaceLog Back = new ApiInterfaceLog();
                            Back.TaskNo = ApiinterfaceLog.TaskNo;
                            Back.Request = inAndBackModel.ToJson();
                            Back.Type = "入库回传成功";
                            Back.Status = 1;
                            Back.Message = MESesponse.ToJson();
                            Back.CreateTime = DateTime.Now;
                            Back.CreateBy = "ApiinterfaceLog";
                            _unitWork.Add(Back);

                            if (ApiinterfaceLog.Status == 0)
                            {
                                ApiinterfaceLog.Status = 1;
                                ApiinterfaceLog.Message = "已全部回传成功:" + DateTime.Now;
                                ApiinterfaceLog.UpdateTime = DateTime.Now;
                            }

                            _unitWork.Update(ApiinterfaceLog);

                            //更新主入库单头尾状态
                            ReceiptHeader receiptHeader = _unitWork.Find<ReceiptHeader>(u => u.SourceCode == ApiinterfaceLog.TaskNo).FirstOrDefault();
                            ReceiptDetail receiptDetail1 = _unitWork.Find<ReceiptDetail>(u => u.SourceCode == ApiinterfaceLog.TaskNo).OrderBy(a => a.Status).FirstOrDefault();
                            if (receiptHeader.FirstStatus < receiptDetail1.Status)
                            {
                                receiptHeader.FirstStatus = receiptDetail1.Status;
                            }
                            if (receiptHeader.LastStatus < receiptDetail1.Status)
                            {
                                receiptHeader.LastStatus = receiptDetail1.Status;
                            }
                            receiptHeader.UpdateTime = DateTime.Now;
                            _unitWork.Update(receiptHeader);

                            //已完成的入库单据迁移至历史出库单据,暂以过账状态,后期与ERP对接后以回传状态
                            if (receiptHeader.LastStatus == ReceiptHeaderStatus.回传)
                            {
                                ReceiptHeaderHistory receiptHeaderHistory = new ReceiptHeaderHistory();
                                receiptHeader.CopyTo(receiptHeaderHistory);
                                receiptHeaderHistory.Id = null;
                                receiptHeaderHistory.UpdateBy = "ApiInterfaceLogAction";
                                _unitWork.Add(receiptHeaderHistory);

                                List<ReceiptDetail> receiptDetail1s = _unitWork.Find<ReceiptDetail>(u => u.SourceCode == ApiinterfaceLog.TaskNo).ToList();
                                foreach (ReceiptDetail rd in receiptDetail1s)
                                {
                                    ReceiptDetailHistory receiptDetailHistory = new ReceiptDetailHistory();
                                    rd.CopyTo(receiptDetailHistory);
                                    receiptDetailHistory.Id = null;
                                    receiptDetailHistory.ReceiptId = receiptHeaderHistory.Id;
                                    receiptDetailHistory.UpdateBy = "ApiInterfaceLogAction";
                                    _unitWork.Add(receiptDetailHistory);

                                    _unitWork.Delete(rd);
                                }
                                _unitWork.Delete(receiptHeader);
                            }
                        }
                        else
                        {
                            ApiInterfaceLog Back = new ApiInterfaceLog();
                            Back.TaskNo = ApiinterfaceLog.TaskNo;
                            Back.Request = inAndBackModel.ToJson();
                            Back.Type = "入库回传报错";
                            Back.Status = 4;
                            Back.Message = MESesponse.ToJson();
                            Back.CreateTime = DateTime.Now;
                            Back.CreateBy = "ApiinterfaceLog";
                            _unitWork.Add(Back);
                            ApiinterfaceLog.Status = 4;
                            ApiinterfaceLog.Message = MESesponse.ErrorMsg + DateTime.Now;
                            ApiinterfaceLog.UpdateTime = DateTime.Now;
                            _unitWork.Update(ApiinterfaceLog);
                        }
                    }
                    //出库回传
                    else if (ApiinterfaceLog.Type == "ApiLK_Out")
                    {
                        List<InventoryOut> inventoryOuts = _unitWork.Find<InventoryOut>(n => n.SourceCode == ApiinterfaceLog.TaskNo).ToList();
                        ApiShipmentDetail apiShipmentDetails = _unitWork.Find<ApiShipmentDetail>(n => n.SourceCode == ApiinterfaceLog.TaskNo).FirstOrDefault();
                        ApiShipmentHeader apiShipmentHeader = _unitWork.Find<ApiShipmentHeader>(n => n.SourceCode == ApiinterfaceLog.TaskNo).FirstOrDefault();
                        if (inventoryOuts.Count() > 0)
                        {
                            InAndBackModel inAndBackModel = new InAndBackModel();
                            if (apiShipmentDetails != null)
                            {
                                inAndBackModel.State = "2";
                                inAndBackModel.MO = inventoryOuts.First().Lot;
                                inAndBackModel.PN = inventoryOuts.First().MaterialCode;
                                inAndBackModel.TaskNumber = ApiinterfaceLog.TaskNo;

                                //回传单身
                                List<InAndBackDetailModel> inAndBackDetails = new List<InAndBackDetailModel>();
                                foreach (var inventoryOut in inventoryOuts)
                                {
                                    var lot = inventoryOut.BLot;
                                    if (apiShipmentHeader.Type == "CallPPForPrivateUse")
                                    {
                                        lot = inventoryOut.OldBatch;
                                    }
                                    string Station = apiShipmentHeader.Station;
                                    //转换站台
                                    switch (apiShipmentHeader.Station)
                                    {
                                        case "ProductStationD01":
                                            Station = "DT1";
                                            break;
                                        case "ProductStationD02":
                                            Station = "DT2";
                                            break;
                                        case "ProductStationD03":
                                            Station = "DT3";
                                            break;
                                        case "ProductStationD04":
                                            Station = "DT4";
                                            break;
                                        case "ProductStationD05":
                                            Station = "DT5";
                                            break;
                                        case "ProductStationD06":
                                            Station = "DT6";
                                            break;
                                        case "ProductStationD07":
                                            Station = "DT7";
                                            break;
                                        case "ProductStationD08":
                                            Station = "DT8";
                                            break;
                                        default:
                                            break;
                                    }
                                    InAndBackDetailModel inAndBackDetail = new InAndBackDetailModel();
                                    inAndBackDetail.Barcode = inventoryOut.QrCode;
                                    inAndBackDetail.Lot = lot;
                                    inAndBackDetail.Quantity = (decimal)inventoryOut.Qty;
                                    inAndBackDetail.Supllier = inventoryOut.Supplier;
                                    inAndBackDetail.LWH = inventoryOut.LWH;
                                    inAndBackDetail.LWHSL = Station;
                                    inAndBackDetail.Uow = inventoryOut.Unit;

                                    inAndBackDetails.Add(inAndBackDetail);
                                }
                                inAndBackModel.DeliveryResults = inAndBackDetails;
                            }
                            //铜箔出库MES单据完成后向ERP接口表中填入数据,等待向ERP回传
                            if (apiShipmentHeader.Type == "CallCopperFoil")
                            {
                                InterfaceLog inter = new InterfaceLog
                                {
                                    Type = "ErpSourceCode",
                                    AllNum = 1,
                                    ComNum = 1,
                                    Request = "",
                                    Status = 0,
                                    TaskNo = apiShipmentHeader.ErpSourceCode,
                                    CreateTime = DateTime.Now,
                                    CreateBy = "WMS"
                                };
                                _unitWork.Add(inter);
                            }

                            ApiRequest apiRequest = new ApiRequest("MES");
                            //MesResponse MESesponse = new MesResponse
                            //{
                            //    Status = "S"
                            //};
                            MesResponse MESesponse = apiRequest.TokenPost<MesResponse>(JsonHelper.Instance.Serialize(inAndBackModel), "WmsService/ReportMaterialDistribution", "出库单据回传");

                            //回传状态成功
                            if (MESesponse.Status == "S")
                            {
                                foreach (InventoryOut inv in inventoryOuts)
                                {
                                    inv.ShipmentState = 2;
                                    _unitWork.Update(inv);
                                }

                                apiShipmentDetails.Status = ShipmentHeaderStatus.回传;
                                apiShipmentDetails.UpdateBy = "wms";
                                apiShipmentDetails.UpdateTime = DateTime.Now;
                                _unitWork.Update(apiShipmentDetails);
                                //修改接口表状态
                                ApiInterfaceLog Back = new ApiInterfaceLog();
                                Back.TaskNo = ApiinterfaceLog.TaskNo;
                                Back.Request = inAndBackModel.ToJson();
                                Back.Type = "出库回传成功";
                                Back.Status = 1;
                                Back.Message = MESesponse.ToJson();
                                Back.CreateTime = DateTime.Now;
                                Back.CreateBy = "ApiinterfaceLog";
                                _unitWork.Add(Back);

                                if (ApiinterfaceLog.Status == 0)
                                {
                                    ApiinterfaceLog.Status = 1;
                                    ApiinterfaceLog.Message = "已全部回传成功:" + DateTime.Now;
                                    ApiinterfaceLog.UpdateTime = DateTime.Now;
                                }
                                _unitWork.Update(ApiinterfaceLog);


                                //删除出库单据
                                ApiShipmentHeaderHistory apiShipmentHeaderHistory = new ApiShipmentHeaderHistory();
                                apiShipmentHeader.CopyTo(apiShipmentHeaderHistory);
                                apiShipmentHeaderHistory.Id = null;
                                apiShipmentHeaderHistory.UpdateBy = "ApiInterfaceLogAction";
                                _unitWork.Add(apiShipmentHeaderHistory);
                               
                                ApiShipmentDetailHistory apiShipmentDetailHistory = new ApiShipmentDetailHistory();
                                apiShipmentDetails.CopyTo(apiShipmentDetailHistory);
                                apiShipmentDetailHistory.Id = null;
                                apiShipmentDetailHistory.ShipmentId = apiShipmentHeader.Id;
                                apiShipmentDetailHistory.UpdateBy = "ApiInterfaceLogAction";
                                _unitWork.Add(apiShipmentDetailHistory);

                                _unitWork.Delete(apiShipmentDetails);
                                _unitWork.Delete(apiShipmentHeader);
                            }
                            else
                            {
                                ApiInterfaceLog error11 = new ApiInterfaceLog();
                                error11.TaskNo = ApiinterfaceLog.TaskNo;
                                error11.Request = inAndBackModel.ToJson();
                                error11.Type = "出库回传报错";
                                error11.Status = 4;
                                error11.Message = MESesponse.ToJson();
                                error11.CreateTime = DateTime.Now;
                                _unitWork.Add(error11);
                                ApiinterfaceLog.Status = 4;
                                ApiinterfaceLog.Message = MESesponse.ErrorMsg + DateTime.Now;
                                ApiinterfaceLog.UpdateTime = DateTime.Now;
                                _unitWork.Update(ApiinterfaceLog);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ApiInterfaceLog Apiinter = new ApiInterfaceLog
                {
                    Type = "接口回传定时器报错",
                    AllNum = 0,
                    ComNum = 0,
                    Request = "",
                    Status = 4,
                    TaskNo = SourceCode,
                    Message = "异常单据号:" + SourceCode + ",Message:" + ex.Message + ",StackTrace:" + ex.StackTrace + ",Source" + ex.Source + ",Data" + ex.Data,
                    CreateTime = DateTime.Now,
                    CreateBy = "Api"
                };
                jobContainer.ExceptionInfo = ex.Message;
            }
        }
    }
}