using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using DSWeb.Areas.MvcShipping.Models.MsOpStatusNew;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using DSWeb.MvcShipping.Models.MsOpSeae;
using qingdaoport;
using Ivony.Html;
using Ivony.Html.Parser;
using System.Text.RegularExpressions;
namespace DSWeb.Areas.MvcShipping.DAL
{
public class MsOpStatusNewDAL
{
///
/// 创建/更新 状态树主表
/// 海运出口保存时只产生主状态、不产生集装箱状态
///
///
/// 操作人
///
public bool SaveStatusTree ( string bsno ,string oper) {
//根据提单号获取Opseae数据
OpSeaeModel OpSeaeModel = GetOpSeae(bsno);
if (OpSeaeModel.BSNO==null)
{
return false;
}
//如果主题单号下已有状态树则执行更新操作
StringBuilder sb = new StringBuilder();
if (CheckOpSeaeHasStatusTree(bsno))
{
//更新状态树
sb.Append("update Op_StatusMain set ");
sb.Append("ChuanGongSi = '" + OpSeaeModel.ChuanGongSi+ "',");
sb.Append("ChuanMingHangCi = '" + OpSeaeModel.ChuanMingHangCi + "',");
sb.Append("ShiFaGang = '" + OpSeaeModel.ShiFaGang + "',");
sb.Append("MuDiGang = '" + OpSeaeModel.MuDiGang + "',");
sb.Append("MBLNO = '" + OpSeaeModel.MBLNO + "',");
sb.Append("XiangXingXiangLiang = '" + OpSeaeModel.XiangXingXiangLiang.Replace("'","''") + "',");
sb.Append("ETD = '" + OpSeaeModel.ETD + "',");
sb.Append("ETA = '" + OpSeaeModel.ETA + "'");
sb.Append(" where BSNO = '" + bsno + "'");
//删除之前的明细
DeleteStatusDetails(bsno);
}
else
{
//新建状态树
sb.Append(@"Insert into Op_StatusMain
(GID,BSNO,ChuanGongSi,ChuanMingHangCi,ShiFaGang,MuDiGang,MBLNO,XiangXingXiangLiang,ETD,ETA) ");
sb.Append("values (");
sb.Append("'"+Guid.NewGuid()+"',");
sb.Append("'" + OpSeaeModel.BSNO + "',");
sb.Append("'" + OpSeaeModel.ChuanGongSi + "',");
sb.Append("'" + OpSeaeModel.ChuanMingHangCi + "',");
sb.Append("'" + OpSeaeModel.ShiFaGang + "',");
sb.Append("'" + OpSeaeModel.MuDiGang + "',");
sb.Append("'" + OpSeaeModel.MBLNO + "',");
sb.Append("'" + OpSeaeModel.XiangXingXiangLiang.Replace("'", "''") + "',");
sb.Append("'" + OpSeaeModel.ETD + "',");
sb.Append("'" + OpSeaeModel.ETA + "'");
sb.Append(")");
}
Database db = DatabaseFactory.CreateDatabase();
int flag = db.ExecuteNonQuery(CommandType.Text,sb.ToString());
//主提单号为空时,状态更新为【已接单】
if (OpSeaeModel.MBLNO==""||OpSeaeModel.MBLNO==null)
{
UpdateMainStatus(OpSeaeModel.BSNO, 0,oper,OpSeaeModel.CreateTime);
}
else
{
//主提单号不为空时,状态更新为【已出号】
UpdateMainStatus(OpSeaeModel.BSNO, 3, oper);
}
return flag > 0;
}
///
/// 更新状态节点-主表
///
/// 业务编号
/// 主提单号
/// 0:已结单 1:已发入货通知 2:已订舱 3:已出号 4:已提单确认 5:费用已提交
/// 状态完成时间
///
public bool UpdateMainStatus (string bsno,int statusNode,string Oper,string statusTime="") {
StringBuilder sb = new StringBuilder();
string timeStr = statusTime==""?DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"):statusTime;
sb.Append("update Op_StatusMain ");
if (statusNode==0)
{
sb.Append(" set YiJieDanTime = '" + timeStr + "',");
sb.Append(" YiJieDanOp = '" + Oper + "'");
}
else if(statusNode==1)
{
sb.Append(" set YiFaRuHuoTongZhiTime = '" + timeStr + "',");
sb.Append(" YiFaRuHuoTongZhiOp = '" + Oper + "'");
}
else if (statusNode == 2)
{
sb.Append(" set YiDingCangTime = '" + timeStr + "',");
sb.Append(" YiDingCangOp = '" + Oper + "'");
}
else if (statusNode == 3)
{
sb.Append(" set YiChuHaoTime = '" + timeStr + "',");
sb.Append(" YiChuHaoOp = '" + Oper + "'");
}
else if (statusNode == 4)
{
sb.Append(" set YiTiDanQueRenTime = '" + timeStr + "',");
sb.Append(" YiTiDanQueRenOp = '" + Oper + "'");
}
else if (statusNode == 5)
{
sb.Append(" set FeiYongYiTiJiaoTime = '" + timeStr + "',");
sb.Append(" FeiYongYiTiJiaoOp = '" + Oper + "'");
}
sb.Append(" where BSNO in ('" + bsno + "')");
Database db = DatabaseFactory.CreateDatabase();
int flag = db.ExecuteNonQuery(CommandType.Text, sb.ToString());
return flag > 0;
}
///
/// 更新状态节点-青岛港码头
///
///
public bool UpdateQdportStatus (OpSeaeQDPortDataModel model,string bsno) {
if (!checkOpSeaeQDPortDataModel(model))
{
return false;
}
//如果没有主表数据,创建主表数据
if (!CheckOpSeaeHasStatusTree(bsno))
{
SaveStatusTree(bsno, "System");
}
StringBuilder sb = new StringBuilder();
List listDetails = new List();
var mtxxList = model.mtxxList.Where(m => m.DQZTMC=="已装船").OrderBy(m => m.XH).ToList();
var ydbgList = model.ydbgList.OrderBy(m => m.XH).ToList();
var hgbgdfxList = model.bgdfxList.OrderBy(m => m.CONTA_ID).ToList();
var zzfxList = model.zzfxList.OrderBy(m => m.XH).ToList();
for (int i = 0; i < mtxxList.Count; i++)
{
StatusDetails sd = new StatusDetails();
sd.BSNO = bsno;
sd.MBLNO = mtxxList[i].TDH;
sd.XiangHao = mtxxList[i].XH;
sd.YunDiZhengChangMsg = ydbgList[i].HZMS;
sd.YunDiZhengChangTime =ydbgList.Count>0?ydbgList[i].HZSJ.ToString():"";
sd.YiRuZhaKouTime =mtxxList.Count>0?mtxxList[i].SJRGSJ.ToString():"";
sd.BaoGuanDanYiTongGuanFangXingTime =hgbgdfxList.Count>0?hgbgdfxList[i].R_DATE.ToString():"";
sd.CangDanYiZhuangZaiFangXingTime = zzfxList.Count>0?zzfxList[i].RDATE.ToString():"";
sd.YiZhuangChuanTime =mtxxList.Count>0?mtxxList[i].SJCGSJ.ToString():"";
listDetails.Add(sd);
}
foreach (var item in listDetails)
{
SaveStatusDetails(item, bsno);
}
string mblno = GetMBLNOWithBSNO(bsno);
string cgs = GetChuanGongSiWithMBLNO(mblno);
if (cgs=="达飞"|| cgs=="CMA" || cgs=="cma")
{
GetCMAData(mblno);
}
else if (cgs == "MAERSK") {
GetMaerskDetailsIntoDBWithMBLNO(mblno);
}
return true;
}
private bool SaveStatusDetails ( StatusDetails model,string bsno) {
StringBuilder sb = new StringBuilder();
if (CheckHasCtnWithBSNOAndCTNNO(bsno, model.XiangHao))
{
//如果同提单号下的该箱号已存在,对本条状态执行更新操作
sb.Append("update Op_StatusDetails set ");
sb.Append("CangDanJieShouShenBaoTime = '" + model.CangDanJieShouShenBaoTime + "', ");
sb.Append("YunDiZhengChangMsg = '" + model.YunDiZhengChangMsg + "', ");
sb.Append("YunDiZhengChangTime = '" + model.YunDiZhengChangTime + "', ");
sb.Append("YiRuZhaKouTime = '" + model.YiRuZhaKouTime + "', ");
sb.Append("BaoGuanDanYiTongGuanFangXingTime = '" + model.BaoGuanDanYiTongGuanFangXingTime + "', ");
sb.Append("CangDanYiZhuangZaiFangXingTime = '" + model.CangDanYiZhuangZaiFangXingTime + "', ");
sb.Append("YiZhuangChuanTime = '" + model.YiZhuangChuanTime + "' ");
sb.Append("where bsno in ('" + bsno + "') and XiangHao = '" + model.XiangHao + "'");
}
else
{
//如果同提单号下的该箱号不存在,执行插入操作
sb.Append("insert into Op_StatusDetails ");
sb.Append("values(");
sb.Append("'" + Guid.NewGuid() + "',");
sb.Append("'" + model.BSNO + "',");
sb.Append("'" + model.MBLNO + "',");
sb.Append("'" + model.XiangHao + "',");
sb.Append("'" + model.YiTiXiangTime + "',");
sb.Append("'" + model.YiFanChangTime + "',");
sb.Append("'" + model.CangDanJieShouShenBaoTime + "',");
sb.Append("'" + model.YunDiZhengChangMsg + "',");
sb.Append("'" + model.YunDiZhengChangTime + "',");
sb.Append("'" + model.YiRuZhaKouTime + "',");
sb.Append("'" + model.BaoGuanDanYiTongGuanFangXingTime + "',");
sb.Append("'" + model.CangDanYiZhuangZaiFangXingTime + "',");
sb.Append("'" + model.YiZhuangChuanTime + "'");
sb.Append(")");
}
Database db = DatabaseFactory.CreateDatabase();
int flag = db.ExecuteNonQuery(CommandType.Text, sb.ToString());
return flag > 0;
}
///
/// 获取状态数据
///
///
///
public static List GetStatusData ( string mblno="" )
{
var strSql = new StringBuilder();
strSql.Append(@"select
os.ChuanGongSi,
os.ChuanMingHangCi,
os.ShiFaGang,
os.MuDiGang,
os.BSNO,
os.MBLNO,
os.XiangXingXiangLiang,
os.ETD,
os.ETA,
os.YiJieDanTime,
os.YiJieDanOp,
os.YiFaRuHuoTongZhiTime,
os.YiFaRuHuoTongZhiOp,
os.YiDingCangTime,
os.YiDingCangOp,
os.YiChuHaoTime,
os.YiChuHaoOp,
os.YiTiDanQueRenTime,
os.YiTiDanQueRenOp,
os.FeiYongYiTiJiaoTime,
os.FeiYongYiTiJiaoOp,
osd.XiangHao ,
osd.YiTiXiangTime ,
osd.YiFanChangTime ,
osd.CangDanJieShouShenBaoTime ,
osd.YunDiZhengChangMsg ,
osd.YunDiZhengChangTime ,
osd.YiRuZhaKouTime ,
osd.BaoGuanDanYiTongGuanFangXingTime ,
osd.CangDanYiZhuangZaiFangXingTime ,
osd.YiZhuangChuanTime
from Op_StatusMain os
LEFT JOIN Op_StatusDetails osd on os.bsno = osd.BSNO ");
if (mblno != "")
{
strSql.Append(" where os.mblno= '" + mblno + "'");
}
return SetStatusData(strSql);
}
private static List SetStatusData ( StringBuilder strSql )
{
var list = new List();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
{
while (reader.Read())
{
MsOpStatusNew s = new MsOpStatusNew();
//创建主状态模型
StatusMain sm = new StatusMain();
sm.ChuanGongSi = reader["ChuanGongSi"].ToString();
sm.ChuanMingHangCi = reader["ChuanMingHangCi"].ToString();
sm.ShiFaGang = reader["ShiFaGang"].ToString();
sm.MuDiGang = reader["MuDiGang"].ToString();
sm.BSNO = reader["BSNO"].ToString();
sm.MBLNO = reader["MBLNO"].ToString();
sm.XiangXingXiangLiang = reader["XiangXingXiangLiang"].ToString();
sm.ETD = reader["ETD"].ToString();
sm.ETA = reader["ETA"].ToString();
sm.YiJieDanTime = convertTimeStr(reader["YiJieDanTime"].ToString());
sm.YiJieDanOp = reader["YiJieDanOp"].ToString();
sm.YiFaRuHuoTongZhiTime = convertTimeStr(reader["YiFaRuHuoTongZhiTime"].ToString());
sm.YiFaRuHuoTongZhiOp = reader["YiFaRuHuoTongZhiOp"].ToString();
sm.YiDingCangTime = convertTimeStr(reader["YiDingCangTime"].ToString());
sm.YiDingCangOp = reader["YiDingCangOp"].ToString();
sm.YiChuHaoTime = convertTimeStr( reader["YiChuHaoTime"].ToString());
sm.YiChuHaoOp = reader["YiChuHaoOp"].ToString();
sm.YiTiDanQueRenTime = convertTimeStr( reader["YiTiDanQueRenTime"].ToString());
sm.YiTiDanQueRenOp = reader["YiTiDanQueRenOp"].ToString();
sm.FeiYongYiTiJiaoTime = convertTimeStr(reader["FeiYongYiTiJiaoTime"].ToString());
sm.FeiYongYiTiJiaoOp = reader["FeiYongYiTiJiaoOp"].ToString();
s.StatusMain = sm;
//创建明细状态模型
StatusDetails sd = new StatusDetails();
sd.XiangHao = reader["XiangHao"].ToString();
sd.YiTiXiangTime = convertTimeStr(reader["YiTiXiangTime"].ToString());
sd.YiFanChangTime = convertTimeStr( reader["YiFanChangTime"].ToString());
sd.CangDanJieShouShenBaoTime = convertTimeStr( reader["CangDanJieShouShenBaoTime"].ToString());
sd.YunDiZhengChangMsg = reader["YunDiZhengChangMsg"].ToString();
sd.YunDiZhengChangTime = convertTimeStr(reader["YunDiZhengChangTime"].ToString());
sd.YiRuZhaKouTime = convertTimeStr(reader["YiRuZhaKouTime"].ToString());
sd.BaoGuanDanYiTongGuanFangXingTime = convertTimeStr(reader["BaoGuanDanYiTongGuanFangXingTime"].ToString());
sd.CangDanYiZhuangZaiFangXingTime = convertTimeStr( reader["CangDanYiZhuangZaiFangXingTime"].ToString());
sd.YiZhuangChuanTime = convertTimeStr( reader["YiZhuangChuanTime"].ToString());
s.StatusDetails = sd;
list.Add(s);
}
reader.Close();
}
return list;
}
///
/// 获取OpSeae信息
///
///
///
private static OpSeaeModel GetOpSeae (string BSNO) {
string sql = @"select BSNO,MBLNO,CARRIER ChuanGongSi,MBLNO,VESSEL+'/'+VOYNO ChuanMingHangCi,
CNTRTOTAL XiangXingXiangLiang,PORTLOAD ShiFaGang,
PORTDISCHARGE MuDiGang,ETD,ETA,CreateTime,OP
from op_seae where BSNO = '" + BSNO + "'";
string sql2 = @" select CNTRNO XiangHao from op_ctn where bsno = '" + BSNO + "'";
OpSeaeModel model = new OpSeaeModel();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, sql))
{
while (reader.Read())
{
model.BSNO = reader["BSNO"].ToString();
model.MBLNO = reader["MBLNO"].ToString();
model.ChuanGongSi = reader["ChuanGongSi"].ToString();
model.ChuanMingHangCi = reader["ChuanMingHangCi"].ToString();
model.XiangXingXiangLiang = reader["XiangXingXiangLiang"].ToString();
model.ShiFaGang = reader["ShiFaGang"].ToString();
model.MuDiGang = reader["MuDiGang"].ToString();
model.ETD = reader["ETD"].ToString();
model.ETA = reader["ETA"].ToString();
model.CreateTime = reader["CreateTime"].ToString();
model.Creator = reader["OP"].ToString();
}
reader.Close();
}
List list = new List();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, sql2))
{
while (reader.Read())
{
}
reader.Close();
}
model.CtnList = list;
return model;
}
///
/// 判断是否已有状态树
///
///
///
private static bool CheckOpSeaeHasStatusTree ( string bsno ) {
string sql = "select count (*) from Op_StatusMain where bsno = '" + bsno + "'";
Database db = DatabaseFactory.CreateDatabase();
int flag = Convert.ToInt32(db.ExecuteScalar(CommandType.Text,sql));
if (flag>0)
{
return true;
}
else
{
return false;
}
}
///
/// 判断是否已有明细状态
///
///
///
///
private static bool CheckHasCtnWithBSNOAndCTNNO (string bsno,string xianghao) {
string sql = "select count (*) from Op_StatusDetails where BSNO = '" + bsno + "' and XiangHao = '" + xianghao + "'";
Database db = DatabaseFactory.CreateDatabase();
int flag = Convert.ToInt32(db.ExecuteScalar(CommandType.Text, sql));
if (flag > 0)
{
return true;
}
else
{
return false;
}
}
public static string GetBSNOWithMBLNO ( string mblno ) {
string sql = "select bsno from op_seae where mblno = '"+mblno+"'";
Database db = DatabaseFactory.CreateDatabase();
string bsno = Convert.ToString(db.ExecuteScalar(CommandType.Text, sql));
return bsno;
}
public static string GetMBLNOWithBSNO ( string bsno )
{
string sql = "select mblno from op_seae where bsno in ('" + bsno + "')";
Database db = DatabaseFactory.CreateDatabase();
string mblno = Convert.ToString(db.ExecuteScalar(CommandType.Text, sql));
return mblno;
}
private static string convertTimeStr ( string time ) {
if (time=="")
{
return "[未取到相关时间]";
}
else
{
return time;
}
}
private static bool checkOpSeaeQDPortDataModel ( OpSeaeQDPortDataModel model )
{
if (model==null||model.bgdfxList==null||model.mtxxList==null||model.wllhList==null||model.wlshList==null
||model.ydbgList==null||model.zxdList==null||model.zzfxList==null)
{
return false;
}
else
{
return true;
}
}
#region 船公司相关-CMA
public static bool GetCMAData (string mblno) {
if (mblno=="")
{
return false;
}
HttpHelper http = new HttpHelper();
string cmaUrl = "http://www.cma-cgm.com/ebusiness/tracking/search?SearchBy=BL&Reference="+mblno;
HttpItem item = new HttpItem()
{
URL = cmaUrl,//URL 必需项
Method = "get",//URL 可选项 默认为Get
};
HttpResult result1 = http.GetHtml(item);
string html = result1.Html;
IHtmlDocument document = new JumonyParser().Parse(html);
IEnumerable result = document.Find("tbody[class=sep-horiz]");
//多箱
if (result.ToList().Count==0)
{
SaveCMAWithMultiData(document,mblno);
}
//单箱
else
{
SaveCMAWithSingleData(document,mblno);
}
return true;
}
private static void SaveCMAWithSingleData ( IHtmlDocument document,string mblno)
{
IEnumerable result = document.Find("tbody[class=sep-horiz]");
List ctnList = new List();
List msgList = new List();
List timeList = new List();
IHtmlElement title = document.FindFirst("title");
string titlestr = title.InnerText();
string XiangHao = titlestr.Replace("Tracking details for Container", "").Trim();
foreach (var r in result)
{
//当前步骤获取
ctnList.Add(XiangHao);
//状态
IHtmlElement currtr = r.FindFirst("tr[class=date-current]");
IHtmlElement currstatus = currtr.FindFirst("td[data-move=status]");
string currstatusstr = currstatus.InnerText();
msgList.Add(currstatusstr);
//时间
IHtmlElement currstatustime = currtr.FindFirst("td[data-move=date]");
string currstatustimestr = currstatustime.InnerText();
try
{
string dt = DateTime.Parse(currstatustimestr).ToString("yyyy-MM-dd hh:mm");
timeList.Add(dt);
}
catch (Exception)
{
timeList.Add("");
}
}
//插入数据库
if (ctnList.Count > 0)
{
//删除
string delSql = "delete from Op_StatusCarrier where MBLNO = '" + mblno + "' and Carrier = 'CMA'";
Database db = DatabaseFactory.CreateDatabase();
db.ExecuteNonQuery(CommandType.Text, delSql);
StringBuilder sbValues = new StringBuilder();
for (int i = 0; i < ctnList.Count; i++)
{
sbValues.Append("(");
sbValues.Append("'" + Guid.NewGuid() + "',");
sbValues.Append("'CMA',");
sbValues.Append("'" + mblno + "',");
sbValues.Append("'" + ctnList[i] + "',");
sbValues.Append("'" + msgList[i] + "',");
sbValues.Append("'" + timeList[i] + "'");
sbValues.Append("),");
}
//插入
string valuestr = sbValues.ToString().Substring(0, sbValues.ToString().Length - 1);
StringBuilder insSb = new StringBuilder();
insSb.Append("insert into Op_StatusCarrier (GID,Carrier,MBLNO,XiangHao,StatusMsg,StatusTime) ");
insSb.Append("values" + valuestr);
string sql = insSb.ToString();
db.ExecuteNonQuery(CommandType.Text, sql);
}
GetCMADataDetailsInToDBWithDocumentForSingleData(document,mblno,XiangHao);
}
private static void SaveCMAWithMultiData ( IHtmlDocument document, string mblno )
{
IEnumerable result = document.Find("tbody");
List ctnList = new List();
List msgList = new List();
List timeList = new List();
foreach (var r in result)
{
IEnumerable tds1 = r.Find("td[data-ctnr=id]");
foreach (var td in tds1)
{
ctnList.Add(td.InnerText());
}
IEnumerable tds2 = r.Find("td[data-ctnr=date]");
foreach (var td in tds2)
{
try
{
string dt = DateTime.Parse(td.InnerText()).ToString("yyyy-MM-dd hh:mm");
timeList.Add(dt);
}
catch (Exception)
{
timeList.Add("");
}
}
IEnumerable tds3 = r.Find("td[data-ctnr=status]");
foreach (var td in tds3)
{
msgList.Add(td.InnerText());
}
}
//插入数据库
if (ctnList.Count > 0)
{
//删除
string delSql = "delete from Op_StatusCarrier where MBLNO = '" + mblno + "' and Carrier = 'CMA'";
Database db = DatabaseFactory.CreateDatabase();
db.ExecuteNonQuery(CommandType.Text, delSql);
StringBuilder sbValues = new StringBuilder();
for (int i = 0; i < ctnList.Count; i++)
{
sbValues.Append("(");
sbValues.Append("'" + Guid.NewGuid() + "',");
sbValues.Append("'CMA',");
sbValues.Append("'" + mblno + "',");
sbValues.Append("'" + ctnList[i] + "',");
sbValues.Append("'" + msgList[i] + "',");
sbValues.Append("'" + timeList[i] + "'");
sbValues.Append("),");
}
//插入
string valuestr = sbValues.ToString().Substring(0, sbValues.ToString().Length - 1);
StringBuilder insSb = new StringBuilder();
insSb.Append("insert into Op_StatusCarrier (GID,Carrier,MBLNO,XiangHao,StatusMsg,StatusTime) ");
insSb.Append("values" + valuestr);
string sql = insSb.ToString();
db.ExecuteNonQuery(CommandType.Text, sql);
//插入集装箱状态明细
foreach (var ctn in ctnList)
{
GetCMADataDetailsInToDBWithMBLNOAndXiangHaoForMultiData(mblno, ctn);
}
}
}
public static List GetCMADataFromDB(string mblno,string xianghao){
string sql = "select * from Op_StatusCarrier where Carrier = 'CMA' and MBLNO = '"+mblno+"' and XiangHao = '"+xianghao+"'";
Database db = DatabaseFactory.CreateDatabase();
List list = new List();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, sql))
{
while (reader.Read())
{
OpCMAStatus s = new OpCMAStatus();
s.MBLNO = reader["MBLNO"].ToString();
s.XiangHao = reader["XiangHao"].ToString();
s.StatusMsg = reader["StatusMsg"].ToString();
s.StatusTime = reader["StatusTime"].ToString();
list.Add(s);
}
}
return list;
}
public static List GetStatusDetailsDataFromDB ( string mblno, string xianghao )
{
string sql = "select * from Op_StatusCarrierDetails where MBLNO = '" + mblno + "' and XiangHao = '" + xianghao + "' order by StatusTime";
Database db = DatabaseFactory.CreateDatabase();
List list = new List();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, sql))
{
while (reader.Read())
{
OpCMAStatus s = new OpCMAStatus();
s.MBLNO = reader["MBLNO"].ToString();
s.XiangHao = reader["XiangHao"].ToString();
s.StatusMsg = reader["StatusMsg"].ToString();
s.StatusTime = reader["StatusTime"].ToString();
s.ChuanMing = reader["Vessel"].ToString();
s.HangCi = reader["Voyage"].ToString();
list.Add(s);
}
}
return list;
}
private static bool GetCMADataDetailsInToDBWithMBLNOAndXiangHaoForMultiData ( string mblno, string xianghao ) {
string urlstr = "http://www.cma-cgm.com/eBusiness/tracking/detail/"+xianghao+"?SearchCriteria=BL&SearchByReference="+mblno;
HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem()
{
// URL = textBox1.Text,//URL 必需项
URL = urlstr,
Method = "get",//URL 可选项 默认为Get
};
HttpResult result1 = http.GetHtml(item);
string html = result1.Html;
IHtmlDocument document = new JumonyParser().Parse(html);
IEnumerable result = document.Find("tbody[class=sep-horiz]");
List statusList = new List();
List timeList = new List();
List locList = new List();
List vesselList = new List();
List voyageList = new List();
foreach (var r in result)
{
//状态
IEnumerable statusTd = r.Find("td[data-move=status]");
foreach (var status in statusTd)
{
statusList.Add(status.InnerText());
}
//时间
IEnumerable timeTd = r.Find("td[data-move=date]");
foreach (var time in timeTd)
{
try
{
string dt = DateTime.Parse(time.InnerText()).ToString("yyyy-MM-dd hh:mm");
timeList.Add(dt);
}
catch (Exception)
{
timeList.Add("");
}
}
//位置
IEnumerable locTd = r.Find("td[data-move=location]");
foreach (var loc in locTd)
{
locList.Add(loc.InnerText());
}
//船名
IEnumerable vesselTd = r.Find("td[data-move=vessel]");
foreach (var vessel in vesselTd)
{
vesselList.Add(vessel.InnerText());
}
//航次
IEnumerable voyageTd = r.Find("td[data-move=voyage]");
foreach (var voyage in voyageTd)
{
voyageList.Add(voyage.InnerText());
}
//数据库操作
if (statusList.Count>0)
{
//删除
string delSql = "delete from Op_StatusCarrierDetails where MBLNO = '" + mblno + "' and xianghao='"+xianghao+"'";
Database db = DatabaseFactory.CreateDatabase();
db.ExecuteNonQuery(CommandType.Text, delSql);
StringBuilder sbValues = new StringBuilder();
for (int i = 0; i < statusList.Count; i++)
{
sbValues.Append("(");
sbValues.Append("'" + Guid.NewGuid() + "',");
sbValues.Append("'"+mblno+"',");
sbValues.Append("'" + xianghao + "',");
sbValues.Append("'" + statusList[i] + "',");
sbValues.Append("'" + timeList[i] + "',");
sbValues.Append("'" + locList[i] + "',");
sbValues.Append("'" + vesselList[i] + "',");
sbValues.Append("'" + voyageList[i] + "'");
sbValues.Append("),");
}
//插入
string valuestr = sbValues.ToString().Substring(0, sbValues.ToString().Length - 1);
StringBuilder insSb = new StringBuilder();
insSb.Append("insert into Op_StatusCarrierDetails (GID,MBLNO,XIANGHAO,StatusMsg,StatusTime,LOC,Vessel,Voyage) ");
insSb.Append("values" + valuestr);
string sql = insSb.ToString();
db.ExecuteNonQuery(CommandType.Text, sql);
}
}
return true;
}
private static bool GetCMADataDetailsInToDBWithDocumentForSingleData ( IHtmlDocument document,string mblno,string xianghao )
{
IEnumerable result = document.Find("tbody[class=sep-horiz]");
List statusList = new List();
List timeList = new List();
List locList = new List();
List vesselList = new List();
List voyageList = new List();
foreach (var r in result)
{
//状态
IEnumerable statusTd = r.Find("td[data-move=status]");
foreach (var status in statusTd)
{
statusList.Add(status.InnerText());
}
//时间
IEnumerable timeTd = r.Find("td[data-move=date]");
foreach (var time in timeTd)
{
try
{
string dt = DateTime.Parse(time.InnerText()).ToString("yyyy-MM-dd hh:mm");
timeList.Add(dt);
}
catch (Exception)
{
timeList.Add("");
}
}
//位置
IEnumerable locTd = r.Find("td[data-move=location]");
foreach (var loc in locTd)
{
locList.Add(loc.InnerText());
}
//船名
IEnumerable vesselTd = r.Find("td[data-move=vessel]");
foreach (var vessel in vesselTd)
{
vesselList.Add(vessel.InnerText());
}
//航次
IEnumerable voyageTd = r.Find("td[data-move=voyage]");
foreach (var voyage in voyageTd)
{
voyageList.Add(voyage.InnerText());
}
//数据库操作
if (statusList.Count>0)
{
//删除
string delSql = "delete from Op_StatusCarrierDetails where MBLNO = '" + mblno + "' and xianghao = '"+xianghao+"'";
Database db = DatabaseFactory.CreateDatabase();
db.ExecuteNonQuery(CommandType.Text, delSql);
StringBuilder sbValues = new StringBuilder();
for (int i = 0; i < statusList.Count; i++)
{
sbValues.Append("(");
sbValues.Append("'" + Guid.NewGuid() + "',");
sbValues.Append("'"+mblno+"',");
sbValues.Append("'" + xianghao + "',");
sbValues.Append("'" + statusList[i] + "',");
sbValues.Append("'" + timeList[i] + "',");
sbValues.Append("'" + locList[i] + "',");
sbValues.Append("'" + vesselList[i] + "',");
sbValues.Append("'" + voyageList[i] + "'");
sbValues.Append("),");
}
//插入
string valuestr = sbValues.ToString().Substring(0, sbValues.ToString().Length - 1);
StringBuilder insSb = new StringBuilder();
insSb.Append("insert into Op_StatusCarrierDetails (GID,MBLNO,XIANGHAO,StatusMsg,StatusTime,LOC,Vessel,Voyage) ");
insSb.Append("values" + valuestr);
string sql = insSb.ToString();
db.ExecuteNonQuery(CommandType.Text, sql);
}
}
return true;
}
#endregion
#region 船公司相关-马士基
private static void GetMaerskDetailsIntoDBWithMBLNO ( string mblno ) {
string url = "https://my.maerskline.com/tracking/search?searchNumber="+mblno;
HttpItem item = new HttpItem()
{
URL = url,//URL 必需项
Method = "get"//URL 可选项 默认为Get
};
HttpHelper http = new HttpHelper();
HttpResult result1 = http.GetHtml(item);
string html = result1.Html;
IHtmlDocument document = new JumonyParser().Parse(html);
IEnumerable result = document.Find("table[class=table table-striped cols20 schedule-table]");
foreach (var r in result)
{
string xianghao = r.FindFirst("span[class=tracking_container_id]").InnerText().Trim();
List statusList = new List();
List timeList = new List();
List locList = new List();
List vesselList = new List();
List voyageList = new List();
IEnumerable details = r.NextElement().Find("table[class=row container-move past transport-plan-tbl cols20]");
foreach (var d in details)
{
string loc = d.PreviousElement().InnerText();
loc = loc == null ? "" : loc;
locList.Add(loc);
IEnumerable tds = d.Find("td");
int index = 0;
foreach (var td in tds)
{
//时间
if (index==2)
{
string timestr = td.InnerText().Replace("\r\n"," ");
timestr = DateTime.Parse(timestr).ToString("yyyy-MM-dd HH:mm");
timeList.Add(timestr);
}
//状态、船名、航次
if (index==3)
{
string statusstr = td.InnerText().Replace("\r\n", " ");
string[] sArray = Regex.Split(statusstr, "Voyage No.:", RegexOptions.IgnoreCase);
if (sArray.Length >= 2)
{
string hangci = sArray[1].Trim();
string status = "Load on";
string chuanming = sArray[0].Replace("Load on", "").TrimStart().TrimEnd();
vesselList.Add(chuanming); ;
voyageList.Add(hangci);
statusList.Add(status);
}
else
{
vesselList.Add(""); ;
voyageList.Add("");
statusList.Add(sArray[0]);
}
}
index++;
}
}
//插入数据库
if (statusList.Count>0)
{
string delsql = "delete from Op_StatusCarrierDetails where mblno ='"+mblno+"' and xianghao = '"+xianghao+"'";
Database db = DatabaseFactory.CreateDatabase();
db.ExecuteNonQuery(CommandType.Text, delsql);
StringBuilder sbValues = new StringBuilder();
for (int i = 0; i < statusList.Count; i++)
{
sbValues.Append("(");
sbValues.Append("'" + Guid.NewGuid() + "',");
sbValues.Append("'" + mblno + "',");
sbValues.Append("'" + xianghao + "',");
sbValues.Append("'" + statusList[i] + "',");
sbValues.Append("'" + timeList[i] + "',");
sbValues.Append("'" + locList[i] + "',");
sbValues.Append("'" + vesselList[i] + "',");
sbValues.Append("'" + voyageList[i] + "'");
sbValues.Append("),");
}
//插入
string valuestr = sbValues.ToString().Substring(0, sbValues.ToString().Length - 1);
StringBuilder insSb = new StringBuilder();
insSb.Append("insert into Op_StatusCarrierDetails (GID,MBLNO,XIANGHAO,StatusMsg,StatusTime,LOC,Vessel,Voyage) ");
insSb.Append("values" + valuestr);
string sql = insSb.ToString();
db.ExecuteNonQuery(CommandType.Text, sql);
}
}
}
#endregion
private static bool DeleteStatusDetails(string bsno)
{
string sql = "delete from Op_StatusDetails where bsno = '"+bsno+"'";
Database db = DatabaseFactory.CreateDatabase();
bool rst= db.ExecuteNonQuery(CommandType.Text,sql)>0;
return rst;
}
private static string GetChuanGongSiWithMBLNO ( string mblno ) {
string sql = "select top 1 CARRIER from op_seae where mblno = '"+mblno+"'";
Database db = DatabaseFactory.CreateDatabase();
var cgs = db.ExecuteScalar(CommandType.Text,sql);
if (cgs==null)
{
return "";
}
else
{
return cgs.ToString();
}
}
}
}