ERPReceiptAction.cs 14.9 KB
using System;
using System.Collections.Generic;
using System.Data;
using Infrastructure;
using Quartz;
using WebApp;
using WebRepository;
using System.Linq;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using System.Xml;
using System.Configuration;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Database;

namespace WebMvc
{
    /// <summary>
    /// 在入库单据中检索铜箔入库单据,通过铜箔入库单向ERP申请单号,申请成功后为入库单据打上标记
    /// </summary>
    [PersistJobDataAfterExecution]
    [DisallowConcurrentExecution]
    public class ERPReceiptAction
    {
        private string ConnString { set; get; }
        IJobExecutionContext Context { set; get; }

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

        public void Execute(JobContainer jobContainer)
        {
            UnitWork _unitWork = new UnitWork(AppSettingsJson.JobContext(ConnString));
            string str = "";
            try
            {
                List<ReceiptHeader> ReceiptHeaders = _unitWork.Find<ReceiptHeader>(n => n.Status == 0 && n.Type == "ReturnCopperFoil" ).OrderBy(n => n.CreateTime).ToList();
                foreach (ReceiptHeader ReceiptHeader in ReceiptHeaders)
                {
                    //建立回传 XmlDocument
                    int i_doc_isgood = 0;
                    XmlDocument doc = new XmlDocument();
                    var config = AppSettingsJson.GetAppSettings();
                    //加载模板
                    doc.Load(config.GetSection("ErpSourceCodeService:Url").Value);
                    //建立中转的XmlDocument(要以模板创建新的xml进行回传)
                    XmlDocument doc_isgood_copy = new XmlDocument();
                    ConfigXmlDocument Config = new ConfigXmlDocument();
                    ServiceReference9.TIPTOPServiceGateWayPortTypeClient OutBack = new ServiceReference9.TIPTOPServiceGateWayPortTypeClient();

                    List<ReceiptDetail> receiptDetails = _unitWork.Find<ReceiptDetail>(n => n.SourceCode == ReceiptHeader.SourceCode).ToList();
                    if (receiptDetails.Count() > 0)
                    {
                        //账套
                        XmlNodeList OrganizationsOut = doc.SelectNodes("//Organization");
                        foreach (XmlElement OrganizationOut in OrganizationsOut)
                        {
                            OrganizationOut.SetAttribute("name", ReceiptHeader.Organization);
                        }
                        XmlElement xmlElement_Document = doc_isgood_copy.CreateElement("Document");
                        doc_isgood_copy.AppendChild(xmlElement_Document);
                        foreach (var receiptDetail in receiptDetails)
                        {
                            //创建RecordSet标签
                            XmlElement xmlElement_RecordSet = doc_isgood_copy.CreateElement("RecordSet");
                            xmlElement_RecordSet.SetAttribute("id", (++i_doc_isgood).ToString());
                            XmlNode node_RecordSet = doc_isgood_copy.SelectSingleNode("//Document").AppendChild(xmlElement_RecordSet);
                            //添加表头
                            XmlElement xmlElement_Master = doc_isgood_copy.CreateElement("Master");
                            xmlElement_Master.SetAttribute("name", "Master");
                            XmlNode node_Master = node_RecordSet.AppendChild(xmlElement_Master);
                            XmlElement xmlElement_HaerRecord = doc_isgood_copy.CreateElement("Record");
                            XmlNode node_HaerRecord = node_Master.AppendChild(xmlElement_HaerRecord);

                            //添加表单
                            XmlElement xmlElement_Detail = doc_isgood_copy.CreateElement("Detail");
                            xmlElement_Detail.SetAttribute("name", "report_detail");
                            node_Master = node_RecordSet.AppendChild(xmlElement_Detail);
                            XmlElement xmlElement_DeatilRecord = doc_isgood_copy.CreateElement("Record");
                            XmlNode node_DetailRecord = node_Master.AppendChild(xmlElement_DeatilRecord);


                            //获取表头数据
                            XmlNode node_Head = doc.SelectSingleNode(string.Format("Request/RequestContent/Document/RecordSet/Master/Record"));
                            //获取表单
                            XmlNode node_Detail = doc.SelectSingleNode(string.Format("Request/RequestContent/Document/RecordSet/Detail/Record"));
                            node_HaerRecord.InnerXml = node_Head.InnerXml;
                            node_DetailRecord.InnerXml = node_Detail.InnerXml;
                            //修改表头内容
                            XmlNodeList nodes_Hears = node_HaerRecord.ChildNodes;
                            foreach (XmlElement nodes_Hear in nodes_Hears)
                            {
                                //叫料类型
                                if (nodes_Hear.GetAttribute("name") == "type")
                                {
                                    nodes_Hear.SetAttribute("value", "1");
                                }
                                //MES单号
                                else if (nodes_Hear.GetAttribute("name") == "wo_no")
                                {
                                    nodes_Hear.SetAttribute("value", receiptDetail.SourceCode);
                                }
                                //调拨单别
                                else if (nodes_Hear.GetAttribute("name") == "imm01")
                                {
                                    nodes_Hear.SetAttribute("value", ReceiptHeader.ErpListType);
                                }
                                //调拨日期
                                else if (nodes_Hear.GetAttribute("name") == "imm02")
                                {
                                    nodes_Hear.SetAttribute("value", DateTime.Now.ToString("d"));
                                }
                                //部门
                                else if (nodes_Hear.GetAttribute("name") == "imm14")
                                {
                                    nodes_Hear.SetAttribute("value", "0703");
                                }
                                //申请人
                                else if (nodes_Hear.GetAttribute("name") == "imm16")
                                {
                                    nodes_Hear.SetAttribute("value", "tiptop");
                                }
                                //是否过账
                                else if (nodes_Hear.GetAttribute("name") == "is_posting")
                                {
                                    nodes_Hear.SetAttribute("value", "N");
                                }
                            }
                            //修改表单内容
                            XmlNodeList nodes_Deatils = node_DetailRecord.ChildNodes;
                            foreach (XmlElement nodes_Deatil in nodes_Deatils)
                            {
                                if (nodes_Deatil.GetAttribute("name") == "imn03")
                                {
                                    nodes_Deatil.SetAttribute("value", receiptDetail.MaterialCode);
                                }
                                //拨出仓库(起始点)
                                else if (nodes_Deatil.GetAttribute("name") == "imn04")
                                {
                                    nodes_Deatil.SetAttribute("value", ReceiptHeader.LWH);
                                }
                                //拨出库位
                                else if (nodes_Deatil.GetAttribute("name") == "imn05")
                                {
                                    nodes_Deatil.SetAttribute("value", ReceiptHeader.LWHSL);
                                }
                                //拨出批号
                                else if (nodes_Deatil.GetAttribute("name") == "imn06")
                                {
                                    nodes_Deatil.SetAttribute("value", receiptDetail.BLot);
                                }
                                //库存单位
                                else if (nodes_Deatil.GetAttribute("name") == "imn09")
                                {
                                    nodes_Deatil.SetAttribute("value", receiptDetail.Unit);
                                }
                                //拨出数量
                                else if (nodes_Deatil.GetAttribute("name") == "imn10")
                                {
                                    nodes_Deatil.SetAttribute("value", receiptDetail.Qty.ToString());
                                }
                                //拨入仓库(目的地)
                                else if (nodes_Deatil.GetAttribute("name") == "imn15")
                                {
                                    nodes_Deatil.SetAttribute("value", ReceiptHeader.WH);
                                }
                                //拨入库位
                                else if (nodes_Deatil.GetAttribute("name") == "imn16")
                                {
                                    nodes_Deatil.SetAttribute("value", ReceiptHeader.WHSL);
                                }
                                //拨入批号
                                else if (nodes_Deatil.GetAttribute("name") == "imn17")
                                {
                                    nodes_Deatil.SetAttribute("value", receiptDetail.BLot);
                                }
                                //库存单位
                                else if (nodes_Deatil.GetAttribute("name") == "imn20")
                                {
                                    nodes_Deatil.SetAttribute("value", receiptDetail.Unit);
                                }
                                //单位转换率
                                else if (nodes_Deatil.GetAttribute("name") == "imn21")
                                {
                                    nodes_Deatil.SetAttribute("value", "");
                                }
                                //转换后数量
                                else if (nodes_Deatil.GetAttribute("name") == "imn22")
                                {
                                    nodes_Deatil.SetAttribute("value", "");
                                }
                                //拨出条码
                                else if (nodes_Deatil.GetAttribute("name") == "barcode_out")
                                {
                                    nodes_Deatil.SetAttribute("value", receiptDetail.Project);
                                }
                                //拨入条码
                                else if (nodes_Deatil.GetAttribute("name") == "barcode_in")
                                {
                                    nodes_Deatil.SetAttribute("value", receiptDetail.Project);
                                }
                                //厂商
                                else if (nodes_Deatil.GetAttribute("name") == "sfa36")
                                {
                                    nodes_Deatil.SetAttribute("value", ReceiptHeader.SupplierCode);
                                }
                                //
                                else if (nodes_Deatil.GetAttribute("name") == "checkud")
                                {
                                    nodes_Deatil.SetAttribute("value","");
                                }
                                //备注
                                else if (nodes_Deatil.GetAttribute("name") == "remark")
                                {
                                    nodes_Deatil.SetAttribute("value", "");
                                }
                            }
                        }
                        doc.SelectSingleNode("//RequestContent").RemoveAll();
                        doc.SelectSingleNode("//RequestContent").InnerXml = doc_isgood_copy.InnerXml;

                    }
                    //回传数据
                    var response = OutBack.CreateMesAimt324(doc.InnerXml);
                    //处理调用后回传数据
                    Config.LoadXml(response);
                    XmlElement element = (XmlElement)Config.SelectSingleNode("//Status");
                    string code = element.GetAttribute("code");
                    string msg = element.GetAttribute("description");
                    //string code = "0";
                    //string msg = "";
                    //回传成功
                    if (code == "0")
                    {
                        var erpSourcecode = msg.Split("/")[0];

                        ReceiptHeader.Status = 1;
                        ReceiptHeader.ErpSourceCode = erpSourcecode;
                        _unitWork.Update(ReceiptHeader);
                        InterfaceLog interfaceLog = new InterfaceLog();
                        interfaceLog.TaskNo = ReceiptHeader.SourceCode;
                        interfaceLog.Request = doc.InnerXml;
                        interfaceLog.Type = "申请入库单号成功";
                        interfaceLog.Status = 1;
                        interfaceLog.Message = Config.InnerXml;
                        interfaceLog.CreateTime = DateTime.Now;
                        _unitWork.Add(interfaceLog);
                    }
                    else
                    {
                        ReceiptHeader.Status = 2;
                        _unitWork.Update(ReceiptHeader);
                        InterfaceLog error11 = new InterfaceLog();
                        error11.TaskNo = ReceiptHeader.SourceCode;
                        error11.Request = doc.InnerXml;
                        error11.Type = "申请入库单号失败";
                        error11.Status = 4;
                        error11.Message = Config.InnerXml;
                        error11.CreateTime = DateTime.Now;
                        _unitWork.Add(error11);
                    }
                }
            }
            catch (Exception ex)
            {
                InterfaceLog error11 = new InterfaceLog();
                error11.TaskNo = "";
                error11.Request = "";
                error11.Type = "接口回传定时器报错";
                error11.Status = 4;
                error11.Message = "Message:" + ex.Message + ",StackTrace:" + ex.StackTrace + ",Source" + ex.Source + ",Data" + ex.Data;
                error11.CreateTime = DateTime.Now;
                _unitWork.Add(error11);
                jobContainer.ExceptionInfo = ex.Message;
            }
        }
    }
}