using DSWeb.Areas.MvcShipping.DAL.Message.vgm; using DSWeb.Common.DB; using DSWeb.Common.Model; using DSWeb.MvcShipping.DAL.MsBaseInfoDAL; using DSWeb.MvcShipping.DAL.MsOpSeaeEdiVGM; using DSWeb.MvcShipping.Helper; using DSWeb.MvcShipping.Models.MsOpSeaeEdiVGM; using HcUtility.Comm; using HcUtility.Core; using log4net; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Web; namespace DSWeb.Interface { /// /// CustVGM 的摘要说明 /// public class CustVGM : IHttpHandler { private const int DURATION = 1; private const int TIMES = 10; private ILog log = LogManager.GetLogger("CustVGM"); public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string rst = "1"; if (!IsValid()) { rst = "操作太频繁!"; } else { if (context.Request["ac"] == "vgm") { if (ConfigurationManager.AppSettings["CustVgmSendUseNew"] == "1") { rst = SendCustVgmNew(context); } else { rst = SendCustVGM(context); } } else if (context.Request["ac"] == "vgmstatus") { rst = GetVGMSTATUS(context); } else { rst = "参数有误"; } } context.Response.Write(rst); } public bool IsReusable { get { return false; } } public string SendCustVGM(HttpContext context) { string uid = context.Request["uid"]; string skey = context.Request["skey"]; string optype = context.Request["optype"]; string mdata = context.Request["mdata"]; string ctndata = context.Request["ctndata"]; if (uid == null || optype == null || mdata == null || ctndata == null) { return "参数有误!"; } if (!MsOpSeaeEdiVGMDAL.CheckAuth(uid, skey)) { return "身份验证错误!"; } var headData = JsonConvert.Deserialize(mdata); List bodyList = JsonConvert.Deserialize>(ctndata); List listTemp = JsonConvert.Deserialize>(ctndata); string userid = uid; var isPost = true; string ctnStr = ""; string cntrnohis = ""; string sealnohis = ""; if (bodyList != null) { if (bodyList.Count > 0) { bool IsDongGui = false; int i = 0; string ctnTemp = ""; foreach (var item in bodyList) { ctnTemp = item.CTNALL; List list = listTemp.FindAll(b => b.CTNALL == ctnTemp).ToList(); if (list.Count != 0) { ctnStr += ctnTemp + "*" + list.Count.ToString() + ","; listTemp.RemoveAll(a => a.CTNALL == ctnTemp); } if (IsDongGui == false) { if (item.CTNALL.Contains("RH") || item.CTNALL.Contains("RF") || item.CTNALL.Contains("RQ")) { IsDongGui = true; } } if (i == 0) { if (headData.BaoZhuang == "" || headData.BaoZhuang == null) { headData.BaoZhuang = item.KINDPKGS; } } //更新箱型信息 item.CTNALL = MsOpSeaeEdiVGMDAL.GetCTNALLWithEDICODE(item.CTNALL); string strp1 = item.CNTRNO.Substring(0, 4); string strp2 = item.CNTRNO.Substring(4, item.CNTRNO.Length - 4);//AAAA1234567 bool CNTRNOENABLED = true; if (item.CNTRNO.Length != 11) { CNTRNOENABLED = false; } if (!System.Text.RegularExpressions.Regex.IsMatch(strp1, @"(?i)^[a-zA-Z]+$")) { CNTRNOENABLED = false; } if (!System.Text.RegularExpressions.Regex.IsMatch(strp2, @"(?i)^[0-9]+$")) { CNTRNOENABLED = false; } if (!CNTRNOENABLED) { return "箱号格式错误!"; } if (cntrnohis.Contains(item.CNTRNO)) { return "箱号重复"; } cntrnohis += item.CNTRNO + ","; if (sealnohis.Contains(item.SEALNO)) { return "封号重复"; } sealnohis += item.SEALNO + ","; i++; } } } if (ctnStr.Length > 0) { ctnStr = ctnStr.Substring(0, ctnStr.Length - 1); } var errorstr = ""; headData.JiZhuangXiang = ctnStr; //更新船公司信息 headData.CARRIER = MsOpSeaeEdiVGMDAL.GetCGSWithEDICODE(headData.CARRIER); headData.LuRuRenId = userid; headData.LuRuRen = MsOpSeaeEdiVGMDAL.GetShowNameWithGID(userid); headData.LuRuRiQi = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); #region 根据船公司校验ORDERNO和VGMCLOSETIME两个字段 if ((headData.CARRIER == "HLC" || headData.CARRIER == "WHL") && string.IsNullOrEmpty(headData.ORDERNO)) { return JsonConvert.Serialize(new JsonResponse() { Success = false, Message = "订舱编号必填,请填写!" }); } if ((headData.CARRIER == "MSK" || headData.CARRIER == "HMM")) { if (string.IsNullOrEmpty(headData.VGMCLOSETIME)) { return JsonConvert.Serialize(new JsonResponse() { Success = false, Message = "VGM截止日期必填,请填写!" }); } else { if (DateTime.TryParse(headData.VGMCLOSETIME, out DateTime vgmCloseTime)) { if (vgmCloseTime < DateTime.Today) { return JsonConvert.Serialize(new JsonResponse() { Success = false, Message = "VGM截止日期不能小于当天,请检查!" }); } } else { return JsonConvert.Serialize(new JsonResponse() { Success = false, Message = "VGM截止日期格式有误!" }); } } } #endregion //判断一个用户下的主提单号,分提单号是否重复 string MFNO = ""; string status = ""; if (MsOpSeaeEdiVGMDAL.HasSameMBLNOWithUserIdForInterface(headData.MBLNO, userid, out MFNO, out status)) { headData.MFNO = MFNO; headData.SENDSTATUS = status; headData.DbOperationType = DbOperationType.DbotUpd; } else { headData.SENDSTATUS = "已录入"; headData.DbOperationType = DbOperationType.DbotIns; headData.MFNO = Guid.NewGuid().ToString(); } if (headData.ETD == "") { headData.ETD = null; } else { headData.ETD = DateTime.Parse(headData.ETD).ToString("yyyy-MM-dd"); } headData.CARRIER = MsBaseInfoDAL.DelPortCn(headData.CARRIER); headData.ChuanMing = MsBaseInfoDAL.DelPortCn(headData.ChuanMing); headData.HangCi = MsBaseInfoDAL.DelPortCn(headData.HangCi); headData.ZhuangHuoGang = MsBaseInfoDAL.DelPortCn(headData.ZhuangHuoGang); headData.XieHuoGang = MsBaseInfoDAL.DelPortCn(headData.XieHuoGang); headData.MuDiDi = MsBaseInfoDAL.DelPortCn(headData.MuDiDi); headData.ShouHuoDi = MsBaseInfoDAL.DelPortCn(headData.ShouHuoDi); var BSNO = headData.MFNO; var modb = new ModelObjectDB(); DBResult result = modb.Save(headData); if (result.Success == true) { result = MsOpSeaeEdiVGMDAL.SaveDetailForInterface(headData, bodyList, null); if (result.Success) { string outmsg = ""; bool allow = vgmptDAL.CheckAllowSend(headData.MFNO, optype, out outmsg); if (!allow) { var json2 = JsonConvert.Serialize(new { Success = true, Message = outmsg }); return json2; } string path = HttpContext.Current.Server.MapPath("/VGMFile/"); string filepath = ""; string msg = vgmptDAL.writeAndSendVgmXml("'" + headData.MFNO + "'", userid, optype, path, out filepath); headData.SENDSTATUS = "已发送"; modb.Save(headData); var json = JsonConvert.Serialize(new { Success = true, Message = msg }); return json; } } var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message }; return JsonConvert.Serialize(jsonRespose); } //发送vgm接口,2022-5-13根据最新逻辑重写 public string SendCustVgmNew(HttpContext context) { CommonDataContext commonData = new CommonDataContext(); VgmDataContext vgmData = new VgmDataContext(); vgmData.Database.Log = (x) => { log.Debug(x); }; RespCommon resp = new RespCommon(); string uid = context.Request["uid"]; string skey = context.Request["skey"]; string optype = context.Request["optype"]; string mdata = context.Request["mdata"]; string ctndata = context.Request["ctndata"]; if (string.IsNullOrEmpty(uid) || string.IsNullOrEmpty(optype) || string.IsNullOrEmpty(mdata) || string.IsNullOrEmpty(ctndata)) { resp.Success = false; resp.Message = "参数有误"; return Newtonsoft.Json.JsonConvert.SerializeObject(resp); } var custAuth = commonData.CUST_AUTH.AsNoTracking().Where(ca => ca.USERID == uid && ca.SKEY == skey).FirstOrDefault(); if (custAuth == null) { resp.Success = false; resp.Message = "身份验证错误"; return Newtonsoft.Json.JsonConvert.SerializeObject(resp); } var iptUser = commonData.Users.First(u => u.GID == custAuth.USERID); var headData = Newtonsoft.Json.JsonConvert.DeserializeObject(mdata); var ctnDataList = Newtonsoft.Json.JsonConvert.DeserializeObject>(ctndata); var dbHeadData = vgmData.Vgms.FirstOrDefault(x => x.MBLNO == headData.MBLNO); #region 2022-5-13最新逻辑校验 if (optype == "9") //新增 { if (dbHeadData != null && (dbHeadData.SENDSTATUS == "已发送" || dbHeadData.LuRuRenId != uid)) //已存在相同单号,且已发送;或者录入人不是本次提交人 { resp.Success = false; resp.Message = $"提单号{headData.MBLNO}已存在,录入公司:{iptUser.COMNAME},录入人:{iptUser.SHOWNAME}"; return Newtonsoft.Json.JsonConvert.SerializeObject(resp); } } else if (optype == "5" || optype == "1") //修改和删除 { if (dbHeadData == null || dbHeadData.SENDSTATUS == "已录入") { resp.Success = false; resp.Message = $"未找到发送记录,类型请选择新增"; return Newtonsoft.Json.JsonConvert.SerializeObject(resp); } else if (dbHeadData != null && dbHeadData.LuRuRenId != uid) { resp.Success = false; resp.Message = $"非该用户所属业务,不能修改或删除!"; return Newtonsoft.Json.JsonConvert.SerializeObject(resp); } } else { resp.Success = false; resp.Message = "参数有误"; return Newtonsoft.Json.JsonConvert.SerializeObject(resp); } #endregion #region 根据船公司校验ORDERNO和VGMCLOSETIME两个字段 if ((headData.CARRIER == "HLC" || headData.CARRIER == "WHL") && string.IsNullOrEmpty(headData.ORDERNO)) { resp.Success = false; resp.Message = "订舱编号必填,请填写!"; return Newtonsoft.Json.JsonConvert.SerializeObject(resp); } if ((headData.CARRIER == "MSK" || headData.CARRIER == "HMM")) { if (!headData.VGMCLOSETIME.HasValue) { resp.Success = false; resp.Message = "VGM截止日期必填,请填写!"; return Newtonsoft.Json.JsonConvert.SerializeObject(resp); } else if (headData.VGMCLOSETIME.Value < DateTime.Today) { resp.Success = false; resp.Message = "VGM截止日期不能小于当天,请检查!"; return Newtonsoft.Json.JsonConvert.SerializeObject(resp); } } #endregion #region 原有箱信息校验重写 //校验箱封号重复 var repeatCNTRNO = ctnDataList.GroupBy(x => x.CNTRNO).Select(x => new { CNTRNO = x.Key, COUNT = x.Count() }).Where(x => x.COUNT > 1).Count(); if (repeatCNTRNO > 0) { resp.Success = false; resp.Message = "箱号重复"; return Newtonsoft.Json.JsonConvert.SerializeObject(resp); } var repeatSEALNO = ctnDataList.GroupBy(x => x.SEALNO).Select(x => new { SEALNO = x.Key, COUNT = x.Count() }).Where(x => x.COUNT > 1).Count(); if (repeatSEALNO > 0) { resp.Success = false; resp.Message = "封号重复"; return Newtonsoft.Json.JsonConvert.SerializeObject(resp); } //箱号格式校验 var errCountCNTRNO = ctnDataList.Where(x => x.CNTRNO.Length != 11 || !System.Text.RegularExpressions.Regex.IsMatch(x.CNTRNO.Substring(0, 4), @"(?i)^[a-zA-Z]+$") || !System.Text.RegularExpressions.Regex.IsMatch(x.CNTRNO.Substring(4, x.CNTRNO.Length - 4), @"(?i)^[0-9]+$") || !Areas.MvcShipping.Comm.CommHelper.VerifyContainerCode(x.CNTRNO)) .Count(); if (errCountCNTRNO > 0) { resp.Success = false; resp.Message = "箱号格式错误!"; return Newtonsoft.Json.JsonConvert.SerializeObject(resp); } #endregion if (dbHeadData != null) { log.Debug($"发送vgm{headData.MBLNO}提单号存在"); headData.MFNO = dbHeadData.MFNO; headData.SENDSTATUS = dbHeadData.SENDSTATUS; Common.Helper.ModelCopyHelper.CopyModel(headData, dbHeadData); headData = dbHeadData; vgmData.Ctns.RemoveRange(vgmData.Ctns.Where(cc => cc.MFNO == dbHeadData.MFNO)); } else { log.Debug($"发送vgm{headData.MBLNO}提单号不存在"); headData.SENDSTATUS = "已录入"; headData.MFNO = Guid.NewGuid().ToString(); vgmData.Vgms.Add(headData); } vgmData.Ctns.AddRange(ctnDataList); //箱型转换 foreach (var item in ctnDataList) { item.ctn_id = Guid.NewGuid().ToString(); item.MFNO = headData.MFNO; item.CTNALL = MsOpSeaeEdiVGMDAL.GetCTNALLWithEDICODE(item.CTNALL); //以下赋值参考【MsOpSeaeEdiVGMDAL】的【SaveDetailForInterface】方法 item.ChengZhongFangShi = "累加"; item.ZhongKongBiaoShi = 5; item.ChengZhongZhongLiang = item.ChengZhongZhongLiang.HasValue ? item.ChengZhongZhongLiang.Value : 0; item.SIZE = item.CTNALL.ToString().Trim().Substring(0, 2); if (item.CTNALL.Length == 5) { item.CTN = item.CTNALL.ToString().Trim().Substring(3); } else { item.CTN = item.CTNALL.ToString().Trim().Substring(2); } int TEU = item.CTNNUM; int iTEU = int.Parse(item.CTNALL.ToString().Trim().Substring(0, 1)); if (iTEU == 4) { TEU = TEU * 2; } item.TEU = TEU; } //箱型箱量字符串 var groupListCNTRNO = ctnDataList.GroupBy(x => x.CTNALL).Select(x => new { CTNALL = x.Key, COUNT = x.Sum(cc => cc.CTNNUM) }).ToList(); headData.JiZhuangXiang = string.Join(",", groupListCNTRNO.Select(x => $"{x.CTNALL}*{x.COUNT}")); //赋值第一个箱子的包装类型 if (string.IsNullOrEmpty(headData.BaoZhuang) && ctnDataList.Count > 0) { headData.BaoZhuang = ctnDataList[0].KINDPKGS; } //更新船公司信息 headData.CARRIER = MsOpSeaeEdiVGMDAL.GetCGSWithEDICODE(headData.CARRIER); headData.LuRuRenId = uid; headData.LuRuRen = MsOpSeaeEdiVGMDAL.GetShowNameWithGID(uid); headData.LuRuRiQi = DateTime.Now; headData.CARRIER = MsBaseInfoDAL.DelPortCn(headData.CARRIER); headData.ChuanMing = MsBaseInfoDAL.DelPortCn(headData.ChuanMing); headData.HangCi = MsBaseInfoDAL.DelPortCn(headData.HangCi); headData.ZhuangHuoGang = MsBaseInfoDAL.DelPortCn(headData.ZhuangHuoGang); headData.XieHuoGang = MsBaseInfoDAL.DelPortCn(headData.XieHuoGang); headData.MuDiDi = MsBaseInfoDAL.DelPortCn(headData.MuDiDi); headData.ShouHuoDi = MsBaseInfoDAL.DelPortCn(headData.ShouHuoDi); try { vgmData.SaveChanges(); } catch (Exception ex) { resp.Success = false; resp.Message = $"发送失败,错误信息:{ex.Message}"; var exp = ex; while (exp != null) { log.Error(exp.Message); log.Error(exp.StackTrace); exp = exp.InnerException; } return Newtonsoft.Json.JsonConvert.SerializeObject(resp); } string path = HttpContext.Current.Server.MapPath("/VGMFile/"); string filepath = ""; //调用发送 string msg = vgmptDAL.writeAndSendVgmXml("'" + headData.MFNO + "'", uid, optype, path, out filepath); if (msg.Contains("成功")) { headData.SENDSTATUS = "已发送"; vgmData.SaveChanges(); resp.Success = true; resp.Message = msg; } else { //调用发送失败后删除数据,用户可修改后再次发送 vgmData.Vgms.Remove(headData); vgmData.Ctns.RemoveRange(ctnDataList); vgmData.SaveChanges(); resp.Success = false; resp.Message = msg; } return Newtonsoft.Json.JsonConvert.SerializeObject(resp); } public string GetVGMSTATUS(HttpContext context) { string uid = context.Request["uid"]; string skey = context.Request["skey"]; string mblnos = context.Request["mblno"]; if (uid == null) { return "参数有误!"; } if (!MsOpSeaeEdiVGMDAL.CheckAuth(uid, skey)) { return "身份验证错误!"; } List list = MsOpSeaeEdiVGMDAL.GetVGMSTATUS(mblnos); var jsonRespose = new JSONRSB { Success = true, Data = list }; return JsonConvert.Serialize(jsonRespose); } public static bool IsValid() { return true; HttpContext context = HttpContext.Current; if (context.Request.Browser.Crawler) return false; string key = context.Request.UserHostAddress; int hit = (Int32)(context.Cache[key] ?? 0); if (hit > TIMES) return false; else hit++; if (hit == 1) { //context.Cache.Add(key, hit, null, DateTime.Now.AddMinutes(DURATION), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null); context.Cache.Insert(key, hit, null, DateTime.Now.AddMinutes(DURATION), TimeSpan.Zero); } else { context.Cache[key] = hit; } return true; } } }