InterfaceLogAction.cs 18.1 KB
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Xml;
using Infrastructure;
using Quartz;
using ServiceReference3;
using WebApp;
using WebRepository;

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

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

        public void Execute(JobContainer jobContainer)
        {
            UnitWork _unitWork = new UnitWork(AppSettingsJson.JobContext(ConnString));
            try
            {
                List<InterfaceLog> interfaceLogs = _unitWork.Find<InterfaceLog>(n => (n.AllNum == n.ComNum && n.AllNum > 0 && n.Status == 0) || (n.ComNum > 0 && n.AllNum > 0 && n.Status == 2 && n.Type == "LK_In")).ToList();
                foreach (InterfaceLog interfaceLog in interfaceLogs)
                {
                    if (interfaceLog.Type == "LK_In")
                    {
                        string name = "";
                        string value = "";
                        string Location = "";
                        string WareCell = "";
                        XmlDocument doc = new XmlDocument();
                        ConfigXmlDocument Config = new ConfigXmlDocument();
                        doc.LoadXml(interfaceLog.Request);
                        TIPTOPServiceGateWayPortTypeClient InBack = new TIPTOPServiceGateWayPortTypeClient();
                        XmlNodeList nodes = doc.SelectNodes("Request/RequestContent/Document/RecordSet/Master/Record/Field[@name='tc_dcf025']");
                        foreach (XmlNode node in nodes)
                        {
                            value = node.Attributes["value"].Value;
                            Inventory inventory = _unitWork.Find<Inventory>(n => n.SourceCode == interfaceLog.TaskNo && n.QrCode == value).FirstOrDefault();
                            if (inventory != null)
                            {
                                if (inventory.Status != "good")
                                {

                                }
                                if (inventory.WarehouseType == "YCL_WareCell")
                                {
                                    Location = "B1032";
                                    WareCell = "B0030";
                                }
                                else if(inventory.WarehouseType == "PPJ_WareCell")
                                {
                                    Location = "B1011";
                                    WareCell = "B0030";
                                }
                                else if (inventory.WarehouseType == "PPP_WareCell")
                                {
                                    Location = "B1020";
                                    WareCell = "B0030";
                                }
                                else if (inventory.WarehouseType == "TB_WareCell")
                                {
                                    Location = "B1031";
                                    WareCell = "B0030";
                                }
                                else if (inventory.WarehouseType == "CP_WareCell")
                                {
                                    Location = "B1010";
                                    WareCell = "B0030";
                                }
                                XmlNodeList nodes_1 = node.SelectSingleNode("parent::*").ChildNodes;
                                foreach (XmlElement nodeEle in nodes_1)
                                {
                                    if (nodeEle.GetAttribute("name") == "tc_dcf022")
                                    {
                                        nodeEle.SetAttribute("value", inventory.Qty.ToString());
                                    }
                                    else if (nodeEle.GetAttribute("name") == "tc_dcf023")
                                    {
                                        nodeEle.SetAttribute("value", "1");
                                    }
                                    else if (nodeEle.GetAttribute("name") == "tc_dcf024")
                                    {
                                        nodeEle.SetAttribute("value", "");
                                    }
                                    //仓库
                                    else if (nodeEle.GetAttribute("name") == "tc_dcf029")
                                    {
                                        nodeEle.SetAttribute("value", Location);
                                    }
                                    //else if (nodeEle.GetAttribute("name") == "tc_dcf031")
                                    //{
                                    //    nodeEle.SetAttribute("value", inventory.LocationCode);
                                    //}
                                }
                                ReceiptDetail receiptDetail = _unitWork.Find<ReceiptDetail>(n => n.SourceCode == interfaceLog.TaskNo && n.Project == inventory.QrCode && n.Status == ReceiptHeaderStatus.过账).FirstOrDefault();
                                if (receiptDetail != null)
                                {
                                    receiptDetail.Status = ReceiptHeaderStatus.回传;
                                    receiptDetail.UpdateBy = "wms";
                                    receiptDetail.UpdateTime = DateTime.Now;
                                    _unitWork.Update(receiptDetail);
                                }
                            }
                            else
                            {
                                XmlNodeList nodes_1 = node.SelectSingleNode("parent::*").ChildNodes;
                                foreach (XmlElement nodeEle in nodes_1)
                                {
                                    if (nodeEle.GetAttribute("name") == "tc_dcf022")
                                    {
                                        nodeEle.SetAttribute("value", "0");
                                    }
                                    else if (nodeEle.GetAttribute("name") == "tc_dcf023")
                                    {
                                        nodeEle.SetAttribute("value", "1");
                                    }
                                    else if (nodeEle.GetAttribute("name") == "tc_dcf024")
                                    {
                                        nodeEle.SetAttribute("value", "");
                                    }
                                    //仓库
                                    //else if (nodeEle.GetAttribute("name") == "tc_dcf029")
                                    //{
                                    //    nodeEle.SetAttribute("value", Location);
                                    //}
                                }
                            }
                        }
                        //调用方法
                        var response = InBack.intostore_post(doc.InnerXml);
                        //处理调用后回传数据
                        Config.LoadXml(response);

                        XmlElement element = (XmlElement)Config.SelectSingleNode("//Status");
                        string code = element.GetAttribute("code");
                        string msg = element.GetAttribute("description");
                        //回传状态成功
                        if (code == "0")
                        {
                            if (interfaceLog.Status == 0)
                            {
                                interfaceLog.Status = 1;
                            }
                            else if (interfaceLog.Status == 2)
                            {
                                interfaceLog.Status = 0;
                                interfaceLog.Message = "已部分回传:" + DateTime.Now;
                            }
                            
                            _unitWork.Update(interfaceLog);

                            //更新主入库单头尾状态
                            ReceiptHeader receiptHeader = _unitWork.Find<ReceiptHeader>(u => u.SourceCode == interfaceLog.TaskNo).FirstOrDefault();
                            ReceiptDetail receiptDetail1 = _unitWork.Find<ReceiptDetail>(u => u.SourceCode == interfaceLog.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.UpdateBy = "wms";
                            receiptHeader.UpdateTime = DateTime.Now;
                            _unitWork.Update(receiptHeader);

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

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

                                    _unitWork.Delete(rd);
                                }
                                _unitWork.Delete(receiptHeader);
                            }
                        }
                        else
                        {
                            interfaceLog.Message = msg + DateTime.Now;
                            _unitWork.Update(interfaceLog);
                        }
                    }

                    if (interfaceLog.Type == "LK_Out")
                    {
                        string name = "";
                        string value = "";
                        XmlDocument doc = new XmlDocument();
                        doc.LoadXml(interfaceLog.Request);
                        XmlDocument doc_copy = new XmlDocument();
                        ConfigXmlDocument Config = new ConfigXmlDocument();
                        TIPTOPServiceGateWayPortTypeClient OutBack = new TIPTOPServiceGateWayPortTypeClient();
                        XmlElement xmlElement_Document = doc_copy.CreateElement("Document");
                        doc_copy.AppendChild(xmlElement_Document);
                        List<ShipmentDetail> shipmentDetails = _unitWork.Find<ShipmentDetail>(n => n.SourceCode == interfaceLog.TaskNo && n.Status == ShipmentHeaderStatus.过账).ToList();
                        int i = 1;
                        foreach (ShipmentDetail sd in shipmentDetails)
                        {
                            List<InventoryOut> list = _unitWork.Find<InventoryOut>(n => n.SourceCode == sd.SourceCode && n.ShipmentState == 1).ToList();
                            foreach (InventoryOut inv in list)
                            {
                                XmlElement xmlElement_RecordSet = doc_copy.CreateElement("RecordSet");
                                string str = string.Format("Request/RequestContent/Document/RecordSet/Master/Record/Field[@value='{0}']", sd.SourceCode.Split("%")[0]);
                                XmlNode node = doc.SelectSingleNode(str);
                                xmlElement_RecordSet.InnerXml = node.SelectSingleNode("parent::*").InnerXml;
                                xmlElement_RecordSet.SetAttribute("id", i++.ToString());
                                XmlNodeList nodes = xmlElement_RecordSet.SelectNodes("//Field");
                                foreach (XmlElement nodeEle in nodes)
                                {
                                    if (nodeEle.GetAttribute("name") == "tc_dfa026")
                                    {
                                        nodeEle.SetAttribute("value", sd.QtyDivided.ToString());
                                    }
                                    else if (nodeEle.GetAttribute("name") == "tc_dfa031")
                                    {
                                        nodeEle.SetAttribute("value", "1");
                                    }
                                    else if (nodeEle.GetAttribute("name") == "tc_dfa032")
                                    {
                                        nodeEle.SetAttribute("value", "");
                                    }
                                    else if (nodeEle.GetAttribute("name") == "tc_dfa038")
                                    {
                                        nodeEle.SetAttribute("value", "");
                                    }
                                }
                                doc_copy.SelectSingleNode("//Document").AppendChild(xmlElement_RecordSet);
                                inv.ShipmentState = 2;
                                _unitWork.Update(inv);
                            }
                            sd.Status = ShipmentHeaderStatus.回传;
                            sd.UpdateBy = "wms";
                            sd.UpdateTime = DateTime.Now;
                            _unitWork.Update(sd);
                        }
                        doc.SelectSingleNode("//RequestContent").RemoveAll();
                        doc.SelectSingleNode("//RequestContent").InnerXml = doc_copy.InnerXml;
                        //string response = doc.InnerXml;

                        //var response = OutBack.outstore_post(doc.InnerXml);
                        ////处理调用后回传数据
                        //Config.LoadXml(response);

                        //XmlElement element = (XmlElement)Config.SelectSingleNode("//Status");
                        //string code = element.GetAttribute("code");
                        //string msg = element.GetAttribute("description");
                        string code = "1";
                        string msg = "";
                        if (code == "1")
                        {
                            interfaceLog.Status = 1;
                            _unitWork.Update(interfaceLog);

                            //更新主出库单头尾状态
                            ShipmentHeader shipmentHeader = _unitWork.Find<ShipmentHeader>(u => u.SourceCode == interfaceLog.TaskNo).FirstOrDefault();
                            ShipmentDetail shipmentDetail = _unitWork.Find<ShipmentDetail>(u => u.SourceCode == interfaceLog.TaskNo).OrderBy(a => a.Status).FirstOrDefault();
                            if (shipmentHeader.FirstStatus < shipmentDetail.Status)
                            {
                                shipmentHeader.FirstStatus = shipmentDetail.Status;
                            }
                            if (shipmentHeader.LastStatus < shipmentDetail.Status)
                            {
                                shipmentHeader.LastStatus = shipmentDetail.Status;
                            }
                            shipmentHeader.UpdateBy = "wms";
                            shipmentHeader.UpdateTime = DateTime.Now;
                            _unitWork.Update(shipmentHeader);

                            //已完成的出库单据迁移至历史出库单据,暂以过账状态,后期与ERP对接后以回传状态
                            if (shipmentHeader.LastStatus == ShipmentHeaderStatus.回传)
                            {
                                ShipmentHeaderHistory shipmentHeaderHistory = new ShipmentHeaderHistory();
                                shipmentHeader.CopyTo(shipmentHeaderHistory);
                                shipmentHeaderHistory.Id = null;
                                _unitWork.Add(shipmentHeaderHistory);

                                List<ShipmentDetail> shipdtls = _unitWork.Find<ShipmentDetail>(u => u.ShipmentCode == shipmentHeader.Code).ToList();
                                foreach (ShipmentDetail sd in shipdtls)
                                {
                                    ShipmentDetailHistory shipmentDetailHistory = new ShipmentDetailHistory();
                                    sd.CopyTo(shipmentDetailHistory);
                                    shipmentDetailHistory.Id = null;
                                    shipmentDetailHistory.ShipmentId = shipmentHeader.Id;
                                    _unitWork.Add(shipmentDetailHistory);

                                    _unitWork.Delete(sd);
                                }
                                _unitWork.Delete(shipmentHeader);
                            }
                        }
                        else
                        {
                            interfaceLog.Message = msg + DateTime.Now;
                            _unitWork.Update(interfaceLog);
                        }
                        
                    }
                }
            }
            catch (Exception ex)
            {
                jobContainer.ExceptionInfo = ex.Message;
            }
        }
    }
}