InterfaceLogAction.cs 29.5 KB
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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
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创建
                        int i_doc_isgood = 0;
                        XmlDocument doc_isgood = new XmlDocument();
                        XmlDocument doc_isgood_copy = new XmlDocument();
                        var config = AppSettingsJson.GetAppSettings();
                        doc_isgood.Load(config.GetSection("NouYaWebRGVService:Url").Value);
                        XmlElement xmlElement_Document = doc_isgood_copy.CreateElement("Document");
                        doc_isgood_copy.AppendChild(xmlElement_Document);

                        XmlDocument doc = new XmlDocument();
                        ConfigXmlDocument Config = new ConfigXmlDocument();
                        doc.LoadXml(interfaceLog.Request);
                        ServiceReference4.TIPTOPServiceGateWayPortTypeClient InBack = new ServiceReference4.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 == "scrap")
                                {
                                    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_Record = doc_isgood_copy.CreateElement("Record");
                                    XmlNode node_Record = node_Master.AppendChild(xmlElement_Record);
                                    XmlNode node_isgood = doc_isgood.SelectSingleNode(string.Format("Request/RequestContent/Document/RecordSet/Master/Record"));
                                    node_Record.InnerXml = node_isgood.InnerXml;
                                    XmlNodeList nodes_isgood = node_Record.ChildNodes;
                                    foreach (XmlElement nodeEle in nodes_isgood)
                                    {
                                        if (nodeEle.GetAttribute("name") == "imn03")
                                        {
                                            nodeEle.SetAttribute("value", inventory.MaterialCode);
                                        }
                                        else if (nodeEle.GetAttribute("name") == "imn04")
                                        {
                                            nodeEle.SetAttribute("value", inventory.WarehouseType);
                                        }
                                        else if (nodeEle.GetAttribute("name") == "imn15")
                                        {
                                            nodeEle.SetAttribute("value", inventory.WarehouseType);
                                        }
                                        else if (nodeEle.GetAttribute("name") == "imn16")
                                        {
                                            nodeEle.SetAttribute("value", "B01");
                                        }
                                    }
                                    //node_Record.AppendChild(xmlElement_RecordSet);
                                }
                                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);
                                    //}
                                }
                            }
                        }

                        //不良品回传
                        if (i_doc_isgood > 0)
                        {
                            doc_isgood.SelectSingleNode("//RequestContent").RemoveAll();
                            doc_isgood.SelectSingleNode("//RequestContent").InnerXml = doc_isgood_copy.InnerXml;
                            string s = doc_isgood.InnerXml;
                            InterfaceLog GoodBack = new InterfaceLog();
                            GoodBack.TaskNo = interfaceLog.TaskNo;
                            GoodBack.Request = doc.InnerXml;
                            GoodBack.Type = "GoodBack";
                            GoodBack.Status = 4;
                            GoodBack.Message = "不良品回传";
                            _unitWork.Add(GoodBack);
                            var Dresponse = InBack.intostore_post(doc_isgood.InnerXml);
                            Config.LoadXml(Dresponse);
                        }

                        //调用方法
                        var response = InBack.intostore_post(doc.InnerXml);
                        InterfaceLog Back = new InterfaceLog();
                        Back.TaskNo = interfaceLog.TaskNo;
                        Back.Request = doc.InnerXml;
                        Back.Type = "InBack";
                        Back.Status = 4;
                        Back.Message = "入库回传";
                        _unitWork.Add(Back);
                        //处理调用后回传数据
                        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 = "";
                        string Ware = "";
                        string Sta = "";
                        XmlDocument doc = new XmlDocument();
                        doc.LoadXml(interfaceLog.Request);
                        XmlDocument doc_copy = new XmlDocument();
                        ConfigXmlDocument Config = new ConfigXmlDocument();
                        ServiceReference4.TIPTOPServiceGateWayPortTypeClient OutBack = new ServiceReference4.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;
                        List<InventoryOut> list = new List<InventoryOut>(); ;
                        foreach (ShipmentDetail sd in shipmentDetails)
                        {
                            list = _unitWork.Find<InventoryOut>(n => n.SourceCode == sd.ShipmentCode && n.ShipmentState == 1).ToList();
                            foreach (InventoryOut inv in list)
                            {
                                ShipmentHeader shph = _unitWork.Find<ShipmentHeader>(n => n.SourceCode == sd.SourceCode).FirstOrDefault();
                                if (shph != null)
                                {
                                    Ware = shph.WarehouseType;
                                    if (Ware == "YCL_WareCell")
                                    {
                                        Ware = "B1032";
                                        if (inv.Status == "scrap")
                                        {
                                            Sta = "B01";
                                        }
                                        else if (inv.Status == "good")
                                        {
                                            Sta = "B00";
                                        }
                                    }
                                    else if (Ware == "PPJ_WareCell")
                                    {
                                        Ware = "B1011";
                                        if (inv.Status == "scrap")
                                        {
                                            Sta = "B01";
                                        }
                                        else if (inv.Status == "good")
                                        {
                                            Sta = "B00";
                                        }
                                        //打包
                                        else if (inv.IsPack == "Ispack")
                                        {
                                            Sta = "Z01";
                                        }
                                    }
                                    else if (Ware == "PPP_WareCell")
                                    {
                                        Ware = "B1020";
                                        if (inv.Status == "scrap")
                                        {
                                            Sta = "B01";
                                        }
                                        else if (inv.Status == "good")
                                        {
                                            Sta = "B00";
                                        }
                                        //排版
                                        else if (inv.IsFold == "Isflod")
                                        {
                                            Sta = "P01";
                                        }
                                    }
                                    else if (Ware == "TB_WareCell")
                                    {
                                        Ware = "B1031";
                                        if (inv.Status == "scrap")
                                        {
                                            Sta = "B01";
                                        }
                                        else if (inv.Status == "good")
                                        {
                                            Sta = "B00";
                                        }
                                    }
                                    else if (Ware == "CP_WareCell")
                                    {
                                        Ware = "B1010";
                                        if (inv.Status == "scrap")
                                        {
                                            Sta = "B01";
                                        }
                                        else if (inv.Status == "good")
                                        {
                                            Sta = "B00";
                                        }
                                        //打包
                                        else if (inv.IsPack == "Ispack")
                                        {
                                            Sta = "Z01";
                                        }
                                    }
                                }
                                XmlElement xmlElement_RecordSet = doc_copy.CreateElement("RecordSet");
                                xmlElement_RecordSet.SetAttribute("id", (i++).ToString());
                                XmlNode node_RecordSet = doc_copy.SelectSingleNode("//Document").AppendChild(xmlElement_RecordSet);
                                XmlElement xmlElement_Master = doc_copy.CreateElement("Master");
                                xmlElement_Master.SetAttribute("name", "Master");
                                XmlNode node_Master = node_RecordSet.AppendChild(xmlElement_Master);
                                XmlElement xmlElement_Record = doc_copy.CreateElement("Record");
                                XmlNode node_Record = node_Master.AppendChild(xmlElement_Record);
                                XmlNode node = doc.SelectSingleNode(string.Format("Request/RequestContent/Document/RecordSet/Master/Record"));
                                node_Record.InnerXml = node.InnerXml;
                                XmlNodeList nodes = node_Record.ChildNodes;
                                foreach (XmlElement nodeEle in nodes)
                                {
                                    if (nodeEle.GetAttribute("name") == "tc_dfa023")
                                    {
                                        nodeEle.SetAttribute("value", inv.MaterialCode);
                                    }
                                    else if (nodeEle.GetAttribute("name") == "tc_dfa026")
                                    {
                                        nodeEle.SetAttribute("value", inv.Qty.ToString());
                                    }
                                    else if (nodeEle.GetAttribute("name") == "tc_dfa027")
                                    {
                                        nodeEle.SetAttribute("value", Ware);
                                    }
                                    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_dfa033")
                                    {
                                        nodeEle.SetAttribute("value", inv.QrCode);
                                    }
                                    else if (nodeEle.GetAttribute("name") == "tc_dfa037")
                                    {
                                        nodeEle.SetAttribute("value", Sta);
                                    }
                                }
                                //doc_copy.SelectSingleNode("//Master").AppendChild(xmlElement_RecordSet);
                            }
                        }
                        doc.SelectSingleNode("//RequestContent").RemoveAll();
                        doc.SelectSingleNode("//RequestContent").InnerXml = doc_copy.InnerXml;
                        InterfaceLog Back = new InterfaceLog();
                        Back.TaskNo = interfaceLog.TaskNo;
                        Back.Request = doc.InnerXml;
                        Back.Type = "Back";
                        Back.Status = 4;
                        Back.Message = "出库回传";
                        _unitWork.Add(Back);
                        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 = "0";
                        //string msg = "";
                        if (code == "0")
                        {
                            interfaceLog.Status = 1;
                            _unitWork.Update(interfaceLog);

                            foreach (InventoryOut inv in list)
                            {
                                inv.ShipmentState = 2;
                                _unitWork.Update(inv);
                            }

                            foreach (ShipmentDetail sd in shipmentDetails)
                            {
                                sd.Status = ShipmentHeaderStatus.回传;
                                sd.UpdateBy = "wms";
                                sd.UpdateTime = DateTime.Now;
                                _unitWork.Update(sd);
                            }

                                //更新主出库单头尾状态
                            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 != null && shipmentDetail != null)
                            {
                                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 = "出库主表或明细未找到";
                                _unitWork.Update(interfaceLog);
                            }
                            
                        }
                        else
                        {
                            InterfaceLog error11 = new InterfaceLog();
                            error11.TaskNo = interfaceLog.TaskNo;
                            error11.Request = doc.InnerXml;
                            error11.Type = "error";
                            error11.Status = 3;
                            error11.Message = code;
                            _unitWork.Add(error11);
                            interfaceLog.Status = 3;
                            interfaceLog.Message = "接口报错:"+msg + DateTime.Now;
                            _unitWork.Update(interfaceLog);
                        }
                        
                    }
                }
            }
            catch (Exception ex)
            {
                jobContainer.ExceptionInfo = ex.Message;
            }
        }
    }
}