private void WriteErpFileList(string localPath, ErpInterfaceType type, string prefix) { try { using (SpareEntities db = EntitiesFactory.CreateSpareInstance()) { var list = ErpInterfaceController.GetNewList(db, type).ToList(); if (list.Count == 0) { Console.WriteLine(@"未发现需要发送的 " + type + " 接口数据"); return; } var billList = list.DistinctBy(p => p.BillNum); foreach (var bill in billList) { var sb = new StringBuilder(); foreach (var tsInterface in list.Where(p => p.BillNum == bill.BillNum).OrderBy(p => p.p08)) { sb.AppendLine(tsInterface.InterfaceString); } WriteErpFile(localPath, bill.BillNum, prefix, sb.ToString()); } try { // ErpInterfaceController.UpdateList(db, list); EntitiesFactory.SaveDb(db); } catch (WmsException ex) { Console.WriteLine(ex.ToString()); } } Console.WriteLine(@"生成" + type + "接口文件完成"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
private void WriteErpFiles放弃() { string localPath = IoHelper.GetFullPath(_localRootPath, _localToErp); if (!Directory.Exists(localPath)) { Console.WriteLine(@"接口文件夹 " + localPath + @" 不存在,发送失败"); return; } Console.WriteLine(@"汇总接口数据开始"); using (SpareEntities db = EntitiesFactory.CreateSpareInstance()) { var list = ErpInterfaceController.GetNewList(db).ToList(); if (list.Count == 0) { Console.WriteLine(@"未发现需要汇总的接口数据"); return; } // var allList = list.DistinctBy(p => p.BillNum); var sumList = new List <TL_INTERFACE>(); foreach (var bill in list) { TL_INTERFACE erpInterface = null; switch (bill.InterfaceType) { case "StockMove": erpInterface = sumList.FirstOrDefault(p => p.BillType == bill.BillType.ToString() && p.BillNum == bill.BillNum && p.InterfaceType == bill.InterfaceType.ToString() && p.p01 == bill.p01 // && p.p02 == bill.p02 && p.p03 == bill.p03 && p.p04 == bill.p04 && p.p05 == bill.p05 && p.p06 == bill.p06 && p.p07 == bill.p07 && p.p08 == bill.p08 && p.p09 == bill.p09 && p.p10 == bill.p10 && p.p11 == bill.p11 && p.p12 == bill.p12 && p.p13 == bill.p13 && p.p14 == bill.p14 && p.p15 == bill.p15 && p.p16 == bill.p16 && p.p17 == bill.p17 && p.p18 == bill.p18 && p.p19 == bill.p19 && p.p20 == bill.p20 ); break; case "BackFlush": erpInterface = sumList.FirstOrDefault(p => p.BillType == bill.BillType.ToString() && p.BillNum == bill.BillNum && p.InterfaceType == bill.InterfaceType.ToString() && p.p01 == bill.p01 && p.p02 == bill.p02 && p.p03 == bill.p03 && p.p04 == bill.p04 && p.p05 == bill.p05 && p.p06 == bill.p06 && p.p07 == bill.p07 && p.p08 == bill.p08 && p.p09 == bill.p09 && p.p10 == bill.p10 && p.p11 == bill.p11 //&& p.p12 == bill.p12 && p.p13 == bill.p13 && p.p14 == bill.p14 && p.p15 == bill.p15 && p.p16 == bill.p16 && p.p17 == bill.p17 && p.p18 == bill.p18 && p.p19 == bill.p19 && p.p20 == bill.p20 ); break; case "StockSell": erpInterface = sumList.FirstOrDefault(p => p.BillType == bill.BillType.ToString() && p.BillNum == bill.BillNum && p.InterfaceType == bill.InterfaceType.ToString() && p.p01 == bill.p01 && p.p02 == bill.p02 && p.p03 == bill.p03 && p.p04 == bill.p04 && p.p05 == bill.p05 //&& p.p06 == bill.p06 && p.p07 == bill.p07 && p.p08 == bill.p08 && p.p09 == bill.p09 && p.p10 == bill.p10 && p.p11 == bill.p11 && p.p12 == bill.p12 && p.p13 == bill.p13 && p.p14 == bill.p14 && p.p15 == bill.p15 && p.p16 == bill.p16 && p.p17 == bill.p17 && p.p18 == bill.p18 && p.p19 == bill.p19 && p.p20 == bill.p20 ); break; } if (erpInterface == null) { erpInterface = new TL_INTERFACE { BillType = bill.BillType, BillNum = bill.BillNum, InterfaceType = bill.InterfaceType, State = (int)BillState.New, pQty = bill.pQty, p01 = bill.p01, p02 = bill.p02, p03 = bill.p03, p04 = bill.p04, p05 = bill.p05, p06 = bill.p06, p07 = bill.p07, p08 = bill.p08, p09 = bill.p09, p10 = bill.p10, p11 = bill.p11, p12 = bill.p12, p13 = bill.p13, p14 = bill.p14, p15 = bill.p15, p16 = bill.p16, p17 = bill.p17, p18 = bill.p18, p19 = bill.p19, p20 = bill.p20, }; sumList.Add(erpInterface); } else { switch (bill.InterfaceType) { case "StockMove": erpInterface.p02 = (Convert.ToDecimal(erpInterface.p02) + Convert.ToDecimal(bill.p02)).ToString(); break; case "BackFlush": erpInterface.p12 += (Convert.ToDecimal(erpInterface.p12) + Convert.ToDecimal(bill.p12)).ToString(); break; case "StockSell": erpInterface.p06 += (Convert.ToDecimal(erpInterface.p06) + Convert.ToDecimal(bill.p06)).ToString(); break; } } } var billNumList = (from c in sumList select c.BillNum).Distinct(); foreach (var billNum in billNumList) { var sb = new StringBuilder(); string prefix = string.Empty; foreach ( TL_INTERFACE f in sumList.Where(p => p.BillNum == billNum && p.InterfaceType == ErpInterfaceType.TR.ToString()) .OrderBy(p => p.p08)) { f.InterfaceString = GetInterfaceString(f); sb.AppendLine(f.InterfaceString); } prefix = "tr"; WriteErpFile(localPath, billNum, prefix, sb.ToString()); foreach ( TL_INTERFACE f in sumList.Where(p => p.BillNum == billNum && p.InterfaceType == ErpInterfaceType.BK.ToString()) .OrderBy(p => p.p04)) { f.InterfaceString = GetInterfaceString(f); sb.AppendLine(f.InterfaceString); } prefix = "bk"; WriteErpFile(localPath, billNum, prefix, sb.ToString()); foreach ( TL_INTERFACE f in sumList.Where(p => p.BillNum == billNum && p.InterfaceType == ErpInterfaceType.SH.ToString()) .OrderBy(p => p.p08)) { f.InterfaceString = GetInterfaceString(f); sb.AppendLine(f.InterfaceString); } prefix = "sh"; WriteErpFile(localPath, billNum, prefix, sb.ToString()); } try { ErpInterfaceController.RemoveList(db, list); EntitiesFactory.SaveDb(db); } catch (WmsException ex) { Console.WriteLine(ex.ToString()); } } Console.WriteLine(@"汇总接口数据结束"); Console.WriteLine(@"发送数据开始"); var fileList = Directory.GetFiles(localPath).ToList(); if (_isFtp) { try { Console.WriteLine(@"上传文件开始"); _ftp.GotoDirectory("//" + _ftpToErp, false); _ftp.UploadList(fileList); Console.WriteLine(@"上传文件完成"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } foreach (string localFileName in fileList) { string txtName = Path.GetFileName(localFileName); string bakFileName = IoHelper.GetFullFilename(_localRootPath + _localBackup + "\\" + _localToErp, txtName, false); if (File.Exists(bakFileName)) { File.Delete(bakFileName); } File.Move(localFileName, bakFileName); // File.Delete(localFileName); } }