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("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.Append($" and (SELECT TOP 1 COMPTIME from OP_STATUS WHERE BSNO=S.BSNO AND STATUS='装船出运') >= '{jsonObj.KaiChuanRi_START.Value.ToString("yyyy-MM-dd")}'");
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")}'");
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")}'");
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)}");
@"if (select bsno from op_status where bsno='"+_bill.BookingId+"' and STATUS='"+_bill.Status+"') is null insert into op_status(ST_ID,BSNO,STATUS,ISCOMP,COMPTIME,INPUTTIME,STTYPE) values (NEWID(),'"+_bill.BookingId+"','"+_bill.Status+"',1,'"+Convert.ToDateTime(_bill.OPTime).ToString()+"','"+DateTime.Now.ToString()+"','2') ");
db.ExecuteNonQuery(cmdUpdate,tran);
}
else
{
varoptime="";
if(_bill.detail!=null&&_bill.detail.Count!=0)
{
optime=_bill.detail[0].OPTime;
}
varcmdUpdate=db.GetSqlStringCommand(
@"if (select bsno from op_status where bsno='"+_bill.BookingId+"' and STATUS='"+_bill.Status+"') is null insert into op_status(ST_ID,BSNO,STATUS,ISCOMP,COMPTIME,INPUTTIME,STTYPE) values (NEWID(),'"+_bill.BookingId+"','"+_bill.Status+"',1,'"+Convert.ToDateTime(optime).ToString()+"','"+DateTime.Now.ToString()+"','2') ");