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.

552 lines
24 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 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 System.Text.RegularExpressions;
namespace DSWeb.Areas.MvcShipping.DAL
{
public class MsOpStatusNewDAL
{
/// <summary>
/// 创建/更新 状态树主表
/// 海运出口保存时只产生主状态、不产生集装箱状态
/// </summary>
/// <param name="bsno"></param>
/// <param name="oper">操作人</param>
/// <returns></returns>
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 + "',");
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 + "',");
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;
}
/// <summary>
/// 更新状态节点-主表
/// </summary>
/// <param name="bsno">业务编号</param>
/// <param name="mblno">主提单号</param>
/// <param name="statusNode">0已结单 1:已发入货通知 2已订舱 3已出号 4已提单确认 5:费用已提交</param>
/// <param name="statusTime">状态完成时间</param>
/// <returns></returns>
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 = '" + bsno + "'");
Database db = DatabaseFactory.CreateDatabase();
int flag = db.ExecuteNonQuery(CommandType.Text, sb.ToString());
return flag > 0;
}
/// <summary>
/// 更新状态节点-青岛港码头
/// </summary>
/// <returns></returns>
public bool UpdateQdportStatus (OpSeaeQDPortDataModel model,string bsno) {
if (!checkOpSeaeQDPortDataModel(model))
{
return false;
}
//如果没有主表数据,创建主表数据
if (!CheckOpSeaeHasStatusTree(bsno))
{
SaveStatusTree(bsno, "System");
}
StringBuilder sb = new StringBuilder();
List<StatusDetails> listDetails = new List<StatusDetails>();
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);
}
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 = '" + 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;
}
/// <summary>
/// 获取状态数据
/// </summary>
/// <param name="strCondition"></param>
/// <returns></returns>
public static List<MsOpStatusNew> 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<MsOpStatusNew> SetStatusData ( StringBuilder strSql )
{
var list = new List<MsOpStatusNew>();
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;
}
/// <summary>
/// 获取OpSeae信息
/// </summary>
/// <param name="mblno"></param>
/// <returns></returns>
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<OpSeaeCtnModel> list = new List<OpSeaeCtnModel>();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, sql2))
{
while (reader.Read())
{
}
reader.Close();
}
model.CtnList = list;
return model;
}
/// <summary>
/// 判断是否已有状态树
/// </summary>
/// <param name="mblno"></param>
/// <returns></returns>
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;
}
}
/// <summary>
/// 判断是否已有明细状态
/// </summary>
/// <param name="mblno"></param>
/// <param name="xianghao"></param>
/// <returns></returns>
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 = '" + 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;
return true;
}
public static List<OpCMAStatus> 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<OpCMAStatus> list = new List<OpCMAStatus>();
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<OpCMAStatus> 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<OpCMAStatus> list = new List<OpCMAStatus>();
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;
}
#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();
}
}
}
}