using System; using System.Data; using System.Collections.Generic; using System.Text; using Microsoft.Practices.EnterpriseLibrary.Data; using DSWeb.Areas.CommMng.Models; using DSWeb.Areas.Dispatch.Models; namespace DSWeb.Dispatch.DAL { public class MsOpSeaeDAL { #region 查询 static public List GetSaleDataList(string shortName, string condition = null, string sort = null, string order = null, bool hasNoFee = true) { var strSql = new StringBuilder(); strSql.AppendLine("SELECT BSNO,VESSEL,VOYNO,MBLNO,INVNO,ETD,ATD,CNTRTOTAL,KGS,YARD,CNTRSEALNO,CUSTOMNO,ETA,SERVICECONTRACTNO,"); strSql.AppendLine("ISNULL((SELECT TOP 1 FACTRYNAME FROM OP_LETTER_TRUCK WHERE BSNO=S.BSNO ORDER BY TRUCKDATE),'') FACTORYADDR,"); strSql.AppendLine("ISNULL((SELECT TOP 1 CARGODATE FROM OP_LETTER_TRUCK WHERE BSNO=S.BSNO ORDER BY TRUCKDATE),'') CARGODATE,"); strSql.AppendLine("(SELECT COMPTIME FROM OP_STATUS WHERE BSNO=S.BSNO AND ISCOMP=1 and [status]='重箱返场') STA_ZXFC,"); strSql.AppendLine("(SELECT COMPTIME FROM OP_STATUS WHERE BSNO=S.BSNO AND ISCOMP=1 and [status]='海关放行') STA_HGFX,"); strSql.AppendLine("(SELECT SUM(AMOUNT) FROM CH_FEE WHERE FEETYPE=1 AND FEESTATUS IN (0,8,9) AND CUSTOMERNAME=S.CUSTOMERNAME AND BSNO=S.BSNO AND CURRENCY='RMB') AMOUNT_RMB,"); strSql.AppendLine("(SELECT SUM(AMOUNT) FROM CH_FEE WHERE FEETYPE=1 AND FEESTATUS IN (0,8,9) AND CUSTOMERNAME=S.CUSTOMERNAME AND BSNO=S.BSNO AND CURRENCY='USD') AMOUNT_USD,"); strSql.AppendLine("(select TOP 1 STATUS from OP_STATUS WHERE BSNO=S.BSNO ORDER BY INPUTTIME DESC) LAST_STATUS,"); strSql.AppendLine("ISNULL((SELECT TOP 1 COMPTIME from OP_STATUS WHERE BSNO=S.BSNO AND STATUS='装船出运'),ETD) KaiChuanRi"); strSql.AppendLine("FROM OP_SEAE S"); strSql.AppendLine($"WHERE CUSTOMERNAME='{shortName}'"); if (!hasNoFee) { strSql.AppendLine("AND (SELECT COUNT(1) FROM CH_FEE WHERE FEETYPE=1 AND FEESTATUS IN (0,8,9) AND CUSTOMERNAME=S.CUSTOMERNAME AND BSNO=S.BSNO)>0"); } if (!string.IsNullOrWhiteSpace(condition)) { var obj = new { VESSEL = "", VOYNO = "", MBLNO = "", INVNO = "", BSNO = "" }; var jsonObj = Newtonsoft.Json.JsonConvert.DeserializeAnonymousType(condition, obj); if (!string.IsNullOrWhiteSpace(jsonObj.VESSEL)) { strSql.Append($" and VESSEL like '%{jsonObj.VESSEL.Replace("'", "")}%'"); } if (!string.IsNullOrWhiteSpace(jsonObj.VOYNO)) { strSql.Append($" and VOYNO like '%{jsonObj.VOYNO.Replace("'", "")}%'"); } if (!string.IsNullOrWhiteSpace(jsonObj.MBLNO)) { strSql.Append($" and MBLNO='{jsonObj.MBLNO.Replace("'", "")}'"); } if (!string.IsNullOrWhiteSpace(jsonObj.INVNO)) { strSql.Append($" and INVNO='{jsonObj.INVNO.Replace("'", "")}'"); } if (!string.IsNullOrWhiteSpace(jsonObj.BSNO)) { strSql.Append($" and BSNO='{jsonObj.BSNO.Replace("'", "")}'"); } } if (!string.IsNullOrEmpty(sort)) { strSql.AppendLine($" order by {sort} {(string.IsNullOrWhiteSpace(order) ? string.Empty : order)}"); } else { strSql.AppendLine(" order by KaiChuanRi desc"); } return SetData(strSql); } private static List SetData(StringBuilder strSql) { var headList = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { OpSeaeModel data = new OpSeaeModel(); #region Set DB data to Object data.BSNO = Convert.ToString(reader["BSNO"]); data.VESSEL = Convert.ToString(reader["VESSEL"]); data.VOYNO = Convert.ToString(reader["VOYNO"]); data.MBLNO = Convert.ToString(reader["MBLNO"]); data.INVNO = Convert.ToString(reader["INVNO"]); data.CNTRTOTAL = Convert.ToString(reader["CNTRTOTAL"]); data.KGS = Convert.ToString(reader["KGS"]); data.YARD = Convert.ToString(reader["YARD"]); data.CNTRSEALNO = Convert.ToString(reader["CNTRSEALNO"]); data.CUSTOMNO = Convert.ToString(reader["CUSTOMNO"]); data.FACTORYADDR = Convert.ToString(reader["FACTORYADDR"]); data.CARGODATE = Convert.ToString(reader["CARGODATE"]); data.SERVICECONTRACTNO = Convert.ToString(reader["SERVICECONTRACTNO"]); data.LAST_STATUS = Convert.ToString(reader["LAST_STATUS"]); if (reader["STA_ZXFC"] != DBNull.Value) data.STA_ZXFC = Convert.ToString(reader["STA_ZXFC"]); if (reader["STA_HGFX"] != DBNull.Value) data.STA_HGFX = Convert.ToString(reader["STA_HGFX"]); if (reader["ETD"] != DBNull.Value) data.ETD = Convert.ToDateTime(reader["ETD"]).ToString("yyyy-MM-dd"); if (reader["ATD"] != DBNull.Value) data.ATD = Convert.ToDateTime(reader["ATD"]).ToString("yyyy-MM-dd"); if (reader["ETA"] != DBNull.Value) data.ETA = Convert.ToDateTime(reader["ETA"]).ToString("yyyy-MM-dd"); if (reader["AMOUNT_RMB"] != DBNull.Value) data.AMOUNT_RMB = Convert.ToDecimal(reader["AMOUNT_RMB"]); if (reader["AMOUNT_USD"] != DBNull.Value) data.AMOUNT_USD = Convert.ToDecimal(reader["AMOUNT_USD"]); if (reader["KaiChuanRi"] != DBNull.Value) data.KaiChuanRi = Convert.ToDateTime(reader["KaiChuanRi"]).ToString("yyyy-MM-dd"); #endregion headList.Add(data); } reader.Close(); } return headList; } #endregion #region 查询-报表 static public List GetReportDataList(string shortName, string condition = null, string sort = null, string order = null) { var strSql = new StringBuilder(); strSql.AppendLine("SELECT BSNO,MBLNO,INVNO,CNTRTOTAL,ACCDATE,"); strSql.AppendLine("ISNULL((SELECT TOP 1 FACTRYTIME FROM OP_LETTER_TRUCK WHERE BSNO=S.BSNO ORDER BY TRUCKDATE),'') FACTRYTIME,"); strSql.AppendLine("(SELECT SUM(AMOUNT) FROM CH_FEE WHERE FEETYPE=1 AND FEESTATUS IN (0,8,9) AND CUSTOMERNAME=S.CUSTOMERNAME AND BSNO=S.BSNO AND CURRENCY='RMB') AMOUNT_RMB,"); strSql.AppendLine("(SELECT SUM(AMOUNT) FROM CH_FEE WHERE FEETYPE=1 AND FEESTATUS IN (0,8,9) AND CUSTOMERNAME=S.CUSTOMERNAME AND BSNO=S.BSNO AND CURRENCY='USD') AMOUNT_USD,"); strSql.AppendLine("ISNULL((SELECT TOP 1 COMPTIME from OP_STATUS WHERE BSNO=S.BSNO AND STATUS='装船出运'),ETD) KaiChuanRi"); strSql.AppendLine("FROM OP_SEAE S"); strSql.AppendLine($"WHERE CUSTOMERNAME='{shortName}'"); if (!string.IsNullOrWhiteSpace(condition)) { DateTime? dtDef = DateTime.MinValue; var obj = new { ACCDATE_START = "", KaiChuanRi_START = dtDef, FACTRYTIME_START = dtDef, ACCDATE_END = "", KaiChuanRi_END = dtDef, FACTRYTIME_END = dtDef }; var jsonObj = Newtonsoft.Json.JsonConvert.DeserializeAnonymousType(condition, obj); if (!string.IsNullOrEmpty(jsonObj.ACCDATE_START)) { strSql.Append($" and ACCDATE >= '{jsonObj.ACCDATE_START}'"); } if (!string.IsNullOrEmpty(jsonObj.ACCDATE_END)) { strSql.Append($" and ACCDATE <= '{jsonObj.ACCDATE_END}'"); } //查询结果中,开船日可能是实际开船日,也可能是录入的开船日期,条件筛选时,只按照实际开船日期筛选 if (jsonObj.KaiChuanRi_START.HasValue && jsonObj.KaiChuanRi_START != DateTime.MinValue) { strSql.Append($" and (SELECT TOP 1 COMPTIME from OP_STATUS WHERE BSNO=S.BSNO AND STATUS='装船出运') >= '{jsonObj.KaiChuanRi_START.Value.ToString("yyyy-MM-dd")}'"); } if (jsonObj.KaiChuanRi_END.HasValue && jsonObj.KaiChuanRi_END != DateTime.MinValue) { strSql.Append($" and (SELECT TOP 1 COMPTIME from OP_STATUS WHERE BSNO=S.BSNO AND STATUS='装船出运') < '{jsonObj.KaiChuanRi_END.Value.AddDays(1).ToString("yyyy-MM-dd")}'"); } if (jsonObj.FACTRYTIME_START.HasValue && jsonObj.FACTRYTIME_START != DateTime.MinValue) { strSql.Append($" and (SELECT TOP 1 FACTRYTIME FROM OP_LETTER_TRUCK WHERE BSNO=S.BSNO ORDER BY TRUCKDATE) >= '{jsonObj.FACTRYTIME_START.Value.ToString("yyyy-MM-dd")}'"); } if (jsonObj.FACTRYTIME_END.HasValue && jsonObj.FACTRYTIME_END != DateTime.MinValue) { strSql.Append($" and (SELECT TOP 1 FACTRYTIME FROM OP_LETTER_TRUCK WHERE BSNO=S.BSNO ORDER BY TRUCKDATE) < '{jsonObj.FACTRYTIME_END.Value.AddDays(1).ToString("yyyy-MM-dd")}'"); } } if (!string.IsNullOrEmpty(sort)) { strSql.AppendLine($" order by {sort} {(string.IsNullOrWhiteSpace(order) ? string.Empty : order)}"); } else { strSql.AppendLine(" order by KaiChuanRi desc"); } return SetDataReport(strSql, shortName); } private static List SetDataReport(StringBuilder strSql, string shortName) { var headList = new List(); Database db = DatabaseFactory.CreateDatabase(); var dsOpSeae = db.ExecuteDataSet(CommandType.Text, strSql.ToString()); foreach (DataRow dr in dsOpSeae.Tables[0].Rows) { OpSeaeReportModel data = new OpSeaeReportModel(); #region Set DB data to Object data.BSNO = Convert.ToString(dr["BSNO"]); data.MBLNO = Convert.ToString(dr["MBLNO"]); data.INVNO = Convert.ToString(dr["INVNO"]); data.CNTRTOTAL = Convert.ToString(dr["CNTRTOTAL"]); data.ACCDATE = Convert.ToString(dr["ACCDATE"]); if (dr["AMOUNT_RMB"] != DBNull.Value) data.AMOUNT_RMB = Convert.ToDecimal(dr["AMOUNT_RMB"]); if (dr["AMOUNT_USD"] != DBNull.Value) data.AMOUNT_USD = Convert.ToDecimal(dr["AMOUNT_USD"]); if (dr["KaiChuanRi"] != DBNull.Value) data.KaiChuanRi = Convert.ToDateTime(dr["KaiChuanRi"]).ToString("yyyy-MM-dd"); if (dr["FACTRYTIME"] != DBNull.Value && !string.IsNullOrEmpty(dr["FACTRYTIME"].ToString())) data.FACTRYTIME = Convert.ToDateTime(dr["FACTRYTIME"]).ToString("yyyy-MM-dd"); #endregion headList.Add(data); data.FeeList = GetReportFeeList(shortName, data.BSNO); } return headList; } #region 查询费用数据 public static List GetReportFeeList(string shortName, string BSNO) { var strSql = new StringBuilder(); strSql.AppendLine("SELECT FEENAME,UNIT,UNITPRICE,QUANTITY,AMOUNT,CURRENCY,EXCHANGERATE"); strSql.AppendLine("FROM CH_FEE"); strSql.AppendLine($"WHERE FEETYPE=1 AND FEESTATUS IN (0,8,9) AND CUSTOMERNAME='{shortName}' AND BSNO='{BSNO}'"); strSql.AppendLine(" ORDER BY SORT,LINENUM "); return SetDataFee(strSql); } private static List SetDataFee(StringBuilder strSql) { var bodyList = new List(); Database db = DatabaseFactory.CreateDatabase(); using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString())) { while (reader.Read()) { ChFeeModel data = new ChFeeModel(); #region Set DB data to Object data.FeeName = Convert.ToString(reader["FeeName"]); data.Unit = Convert.ToString(reader["Unit"]); data.UnitPrice = Convert.ToString(reader["UnitPrice"]); data.Quantity = Convert.ToString(reader["Quantity"]); data.Amount = Convert.ToString(reader["Amount"]); data.Currency = Convert.ToString(reader["Currency"]); data.ExChangerate = Convert.ToString(reader["ExChangerate"]); #endregion bodyList.Add(data); } reader.Close(); } return bodyList; } #endregion #endregion public static OpSeaeModel GetOneByBSNO(string bsno) { var strSql = new StringBuilder(); strSql.AppendLine("SELECT BSNO,VESSEL,VOYNO,MBLNO,INVNO,ETD,ATD,CNTRTOTAL,KGS,YARD,CNTRSEALNO,CUSTOMNO,"); strSql.AppendLine("(SELECT dbo.Fun_JoinFactoryAddr(S.BSNO)) FACTORYADDR,"); strSql.AppendLine("ISNULL((SELECT TOP 1 CARGODATE FROM OP_LETTER_TRUCK WHERE BSNO=S.BSNO ORDER BY TRUCKDATE DESC),'') CARGODATE,"); strSql.AppendLine("(SELECT COMPTIME FROM OP_STATUS WHERE BSNO=S.BSNO AND ISCOMP=1 and [status]='重箱返场') STA_ZXFC,"); strSql.AppendLine("(SELECT COMPTIME FROM OP_STATUS WHERE BSNO=S.BSNO AND ISCOMP=1 and [status]='海关放行') STA_HGFX"); strSql.AppendLine("FROM OP_SEAE S"); strSql.AppendLine($"WHERE BSNO='{bsno}'"); var list = SetData(strSql); if (list.Count > 0) { return list[0]; } return null; } } }