You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2492 lines
118 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using DSWeb.Common.DB;
using DSWeb.Common.Helper;
using ICSharpCode.SharpZipLib.Zip;
using log4net;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Linq;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using DSWeb.DLL.SendEdiToYard;
using System.Configuration;
using DSWeb.MvcShipping.DAL.MsOpSeaeEdiDAL;
using DSWeb.MvcShipping.Models.MsOpSeaeEdi;
using DSWeb.MvcShipping.DAL.MsOpSeaeEdiPortDAL;
using MimeKit;
public static class EdiHelper
{
private static ILog log = LogManager.GetLogger(typeof(EdiHelper));
/// <summary>
/// 舱单直发(分单),根据舱单直发权限配置(分单)进行拦截处理
/// </summary>
/// <param name="bsnos">舱单的mfno多票以逗号隔开</param>
/// <param name="type">类型,例如:删除、原始重发、修改等</param>
/// <param name="userid">发送用户ID</param>
/// <param name="pathFileBase">生成报文的根文件夹</param>
/// <param name="reason">若Processed为true则说明已处理调用方需要返回信息不能再继续处理</param>
/// <returns></returns>
public static CangDanDirectRtn CangDanDirectSend(string bsnos, string type, string userid, string pathFileBase, string reason)
{
log.Debug($"舱单直发(分单):{bsnos}{type}{userid}{pathFileBase}{reason}");
EdiDataContext edc = new EdiDataContext();
var _user = edc.Users.FirstOrDefault(x => x.GID == userid);
var _userbaseinfo = edc.UserBases.FirstOrDefault(x => x.USERID == userid);
var companyid = _user.CompId;
var companyname = _user.COMNAME;
#region 新直发
var debugNoSendEdiFile = ConfigurationManager.AppSettings["CangDanDirectDebugSwitch"] == "1"; //调试时不真正发送edi文件给爬虫
List<OpSeaeEdi> ediList = null;
//现有页面的逻辑是编辑页面提交的单个bsno且带有货代列表页面的不带货代且bsno带单引号和逗号
if (bsnos.IndexOf("'") == -1 && bsnos.IndexOf(",") == -1)
{
var edi = edc.Edis.First(x => x.MFNO == bsnos);
ediList = new List<OpSeaeEdi>();
ediList.Add(edi);
}
else
{
//处理bsno编辑页面过来的的不带单引号列表页面的带有单引号且逗号隔开
var arrBSNO = bsnos.Replace("'", "").Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
ediList = edc.Edis.Where(x => arrBSNO.Contains(x.MFNO)).ToList();
}
var hasMain = ediList.Count(x => string.IsNullOrEmpty(x.HBLNO)) > 0; //含有主单
var hasSub = ediList.Count(x => !string.IsNullOrEmpty(x.HBLNO)) > 0; //含有分单
//配置了直发权限
var directConfig = edc.DirectSendConfig.FirstOrDefault(c => c.IsEnable && c.UserId == userid && c.Forwarder == "分单");
#region 直发删除
/*
2022年9月28日增加直发删除
逻辑规则:
若配置了当前用户、当前船代的直发(分单)权限,则生成直发报文发送出去,随后发邮件通知东胜,前提条件是只能是分单;
若没有直发(分单)权限,继续执行原有逻辑
2022年12月8日增加逻辑只选择主单的继续执行原有逻辑否则走新直发逻辑且主分单同时选择时给与提示
*/
var result = new CangDanDirectRtn();
if (type == "删除" && directConfig != null && directConfig.IsDelete && hasSub)
{
result.Processed = true;
try
{
log.Debug($"舱单直发(分单)删除");
//包含主单
if (hasMain)
{
result.Success = false;
result.Message = "主分单请分开发送删除";
return result;
}
if (ediList.Count(x => x.EDISTATUS != "已直发") > 0)
{
result.Success = false;
result.Message = "当前状态不允许删除";
return result;
}
if (ediList.Select(x => x.MBLNO).Distinct().Count() > 1)
{
result.Success = false;
result.Message = "只能选择同一主单下的分单";
return result;
}
if (string.IsNullOrEmpty(reason))
{
result.Success = false;
result.Message = "请输入原因内容";
return result;
}
var listHBL = ediList.Select(x => x.HBLNO).ToList();
var ediModel = ediList[0];
ZDXXModel hz = null;
List<ZDXXDetailModel> hzCtn = null;
//先读取主单回执
if (EdiHelper.ReadResponseZD(userid, ediModel.BSNO, out string msgRtn, out hz, out hzCtn))
{
if (hzCtn.Count == 0)
{
result.Success = false;
result.Message = "舱单已经删除,请勿重复发送删除请求。";
return result;
}
var respZD = edc.op_seae_edi_mainstatus.First(x => x.mfno == ediModel.BSNO);
//生成报文
var succ = EdiHelper.GenDirectEdiFileFenDel(userid, ediModel.MBLNO, listHBL, reason, _user.SHOWNAME, _userbaseinfo.MOBILE, pathFileBase, out string rtnMsg);
if (succ)
{
var ediFileName = rtnMsg;
var ediFullPth = Path.Combine(pathFileBase, ediFileName);
log.Debug($"成功生成舱单直发删除报文:{ediFullPth}");
decimal price = 0;
var canFee = EdiHelper.CheckEdiFee(_user.COMNAME, 8, out string rtnFee, out price, num: listHBL.Count);
if (canFee)
{
//发送到ftp爬虫
string rtnFtp = string.Empty;
if (debugNoSendEdiFile)
{
succ = true;
}
else
{
succ = EdiHelper.SendEdiFileToFtpSpider(ediFullPth, userid, out rtnFtp);
}
if (succ)
{
//扣费并执行后续操作
var bala = edc.Cust_Balance.First(b => b.COMNAME == _user.COMNAME);
foreach (var ee in ediList)
{
log.Debug($"舱单直发删除准备扣费:{ee.MBLNO} {ee.HBLNO} {_user.COMNAME}");
//扣费和生成消费记录
var feeMD = new Cust_Fee_md(ee, 8, _user.GID, _user.SHOWNAME, _user.comid, _user.COMNAME, edc);
feeMD.PRICE = price;
edc.Cust_Fee.Add(feeMD);
bala.Balance -= feeMD.PRICE;
//添加tips
var tips = EdiHelper.CreateEdiTips(ee, _user.SHOWNAME, "[已删除]", "已删除");
edc.OP_SEAE_EDI_TIPS.Add(tips);
}
edc.SaveChanges();
log.Debug($"舱单直发删除扣费完成:{ediModel.MBLNO} {string.Join(",", listHBL)} {_user.COMNAME}");
//发送邮件通知(衣国豪:删除邮件不需要带报文)
foreach (var ee in ediList)
{
EdiHelper.SendDirectEdiMailDS(ee.MBLNO, ee.HBLNO, ee.FORWARDER, _user.COMNAME, _user.SHOWNAME, _userbaseinfo.EMAIL1, _userbaseinfo.MOBILE, "删除(自动)", reason);
}
log.Debug($"舱单直发删除已发送邮件");
result.Success = true;
result.Message = "直发删除完成";
return result;
}
else
{
result.Success = false;
result.Message = rtnFtp;
return result;
}
}
else
{
result.Success = false;
result.Message = rtnFee;
return result;
}
}
else
{
result.Success = false;
result.Message = rtnMsg;
return result;
}
}
else
{
result.Success = false;
result.Message = $"读取主单回执失败:{msgRtn}";
log.Error($"读取主单回执({ediModel.BSNO} {ediModel.MBLNO})失败:{msgRtn}");
return result;
}
}
catch (Exception ex)
{
result.Success = false;
result.Message = ex.Message;
log.Error($"处理直发(分单)失败:{ex.Message}");
log.Error(ex.StackTrace);
return result;
}
}
#endregion
#region 原始重发
/*
只针对分单
a. 拥有直发权限的用户,若非直发用户,点击时给出提示:类型请选择新增/修改/删除
b. 发送状态为已直发。其他状态点击时,给出提示:该功能仅支持状态为已直发的业务
c. 所选业务最新回执状态为11104 舱单运输工具无预报动态,海关审核不通过。的业务,原始重发不扣费,其他按新增标准扣费
原始重发时需获取一遍主单最新的回执状态并填充主单回执表op_seae_edi_mainstatus_xhinfoop_seae_edi_mainstatus
原始重发需要校验业务字段,发送邮件,邮件主题需带上船代直发字样。
校验、报文生成调用原有代码逻辑
*/
if (type == "原始重发")
{
result.Processed = true;
if (directConfig == null || !directConfig.IsAdd)
{
result.Success = false;
result.Message = "类型请选择新增/修改/删除。";
result.Processed = true;
return result;
}
try
{
log.Debug($"舱单直发(分单)原始重发");
if (directConfig != null)
{
List<string> listHBL = null;
//包含主单
if (hasMain)
{
result.Success = false;
result.Message = "请选择分单";
return result;
}
if (ediList.Count(x => x.EDISTATUS != "已直发") > 0)
{
result.Success = false;
result.Message = "该功能仅支持状态为已直发的业务";
return result;
}
if (ediList.Select(x => x.MBLNO).Distinct().Count() > 1)
{
result.Success = false;
result.Message = "只能选择同一主单下的分单";
return result;
}
listHBL = ediList.Select(x => x.HBLNO).ToList();
if (!EdiHelper.CheckSendData(bsnos, "原始重发", userid, out string checkRtn))
{
result.Success = false;
result.Message = checkRtn;
return result;
}
var ediModel = ediList[0];
ZDXXModel hz = null;
List<ZDXXDetailModel> hzCtn = null;
//先读取主单回执
if (EdiHelper.ReadResponseZD(userid, ediList[0].BSNO, out string msgRtn, out hz, out hzCtn))
{
//生成报文
log.Debug($"准备生成舱单直发原始重发报文:{ediList[0].BSNO} {ediList[0].MBLNO}");
var ediFile = DoSend.MakeFenDanFile(ediList, 9, string.Empty, pathFileBase, userid);
log.Debug($"成功生成舱单直发原始重发报文:{ediFile}");
//发送到ftp爬虫
var succ = false;
string rtnFtp = string.Empty;
if (debugNoSendEdiFile)
{
succ = true;
}
else
{
succ = EdiHelper.SendEdiFileToFtpSpider(ediFile, userid, out rtnFtp);
}
if (succ)
{
var staList = edc.op_seae_edi_status.AsNoTracking().OrderByDescending(x => x.createtime).Where(x => listHBL.Contains(x.mblno)).ToList(); //获取所有主分单回执状态
//扣费并执行后续操作
decimal price = 0;
if (EdiHelper.CheckEdiFee(_user.COMNAME, 4, out string rtnFee, out price, num: listHBL.Count))
{
var bala = edc.Cust_Balance.First(b => b.COMNAME == _user.COMNAME);
foreach (var ee in ediList)
{
var sta = staList.FirstOrDefault(x => x.mblno == ee.HBLNO);
var notFee = sta != null && !string.IsNullOrEmpty(sta.status_detai) && sta.status_detai.StartsWith("11104") ? true : false; //不需要扣费
ee.EDISTATUS = "已直发";//置状态
if (!notFee)
{
log.Debug($"舱单直发原始重发准备扣费:{ee.MBLNO} {ee.HBLNO} {_user.COMNAME}");
//扣费和生成消费记录
var feeMD = new Cust_Fee_md(ee, 4, _user.GID, _user.SHOWNAME, _user.comid, _user.COMNAME, edc);
feeMD.PRICE = price;
edc.Cust_Fee.Add(feeMD);
bala.Balance -= feeMD.PRICE;
var ctnlist = edc.EdiCtns.AsNoTracking().Where(x => x.MFNO == ee.MFNO).ToList();
foreach (var ctn in ctnlist)
{
var feeCtn = new Cust_Fee_Ctn_md(ctn, feeMD.GID);
edc.Cust_Fee_Ctn.Add(feeCtn);
}
}
}
log.Debug($"舱单直发原始重发扣费完成:{ediList[0].MBLNO} {string.Join(",", listHBL)} {_user.COMNAME}");
foreach (var ee in ediList)
{
//添加tips
var tips = EdiHelper.CreateEdiTips(ee, _user.SHOWNAME, "[原始重发]", "原始重发");
edc.OP_SEAE_EDI_TIPS.Add(tips);
}
edc.SaveChanges();
//生成东胜报文文件,并发送
var tempDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TempFiles");
if (!Directory.Exists(tempDir))
{
Directory.CreateDirectory(tempDir);
}
//发送邮件通知和ftp
foreach (var ee in ediList)
{
var tmpEdiFile = MsOpSeaeEdiDAL.CreateCustomEdiDS(AutoMapper.Mapper.Map<MsOpSeaeEdi>(ee), "", tempDir, new DSWeb.MvcShipping.Models.FtpSet.CodeFtpSet(), ee.CARRIER, companyname, ee.MBLNO);
var ediFileName = Path.GetFileName(tmpEdiFile);
log.Debug($"处理{ee.HBLNO}的直发原始重发分单邮件和ftp发送edi文件{tmpEdiFile}");
//邮件
EdiHelper.SendDirectEdiMailDS(ee.MBLNO, ee.HBLNO, ee.FORWARDER, _user.COMNAME, _user.SHOWNAME, _userbaseinfo.EMAIL1, _userbaseinfo.MOBILE, "原始重发(自动)", reason, new AttachFileModel() { FilePath = tmpEdiFile, FileName = ediFileName });
//发送ftp
EdiHelper.CangDanDSFtp(ee.MBLNO, ee.HBLNO, ee.FORWARDER, _user.COMNAME, _user.SHOWNAME, _userbaseinfo.EMAIL1, _userbaseinfo.MOBILE, "原始重发(自动)", reason, tmpEdiFile);
log.Debug($"处理{ee.HBLNO}的原始重发分单邮件和ftp发送完成");
}
result.Success = true;
result.Message = "发送完成";
return result;
}
else
{
result.Success = false;
result.Message = rtnFee;
return result;
}
}
else
{
result.Success = false;
result.Message = rtnFtp;
return result;
}
}
else
{
result.Success = false;
result.Message = $"读取主单回执失败:{msgRtn}";
log.Error($"读取主单回执({ediList[0].BSNO} {ediList[0].MBLNO})失败:{msgRtn}");
}
}
else
{
result.Success = false;
result.Message = "类型请选择新增/修改/删除";
}
result.Processed = true;
return result;
}
catch (Exception ex)
{
result.Success = false;
result.Message = ex.Message;
log.Error($"处理直发(分单)失败:{ex.Message}");
log.Error(ex.StackTrace);
return result;
}
}
#endregion
#region 直发修改
/*
a.只针对分单,有直发权限的用户,采用自动逻辑,其他客户采用原有逻辑不变。
b.需要判断修改内容若修改内容中包含船名航次箱号箱型分提单号箱量船代中的任何一个字段则发送修改时给出提示XX为海关不允许修改字段请先提交删除删除后点击原始重发。
c.需要做修改报文,船代直发权限用户,发送备注必填,作为向海关展示的修改原因。
d.如果修改的内容包含件数,重量,则,要判断是改大还是改小,若是改大,则发送时需要获取主单回执中的件重,与本次修改内容校验,若不一致,给出提示:请先修改主单件重,修改成功后再提交分单修改。若是改小,则不作校验,直接生成修改报文发送。
改大还是改小,以重量作为判断标准。
e.修改报文需调用邮件服务,邮件主题需要标注船代直发字样。
件重修改,只与大简云里的主单做比较,不再调取回执。
因比较箱号,箱量不再比较数量
*/
if (type == "修改" && directConfig != null && directConfig.IsModify && hasSub)
{
result.Processed = true;
try
{
log.Debug($"舱单直发(分单)修改");
//包含主单
if (hasMain)
{
result.Success = false;
result.Message = "主分单请分开修改";
return result;
}
if (ediList.Count(x => x.EDISTATUS != "已直发") > 0)
{
result.Success = false;
result.Message = "该功能仅支持状态为已直发的业务";
return result;
}
if (ediList.Select(x => x.MBLNO).Distinct().Count() > 1)
{
result.Success = false;
result.Message = "只能选择同一主单下的分单";
return result;
}
if (string.IsNullOrEmpty(reason))
{
result.Success = false;
result.Message = "请输入原因内容";
return result;
}
var parentId = ediList[0].BSNO;
var zhudan = edc.Edis.AsNoTracking().First(x => x.MFNO == parentId);
var fendanList = edc.Edis.AsNoTracking().Where(x => x.BSNO == parentId).ToList();
//判断修改内容
var mfnoList = ediList.Select(x => x.MFNO).ToList();
var query = from fee in edc.Cust_Fee
join feectn in edc.Cust_Fee_Ctn on fee.GID equals feectn.FeeID
where mfnoList.Contains(fee.BSNO)
orderby fee.SENDTIME descending
select new
{
fee,
feectn
};
var feeList = query.ToList();
foreach (var item in ediList) //遍历每一票分单,跟扣费记录中保存的值比较
{
var ctns = edc.EdiCtns.AsNoTracking().Where(x => x.MFNO == item.MFNO).ToList();
var lastFee = feeList.First(x => x.fee.BSNO == item.MFNO);
//船名
if (item.VESSEL != lastFee.fee.VESSEL)
{
result.Success = false;
result.Message = "船名为海关不允许修改字段,请先提交删除,删除后点击原始重发。";
return result;
}
//航次
if (item.VOYNO != lastFee.fee.VOYNO)
{
result.Success = false;
result.Message = "航次为海关不允许修改字段,请先提交删除,删除后点击原始重发。";
return result;
}
//分提单号
if (item.HBLNO != lastFee.fee.HBLNO)
{
result.Success = false;
result.Message = "分提单号为海关不允许修改字段,请先提交删除,删除后点击原始重发。";
return result;
}
//船代
if (item.FORWARDER != lastFee.fee.FORWARDER)
{
result.Success = false;
result.Message = "船代为海关不允许修改字段,请先提交删除,删除后点击原始重发。";
return result;
}
//箱号、箱型、箱量判断
var lastFeeCtn = feeList.Where(x => x.feectn.FeeID == lastFee.fee.GID).ToList();
foreach (var ct in ctns)
{
var feeCtn = lastFeeCtn.FirstOrDefault(x => x.feectn.XiangHao == ct.CNTRNO);
if (feeCtn == null)
{
result.Success = false;
result.Message = "箱号为海关不允许修改字段,请先提交删除,删除后点击原始重发。";
return result;
}
else if (feeCtn.feectn.XiangXing != ct.CTNALL)
{
result.Success = false;
result.Message = "箱型为海关不允许修改字段,请先提交删除,删除后点击原始重发。";
return result;
}
}
}
#region 2023年1月16日增加根据箱号进行合计 跟主单比较的判断逻辑
var ctnListZhudan = edc.EdiCtns.AsNoTracking().Where(cc => cc.MFNO == zhudan.MFNO).ToList();
var sumPkgsZhudan = ctnListZhudan.GroupBy(x => x.CNTRNO).Select(x => new { cntrno = x.Key, sum = x.Sum(y => y.PKGS) }).ToList();
var sumKgsZhudan = ctnListZhudan.GroupBy(x => x.CNTRNO).Select(x => new { cntrno = x.Key, sum = x.Sum(y => y.KGS) }).ToList();
var sumCbmZhudan = ctnListZhudan.GroupBy(x => x.CNTRNO).Select(x => new { cntrno = x.Key, sum = x.Sum(y => y.CBM) }).ToList();
var ctnListFen = (from ct in edc.EdiCtns
join bi in edc.Edis on ct.MFNO equals bi.MFNO
where bi.BSNO == zhudan.MFNO
select ct)
.ToList();
var sumPkgsFen = ctnListFen.GroupBy(x => x.CNTRNO).Select(x => new { cntrno = x.Key, sum = x.Sum(y => y.PKGS) }).ToList();
var sumKgsFen = ctnListFen.GroupBy(x => x.CNTRNO).Select(x => new { cntrno = x.Key, sum = x.Sum(y => y.KGS) }).ToList();
var sumCbmFen = ctnListFen.GroupBy(x => x.CNTRNO).Select(x => new { cntrno = x.Key, sum = x.Sum(y => y.CBM) }).ToList();
var cntrnoList = ctnListFen.Select(x => x.CNTRNO).Distinct().ToList(); //所有箱号
var errStrList = new List<string>();
foreach (var cntrno in cntrnoList)
{
if (sumPkgsFen.First(x => x.cntrno == cntrno).sum > sumPkgsZhudan.First(x => x.cntrno == cntrno).sum)
{
errStrList.Add($"箱号为<b>{cntrno}</b>的分单<b>件数</b>合计大于主单合计");
}
if (sumKgsFen.First(x => x.cntrno == cntrno).sum > sumKgsZhudan.First(x => x.cntrno == cntrno).sum)
{
errStrList.Add($"箱号为<b>{cntrno}</b>的分单<b>重量</b>合计大于主单合计");
}
if (sumCbmFen.First(x => x.cntrno == cntrno).sum > sumCbmZhudan.First(x => x.cntrno == cntrno).sum)
{
errStrList.Add($"箱号为<b>{cntrno}</b>的分单<b>尺码</b>合计大于主单合计");
}
}
if (errStrList.Count > 0)
{
result.Success = false;
result.Message = string.Join("<br/>", errStrList);
return result;
}
#endregion
ZDXXModel hz = null;
List<ZDXXDetailModel> hzCtn = null;
//先读取主单回执
if (EdiHelper.ReadResponseZD(userid, ediList[0].BSNO, out string msgRtn, out hz, out hzCtn))
{
foreach (var item in ediList) //遍历每一票分单,跟扣费记录中保存的值比较
{
var lastFee = feeList.First(x => x.fee.BSNO == item.MFNO);
//件数、重量改大校验
if (item.KGS != lastFee.fee.KGS || item.PKGS != lastFee.fee.PKGS)
{
if (fendanList.Sum(x => x.KGS) > hz.KGS || fendanList.Sum(x => x.PKGS) > hz.PKGS) //分单合计与主单回执比较
{
result.Success = false;
result.Message = "请先修改主单件重,修改成功后再提交分单修改。";
return result;
}
}
}
//生成报文
var succ = EdiHelper.GenDirectEdiFileFenModify(userid, ediList, reason, _user.SHOWNAME, _userbaseinfo.MOBILE, pathFileBase, hz, out string rtnMsg);
if (succ)
{
var ediFileName = rtnMsg;
var ediFullPth = Path.Combine(pathFileBase, ediFileName);
log.Debug($"成功生成舱单直发修改报文:{ediFullPth}");
decimal price = 0;
var canFee = EdiHelper.CheckEdiFee(_user.COMNAME, 7, out string rtnFee, out price, num: ediList.Count);
if (canFee)
{
//发送到ftp爬虫
string rtnFtp = string.Empty;
if (debugNoSendEdiFile)
{
succ = true;
}
else
{
succ = EdiHelper.SendEdiFileToFtpSpider(ediFullPth, userid, out rtnFtp);
}
if (succ)
{
//扣费并执行后续操作
var bala = edc.Cust_Balance.First(b => b.COMNAME == _user.COMNAME);
foreach (var ee in ediList)
{
log.Debug($"舱单直发修改准备扣费:{ee.MBLNO} {ee.HBLNO} {_user.COMNAME}");
ee.EDISTATUS = "已直发";//置状态
//扣费和生成消费记录
var feeMD = new Cust_Fee_md(ee, 7, _user.GID, _user.SHOWNAME, _user.comid, _user.COMNAME, edc);
feeMD.PRICE = price;
edc.Cust_Fee.Add(feeMD);
bala.Balance -= feeMD.PRICE;
#region 判断修改内容,写入备注
var ctns = edc.EdiCtns.AsNoTracking().Where(x => x.MFNO == ee.MFNO).ToList();
var lastFee = feeList.First(x => x.fee.BSNO == ee.MFNO);
var lastFeeCtn = feeList.Where(x => x.feectn.FeeID == lastFee.fee.GID).ToList();
var listModify = new List<string>();
if (ee.MBLNO != lastFee.fee.MBLNO && !listModify.Contains("主提单号"))
{
listModify.Add("主提单号");
}
if (ee.HBLNO != lastFee.fee.HBLNO && !listModify.Contains("分提单号"))
{
listModify.Add("分提单号");
}
if (ee.VESSEL != lastFee.fee.VESSEL && !listModify.Contains("船名"))
{
listModify.Add("船名");
}
if (ee.VOYNO != lastFee.fee.VOYNO && !listModify.Contains("航次"))
{
listModify.Add("航次");
}
if (ee.CARRIER != lastFee.fee.CARRIER && !listModify.Contains("船公司"))
{
listModify.Add("船公司");
}
if (ee.FORWARDER != lastFee.fee.FORWARDER && !listModify.Contains("船代"))
{
listModify.Add("船代");
}
if (ee.PORTDISCHARGE != lastFee.fee.PORTDISCHARGE && !listModify.Contains("卸货港"))
{
listModify.Add("卸货港");
}
if (ee.DESTINATION != lastFee.fee.DESTINATION && !listModify.Contains("目的地"))
{
listModify.Add("目的地");
}
if (ee.DESCRIPTION != lastFee.fee.DESCRIPTION && !listModify.Contains("货描"))
{
listModify.Add("货描");
}
if (ee.MARKS != lastFee.fee.MARKS && !listModify.Contains("唛头"))
{
listModify.Add("唛头");
}
if (ee.KGS != lastFee.fee.KGS && !listModify.Contains("重量"))
{
listModify.Add("重量");
}
if (ee.PKGS != lastFee.fee.PKGS && !listModify.Contains("件数"))
{
listModify.Add("件数");
}
if (ee.KINDPKGS != lastFee.fee.KINDPKGS && !listModify.Contains("包装"))
{
listModify.Add("包装");
}
if (ee.CBM != lastFee.fee.CBM && !listModify.Contains("尺码"))
{
listModify.Add("尺码");
}
foreach (var ctn in ctns)
{
var ctnFee = lastFeeCtn.FirstOrDefault(cc => cc.feectn.XiangHao == ctn.CNTRNO);
if (ctnFee == null)
{
if (!listModify.Contains("箱号"))
{
listModify.Add("箱号");
}
}
else
{
if (ctn.SEALNO != ctnFee.feectn.FengHao)
{
if (!listModify.Contains("封号"))
{
listModify.Add("封号");
}
}
if (ctn.CTNALL != ctnFee.feectn.XiangXing)
{
if (!listModify.Contains("箱型"))
{
listModify.Add("箱型");
}
}
if (ctn.PKGS != ctnFee.feectn.JianShu)
{
if (!listModify.Contains("箱件数"))
{
listModify.Add("箱件数");
}
}
if (ctn.KGS != ctnFee.feectn.ZhongLiang)
{
if (!listModify.Contains("箱重量"))
{
listModify.Add("箱重量");
}
}
if (ctn.CBM != ctnFee.feectn.ChiMa)
{
if (!listModify.Contains("箱尺码"))
{
listModify.Add("箱尺码");
}
}
if (ctn.ChengZhongZhongLiang != ctnFee.feectn.ChengZhongZhongLiang)
{
if (!listModify.Contains("称重重量"))
{
listModify.Add("称重重量");
}
}
if (ctn.ChengZhongFangShi != ctnFee.feectn.ChengZhongFangShi)
{
if (!listModify.Contains("称重方式"))
{
listModify.Add("称重方式");
}
}
}
}
feeMD.BEIZHU = $"修改:{string.Join("", listModify)}";
#endregion
var ctnlist = edc.EdiCtns.AsNoTracking().Where(x => x.MFNO == ee.MFNO).ToList();
foreach (var ctn in ctnlist)
{
var feeCtn = new Cust_Fee_Ctn_md(ctn, feeMD.GID);
edc.Cust_Fee_Ctn.Add(feeCtn);
}
}
log.Debug($"舱单直发修改扣费完成:{ediList[0].MBLNO} {string.Join(",", ediList.Select(x => x.HBLNO))} {_user.COMNAME}");
foreach (var ee in ediList)
{
//添加tips
var tips = EdiHelper.CreateEdiTips(ee, _user.SHOWNAME, "[修改]", "修改");
edc.OP_SEAE_EDI_TIPS.Add(tips);
}
edc.SaveChanges();
//生成东胜报文文件,并发送
var tempDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TempFiles");
if (!Directory.Exists(tempDir))
{
Directory.CreateDirectory(tempDir);
}
//发送邮件通知和ftp
foreach (var ee in ediList)
{
var tmpEdiFile = MsOpSeaeEdiDAL.CreateCustomEdiDS(AutoMapper.Mapper.Map<MsOpSeaeEdi>(ee), "", tempDir, new DSWeb.MvcShipping.Models.FtpSet.CodeFtpSet(), ee.CARRIER, companyname, ee.MBLNO);
var ediFN = Path.GetFileName(tmpEdiFile);
log.Debug($"处理{ee.HBLNO}的直发修改分单邮件和ftp发送edi文件{tmpEdiFile}");
//邮件
EdiHelper.SendDirectEdiMailDS(ee.MBLNO, ee.HBLNO, ee.FORWARDER, _user.COMNAME, _user.SHOWNAME, _userbaseinfo.EMAIL1, _userbaseinfo.MOBILE, "修改(自动)", reason, new AttachFileModel() { FilePath = tmpEdiFile, FileName = ediFN });
//发送ftp
EdiHelper.CangDanDSFtp(ee.MBLNO, ee.HBLNO, ee.FORWARDER, _user.COMNAME, _user.SHOWNAME, _userbaseinfo.EMAIL1, _userbaseinfo.MOBILE, "修改(自动)", reason, tmpEdiFile);
log.Debug($"处理{ee.HBLNO}的直发修改分单邮件和ftp发送完成");
}
result.Success = true;
result.Message = "发送完成";
return result;
}
else
{
result.Success = false;
result.Message = rtnFtp;
return result;
}
}
else
{
result.Success = false;
result.Message = rtnFee;
return result;
}
}
else
{
result.Success = false;
result.Message = rtnMsg;
return result;
}
}
else
{
result.Success = false;
result.Message = $"读取主单回执失败:{msgRtn}";
log.Error($"读取主单回执({ediList[0].BSNO} {ediList[0].MBLNO})失败:{msgRtn}");
}
}
catch (Exception ex)
{
result.Success = false;
result.Message = ex.Message;
log.Error($"处理直发(分单)失败:{ex.Message}");
log.Error(ex.StackTrace);
return result;
}
}
#endregion
#endregion
return result;
}
/// <summary>
/// 生成舱单直发删除EDI报文只有分单支持批量
/// </summary>
/// <param name="userid">用户id</param>
/// <param name="mblno">主单号</param>
/// <param name="listHBLNO">分单号列表</param>
/// <param name="reason">变更原因描述</param>
/// <param name="contactName">变更申请联系人姓名</param>
/// <param name="contactTel">变更申请联系人电话</param>
/// <param name="filePath">文件存放路径</param>
/// <param name="rtnMsg">成功时返回报文文件名称(不带路径),失败时返回错误信息</param>
/// <returns>是否成功</returns>
public static bool GenDirectEdiFileFenDel(string userid, string mblno, List<string> listHBLNO, string reason, string contactName, string contactTel, string filePath, out string rtnMsg)
{
log.Debug($"生成舱单直发删除EDI报文{mblno} {string.Join(",", listHBLNO)} {reason}");
EdiDataContext edc = new EdiDataContext();
var user = edc.Users.First(x => x.GID == userid);
if (string.IsNullOrEmpty(user.CompId))
{
rtnMsg = "用户未加入任何已认证的企业";
return false;
}
var comp = edc.CompanyNew.First(c => c.CompId == user.CompId);
if (string.IsNullOrEmpty(comp.CangdanCode))
{
rtnMsg = "所属公司的舱单传输备案号未设置";
return false;
}
var staMain = edc.op_seae_edi_mainstatus.FirstOrDefault(x => x.mblno == mblno);
var staCtn = edc.op_seae_edi_mainstatus_xhinfo.Where(x => x.mblno == mblno).ToList();
if (staMain == null || staCtn.Count == 0)
{
rtnMsg = "主单回执未正确读取";
return false;
}
var xmlDoc = new XmlDocument();
var xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
xmlDoc.AppendChild(xmlDec);
var eleManifest = xmlDoc.CreateElement("Manifest");
xmlDoc.AppendChild(eleManifest);
#region Head
//报文头
var eleHead = xmlDoc.CreateElement("Head");
eleManifest.AppendChild(eleHead);
//报文编号循环次数1
var eleMessageID = xmlDoc.CreateElement("MessageID");
eleMessageID.AppendChild(xmlDoc.CreateTextNode($"{comp.CangdanCode}_{DateTime.Now.ToString("yyyyMMddHHmmss")}"));
eleHead.AppendChild(eleMessageID);
//报文功能代码循环次数1
var eleFunctionCode = xmlDoc.CreateElement("FunctionCode");
eleFunctionCode.AppendChild(xmlDoc.CreateTextNode("3"));
eleHead.AppendChild(eleFunctionCode);
//报文类型代码循环次数1
var eleMessageType = xmlDoc.CreateElement("MessageType");
eleMessageType.AppendChild(xmlDoc.CreateTextNode("MT2101"));
eleHead.AppendChild(eleMessageType);
//发送方代码循环次数1
var eleSenderID = xmlDoc.CreateElement("SenderID");
eleSenderID.AppendChild(xmlDoc.CreateTextNode(comp.CangdanCode));
eleHead.AppendChild(eleSenderID);
//接受方代码循环次数1
var eleReceiverID = xmlDoc.CreateElement("ReceiverID");
eleReceiverID.AppendChild(xmlDoc.CreateTextNode(comp.CangdanCode.Substring(0, 4)));
eleHead.AppendChild(eleReceiverID);
//发送时间循环次数1
var eleSendTime = xmlDoc.CreateElement("SendTime");
eleSendTime.AppendChild(xmlDoc.CreateTextNode(DateTime.Now.ToString("yyyyMMddHHmmssfff")));
eleHead.AppendChild(eleSendTime);
//报文版本号循环次数1
var eleVersion = xmlDoc.CreateElement("Version");
eleVersion.AppendChild(xmlDoc.CreateTextNode("1.0"));
eleHead.AppendChild(eleVersion);
#endregion
//报文体
var eleDeclaration = xmlDoc.CreateElement("Declaration");
eleManifest.AppendChild(eleDeclaration);
#region 承运人
//舱单传输人数据段循环次数1
var eleRepresentativePerson = xmlDoc.CreateElement("RepresentativePerson");
eleDeclaration.AppendChild(eleRepresentativePerson);
//256 Representative person name 舱单传输人名称循环次数1
var eleRepresentativePersonName = xmlDoc.CreateElement("Name");
eleRepresentativePersonName.AppendChild(xmlDoc.CreateTextNode(comp.CangdanCode.Substring(0, 4)));
eleRepresentativePerson.AppendChild(eleRepresentativePersonName);
//运输工具数据段循环次数1
var eleBorderTransportMeans = xmlDoc.CreateElement("BorderTransportMeans");
eleDeclaration.AppendChild(eleBorderTransportMeans);
//航次航班编号循环次数1
var eleBorderTransportMeansJourneyID = xmlDoc.CreateElement("JourneyID");
eleBorderTransportMeans.AppendChild(eleBorderTransportMeansJourneyID);
eleBorderTransportMeansJourneyID.AppendChild(xmlDoc.CreateTextNode(staMain.voyage_number));
//运输工具代码循环次数0-1
var eleBorderTransportMeansID = xmlDoc.CreateElement("ID");//船舶IMO
eleBorderTransportMeans.AppendChild(eleBorderTransportMeansID);
eleBorderTransportMeansID.AppendChild(xmlDoc.CreateTextNode(staMain.IMO));
#endregion
#region 提(运)单数据段
foreach (var hbl in listHBLNO)
{
var eleConsignment = xmlDoc.CreateElement("Consignment");
eleDeclaration.AppendChild(eleConsignment);
//运输合同信息
var eleTransportContractDocument = xmlDoc.CreateElement("TransportContractDocument");
eleConsignment.AppendChild(eleTransportContractDocument);
//总提(运)单号
var eleTransportContractDocumentID = xmlDoc.CreateElement("ID");
eleTransportContractDocument.AppendChild(eleTransportContractDocumentID);
eleTransportContractDocumentID.AppendChild(xmlDoc.CreateTextNode(mblno));
//更改原因代码
var eleTransportContractDocumentChangeReasonCode = xmlDoc.CreateElement("ChangeReasonCode");//
eleTransportContractDocument.AppendChild(eleTransportContractDocumentChangeReasonCode);
eleTransportContractDocumentChangeReasonCode.AppendChild(xmlDoc.CreateTextNode("999"));
//运输合同附加信息
var eleAssociatedTransportDocument = xmlDoc.CreateElement("AssociatedTransportDocument");
eleConsignment.AppendChild(eleAssociatedTransportDocument);
//分提(运)单号
var eleAssociatedTransportDocumentID = xmlDoc.CreateElement("ID");
eleAssociatedTransportDocument.AppendChild(eleAssociatedTransportDocumentID);
eleAssociatedTransportDocumentID.AppendChild(xmlDoc.CreateTextNode(hbl));
}
#endregion
#region 备注
//备注
var eleAdditionalInformation = xmlDoc.CreateElement("AdditionalInformation");
eleDeclaration.AppendChild(eleAdditionalInformation);
//变更原因描述
var eleAdditionalInformationReason = xmlDoc.CreateElement("Reason");
eleAdditionalInformation.AppendChild(eleAdditionalInformationReason);
eleAdditionalInformationReason.AppendChild(xmlDoc.CreateTextNode(reason));
//变更申请联系人姓名
var eleAdditionalInformationContactName = xmlDoc.CreateElement("ContactName");
eleAdditionalInformation.AppendChild(eleAdditionalInformationContactName);
eleAdditionalInformationContactName.AppendChild(xmlDoc.CreateTextNode(contactName));
//变更申请联系人电话
var eleAdditionalInformationContactTel = xmlDoc.CreateElement("ContactTel");
eleAdditionalInformation.AppendChild(eleAdditionalInformationContactTel);
eleAdditionalInformationContactTel.AppendChild(xmlDoc.CreateTextNode(contactTel));
//备注
var eleAdditionalInformationContent = xmlDoc.CreateElement("Content");
eleAdditionalInformation.AppendChild(eleAdditionalInformationContent);
eleAdditionalInformationContent.AppendChild(xmlDoc.CreateTextNode(reason));
#endregion
eleManifest.SetAttribute("xmlns", "urn:Declaration:datamodel:standard:CN:MT2101:1");
if (Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
var fileName = $"{mblno}_edi_del_{DateTime.Now.Ticks}.txt";
var fullPath = Path.Combine(filePath, fileName);
xmlDoc.Save(fullPath);
rtnMsg = fileName;
return true;
}
/// <summary>
/// 生成舱单直发修改EDI报文只有分单支持批量
/// </summary>
/// <param name="userid">用户id</param>
/// <param name="mblno">主单号</param>
/// <param name="listHBLNO">分单号列表</param>
/// <param name="reason">变更原因描述</param>
/// <param name="contactName">变更申请联系人姓名</param>
/// <param name="contactTel">变更申请联系人电话</param>
/// <param name="filePath">文件存放路径</param>
/// <param name="rtnMsg">成功时返回报文文件名称(不带路径),失败时返回错误信息</param>
/// <returns>是否成功</returns>
public static bool GenDirectEdiFileFenModify(string userid, List<OpSeaeEdi> listEdis, string reason, string contactName, string contactTel, string filePath, ZDXXModel zdxx, out string rtnMsg)
{
var mblno = listEdis[0].MBLNO;
log.Debug($"生成舱单直发修改EDI报文{mblno} {string.Join(",", listEdis.Select(x => x.HBLNO))} {reason}");
EdiDataContext edc = new EdiDataContext();
var user = edc.Users.First(x => x.GID == userid);
if (string.IsNullOrEmpty(user.CompId))
{
rtnMsg = "用户未加入任何已认证的企业";
return false;
}
var comp = edc.CompanyNew.First(c => c.CompId == user.CompId);
if (string.IsNullOrEmpty(comp.CangdanCode))
{
rtnMsg = "所属公司的舱单传输备案号未设置";
return false;
}
var staMain = edc.op_seae_edi_mainstatus.FirstOrDefault(x => x.mblno == mblno);
var staCtn = edc.op_seae_edi_mainstatus_xhinfo.Where(x => x.mblno == mblno).ToList();
if (staMain == null || staCtn.Count == 0)
{
rtnMsg = "主单回执未正确读取";
return false;
}
var codeLoad = edc.CodeLoadport.FirstOrDefault(x => x.EDICODE.Contains(zdxx.customs_code));
if (codeLoad == null)
{
rtnMsg = $"未找到起始港关区代码配置:{zdxx.customs_code}";
return false;
}
CommonDataContext commonData = new CommonDataContext();
var codePackages = commonData.Packages.AsNoTracking().ToList();
var codeCtns = commonData.Ctns.AsNoTracking().ToList();
var codePkgEdis = commonData.CodePkgsEdi.AsNoTracking().ToList();
var xmlDoc = new XmlDocument();
var xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
xmlDoc.AppendChild(xmlDec);
var eleManifest = xmlDoc.CreateElement("Manifest");
xmlDoc.AppendChild(eleManifest);
#region Head
//报文头
var eleHead = xmlDoc.CreateElement("Head");
eleManifest.AppendChild(eleHead);
//报文编号循环次数1
var eleMessageID = xmlDoc.CreateElement("MessageID");
eleMessageID.AppendChild(xmlDoc.CreateTextNode($"{comp.CangdanCode}_{DateTime.Now.ToString("yyyyMMddHHmmss")}"));
eleHead.AppendChild(eleMessageID);
//报文功能代码循环次数1
var eleFunctionCode = xmlDoc.CreateElement("FunctionCode");
eleFunctionCode.AppendChild(xmlDoc.CreateTextNode("5"));
eleHead.AppendChild(eleFunctionCode);
//报文类型代码循环次数1
var eleMessageType = xmlDoc.CreateElement("MessageType");
eleMessageType.AppendChild(xmlDoc.CreateTextNode("MT2101"));
eleHead.AppendChild(eleMessageType);
//发送方代码循环次数1
var eleSenderID = xmlDoc.CreateElement("SenderID");
eleSenderID.AppendChild(xmlDoc.CreateTextNode(comp.CangdanCode));
eleHead.AppendChild(eleSenderID);
//接受方代码循环次数1
var eleReceiverID = xmlDoc.CreateElement("ReceiverID");
eleReceiverID.AppendChild(xmlDoc.CreateTextNode(comp.CangdanCode.Substring(0, 4)));
eleHead.AppendChild(eleReceiverID);
//发送时间循环次数1
var eleSendTime = xmlDoc.CreateElement("SendTime");
eleSendTime.AppendChild(xmlDoc.CreateTextNode(DateTime.Now.ToString("yyyyMMddHHmmssfff")));
eleHead.AppendChild(eleSendTime);
//报文版本号循环次数1
var eleVersion = xmlDoc.CreateElement("Version");
eleVersion.AppendChild(xmlDoc.CreateTextNode("1.0"));
eleHead.AppendChild(eleVersion);
#endregion
//报文体
var eleDeclaration = xmlDoc.CreateElement("Declaration");
eleManifest.AppendChild(eleDeclaration);
#region 承运人
//舱单传输人数据段循环次数1
var eleRepresentativePerson = xmlDoc.CreateElement("RepresentativePerson");
eleDeclaration.AppendChild(eleRepresentativePerson);
//256 Representative person name 舱单传输人名称循环次数1
var eleRepresentativePersonName = xmlDoc.CreateElement("Name");
eleRepresentativePersonName.AppendChild(xmlDoc.CreateTextNode(comp.CangdanCode));
eleRepresentativePerson.AppendChild(eleRepresentativePersonName);
//运输工具离境地海关数据段循环次数1
var eleExitCustomsOffice = xmlDoc.CreateElement("ExitCustomsOffice");
eleDeclaration.AppendChild(eleExitCustomsOffice);
//运输工具离境地海关代码循环次数1
var eleExitCustomsOfficeID = xmlDoc.CreateElement("ID");
eleExitCustomsOfficeID.AppendChild(xmlDoc.CreateTextNode(codeLoad.EDICODE));
eleExitCustomsOffice.AppendChild(eleExitCustomsOfficeID);
//运输工具代理企业数据段循环次数0-1
var eleAgent = xmlDoc.CreateElement("Agent");
eleDeclaration.AppendChild(eleAgent);
//运输工具代理企业代码循环次数1
var eleAgentID = xmlDoc.CreateElement("ID");
eleAgentID.AppendChild(xmlDoc.CreateTextNode(comp.CangdanCode));
eleAgent.AppendChild(eleAgentID);
//承运人数据段循环次数1
var eleCarrier = xmlDoc.CreateElement("Carrier");
eleDeclaration.AppendChild(eleCarrier);
//承运人代码循环次数1
var eleCarrierID = xmlDoc.CreateElement("ID");
eleCarrierID.AppendChild(xmlDoc.CreateTextNode(listEdis[0].CARRIER));
eleCarrier.AppendChild(eleCarrierID);
#endregion
#region 运输工具
//运输工具数据段循环次数1
var eleBorderTransportMeans = xmlDoc.CreateElement("BorderTransportMeans");
eleDeclaration.AppendChild(eleBorderTransportMeans);
//航次航班编号循环次数1
var eleBorderTransportMeansJourneyID = xmlDoc.CreateElement("JourneyID");
eleBorderTransportMeans.AppendChild(eleBorderTransportMeansJourneyID);
eleBorderTransportMeansJourneyID.AppendChild(xmlDoc.CreateTextNode(staMain.voyage_number));
//运输方式代码循环次数1
var eleBorderTransportMeansTypeCode = xmlDoc.CreateElement("TypeCode");
eleBorderTransportMeans.AppendChild(eleBorderTransportMeansTypeCode);
eleBorderTransportMeansTypeCode.AppendChild(xmlDoc.CreateTextNode("1"));
//运输工具代码循环次数0-1
var eleBorderTransportMeansID = xmlDoc.CreateElement("ID");//船舶IMO
eleBorderTransportMeans.AppendChild(eleBorderTransportMeansID);
eleBorderTransportMeansID.AppendChild(xmlDoc.CreateTextNode(staMain.IMO));
//运输工具名称循环次数0-1
var eleBorderTransportMeansName = xmlDoc.CreateElement("Name");
eleBorderTransportMeans.AppendChild(eleBorderTransportMeansName);
eleBorderTransportMeansName.AppendChild(xmlDoc.CreateTextNode(listEdis[0].VESSEL));
#endregion
#region 提(运)单数据段
foreach (var bill in listEdis)
{
var eleConsignment = xmlDoc.CreateElement("Consignment");
eleDeclaration.AppendChild(eleConsignment);
#region 付费方式、运输条款
string sSERVICE = "";
if (bill.SERVICE.ToUpper() == "DOOR-DOOR")//Door-to-door
{
sSERVICE = "27";
}
else if (bill.SERVICE.ToUpper() == "DOOR-CY")//Door-to-pier
{
sSERVICE = "28";
}
else if (bill.SERVICE.ToUpper() == "CY-DOOR")//Pier-to-door
{
sSERVICE = "29";
}
else if (bill.SERVICE.ToUpper() == "CY-CY")//Pier-to-pier
{
sSERVICE = "10";
}
else
{
sSERVICE = "10";
}
//运输合同信息循环次数1
var eleTransportContractDocument = xmlDoc.CreateElement("TransportContractDocument");
eleConsignment.AppendChild(eleTransportContractDocument);
//总提单号循环次数1
var eleTransportContractDocumentID = xmlDoc.CreateElement("ID");
eleTransportContractDocument.AppendChild(eleTransportContractDocumentID);
eleTransportContractDocumentID.AppendChild(xmlDoc.CreateTextNode(bill.MBLNO));
//修改原因
var eleTransportContractDocumentChangeReasonCode = xmlDoc.CreateElement("ChangeReasonCode");
eleTransportContractDocument.AppendChild(eleTransportContractDocumentChangeReasonCode);
eleTransportContractDocumentChangeReasonCode.AppendChild(xmlDoc.CreateTextNode("999"));
//运输合同附加信息循环次数0-1
var eleAssociatedTransportDocument = xmlDoc.CreateElement("AssociatedTransportDocument");
eleConsignment.AppendChild(eleAssociatedTransportDocument);
//分提单号循环次数1
var eleAssociatedTransportDocumentID = xmlDoc.CreateElement("ID");
eleAssociatedTransportDocument.AppendChild(eleAssociatedTransportDocumentID);
eleAssociatedTransportDocumentID.AppendChild(xmlDoc.CreateTextNode(bill.HBLNO));
#endregion
#region 货物信息
//装货地信息循环次数0-1
var eleLoadingLocation = xmlDoc.CreateElement("LoadingLocation");
eleConsignment.AppendChild(eleLoadingLocation);
//装货地代码循环次数0-1
//报文节点中的装货地LoadingLocation取大简云的装货港代码
var eleLoadingLocationID = xmlDoc.CreateElement("ID");
eleLoadingLocation.AppendChild(eleLoadingLocationID);
eleLoadingLocationID.AppendChild(xmlDoc.CreateTextNode(codeLoad.EDICODE));
//货物装载运输工具时间循环次数0-1
//报文节点中的LoadingDate取大简云的预抵日期
if (bill.ETA.HasValue)
{
var eleLoadingLocationLoadingDate = xmlDoc.CreateElement("LoadingDate");
eleLoadingLocation.AppendChild(eleLoadingLocationLoadingDate);
eleLoadingLocationLoadingDate.AppendChild(xmlDoc.CreateTextNode(bill.ETA.Value.ToString("yyyyMMddHHmmss")));
}
//卸货地信息循环次数1
var eleUnloadingLocation = xmlDoc.CreateElement("UnloadingLocation");
eleConsignment.AppendChild(eleUnloadingLocation);
//卸货地代码循环次数1
var eleUnloadingLocationID = xmlDoc.CreateElement("ID");
eleUnloadingLocation.AppendChild(eleUnloadingLocationID);
eleUnloadingLocationID.AppendChild(xmlDoc.CreateTextNode(bill.PORTDISCHARGEID));
//收货地信息循环次数0-1
var eleGoodsReceiptPlace = xmlDoc.CreateElement("GoodsReceiptPlace");
eleConsignment.AppendChild(eleGoodsReceiptPlace);
//收货地点代码循环次数0-1
var eleGoodsReceiptPlaceID = xmlDoc.CreateElement("ID");
eleGoodsReceiptPlace.AppendChild(eleGoodsReceiptPlaceID);
eleGoodsReceiptPlaceID.AppendChild(xmlDoc.CreateTextNode(bill.DESTINATIONID));
//收货地点名称循环次数0-1
var eleGoodsReceiptPlaceName = xmlDoc.CreateElement("Name");
eleGoodsReceiptPlace.AppendChild(eleGoodsReceiptPlaceName);
eleGoodsReceiptPlaceName.AppendChild(xmlDoc.CreateTextNode(bill.DESTINATION));
//货物托运地信息
var eleGoodsConsignedPlace = xmlDoc.CreateElement("GoodsConsignedPlace");
eleConsignment.AppendChild(eleGoodsConsignedPlace);
// <!--收货地点代码循环次数0-1-->
var eleGoodsConsignedPlaceID = xmlDoc.CreateElement("ID");
eleGoodsConsignedPlace.AppendChild(eleGoodsConsignedPlaceID);
eleGoodsConsignedPlaceID.AppendChild(xmlDoc.CreateTextNode("CN37"));
//货物海关状态代码循环次数0-99
var eleCustomsStatusCode = xmlDoc.CreateElement("CustomsStatusCode");
eleConsignment.AppendChild(eleCustomsStatusCode);
eleCustomsStatusCode.AppendChild(xmlDoc.CreateTextNode("001"));
var xtype = "PP";
if (bill.BLFRT.IndexOf("FREIGHT COLLECT") >= 0)
xtype = "CC";
//运费支付信息循环次数1
var eleFreightPayment = xmlDoc.CreateElement("FreightPayment");
eleConsignment.AppendChild(eleFreightPayment);
//运费支付方法代码循环次数1
var eleFreightPaymentMethodCode = xmlDoc.CreateElement("MethodCode");
eleFreightPayment.AppendChild(eleFreightPaymentMethodCode);
eleFreightPaymentMethodCode.AppendChild(xmlDoc.CreateTextNode(xtype));
//货物总件数/包装种类代码循环次数1
var eleConsignmentPackaging = xmlDoc.CreateElement("ConsignmentPackaging");
eleConsignment.AppendChild(eleConsignmentPackaging);
//货物总件数循环次数1
var eleConsignmentPackagingQuantityQuantity = xmlDoc.CreateElement("QuantityQuantity");
eleConsignmentPackaging.AppendChild(eleConsignmentPackagingQuantityQuantity);
eleConsignmentPackagingQuantityQuantity.AppendChild(xmlDoc.CreateTextNode(bill.PKGS.ToString()));
// 包装种类代码循环次数0-1
var eleConsignmentPackagingTypeCode = xmlDoc.CreateElement("TypeCode");
eleConsignmentPackaging.AppendChild(eleConsignmentPackagingTypeCode);
var codePkg = codePackages.FirstOrDefault(xx => xx.PKGS == bill.KINDPKGS);
eleConsignmentPackagingTypeCode.AppendChild(xmlDoc.CreateTextNode(codePkg == null ? String.Empty : codePkg.EDICODE));
//货物总毛重循环次数1
var eleTotalGrossMassMeasure = xmlDoc.CreateElement("TotalGrossMassMeasure");
eleConsignment.AppendChild(eleTotalGrossMassMeasure);
eleTotalGrossMassMeasure.AppendChild(xmlDoc.CreateTextNode(bill.KGS.ToString()));
#endregion
#region 收货人
//收货人信息循环次数0-1
var eleConsignee = xmlDoc.CreateElement("Consignee");
eleConsignment.AppendChild(eleConsignee);
//收货人名称循环次数0-1
var strCONSIGNEE = MsOpSeaeEdiPortDAL.formatEdiStr("txt", bill.CONSIGNEE);
var listCONSIGNEE = MsOpSeaeEdiPortDAL.formatlengthStr(strCONSIGNEE, 35);
var eleConsigneeName = xmlDoc.CreateElement("Name");
eleConsignee.AppendChild(eleConsigneeName);
if (listCONSIGNEE.Count > 0)
{
eleConsigneeName.AppendChild(xmlDoc.CreateTextNode(listCONSIGNEE[0]));
}
//收货人地址信息循环次数0-1
var eleConsigneeAddress = xmlDoc.CreateElement("Address");
eleConsignee.AppendChild(eleConsigneeAddress);
//详细地址(街道,邮箱循环次数0-1
var eleConsigneeAddressLine = xmlDoc.CreateElement("Line");
eleConsigneeAddress.AppendChild(eleConsigneeAddressLine);
eleConsigneeAddressLine.AppendChild(xmlDoc.CreateTextNode(bill.CONSIGNEEADDR1));
//国家代码循环次数0-1
var eleConsigneeAddressCountryCode = xmlDoc.CreateElement("CountryCode");
eleConsigneeAddress.AppendChild(eleConsigneeAddressCountryCode);
eleConsigneeAddressCountryCode.AppendChild(xmlDoc.CreateTextNode(bill.CONSIGNEECOUNTRY));
//收货人通讯方式信息循环次数0-3
var eleConsigneeCommunication = xmlDoc.CreateElement("Communication");
eleConsignee.AppendChild(eleConsigneeCommunication);
if (!string.IsNullOrWhiteSpace(bill.SHIPPERTEL))
{
var eleConsigneeCommunicationID = xmlDoc.CreateElement("ID");
eleConsigneeCommunication.AppendChild(eleConsigneeCommunicationID);
eleConsigneeCommunicationID.AppendChild(xmlDoc.CreateTextNode(bill.SHIPPERTEL));
var eleConsigneeCommunicationTypeID = xmlDoc.CreateElement("TypeID");
eleConsigneeCommunication.AppendChild(eleConsigneeCommunicationTypeID);
eleConsigneeCommunicationTypeID.AppendChild(xmlDoc.CreateTextNode("TE"));
}
else
{
var eleConsigneeCommunicationID = xmlDoc.CreateElement("ID");
eleConsigneeCommunication.AppendChild(eleConsigneeCommunicationID);
eleConsigneeCommunicationID.AppendChild(xmlDoc.CreateTextNode(bill.CONSIGNEEEMAIL));
var eleConsigneeCommunicationTypeID = xmlDoc.CreateElement("TypeID");
eleConsigneeCommunication.AppendChild(eleConsigneeCommunicationTypeID);
eleConsigneeCommunicationTypeID.AppendChild(xmlDoc.CreateTextNode("EM"));
}
#endregion
#region 发货人
//发货人信息循环次数0-1
var eleConsignor = xmlDoc.CreateElement("Consignor");
eleConsignment.AppendChild(eleConsignor);
//发货人名称循环次数0-1
var strSHIPPER = MsOpSeaeEdiPortDAL.formatEdiStr("txt", bill.SHIPPER);
var listSHIPPER = MsOpSeaeEdiPortDAL.formatlengthStr(strSHIPPER, 35);
var eleConsignorName = xmlDoc.CreateElement("Name");
eleConsignor.AppendChild(eleConsignorName);
if (listSHIPPER.Count > 0)
{
eleConsignorName.AppendChild(xmlDoc.CreateTextNode(listSHIPPER[0]));
}
//发货人地址信息循环次数0-1
var eleConsignorAddress = xmlDoc.CreateElement("Address");
eleConsignor.AppendChild(eleConsignorAddress);
//详细地址(街道,邮箱循环次数0-1
var eleConsignorAddressLine = xmlDoc.CreateElement("Line");
eleConsignorAddress.AppendChild(eleConsignorAddressLine);
eleConsignorAddressLine.AppendChild(xmlDoc.CreateTextNode(bill.SHIPPERADDR1));
//国家代码循环次数0-1
var eleConsignorAddressCountryCode = xmlDoc.CreateElement("CountryCode");
eleConsignorAddress.AppendChild(eleConsignorAddressCountryCode);
eleConsignorAddressCountryCode.AppendChild(xmlDoc.CreateTextNode(bill.SHIPPERCOUNTRY));
//发货人通讯方式信息循环次数0-3
var eleConsignorCommunication = xmlDoc.CreateElement("Communication");
eleConsignor.AppendChild(eleConsignorCommunication);
if (!string.IsNullOrWhiteSpace(bill.SHIPPERTEL))
{
var eleConsignorCommunicationID = xmlDoc.CreateElement("ID");
eleConsignorCommunication.AppendChild(eleConsignorCommunicationID);
eleConsignorCommunicationID.AppendChild(xmlDoc.CreateTextNode(bill.SHIPPERTEL));
var eleConsignorCommunicationTypeID = xmlDoc.CreateElement("TypeID");
eleConsignorCommunication.AppendChild(eleConsignorCommunicationTypeID);
eleConsignorCommunicationTypeID.AppendChild(xmlDoc.CreateTextNode("TE"));
}
else
{
var eleConsignorCommunicationID = xmlDoc.CreateElement("ID");
eleConsignorCommunication.AppendChild(eleConsignorCommunicationID);
eleConsignorCommunicationID.AppendChild(xmlDoc.CreateTextNode(bill.SHIPPEREMAIL));
var eleConsignorCommunicationTypeID = xmlDoc.CreateElement("TypeID");
eleConsignorCommunication.AppendChild(eleConsignorCommunicationTypeID);
eleConsignorCommunicationTypeID.AppendChild(xmlDoc.CreateTextNode("EM"));
}
#endregion
#region 通知人
//通知人信息循环次数0-1
var eleNotifyParty = xmlDoc.CreateElement("NotifyParty");
eleConsignment.AppendChild(eleNotifyParty);
//通知人名称循环次数0-1
var strNOTIFYPARTY = MsOpSeaeEdiPortDAL.formatEdiStr("txt", bill.NOTIFYPARTY);
var listNOTIFYPARTY = MsOpSeaeEdiPortDAL.formatlengthStr(strNOTIFYPARTY, 35);
var eleNotifyPartyName = xmlDoc.CreateElement("Name");
eleNotifyParty.AppendChild(eleNotifyPartyName);
if (listNOTIFYPARTY.Count > 0)
{
eleNotifyPartyName.AppendChild(xmlDoc.CreateTextNode(listNOTIFYPARTY[0]));
}
//通知人地址信息循环次数0-1
var eleNotifyPartyAddress = xmlDoc.CreateElement("Address");
eleNotifyParty.AppendChild(eleNotifyPartyAddress);
//详细地址(街道,邮箱循环次数0-1
var eleNotifyPartyAddressLine = xmlDoc.CreateElement("Line");
eleNotifyPartyAddress.AppendChild(eleNotifyPartyAddressLine);
eleNotifyPartyAddressLine.AppendChild(xmlDoc.CreateTextNode(bill.NOTIFYPARTYADDR1));
//国家代码循环次数0-1
var eleNotifyPartyAddressCountryCode = xmlDoc.CreateElement("CountryCode");
eleNotifyPartyAddress.AppendChild(eleNotifyPartyAddressCountryCode);
eleNotifyPartyAddressCountryCode.AppendChild(xmlDoc.CreateTextNode(bill.NOTIFYPARTYCOUNTRY));
//通知人通讯方式信息循环次数0-3
var eleNotifyPartyCommunication = xmlDoc.CreateElement("Communication");
eleNotifyParty.AppendChild(eleNotifyPartyCommunication);
if (!string.IsNullOrWhiteSpace(bill.NOTIFYPARTYTEL))
{
var eleNotifyPartyCommunicationID = xmlDoc.CreateElement("ID");
eleNotifyPartyCommunication.AppendChild(eleNotifyPartyCommunicationID);
eleNotifyPartyCommunicationID.AppendChild(xmlDoc.CreateTextNode(bill.NOTIFYPARTYTEL));
var eleNotifyPartyCommunicationTypeID = xmlDoc.CreateElement("TypeID");
eleNotifyPartyCommunication.AppendChild(eleNotifyPartyCommunicationTypeID);
eleNotifyPartyCommunicationTypeID.AppendChild(xmlDoc.CreateTextNode("TE"));
}
else
{
var eleNotifyPartyCommunicationID = xmlDoc.CreateElement("ID");
eleNotifyPartyCommunication.AppendChild(eleNotifyPartyCommunicationID);
eleNotifyPartyCommunicationID.AppendChild(xmlDoc.CreateTextNode(bill.NOTIFYPARTYEMAIL));
var eleNotifyPartyCommunicationTypeID = xmlDoc.CreateElement("TypeID");
eleNotifyPartyCommunication.AppendChild(eleNotifyPartyCommunicationTypeID);
eleNotifyPartyCommunicationTypeID.AppendChild(xmlDoc.CreateTextNode("EM"));
}
#endregion
//箱信息
var ctnList = edc.EdiCtns.AsNoTracking().Where(x => x.MFNO == bill.MFNO).OrderBy(x => x.CTNCODE).ToList();
foreach (var ctn in ctnList)
{
#region 集装箱_箱号
// 集装箱信息循环次数0-9999
var eleTransportEquipment = xmlDoc.CreateElement("TransportEquipment");
eleConsignment.AppendChild(eleTransportEquipment);
//集装箱编号信息循环次数1)
var eleTransportEquipmentIdentification = xmlDoc.CreateElement("EquipmentIdentification");
eleTransportEquipment.AppendChild(eleTransportEquipmentIdentification);
// 集装箱编号循环次数1
var eleTransportEquipmentIdentificationID = xmlDoc.CreateElement("ID");
eleTransportEquipmentIdentification.AppendChild(eleTransportEquipmentIdentificationID);
eleTransportEquipmentIdentificationID.AppendChild(xmlDoc.CreateTextNode(ctn.CNTRNO));
//集装箱尺寸和类型循环次数1
var codeCtn = codeCtns.FirstOrDefault(xx => xx.CTN == ctn.CTNALL);
var eleTransportEquipmentCharacteristicCode = xmlDoc.CreateElement("CharacteristicCode");
eleTransportEquipment.AppendChild(eleTransportEquipmentCharacteristicCode);
eleTransportEquipmentCharacteristicCode.AppendChild(xmlDoc.CreateTextNode(codeCtn == null ? string.Empty : codeCtn.EDICODE));
//集装箱来源代码循环次数0-1
var eleTransportEquipmentSupplierPartyTypeCode = xmlDoc.CreateElement("SupplierPartyTypeCode");
eleTransportEquipment.AppendChild(eleTransportEquipmentSupplierPartyTypeCode);
eleTransportEquipmentSupplierPartyTypeCode.AppendChild(xmlDoc.CreateTextNode("2"));
//重箱或者空箱标识代码循环次数1
var eleTransportEquipmentFullnessCode = xmlDoc.CreateElement("FullnessCode");
eleTransportEquipment.AppendChild(eleTransportEquipmentFullnessCode);
eleTransportEquipmentFullnessCode.AppendChild(xmlDoc.CreateTextNode(ctn.ZhongKongBiaoShi.ToString()));
//SealID的值是封志类型/号码,AgencyCode的值是施加封志人类型循环次数0-9
var eleTransportEquipmentSealID = xmlDoc.CreateElement("SealID");
eleTransportEquipment.AppendChild(eleTransportEquipmentSealID);
eleTransportEquipmentSealID.AppendChild(xmlDoc.CreateTextNode($"M/{ctn.SEALNO}"));
eleTransportEquipmentSealID.SetAttribute("AgencyCode", "SH");
#endregion
}
#region 集装箱_内容
var ctnIdx = 1;
foreach (var ctn in ctnList)
{
//商品项信息循环次数1-999
var eleConsignmentItem = xmlDoc.CreateElement("ConsignmentItem");
eleConsignment.AppendChild(eleConsignmentItem);
//商品项序号循环次数1
var eleConsignmentItemSequenceNumeric = xmlDoc.CreateElement("SequenceNumeric");
eleConsignmentItem.AppendChild(eleConsignmentItemSequenceNumeric);
eleConsignmentItemSequenceNumeric.AppendChild(xmlDoc.CreateTextNode((ctnIdx++).ToString()));
//商品项包装信息循环次数1
var eleConsignmentItemPackaging = xmlDoc.CreateElement("ConsignmentItemPackaging");
eleConsignmentItem.AppendChild(eleConsignmentItemPackaging);
//商品项货物件数循环次数1
var eleConsignmentItemPackagingQuantity = xmlDoc.CreateElement("QuantityQuantity");
eleConsignmentItemPackaging.AppendChild(eleConsignmentItemPackagingQuantity);
eleConsignmentItemPackagingQuantity.AppendChild(xmlDoc.CreateTextNode(ctn.PKGS.ToString()));
//包装种类代码循环次数0-1
var codePkgEdi = codePkgEdis.FirstOrDefault(x => x.PKGS == bill.KINDPKGS && x.EDINAME == "HGYDT");
var eleConsignmentItemPackagingTypeCode = xmlDoc.CreateElement("TypeCode");
eleConsignmentItemPackaging.AppendChild(eleConsignmentItemPackagingTypeCode);
eleConsignmentItemPackagingTypeCode.AppendChild(xmlDoc.CreateTextNode(codePkgEdi == null ? (codePkg == null ? String.Empty : codePkg.EDICODE) : codePkgEdi.EDICODE));
//唛头循环次数0-1
var eleConsignmentItemPackagingMarksNumbers = xmlDoc.CreateElement("MarksNumbers");
eleConsignmentItemPackaging.AppendChild(eleConsignmentItemPackagingMarksNumbers);
eleConsignmentItemPackagingMarksNumbers.AppendChild(xmlDoc.CreateTextNode(bill.MARKS.Replace("\r\n", " ").Replace("\n", " ")));
//商品项简要描述循环次数1
var eleConsignmentItemCommodity = xmlDoc.CreateElement("Commodity");
eleConsignmentItem.AppendChild(eleConsignmentItemCommodity);
//商品项简要描述循环次数1
var eleConsignmentItemCommodityCargoDescription = xmlDoc.CreateElement("CargoDescription");
eleConsignmentItemCommodity.AppendChild(eleConsignmentItemCommodityCargoDescription);
eleConsignmentItemCommodityCargoDescription.AppendChild(xmlDoc.CreateTextNode(bill.DESCRIPTION.Replace("\r\n", " ").Replace("\n", " ")));
//危险品编号循环次数0-1
if (bill.CARGOID.Trim() == "D")
{
var eleConsignmentItemCommodityUNDGCode = xmlDoc.CreateElement("UNDGCode");
eleConsignmentItemCommodity.AppendChild(eleConsignmentItemCommodityUNDGCode);
eleConsignmentItemCommodityUNDGCode.AppendChild(xmlDoc.CreateTextNode(bill.DUNNO));
}
//商品项货物毛重循环次数1
var eleConsignmentItemGoodsMeasure = xmlDoc.CreateElement("GoodsMeasure");
eleConsignmentItem.AppendChild(eleConsignmentItemGoodsMeasure);
//商品项货物毛重循环次数1
var eleConsignmentItemGrossMassMeasure = xmlDoc.CreateElement("GrossMassMeasure");
eleConsignmentItemGoodsMeasure.AppendChild(eleConsignmentItemGrossMassMeasure);
eleConsignmentItemGrossMassMeasure.AppendChild(xmlDoc.CreateTextNode(ctn.KGS.ToString()));
#region 集装箱_封号
//集装箱编号信息循环次数0-9999
var eleConsignmentItemEquipmentIdentification = xmlDoc.CreateElement("EquipmentIdentification");
eleConsignmentItem.AppendChild(eleConsignmentItemEquipmentIdentification);
// 集装箱编号循环次数1
var eleConsignmentItemEquipmentIdentificationID = xmlDoc.CreateElement("ID");
eleConsignmentItemEquipmentIdentification.AppendChild(eleConsignmentItemEquipmentIdentificationID);
eleConsignmentItemEquipmentIdentificationID.AppendChild(xmlDoc.CreateTextNode(ctn.CNTRNO));
#endregion
}
#endregion
}
#endregion
#region 备注
//备注
var eleAdditionalInformation = xmlDoc.CreateElement("AdditionalInformation");
eleDeclaration.AppendChild(eleAdditionalInformation);
//变更原因描述
var eleAdditionalInformationReason = xmlDoc.CreateElement("Reason");
eleAdditionalInformation.AppendChild(eleAdditionalInformationReason);
eleAdditionalInformationReason.AppendChild(xmlDoc.CreateTextNode(reason));
//变更申请联系人姓名
var eleAdditionalInformationContactName = xmlDoc.CreateElement("ContactName");
eleAdditionalInformation.AppendChild(eleAdditionalInformationContactName);
eleAdditionalInformationContactName.AppendChild(xmlDoc.CreateTextNode(contactName));
//变更申请联系人电话
var eleAdditionalInformationContactTel = xmlDoc.CreateElement("ContactTel");
eleAdditionalInformation.AppendChild(eleAdditionalInformationContactTel);
eleAdditionalInformationContactTel.AppendChild(xmlDoc.CreateTextNode(contactTel));
//备注
var eleAdditionalInformationContent = xmlDoc.CreateElement("Content");
eleAdditionalInformation.AppendChild(eleAdditionalInformationContent);
eleAdditionalInformationContent.AppendChild(xmlDoc.CreateTextNode(reason));
#endregion
eleManifest.SetAttribute("xmlns", "urn:Declaration:datamodel:standard:CN:MT2101:1");
if (Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
var fileName = $"{mblno}_edi_modify_{DateTime.Now.Ticks}.xml";
var fullPath = Path.Combine(filePath, fileName);
xmlDoc.Save(fullPath);
rtnMsg = fileName;
return true;
}
/// <summary>
/// 压缩edi文件到zip文件
/// </summary>
/// <param name="srcFile">带压缩的edi文件</param>
/// <param name="rtn">成功时返回压缩文件的文件名称(不带路径),失败时返回错误信息</param>
/// <returns>是否成功</returns>
public static bool ZipEdiFile(string srcFile, out string rtn)
{
var dir = Path.GetDirectoryName(srcFile);
var fileName = Path.GetFileName(srcFile);
var fileNameNoExt = Path.GetFileNameWithoutExtension(srcFile);
var zipName = $"{fileNameNoExt}.zip";
var zipFilePath = Path.Combine(dir, Path.Combine(dir, zipName));
try
{
using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))
{
s.SetLevel(9);
ZipEntry entry = new ZipEntry(fileName);
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
var buffer = File.ReadAllBytes(srcFile);
s.Write(buffer, 0, buffer.Length);
s.Finish();
s.Close();
rtn = zipName;
return true;
}
}
catch (Exception ex)
{
rtn = ex.Message;
log.Error(ex.Message);
log.Error(ex.StackTrace);
return false;
}
}
/// <summary>
/// 根据edi数据生成tips对象
/// </summary>
/// <param name="edi"></param>
/// <param name="showname"></param>
/// <returns></returns>
public static OP_SEAE_EDI_TIPS_md CreateEdiTips(OpSeaeEdi edi, string showname, string tip, string optype)
{
var tips = new OP_SEAE_EDI_TIPS_md();
tips.GID = Guid.NewGuid().ToString();
tips.MFNO = edi.MFNO;
tips.TIPSTIME = DateTime.Now;
tips.TIPS = tip;
tips.CREATETIME = DateTime.Now;
tips.MBLNO = edi.MBLNO;
tips.HBLNO = edi.HBLNO;
tips.FORWARDER = edi.FORWARD;
tips.OPERATOR = showname;
tips.OPTYPE = optype;
return tips;
}
/// <summary>
/// 发送直发EDI邮件给东胜客服邮箱
/// </summary>
/// <param name="mblno">主提单号</param>
/// <param name="hblno">分提单号</param>
/// <param name="forwarder">船代</param>
/// <param name="comname">公司名称</param>
/// <param name="showname">申请人</param>
/// <param name="email">邮箱</param>
/// <param name="tel">电话</param>
/// <param name="type">类型</param>
/// <param name="remark">备注</param>
/// <returns></returns>
public static bool SendDirectEdiMailDS(string mblno, string hblno, string forwarder, string comname, string showname, string email, string tel, string type, string remark, params AttachFileModel[] attFiles)
{
EdiMailSubjectAndBody(mblno, hblno, forwarder, comname, showname, email, tel, type, remark, out string subject, out string body);
return MailHelper.SendMailCommon(subject, body, "dongshengsoft@dongshengsoft.com", attFiles);
}
/// <summary>
/// 发送DS邮件的标题、内容
/// </summary>
/// <param name="mblno"></param>
/// <param name="hblno"></param>
/// <param name="forwarder"></param>
/// <param name="comname"></param>
/// <param name="showname"></param>
/// <param name="email"></param>
/// <param name="tel"></param>
/// <param name="type"></param>
/// <param name="remark"></param>
/// <param name="subject"></param>
/// <param name="body"></param>
private static void EdiMailSubjectAndBody(string mblno, string hblno, string forwarder, string comname, string showname, string email, string tel, string type, string remark, out string subject, out string body, bool html = true)
{
subject = $"舱单发送- 主提单号:[船代直发]{mblno} 分提单号:{hblno} {forwarder}";
var listBody = new List<string>();
listBody.Add($"公司名称:{comname}");
listBody.Add($"申请人:{showname}");
listBody.Add($"邮箱:{email}");
listBody.Add($"电话:{tel}");
listBody.Add($"类型:{type}");
listBody.Add($"备注:{remark}");
listBody.Add($"主提单号:{mblno}");
listBody.Add($"分提单号:{hblno}");
if (html)
{
body = string.Join("<br/>", listBody);
}
else
{
body = string.Join("\r\n", listBody);
}
}
/// <summary>
/// 发送edi文件到ftp爬虫
/// </summary>
/// <param name="file">文件路径</param>
/// <param name="userid">用户ID</param>
/// <param name="rtn"></param>
/// <returns></returns>
public static bool SendEdiFileToFtpSpider(string file, string userid, out string rtn)
{
CommonDataContext commonData = new CommonDataContext();
var paraEdiDirectFtpSpider = commonData.ParamSets.AsNoTracking().First(x => x.PARAMNAME == "paraEdiDirectFtpSpider");
var url = paraEdiDirectFtpSpider.PARAMVALUE;
log.Debug($"发送EDI报文给ftp爬虫{url} {file}");
MemoryStream ms = new MemoryStream(File.ReadAllBytes(file));
var listPara = new List<FormItemModel>();
listPara.Add(new FormItemModel() { Key = "yardid", Value = "WLXX" });
listPara.Add(new FormItemModel() { Key = "custname", Value = "DJYCD" });
listPara.Add(new FormItemModel() { Key = "custpsw", Value = "YGH2021" });
listPara.Add(new FormItemModel() { Key = "webusername", Value = "djy" });
listPara.Add(new FormItemModel() { Key = "webpassword", Value = "djy" });
//判断是否使用客户通道
if (!string.IsNullOrEmpty(userid))
{
var usr = commonData.Users.First(x => x.GID == userid);
if (!string.IsNullOrEmpty(usr.CompId))
{
var cc = commonData.ParamValues.Count(x => x.CompId == usr.CompId && x.ParaCode == "CANGDAN_SEND_PASSAGEWAY" && x.ItemCode == "CUSTOMER");
if (cc > 0)
{
listPara.Add(new FormItemModel() { Key = "compid", Value = usr.CompId });
listPara.Add(new FormItemModel() { Key = "comname", Value = usr.COMNAME });
}
}
}
listPara.Add(new FormItemModel() { Key = "file", Value = "" });
listPara.Add(new FormItemModel() { Key = "file", FileName = Path.GetFileName(file), FileContent = ms });
var postRtn = WebRequestHelper.PostForm(url, listPara);
log.Debug($"发送EDI报文给ftp爬虫返回{url} {postRtn}");
ms.Dispose();
var jobjRtn = Newtonsoft.Json.JsonConvert.DeserializeAnonymousType(postRtn, new { status = "", message = "" });
if (jobjRtn.status == "1")
{
rtn = "发送成功";
return true;
}
else
{
rtn = $"FTP发送失败{jobjRtn.message}";
return false;
}
}
/// <summary>
/// 校验舱单扣费
/// </summary>
/// <param name="comname">公司名称</param>
/// <param name="sendtype">发送类型</param>
/// <param name="msg">返回消息</param>
/// <param name="num">扣费数量默认为1</param>
/// <returns>是否可扣费</returns>
public static bool CheckEdiFee(string comname, int sendtype, out string msg, out decimal price, int num = 1, bool isFen = true)
{
CompanyDataContext companyData = new CompanyDataContext();
var ban = companyData.Cust_Balance.AsNoTracking().FirstOrDefault(x => x.COMNAME == comname);
if (ban == null)
{
msg = $"未找到账号钱包:{comname}";
price = 0;
return false;
}
var priceObj = companyData.Cust_Price.AsNoTracking().FirstOrDefault(x => x.COMNAME == comname && x.BSTYPE == 0 && x.SENDTYPE == sendtype);
if (priceObj == null)
{
msg = $"未找到单价设置:{comname}";
price = 0;
return false;
}
decimal pri = 0;
if (isFen)
{
pri = priceObj.PRICEF;
}
else
{
pri = priceObj.PRICE.Value;
}
if (ban.Balance < num * pri)
{
msg = $"余额不足:{comname}";
price = 0;
return false;
}
msg = $"扣费校验成功";
price = pri;
return true;
}
/// <summary>
/// 读取主单回执
/// </summary>
/// <param name="userid">用户ID</param>
/// <param name="mfno">主单ID</param>
/// <param name="msg"></param>
/// <returns></returns>
public static bool ReadResponseZD(string userid, string mfno, out string msg, out ZDXXModel hz, out List<ZDXXDetailModel> hzCtn)
{
EdiDataContext edc = new EdiDataContext();
var edi = edc.Edis.AsNoTracking().First(x => x.MFNO == mfno);
//读取主单回执,使用最新的出口运踪接口的实时查询
var pServerUrl = edc.sys_param_set.AsNoTracking().First(x => x.PARAMNAME == "billTraceNewServerUrl");
var pUserKey = edc.sys_param_set.AsNoTracking().First(p => p.PARAMNAME == "billTraceNewUserKey");
var pUserSecret = edc.sys_param_set.AsNoTracking().First(p => p.PARAMNAME == "billTraceNewUserSecret");
var serverUrl = pServerUrl.PARAMVALUE.Trim();
if (!serverUrl.EndsWith("/"))
{
serverUrl += "/";
}
serverUrl += "real/query";
JObject jobjSend = new JObject();
jobjSend.Add("user_key", new JValue(pUserKey.PARAMVALUE));
jobjSend.Add("user_secret", new JValue(pUserSecret.PARAMVALUE));
jobjSend.Add("customer_id", new JValue(userid));
jobjSend.Add("customer_name", new JValue("djy"));
jobjSend.Add("web_code", new JValue("HLW_ZH"));
jobjSend.Add("wl_type", string.Empty);
jobjSend.Add("bno", new JValue(edi.MBLNO));
jobjSend.Add("req_type", new JValue("0"));
jobjSend.Add("web_user", new JValue(string.Empty));
jobjSend.Add("web_psw", new JValue(string.Empty));
try
{
var strJson = jobjSend.ToString();
log.Debug($"查询主单回执发送数据url{serverUrl}json{strJson}");
string rtn = WebRequestHelper.DoPost(serverUrl, strJson, timeout: 20000);
log.Debug($"查询主单回执数据,返回:{rtn}");
var jobjRtn = JObject.Parse(rtn);
if (jobjRtn.GetJsonIntValue("code") != 200)
{
msg = jobjRtn.GetJsonStringValue("msg");
hz = null;
hzCtn = null;
return false;
}
else
{
//删除后重新插入
edc.op_seae_edi_mainstatus.RemoveRange(edc.op_seae_edi_mainstatus.Where(x => x.mfno == mfno));
edc.op_seae_edi_mainstatus_xhinfo.RemoveRange(edc.op_seae_edi_mainstatus_xhinfo.Where(x => x.mfno == mfno));
var jobjData = jobjRtn.GetJsonObjectValue("data");
var BillNo = jobjData.GetJsonStringValue("BillNo");
var IMO = jobjData.GetJsonStringValue("IMO");
var voyage_number = jobjData.GetJsonStringValue("voyage_number");
var JianShu = jobjData.GetJsonIntValue("JianShu");
var ZhongLiang = jobjData.GetJsonDecimalValue("ZhongLiang");
var customs_code = jobjData.GetJsonStringValue("customs_code");
var means_transport = jobjData.GetJsonStringValue("means_transport");
var delete_flag = jobjData.GetJsonStringValue("delete_flag");
hz = new ZDXXModel()
{
gid = Guid.NewGuid(),
mfno = mfno,
mblno = BillNo,
voyage_number = voyage_number,
means_transport = means_transport,
customs_code = customs_code,
IMO = IMO,
PKGS = JianShu,
KGS = ZhongLiang,
DELMARK = delete_flag,
};
edc.op_seae_edi_mainstatus.Add(hz);
var jarrCtn = jobjData.GetJsonArrayValue("xh_info");
hzCtn = new List<ZDXXDetailModel>();
foreach (JObject item in jarrCtn)
{
var Identification = item.GetJsonStringValue("Identification");
var QianFengHao = item.GetJsonStringValue("QianFengHao");
var ShiFengRen = item.GetJsonStringValue("ShiFengRen");
var xh = item.GetJsonStringValue("xh");
var xx = item.GetJsonStringValue("xx");
var hzDetail = new ZDXXDetailModel()
{
gid = Guid.NewGuid(),
mfno = mfno,
mblno = BillNo,
xh = xh,
xx = xx,
Identification = Identification,
ShiFengRen = ShiFengRen,
SEALNO = QianFengHao
};
edc.op_seae_edi_mainstatus_xhinfo.Add(hzDetail);
hzCtn.Add(hzDetail);
}
edc.SaveChanges();
log.Debug($"主单回执入库完成:{BillNo}");
msg = "读取成功";
return true;
}
}
catch (Exception ex)
{
log.Error(ex.Message);
log.Error(ex.StackTrace);
msg = $"读取失败:{ex.Message}";
hz = null;
hzCtn = null;
return false;
}
}
/// <summary>
/// 检验数据,逻辑调用原有逻辑,可能会有问题
/// </summary>
/// <param name="bsnos"></param>
/// <param name="type"></param>
/// <param name="userid"></param>
/// <param name="msg"></param>
/// <returns></returns>
public static bool CheckSendData(string bsnos, string type, string userid, out string msg)
{
EdiDataContext edc = new EdiDataContext();
var error = CheckData.CheckCanSave(bsnos, false, type, checkCtnNum: false);
error += CheckData.CheckCanSend(bsnos, type, userid);
error += CheckData.CheckSum(bsnos);
//2022-8-26增加所有箱子件数、重量为0或负数的校验
var arrBSNO = bsnos.Replace("'", "").Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
var zeroCount = edc.EdiCtns.Where(x => arrBSNO.Contains(x.MFNO) && (x.PKGS <= 0 || x.KGS <= 0 || x.PKGS == null || x.KGS == null)).Count();
if (zeroCount > 0)
{
error += "每个箱子的件数和重量都不能为0或负数请检查";
}
if (error.Length > 0)
{
msg = error;
return false;
}
msg = string.Empty;
return true;
}
/// <summary>
/// 舱单直发修改报文
/// </summary>
/// <param name="faSongFang"></param>
/// <param name="mblno"></param>
/// <param name="listFedDan"></param>
/// <param name="shipimo"></param>
/// <param name="voyno"></param>
/// <param name="reason"></param>
/// <param name="contactName"></param>
/// <param name="contactTel"></param>
/// <param name="filePath"></param>
/// <param name="rtnMsg"></param>
/// <returns></returns>
public static bool GenDirectEdiFileFenModify(string faSongFang, string mblno, List<OpSeaeEdi> listFedDan, string shipimo, string voyno, string reason, string contactName, string contactTel, string filePath, out string rtnMsg)
{
log.Debug($"生成舱单直发修改EDI报文{mblno} {string.Join(",", listFedDan.Select(x => x.HBLNO))} {shipimo} {voyno} {reason}");
EdiDataContext edc = new EdiDataContext();
var xmlDoc = new XmlDocument();
var xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
xmlDoc.AppendChild(xmlDec);
var eleManifest = xmlDoc.CreateElement("Manifest");
xmlDoc.AppendChild(eleManifest);
#region Head
//报文头
var eleHead = xmlDoc.CreateElement("Head");
eleManifest.AppendChild(eleHead);
//报文编号循环次数1
var eleMessageID = xmlDoc.CreateElement("MessageID");
eleMessageID.AppendChild(xmlDoc.CreateTextNode($"{mblno}{DateTime.Now.ToString("yyyyMMddHHmmss")}"));
eleHead.AppendChild(eleMessageID);
//报文功能代码循环次数1
var eleFunctionCode = xmlDoc.CreateElement("FunctionCode");
eleFunctionCode.AppendChild(xmlDoc.CreateTextNode("5"));
eleHead.AppendChild(eleFunctionCode);
//报文类型代码循环次数1
var eleMessageType = xmlDoc.CreateElement("MessageType");
eleMessageType.AppendChild(xmlDoc.CreateTextNode("MT2101"));
eleHead.AppendChild(eleMessageType);
//发送方代码循环次数1
var eleSenderID = xmlDoc.CreateElement("SenderID");
eleSenderID.AppendChild(xmlDoc.CreateTextNode(faSongFang));
eleHead.AppendChild(eleSenderID);
//接受方代码循环次数1
var eleReceiverID = xmlDoc.CreateElement("ReceiverID");
eleReceiverID.AppendChild(xmlDoc.CreateTextNode("4200"));
eleHead.AppendChild(eleReceiverID);
//发送时间循环次数1
var eleSendTime = xmlDoc.CreateElement("SendTime");
eleSendTime.AppendChild(xmlDoc.CreateTextNode(DateTime.Now.ToString("yyyyMMddHHmmssfff")));
eleHead.AppendChild(eleSendTime);
//报文版本号循环次数1
var eleVersion = xmlDoc.CreateElement("Version");
eleVersion.AppendChild(xmlDoc.CreateTextNode("1.0"));
eleHead.AppendChild(eleVersion);
#endregion
//报文体
var eleDeclaration = xmlDoc.CreateElement("Declaration");
eleManifest.AppendChild(eleDeclaration);
#region 承运人
//舱单传输人数据段循环次数1
var eleRepresentativePerson = xmlDoc.CreateElement("RepresentativePerson");
eleDeclaration.AppendChild(eleRepresentativePerson);
//256 Representative person name 舱单传输人名称循环次数1
var eleRepresentativePersonName = xmlDoc.CreateElement("Name");
eleRepresentativePersonName.AppendChild(xmlDoc.CreateTextNode(faSongFang));
eleRepresentativePerson.AppendChild(eleRepresentativePersonName);
//运输工具数据段循环次数1
var eleBorderTransportMeans = xmlDoc.CreateElement("BorderTransportMeans");
eleDeclaration.AppendChild(eleBorderTransportMeans);
//航次航班编号循环次数1
var eleBorderTransportMeansJourneyID = xmlDoc.CreateElement("JourneyID");
eleBorderTransportMeans.AppendChild(eleBorderTransportMeansJourneyID);
eleBorderTransportMeansJourneyID.AppendChild(xmlDoc.CreateTextNode(voyno));
//运输工具代码循环次数0-1
var eleBorderTransportMeansID = xmlDoc.CreateElement("ID");//船舶IMO
eleBorderTransportMeans.AppendChild(eleBorderTransportMeansID);
eleBorderTransportMeansID.AppendChild(xmlDoc.CreateTextNode(shipimo));
#endregion
#region 提(运)单数据段
foreach (var fd in listFedDan)
{
var eleConsignment = xmlDoc.CreateElement("Consignment");
eleDeclaration.AppendChild(eleConsignment);
//运输合同信息
var eleTransportContractDocument = xmlDoc.CreateElement("TransportContractDocument");
eleConsignment.AppendChild(eleTransportContractDocument);
//总提(运)单号
var eleTransportContractDocumentID = xmlDoc.CreateElement("ID");
eleTransportContractDocument.AppendChild(eleTransportContractDocumentID);
eleTransportContractDocumentID.AppendChild(xmlDoc.CreateTextNode(mblno));
//更改原因代码
var eleTransportContractDocumentChangeReasonCode = xmlDoc.CreateElement("ChangeReasonCode");//
eleTransportContractDocument.AppendChild(eleTransportContractDocumentChangeReasonCode);
eleTransportContractDocumentChangeReasonCode.AppendChild(xmlDoc.CreateTextNode("001"));
//货物海关状态代码
var eleCustomsStatusCode = xmlDoc.CreateElement("CustomsStatusCode");
eleCustomsStatusCode.AppendChild(xmlDoc.CreateTextNode("001"));
//运输合同附加信息
var eleAssociatedTransportDocument = xmlDoc.CreateElement("AssociatedTransportDocument");
eleConsignment.AppendChild(eleAssociatedTransportDocument);
//分提(运)单号
var eleAssociatedTransportDocumentID = xmlDoc.CreateElement("ID");
eleAssociatedTransportDocument.AppendChild(eleAssociatedTransportDocumentID);
eleAssociatedTransportDocumentID.AppendChild(xmlDoc.CreateTextNode(fd.HBLNO));
#region 收货人
//收货人信息循环次数0-1
var eleConsignee = xmlDoc.CreateElement("Consignee");
eleConsignment.AppendChild(eleConsignee);
//通知人通讯方式信息循环次数0-3
var eleConsigneeCommunication = xmlDoc.CreateElement("Communication");
eleConsignee.AppendChild(eleConsigneeCommunication);
var eleConsigneeCommunicationID = xmlDoc.CreateElement("ID");
eleConsigneeCommunication.AppendChild(eleConsigneeCommunicationID);
eleConsigneeCommunicationID.AppendChild(xmlDoc.CreateTextNode(fd.CONSIGNEETEL));
var eleConsigneeCommunicationTypeID = xmlDoc.CreateElement("TypeID");
eleConsigneeCommunication.AppendChild(eleConsigneeCommunicationTypeID);
eleConsigneeCommunicationTypeID.AppendChild(xmlDoc.CreateTextNode("TE"));
#endregion
#region 发货人
//收货人信息循环次数0-1
var eleShipper = xmlDoc.CreateElement("Consignor");
eleConsignment.AppendChild(eleShipper);
//通知人通讯方式信息循环次数0-3
var eleShipperCommunication = xmlDoc.CreateElement("Communication");
eleShipper.AppendChild(eleShipperCommunication);
var eleShipperCommunicationID = xmlDoc.CreateElement("ID");
eleShipperCommunication.AppendChild(eleShipperCommunicationID);
eleShipperCommunicationID.AppendChild(xmlDoc.CreateTextNode(fd.SHIPPERTEL));
var eleShipperCommunicationTypeID = xmlDoc.CreateElement("TypeID");
eleShipperCommunication.AppendChild(eleShipperCommunicationTypeID);
eleShipperCommunicationTypeID.AppendChild(xmlDoc.CreateTextNode("TE"));
#endregion
#region 通知人
//收货人信息循环次数0-1
var eleNotifyParty = xmlDoc.CreateElement("NotifyParty");
eleConsignment.AppendChild(eleNotifyParty);
//通知人通讯方式信息循环次数0-3
var eleNotifyPartyCommunication = xmlDoc.CreateElement("Communication");
eleNotifyParty.AppendChild(eleNotifyPartyCommunication);
var eleNotifyPartyCommunicationID = xmlDoc.CreateElement("ID");
eleNotifyPartyCommunication.AppendChild(eleNotifyPartyCommunicationID);
eleNotifyPartyCommunicationID.AppendChild(xmlDoc.CreateTextNode(fd.NOTIFYPARTYTEL));
var eleNotifyPartyCommunicationTypeID = xmlDoc.CreateElement("TypeID");
eleNotifyPartyCommunication.AppendChild(eleNotifyPartyCommunicationTypeID);
eleNotifyPartyCommunicationTypeID.AppendChild(xmlDoc.CreateTextNode("TE"));
#endregion
var codeCtns = edc.Code_Ctn.AsNoTracking().ToList();
//箱信息
var ctnList = edc.EdiCtns.AsNoTracking().Where(x => x.MFNO == fd.MFNO).OrderBy(x => x.CTNCODE).ToList();
foreach (var ctn in ctnList)
{
#region 集装箱_箱号
// 集装箱信息循环次数0-9999
var eleTransportEquipment = xmlDoc.CreateElement("TransportEquipment");
eleConsignment.AppendChild(eleTransportEquipment);
//集装箱编号信息循环次数1)
var eleTransportEquipmentIdentification = xmlDoc.CreateElement("EquipmentIdentification");
eleTransportEquipment.AppendChild(eleTransportEquipmentIdentification);
// 集装箱编号循环次数1
var eleTransportEquipmentIdentificationID = xmlDoc.CreateElement("ID");
eleTransportEquipmentIdentification.AppendChild(eleTransportEquipmentIdentificationID);
eleTransportEquipmentIdentificationID.AppendChild(xmlDoc.CreateTextNode(ctn.CNTRNO));
//SealID的值是封志类型/号码,AgencyCode的值是施加封志人类型循环次数0-9
var eleTransportEquipmentSealID = xmlDoc.CreateElement("SealID");
eleTransportEquipment.AppendChild(eleTransportEquipmentSealID);
eleTransportEquipmentSealID.AppendChild(xmlDoc.CreateTextNode($"M/{ctn.SEALNO}"));
eleTransportEquipmentSealID.SetAttribute("AgencyCode", ctn.ShiFengRen);
#endregion
}
var ctnIdx = 1;
foreach (var ctn in ctnList)
{
//商品项信息循环次数1-999
var eleConsignmentItem = xmlDoc.CreateElement("ConsignmentItem");
eleConsignment.AppendChild(eleConsignmentItem);
//商品项序号循环次数1
var eleConsignmentItemSequenceNumeric = xmlDoc.CreateElement("SequenceNumeric");
eleConsignmentItem.AppendChild(eleConsignmentItemSequenceNumeric);
eleConsignmentItemSequenceNumeric.AppendChild(xmlDoc.CreateTextNode((ctnIdx++).ToString()));
//商品项包装信息循环次数1
var eleConsignmentItemPackaging = xmlDoc.CreateElement("ConsignmentItemPackaging");
eleConsignmentItem.AppendChild(eleConsignmentItemPackaging);
//唛头循环次数0-1
var eleConsignmentItemPackagingMarksNumbers = xmlDoc.CreateElement("MarksNumbers");
eleConsignmentItemPackaging.AppendChild(eleConsignmentItemPackagingMarksNumbers);
eleConsignmentItemPackagingMarksNumbers.AppendChild(xmlDoc.CreateTextNode(fd.MARKS.Replace("\r\n", " ").Replace("\n", " ")));
}
}
#endregion
#region 备注
//备注
var eleAdditionalInformation = xmlDoc.CreateElement("AdditionalInformation");
eleDeclaration.AppendChild(eleAdditionalInformation);
//变更原因描述
var eleAdditionalInformationReason = xmlDoc.CreateElement("Reason");
eleAdditionalInformation.AppendChild(eleAdditionalInformationReason);
eleAdditionalInformationReason.AppendChild(xmlDoc.CreateTextNode(reason));
//变更申请联系人姓名
var eleAdditionalInformationContactName = xmlDoc.CreateElement("ContactName");
eleAdditionalInformation.AppendChild(eleAdditionalInformationContactName);
eleAdditionalInformationContactName.AppendChild(xmlDoc.CreateTextNode(contactName));
//变更申请联系人电话
var eleAdditionalInformationContactTel = xmlDoc.CreateElement("ContactTel");
eleAdditionalInformation.AppendChild(eleAdditionalInformationContactTel);
eleAdditionalInformationContactTel.AppendChild(xmlDoc.CreateTextNode(contactTel));
//备注
var eleAdditionalInformationContent = xmlDoc.CreateElement("Content");
eleAdditionalInformation.AppendChild(eleAdditionalInformationContent);
eleAdditionalInformationContent.AppendChild(xmlDoc.CreateTextNode(reason));
#endregion
eleManifest.SetAttribute("xmlns", "urn:Declaration:datamodel:standard:CN:MT2101:1");
if (Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
var fileName = $"{mblno}_{shipimo}_{voyno}_edi_modify_{DateTime.Now.Ticks}.txt";
var fullPath = Path.Combine(filePath, fileName);
xmlDoc.Save(fullPath);
rtnMsg = fileName;
return true;
}
/// <summary>
/// 舱单作废
/// </summary>
/// <param name="mfnoList"></param>
/// <returns></returns>
public static bool CancelEdi(List<string> mfnoList, string userShowName, out string msg)
{
EdiDataContext edc = new EdiDataContext();
var ediList = edc.Edis.Where(x => mfnoList.Contains(x.MFNO)).ToList();
//没找到作废数据
if (ediList.Count == 0)
{
msg = "未找到任何数据";
return false;
}
//验证直发状态
var cNoZF = ediList.Count(x => x.EDISTATUS != "已直发");
if (cNoZF > 0)
{
msg = "不满足作废条件";
return false;
}
//验证单独作废主单
var zdList = ediList.Where(x => string.IsNullOrEmpty(x.HBLNO)).ToList();
foreach (var zd in zdList)
{
var cDB = edc.Edis.Count(x => x.BSNO == zd.MFNO);
var cList = ediList.Count(x => x.BSNO == zd.MFNO);
if (cDB > cList) //数据库中分单数量比选择的分单数量多
{
msg = "请先作废或删除分单,再作废主单";
return false;
}
}
foreach (var item in ediList)
{
if (!string.IsNullOrEmpty(item.HBLNO)) //分单
{
item.BSNO += "(作废)";
item.HBLNO += "(作废)";
}
item.MBLNO += "(作废)";
item.EDISTATUS = "已作废";
//添加tips
var tips = EdiHelper.CreateEdiTips(item, userShowName, "[已作废]", "已作废");
edc.OP_SEAE_EDI_TIPS.Add(tips);
}
edc.SaveChanges();
msg = "作废成功";
return true;
}
/// <summary>
/// 上传舱单报文文件到东胜ftp
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public static bool CangDanDSFtp(string mblno, string hblno, string forwarder, string comname, string showname, string email, string tel, string type, string remark, string file)
{
CommonDataContext commonData = new CommonDataContext();
var pFtpUrl = commonData.ParamSets.AsNoTracking().First(x => x.PARAMNAME == "DSFTP_EDI_FTPADDRESS");
var pFtpAcc = commonData.ParamSets.AsNoTracking().First(x => x.PARAMNAME == "DSFTP_EDI_FTPUSERNAME");
var pFtpPwd = commonData.ParamSets.AsNoTracking().First(x => x.PARAMNAME == "DSFTP_EDI_FTPPASSWORD");
var pFtpFolder = commonData.ParamSets.AsNoTracking().First(x => x.PARAMNAME == "DSFTP_EDI_FTP_EDIPATH");
//打包eml文件
EdiMailSubjectAndBody(mblno, hblno, forwarder, comname, showname, email, tel, type, remark, out string subject, out string body, html: false);
MimeMessage mailMsg = new MimeMessage();
mailMsg.Subject = subject;
var htmlBody = new TextPart("plain")
{
Text = body
};
var mult = new Multipart("mixed") { htmlBody };
var attPart = new MimePart();
attPart.Content = new MimeContent(new MemoryStream(File.ReadAllBytes(file)));
attPart.ContentDisposition = new ContentDisposition(ContentDisposition.Attachment);
attPart.ContentTransferEncoding = ContentEncoding.Base64;
attPart.FileName = "=?UTF-8?B?" + Convert.ToBase64String(Encoding.UTF8.GetBytes(Path.GetFileName(file))) + "?=";
mult.Add(attPart);
mailMsg.Body = mult;
var tempDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TempFiles");
if (!Directory.Exists(tempDir))
{
Directory.CreateDirectory(tempDir);
}
var emlPath = Path.Combine(tempDir, $"舱单发送_ 主提单号_{mblno} 分提单号:{hblno} {forwarder} {DateTime.Now.Ticks}.eml");
mailMsg.WriteTo(emlPath);
//发送ftp
BackgroundTaskFtp ftpTask = new BackgroundTaskFtp();
ftpTask.GID = Guid.NewGuid().ToString();
ftpTask.FtpHost = pFtpUrl.PARAMVALUE;
ftpTask.FtpUser = pFtpAcc.PARAMVALUE;
ftpTask.FtpPassword = pFtpPwd.PARAMVALUE;
ftpTask.FtpData = FtpTaskHelper.GenTaskJson(pFtpFolder.PARAMVALUE, emlPath, null);
ftpTask.Type = BackgroundTaskFtp.TypeFtpUpload;
commonData.BackgroundTaskFtp.Add(ftpTask);
commonData.SaveChanges();
return true;
}
}
public class CangDanDirectRtn
{
/// <summary>
/// 是否已处理
/// </summary>
public bool Processed { get; set; }
//处理成功与否
public bool Success { get; set; }
//提示信息
public string Message { get; set; }
}