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.
DS7/DSWeb/Areas/MvcShipping/DAL/MsOpCtnFeeSearchDAL/MsOpCtnFeeSearchDAL.cs

591 lines
24 KiB
C#

using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DSWeb.Areas.CommMng.Models;
using HcUtility.Comm;
using DSWeb.Areas.CommMng.DAL;
using DSWeb.MvcShipping.Helper;
using DSWeb.MvcShipping.Models.MsStevedores;
using System.Transactions;
using System.Data.SqlClient;
using DSWeb.DataAccess;
using DSWeb.Areas.MvcShipping.Models.MsOpCtnFeeSearch;
using DSWeb.MvcShipping.Models.MsOpCtnStatus;
using System.Linq;
using DSWeb.Areas.MvcShipping.Models.MsOpCtnFeeModelModel;
using DSWeb.Areas.MvcShipping.DAL.MsOpCtnFeeModelDAL;
namespace DSWeb.Areas.MvcShipping.DAL.MsOpCtnFeeSearchDAL
{
public class MsOpCtnFeeSearchDAL
{
public static List<MsOpCtnFee> GetFeeDataList ( string condition )
{
string sql = @" select *,MBLNO= (
STUFF(
(SELECT '/' + MBLNO
FROM Op_Ctn_FeeDetails
WHERE FeeNo = Op_Ctn_Fee.FeeNo
FOR xml path('')
),1,1,''
)
) from Op_Ctn_Fee where 1=1 ";
if (condition != "" && condition != null)
{
sql = sql + " and " + condition;
}
List<MsOpCtnFee> list = new List<MsOpCtnFee>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, sql.ToString()))
{
while (reader.Read())
{
MsOpCtnFee f = new MsOpCtnFee();
f.FeeNo = reader["FeeNo"].ToString();
f.Amount = reader["Amount"].ToString();
f.GID = reader["GID"].ToString();
f.MBLNO = reader["MBLNO"].ToString();
f.PayCust = reader["PayCust"].ToString();
list.Add(f);
}
}
return list;
}
public static List<MsOpCtnFeeSearch> GetDataList ( string condition )
{
List<MsCtnStatus> listStatus = MsOpCtnStatusDAL.GetDataStatusList(condition,"order by blno,box_no,cctime,jctime");
List<MsOpCtnFeeSearch> list = new List<MsOpCtnFeeSearch>();
string blnotemp = "";
foreach (var item in listStatus)
{
//提单号
if (blnotemp!=item.blno)
{
blnotemp = item.blno;
//箱号
string xianghaotemp = "";
foreach (var item2 in listStatus.FindAll(s=>s.blno==blnotemp))
{
if (xianghaotemp!=item2.box_no)
{
xianghaotemp = item2.box_no;
//遍历同提单号、同箱号的数据
//获取最大的时间cctime
DateTime cctimetemp = DateTime.MinValue;
string cctimestr = "";
foreach (var item3 in listStatus.FindAll(s=>s.blno==blnotemp&&s.box_no==xianghaotemp))
{
if (item3.cctime=="")
{
continue;
}
var cctime = DateTime.Parse(item3.cctime);
if (cctime>=cctimetemp)
{
cctimestr = item3.cctime;
cctimetemp = cctime;
}
}
//遍历同提单号、同箱号的数据
//获取最大的时间cctime
DateTime jctimetemp = DateTime.MinValue;
string jctimestr = "";
foreach (var item3 in listStatus.FindAll(s => s.blno == blnotemp && s.box_no == xianghaotemp))
{
if (item3.jctime == "")
{
continue;
}
var jctime = DateTime.Parse(item3.jctime);
if (jctime >= jctimetemp)
{
jctimestr = item3.jctime;
jctimetemp = jctime;
}
}
//如果进场时间jctime>出场时间cctime
//则去进场时间的最大的那一条数据
if (jctimetemp>cctimetemp)
{
var temp4 = listStatus.Find(s => s.blno == blnotemp && s.box_no == xianghaotemp && s.jctime == jctimestr);
MsOpCtnFeeSearch ms = new MsOpCtnFeeSearch();
ms.MBLNO = temp4.blno;
ms.XiangHao = temp4.box_no;
ms.ZhuangTai = temp4.jcyy;
ms.Time = temp4.jctime;
ms.HangCi = temp4.voyage;
ms.ChuanMing = temp4.vessel;
ms.ZhuangTai = temp4.jcyy;
ms.DuiChangMaTou = temp4.cz;
ms.XiangXing = temp4.cntrsize;
list.Add(ms);
}
else
{
var temp4 = listStatus.Find(s => s.blno == blnotemp && s.box_no == xianghaotemp && s.cctime == cctimestr);
MsOpCtnFeeSearch ms = new MsOpCtnFeeSearch();
ms.MBLNO = temp4.blno;
ms.XiangHao = temp4.box_no;
ms.ZhuangTai = temp4.jcyy;
ms.Time = temp4.cctime;
ms.HangCi = temp4.voyage;
ms.ChuanMing = temp4.vessel;
ms.ZhuangTai = temp4.jcyy;
ms.DuiChangMaTou = temp4.cz;
ms.XiangXing = temp4.cntrsize;
list.Add(ms);
}
}
}
}
}
return list;
}
//GetCtnDataList
public static List<MsOpCtnFeeSearch> GetCtnDataList ( string condition ="")
{
string sql = "SELECT DISTINCT blno,box_no,vessel,voyage from container_business where 1=1 ";
if (condition!=""&&condition!=null)
{
sql = sql + " " + condition;
}
List<MsOpCtnFeeSearch> list = new List<MsOpCtnFeeSearch>();
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, sql.ToString()))
{
while (reader.Read())
{
MsOpCtnFeeSearch f = new MsOpCtnFeeSearch();
f.MBLNO = reader["blno"].ToString();
f.XiangHao = reader["box_no"].ToString();
f.ChuanMing = reader["vessel"].ToString();
f.HangCi = reader["voyage"].ToString();
List<MsOpCtnFeeSearchDetails> listtemp = GetDetailsWithMBLNOAndXiangHao(f.MBLNO,f.XiangHao);
f.Amount = listtemp.FindAll(l=>l.DuiZhangRiQi==null || l.DuiZhangRiQi=="").Sum(s=>float.Parse(s.FeiYong)).ToString();
var duizhangriqiFrom = listtemp.Find(l => l.ZhuangTai == "空箱出场");
if (duizhangriqiFrom!=null&&duizhangriqiFrom.ToString()!="")
{
f.DuiZhangRiQiFrom = duizhangriqiFrom.Time;
}
else
{
f.DuiZhangRiQiFrom = "";
}
list.Add(f);
}
}
return list;
}
public static List<MsOpCtnFeeSearchDetails> GetDetailsWithMBLNOAndXiangHao (string mblno,string xianghao) {
string sql = "select (select cntype from container_info where cntrno=container_business.box_no) xiangxing,* from container_business where blno = '" + mblno + "' and box_no = '" + xianghao + "' order by opertime";
Database db = DatabaseFactory.CreateDatabase();
List<MsOpCtnFeeSearch> listMain = new List<MsOpCtnFeeSearch>();
using (IDataReader reader = db.ExecuteReader(CommandType.Text, sql.ToString()))
{
while (reader.Read())
{
MsOpCtnFeeSearch f = new MsOpCtnFeeSearch();
f.MBLNO = reader["blno"].ToString();
f.XiangHao = reader["box_no"].ToString();
f.ChuanMing = reader["vessel"].ToString();
f.HangCi = reader["voyage"].ToString();
f.ZhuangTai = reader["jcyy"].ToString();
f.CcTime = reader["cctime"].ToString();
f.JcTime = reader["jctime"].ToString();
f.DuiChangMaTou = reader["cz"].ToString();
f.XiangXing = reader["xiangxing"].ToString();
f.KaiHangRi = reader["etd"].ToString();
f.DaoGangRi = reader["reachtime"].ToString();
f.DuiZhangRiQi = reader["duizhangriqi"].ToString();
listMain.Add(f);
}
}
List<MsOpCtnFeeSearchDetails> list = new List<MsOpCtnFeeSearchDetails>();
foreach (var item in listMain)
{
MsOpCtnFeeSearchDetails d = new MsOpCtnFeeSearchDetails();
d.ZhuangTai = item.ZhuangTai;
string time = "";
if (item.CcTime==""||item.CcTime==null)
{
time = item.JcTime;
}
else
{
time = item.CcTime;
}
d.MBLNO = item.MBLNO;
d.XiangHao = item.XiangHao;
d.Time = time;
d.DiDian = item.DuiChangMaTou;
d.XiangXing = item.XiangXing;
d.FeiYong = calcFee(d,listMain).ToString();
d.YongXiangTianShu = calcDays(d,listMain).ToString();
d.DuiZhangRiQi = item.DuiZhangRiQi;
list.Add(d);
}
return list;
}
/// <summary>
/// 计算费用
/// </summary>
/// <returns></returns>
private static float calcFee ( MsOpCtnFeeSearchDetails d, List<MsOpCtnFeeSearch> m )
{
string code = "";
//获取费率模板
//如果是空箱还场、则取卸船码头的费用模板
if (d.ZhuangTai=="空箱还场")
{
var port = m.Find(f=>f.ZhuangTai=="卸船");
if (port!=null)
{
code = port.DuiChangMaTou;
}
}
else
{
code = GetDiDianCodeWithDiDian(d.DiDian);
}
string wheresql = @" MainGID = (select GID from Op_Ctn_FEEMODEL where PortAndYardCode = '" + code + "') and XiangXing = '"+d.XiangXing+"'";
List<MsOpCtnFeeDetailsModel> modelList = MsOpCtnFeeModelDAL.MsOpCtnFeeModelDAL.GetDataDetailsList(wheresql);
//用箱天数
int days = calcDays(d, m);
//箱使费
bool isFreeDayCalc =false;
switch (d.ZhuangTai)
{
case "卸船":
isFreeDayCalc=true;
break;
case "出闸口":
isFreeDayCalc=true;
break;
case "空箱还场":
isFreeDayCalc=true;
break;
default:
break;
}
float fee = GetFeeWithTimeDiffAndFeeModels(days,modelList,isFreeDayCalc,d.ZhuangTai,d.MBLNO);
return fee;
}
/// <summary>
/// 计算用箱天数
/// </summary>
/// <param name="d"></param>
/// <param name="m"></param>
/// <returns></returns>
private static int calcDays ( MsOpCtnFeeSearchDetails d, List<MsOpCtnFeeSearch> m )
{
int days = 0;
string time1 = "";
string time2 = "";
try
{
switch (d.ZhuangTai)
{
case "空箱出场":
break;
case "装船":
//空箱出场-装船 天数差
time1 = m.Find(f => f.ZhuangTai == "空箱出场").CcTime.ToString();
time2 = m.Find(f => f.ZhuangTai == "装船").JcTime.ToString();
days = GetTimeDiff(time1, time2);
break;
case "卸船":
break;
case "出闸口":
time1 = m.Find(f => f.ZhuangTai == "卸船").JcTime.ToString();
time2 = m.Find(f => f.ZhuangTai == "出闸口").CcTime.ToString();
days = GetTimeDiff(time1, time2);
break;
case "空箱还场":
var temp = m.Find(f => f.ZhuangTai == "出闸口");
if (temp == null)
{
//无闸口信息
time1 = m.Find(f => f.ZhuangTai == "卸船").JcTime.ToString();
time2 = m.Find(f => f.ZhuangTai == "空箱还场").JcTime.ToString();
days = GetTimeDiff(time1, time2);
}
else
{
//有闸口信息
time1 = m.Find(f => f.ZhuangTai == "出闸口").CcTime.ToString();
time2 = m.Find(f => f.ZhuangTai == "空箱还场").JcTime.ToString();
days = GetTimeDiff(time1, time2);
}
break;
default:
break;
}
}
catch (Exception)
{
days = 0;
}
return days;
}
private static string GetDiDianCodeWithDiDian ( string d)
{
//场站
string sql1 = "select codename from info_client where shortname = '"+d+"' ";
Database db = DatabaseFactory.CreateDatabase();
var code = db.ExecuteScalar(CommandType.Text,sql1);
if (code==null||code.ToString()=="")
{
//港口
return d;
}
return code.ToString();
}
//获取天数差
private static int GetTimeDiff (string d1str,string d2str) {
try
{
DateTime d1 = DateTime.Parse(d1str);
DateTime d2 = DateTime.Parse(d2str);
TimeSpan ts1 = new TimeSpan(d1.Ticks);
TimeSpan ts2 = new TimeSpan(d2.Ticks);
TimeSpan ts3 = ts1.Subtract(ts2).Duration();
return Convert.ToInt32(ts3.TotalDays);
}
catch (Exception)
{
return 0;
throw;
}
}
//根据模板、提单号、天数获取费用
private static float GetFeeWithTimeDiffAndFeeModels ( int timeDiff, List<MsOpCtnFeeDetailsModel> modelList,bool checkShipingBoxFreeDays,string StatusStr,string blno="")
{
//剩余的天数
int residueDays = timeDiff;
float fee = 0f;
//查询shippingbox订舱时填写的免箱期
int FreeDays2 = 0;
int FreeDays1 = GetFreeDaysWithBLNO(blno,out FreeDays2);
foreach (var item in modelList)
{
if (checkShipingBoxFreeDays)
{
if (item.JiFeiDengJi == "1")
{
if (StatusStr=="卸船")
{
if (FreeDays1 > Convert.ToInt32(item.JiFeiQuJian))
{
residueDays -= (FreeDays1 - Convert.ToInt32(item.JiFeiQuJian));
}
}
if (StatusStr=="出闸口")
{
if (FreeDays2 > Convert.ToInt32(item.JiFeiQuJian))
{
residueDays -= (FreeDays2 - Convert.ToInt32(item.JiFeiQuJian));
}
}
}
}
if (residueDays<Convert.ToInt32(item.JiFeiQuJian))
{
fee += float.Parse(item.JiFeiDanJia) * (Math.Abs(residueDays));
}
else
{
residueDays = residueDays - Convert.ToInt32(item.JiFeiQuJian);
fee += float.Parse(item.JiFeiDanJia) * Convert.ToInt32(item.JiFeiQuJian);
}
}
return fee;
}
public static bool Save ( MsOpCtnFee fee,List<MsOpCtnFeeSearch> detailsList, string duizhangriqi, string opstatus )
{
if (fee.GID == "" || fee.GID == null || opstatus=="add")
{
//新建
if (true)
{
string newguid = Guid.NewGuid().ToString();
string feeno = GetFeeNo();
StringBuilder sbmain = new StringBuilder();
sbmain.Append("insert into Op_Ctn_Fee values(");
sbmain.Append("'" + newguid + "','" + feeno + "',0,'"+fee.PayCust+"')");
Database db = DatabaseFactory.CreateDatabase();
db.ExecuteNonQuery(CommandType.Text,sbmain.ToString());
//更新集装箱动态表
string mblnos = "";
string xianghaos = "";
foreach (var item in detailsList)
{
mblnos += "'" + item.MBLNO + "',";
xianghaos += "'"+item.XiangHao+"',";
}
mblnos=mblnos.Length>0?mblnos.Substring(0,mblnos.Length-1):"";
xianghaos = xianghaos.Length > 0 ? xianghaos.Substring(0, xianghaos.Length - 1) : "";
if (mblnos==""||xianghaos=="")
{
return false;
}
else
{
StringBuilder sbDetails = new StringBuilder();
sbDetails.Append("update container_business ");
sbDetails.Append("set duizhangriqi = '" + duizhangriqi + "'");
sbDetails.Append(" where blno in (" + mblnos + ") and box_no in (" + xianghaos + ") and (cctime<'" + duizhangriqi + "' or jctime < '"+duizhangriqi+"')");
bool rst = db.ExecuteNonQuery(CommandType.Text,sbDetails.ToString())>0;
return rst;
}
}
}
else
{
string[] codename = fee.PayCust.Split('-');
string code = "";
if (codename.Length > 1)
{
code = codename[0];
}
else
{
code = fee.PayCust;
}
StringBuilder sb = new StringBuilder();
sb.Append("update Op_Ctn_Fee ");
sb.Append("set PayCust = '" + code + "',");
sb.Append(" FeeNo = '" + fee.FeeNo + "'");
sb.Append("where GID = '" + fee.GID + "'");
Database db = DatabaseFactory.CreateDatabase();
bool rst = db.ExecuteNonQuery(CommandType.Text,sb.ToString())>0;
return rst;
}
}
private static string GetFeeNo ( ) {
DateTime now = DateTime.Now;
string name = "CTNDZ" + now.Year.ToString() + now.Month.ToString() + now.Day
+ now.Hour.ToString() + now.Minute.ToString() + now.Second.ToString();
return name;
}
private static int GetFreeDaysWithBLNO (string BLNO,out int FreeDays2)
{
FreeDays2 = 0;
if (BLNO=="")
{
return 0;
}
try
{
Database db = DatabaseFactory.CreateDatabase("SHIPPINGBOX");
string sql = "select FREEDAYS1 from BookingCargoSpace where MBLNO = '" + BLNO + "'";
int freedays1 = Convert.ToInt32(db.ExecuteScalar(CommandType.Text, sql));
string sql2 = "select FREEDAYS2 from BookingCargoSpace where MBLNO = '" + BLNO + "'";
int freedays2 = Convert.ToInt32(db.ExecuteScalar(CommandType.Text, sql2));
FreeDays2 = freedays2;
return freedays1;
}
catch (Exception)
{
return 0;
}
}
public static float GetTotalRealWithMsOpCtnFeeSearchList (List<MsOpCtnFeeSearch> list)
{
float total = 0f;
foreach (var item in list)
{
float c = GetRealFeeWithMBLNOAndXiangHao(item.MBLNO, item.XiangHao);
total += c;
}
return total;
}
/// <summary>
/// 根据时间、提单号、箱号,获取实际入账的费用
/// </summary>
/// <param name="timePoint"></param>
/// <param name="MBLNO"></param>
/// <returns></returns>
public static float GetRealFeeWithMBLNOAndXiangHao (string MBLNO,string XiangHao )
{
string timeStr = DateTime.Now.ToString("yyyy-MM-dd");
List<MsOpCtnFeeSearchDetails> detailList = GetDetailsWithMBLNOAndXiangHao(MBLNO, XiangHao);
float sum = detailList.Sum(d => float.Parse(d.FeiYong));
float lastTotal = 0f;
foreach (var item in detailList)
{
lastTotal += GetTotalFeeDetails(item, timeStr);
}
return sum-lastTotal;
}
private static float GetTotalFeeDetails (MsOpCtnFeeSearchDetails d,string timeStr) {
//获取已经对账的数额
StringBuilder sb = new StringBuilder();
sb.Append("select sum(duizhangjine) from Op_Ctn_FeeDuiZhuang ");
sb.Append(" where ");
sb.Append(" duizhangtime < '" + timeStr + "'");
sb.Append("and mblno = '"+d.MBLNO+"' ");
sb.Append("and xianghao = '"+d.XiangHao+"'");
Database db = DatabaseFactory.CreateDatabase();
var rst = db.ExecuteScalar(CommandType.Text, sb.ToString());
float total = float.Parse(rst==DBNull.Value?"0":rst.ToString());
return total;
}
}
}