using DSWeb.MvcShipping.DAL.MsCustComauth;
using DSWeb.MvcShipping.Helper;
using DSWeb.MvcShipping.Models.MsOpSeaeEdi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
namespace DSWeb.Interface
{
///
/// CustSrv 的摘要说明
///
public class CustSrv : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string rsp = "";
if (context.Request["ac"] == "sel")
{
string name = context.Request["name"];
rsp = GetSelDataWithName(name);
}
if (context.Request["ac"] == "emfstatus")
{
string blno = context.Request["blno"];
rsp = GetEMFSTATUS(blno);
}
if (context.Request["ac"]=="checkno")
{
int rst = CheckMBLNO(context);
rsp = rst.ToString();
}
string callbackFunName = context.Request["jsoncallback"];
if (callbackFunName==""||callbackFunName==null)
{
context.Response.Write(rsp);
}
else
{
context.Response.Write(callbackFunName + "(" + rsp + ")");
}
}
public bool IsReusable
{
get
{
return false;
}
}
public int CheckMBLNO(HttpContext context)
{
string mblno = context.Request["mblno"];
string carrierid = context.Request["carrierid"];
if (carrierid.IndexOf("-")>0)
{
carrierid = carrierid.Split('-')[0];
}
int isOK = 0;
string sql = "select rules,carrierid from op_seae_mblnocheck order by idx desc";
Database db = DatabaseFactory.CreateDatabase();
List list = new List();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, sql))
{
while (reader.Read())
{
MBLNOCHECKModel m = new MBLNOCHECKModel();
m.rules = reader["rules"].ToString();
m.carrierid = reader["carrierid"].ToString();
list.Add(m);
}
}
//匹配失败次数
int cntfaild = 0;
foreach (var m in list)
{
//匹配标识1:匹配成功 0:不成功
int rst = 0;
if (m.carrierid == "YML")
{
string fnt1 = mblno.Substring(0, 1);
System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[A-Za-z]+$");
bool check1 = reg1.IsMatch(fnt1);
if (check1)
{
string fnt2 = mblno.Substring(1, 3);
if (fnt2 == "24")
{
rst = 1;
}
else
{
rst = 0;
}
}
else
{
rst = 0;
}
} else if (m.carrierid=="PIL") {
string fnt1 = mblno.Substring(0, 2);
if (fnt1 == "TA")
{
string fnt2 = mblno.Substring(2, 1);
if (fnt2 != "O")
{
rst = 1;
}
}
}
else
{
string rules = m.rules.ToString();
if (rules.IndexOf("/") > 0)
{
string[] ruleslist = rules.Split('/');
foreach (string item in ruleslist)
{
string fnt = mblno.Substring(0, item.Length);
if (fnt == item)
{
rst = 1;
}
}
}
else
{
string fnt = mblno.Substring(0, rules.Length);
if (fnt == rules)
{
rst = 1;
}
}
}
if (rst==1)
{
if (m.carrierid == carrierid)
{
isOK = 1;
}
else
{
isOK = 0;
}
}
else
{
cntfaild++;
continue;
}
}
if (isOK==1||cntfaild>=list.Count)
{
return 1;
}
else
{
return 0;
}
}
public string GetSelDataWithName(string name)
{
var dataList = MsCustComauthDAL.GetSelDataWithName(name);
var jsonRespose = new JSONRSB { Success = true, Data = dataList };
return JsonConvert.Serialize(jsonRespose);
}
///
/// 查询舱单状态
///
///
///
///
public string GetEMFSTATUS(string mblno)
{
//判断主单号分单号
string sql = @"select t1.MBLNO, t0.statustext,t0.statustime from op_seae_edi_status t0
join op_seae_edi t1 on t0.mfno = t1.MFNO
where t1.mblno = '" + mblno + "' or t1.hblno ='" + mblno + "' order by t0.statustime desc";
List list = new List();
Database db = DatabaseFactory.CreateDatabase();
try
{
using (IDataReader reader = db.ExecuteReader(CommandType.Text, sql))
{
while (reader.Read())
{
EDIStatusModeli v = new EDIStatusModeli();
v.status = reader["statustext"].ToString();
v.statustime = reader["statustime"].ToString();
v.blno = reader["mblno"].ToString();
list.Add(v);
}
reader.Close();
}
}
catch (Exception)
{
}
var jsonRespose = new JSONRSB { Success = true, Data = list };
return JsonConvert.Serialize(jsonRespose);
}
}
public class MBLNOCHECKModel {
public string rules { get; set; }
public string carrierid { get; set; }
}
}