using System ;
using System.Data ;
using System.Collections.Generic ;
using System.Text ;
using DSWeb.MvcShipping.Models.MsCodeFeeTemplateAgent ;
using Microsoft.Practices.EnterpriseLibrary.Data ;
using DSWeb.Areas.CommMng.Models ;
using HcUtility.Comm ;
namespace DSWeb.MvcShipping.DAL.MsCodeFeeTemplateAgent
{
public class MsCodeFeeTempldateAgentDAL
{
#region Inquery DataList
static public List < CodeFeeTemplateAgent > GetDataList ( string strCondition , string userid , string usercode , string orgcode , string sort = null )
{
var rangstr = GetRangDAStr ( "index" , userid , usercode , orgcode ) ;
if ( ! string . IsNullOrEmpty ( rangstr ) )
{
if ( ! string . IsNullOrEmpty ( strCondition ) )
{
strCondition = strCondition + " and ((" + rangstr + ") or ISPUBLIC='是')" ;
}
else
{
strCondition = " ((" + rangstr + ") or ISPUBLIC='是')" ;
}
}
var strSql = new StringBuilder ( ) ;
strSql . Append ( "SELECT " ) ;
strSql . Append ( "GID,NAME,[DESCRIPTION],CREATEUSER,CREATETIME" ) ;
strSql . Append ( ",MODIFIEDUSER,MODIFIEDTIME,REMARK,CORPID" ) ;
strSql . Append ( ",(select ShowName from [user] where GID=code_fee_template_agent.CREATEUSER) as CREATEUSERREF" ) ;
strSql . Append ( ",ISPUBLIC" ) ;
strSql . Append ( " from code_fee_template_agent " ) ;
if ( ! string . IsNullOrEmpty ( strCondition ) )
{
strSql . Append ( " where " + strCondition ) ;
}
var sortstring = DatasetSort . Getsortstring ( sort ) ;
if ( ! string . IsNullOrEmpty ( sortstring ) )
{
strSql . Append ( " order by " + sortstring ) ;
}
else {
strSql . Append ( " order by NAME" ) ;
}
return SetData ( strSql ) ;
}
static public CodeFeeTemplateAgent GetData ( string condition , string userid , string usercode , string orgcode )
{
CodeFeeTemplateAgent data = null ;
var list = GetDataList ( condition , userid , usercode , orgcode ) ;
if ( list . Count > 0 )
data = list [ 0 ] ;
if ( data = = null )
{
data = new CodeFeeTemplateAgent ( ) ;
data . CREATEUSER = userid ;
data . CORPID = orgcode ;
}
return data ;
}
private static List < CodeFeeTemplateAgent > SetData ( StringBuilder strSql )
{
var headList = new List < CodeFeeTemplateAgent > ( ) ;
Database db = DatabaseFactory . CreateDatabase ( ) ;
using ( IDataReader reader = db . ExecuteReader ( CommandType . Text , strSql . ToString ( ) ) )
{
while ( reader . Read ( ) )
{
CodeFeeTemplateAgent data = new CodeFeeTemplateAgent ( ) ;
#region Set DB data to Object
data . GID = Convert . ToString ( reader [ "GID" ] ) ;
data . NAME = Convert . ToString ( reader [ "NAME" ] ) ;
data . DESCRIPTION = Convert . ToString ( reader [ "DESCRIPTION" ] ) ;
data . CORPID = Convert . ToString ( reader [ "CORPID" ] ) ;
data . REMARK = Convert . ToString ( reader [ "REMARK" ] ) ;
data . CREATEUSER = Convert . ToString ( reader [ "CREATEUSER" ] ) ;
data . CREATEUSERREF = Convert . ToString ( reader [ "CREATEUSERREF" ] ) ;
if ( reader [ "CREATETIME" ] ! = DBNull . Value )
data . CREATETIME = Convert . ToDateTime ( reader [ "CREATETIME" ] ) ;
data . ISPUBLIC = Convert . ToString ( reader [ "ISPUBLIC" ] ) ;
# endregion
headList . Add ( data ) ;
}
reader . Close ( ) ;
}
return headList ;
}
# endregion
#region 明细表
static public List < CodeFeeTemplateAgentDetail > GetBodyList ( string strCondition , string sort = null )
{
var strSql = new StringBuilder ( ) ;
strSql . Append ( "SELECT " ) ;
strSql . Append ( "GID,TEMPLATEID,FEENAME,FEEENAME" ) ;
strSql . Append ( ",CURRENCY,EXCHANGERATE,DRPP,DRCC,CRPP,CRCC,FZPR,AMOUNTDR,AMOUNTCR,UNIT,UNITPRICE,QUANTITY,FEEDESCRIPTION,REMARK,SORT,CREATEUSER,CREATETIME" ) ;
strSql . Append ( " from code_fee_template_agentdetail " ) ;
if ( ! string . IsNullOrEmpty ( strCondition ) )
{
strSql . Append ( " where " + strCondition ) ;
}
var sortstring = DatasetSort . Getsortstring ( sort ) ;
if ( ! string . IsNullOrEmpty ( sortstring ) )
{
strSql . Append ( " order by " + sortstring ) ;
}
else
{
strSql . Append ( " order by SORT " ) ;
}
return SetBodyData ( strSql ) ;
}
private static List < CodeFeeTemplateAgentDetail > SetBodyData ( StringBuilder strSql )
{
var bodyList = new List < CodeFeeTemplateAgentDetail > ( ) ;
Database db = DatabaseFactory . CreateDatabase ( ) ;
using ( IDataReader reader = db . ExecuteReader ( CommandType . Text , strSql . ToString ( ) ) )
{
while ( reader . Read ( ) )
{
CodeFeeTemplateAgentDetail data = new CodeFeeTemplateAgentDetail ( ) ;
#region Set DB data to Object
data . GID = Convert . ToString ( reader [ "GID" ] ) ;
data . TEMPLATEID = Convert . ToString ( reader [ "TEMPLATEID" ] ) ;
data . FEENAME = Convert . ToString ( reader [ "FEENAME" ] ) ;
data . FEEENAME = Convert . ToString ( reader [ "FEEENAME" ] ) ;
data . CURRENCY = Convert . ToString ( reader [ "CURRENCY" ] ) ;
if ( reader [ "EXCHANGERATE" ] ! = DBNull . Value )
data . EXCHANGERATE = Convert . ToDecimal ( reader [ "EXCHANGERATE" ] ) ;
if ( reader [ "DRPP" ] ! = DBNull . Value )
data . DRPP = Convert . ToDecimal ( reader [ "DRPP" ] ) ;
if ( reader [ "DRCC" ] ! = DBNull . Value )
data . DRCC = Convert . ToDecimal ( reader [ "DRCC" ] ) ;
if ( reader [ "CRPP" ] ! = DBNull . Value )
data . CRPP = Convert . ToDecimal ( reader [ "CRPP" ] ) ;
if ( reader [ "CRCC" ] ! = DBNull . Value )
data . CRCC = Convert . ToDecimal ( reader [ "CRCC" ] ) ;
if ( reader [ "AMOUNTDR" ] ! = DBNull . Value )
data . AMOUNTDR = Convert . ToDecimal ( reader [ "AMOUNTDR" ] ) ;
if ( reader [ "AMOUNTCR" ] ! = DBNull . Value )
data . AMOUNTCR = Convert . ToDecimal ( reader [ "AMOUNTCR" ] ) ;
if ( reader [ "UNITPRICE" ] ! = DBNull . Value )
data . UNITPRICE = Convert . ToDecimal ( reader [ "UNITPRICE" ] ) ;
if ( reader [ "QUANTITY" ] ! = DBNull . Value )
data . QUANTITY = Convert . ToDecimal ( reader [ "QUANTITY" ] ) ;
data . UNIT = Convert . ToString ( reader [ "UNIT" ] ) ;
data . CURRENCY = Convert . ToString ( reader [ "CURRENCY" ] ) ;
data . FEEDESCRIPTION = Convert . ToString ( reader [ "FEEDESCRIPTION" ] ) ;
data . REMARK = Convert . ToString ( reader [ "REMARK" ] ) ;
if ( reader [ "SORT" ] ! = DBNull . Value )
data . SORT = Convert . ToInt16 ( reader [ "SORT" ] ) ;
data . CREATEUSER = Convert . ToString ( reader [ "CREATEUSER" ] ) ;
data . CREATETIME = Convert . ToString ( reader [ "CREATETIME" ] ) ;
# endregion
bodyList . Add ( data ) ;
}
reader . Close ( ) ;
}
return bodyList ;
}
public static DBResult SaveDetail ( CodeFeeTemplateAgent headData , List < CodeFeeTemplateAgentDetail > bodyList , string userid )
{
var result = new DBResult ( ) ;
Database db = DatabaseFactory . CreateDatabase ( ) ;
using ( var conn = db . CreateConnection ( ) )
{
conn . Open ( ) ;
var tran = conn . BeginTransaction ( ) ;
try
{
var cmdInsert =
db . GetSqlStringCommand (
@ "insert into code_fee_template_agentdetail (GID,TEMPLATEID,FEENAME,FEEENAME,CURRENCY,EXCHANGERATE,DRPP,DRCC,CRPP,CRCC,AMOUNTDR,AMOUNTCR,UNITPRICE,UNIT,QUANTITY,FEEDESCRIPTION,REMARK,SORT,CREATEUSER,CREATETIME)
values ( @GID , @TEMPLATEID , @FEENAME , @FEEENAME , @CURRENCY , @EXCHANGERATE , @DRPP , @DRCC , @CRPP , @CRCC , @AMOUNTDR , @AMOUNTCR , @UNITPRICE , @UNIT , @QUANTITY , @FEEDESCRIPTION , @REMARK , @SORT , @CREATEUSER , @CREATETIME ) ");
var cmdUpdate =
db . GetSqlStringCommand (
@ "update code_fee_template_agentdetail set FEENAME=@FEENAME,FEEENAME=@FEEENAME,CURRENCY=@CURRENCY,EXCHANGERATE=@EXCHANGERATE,DRPP=@DRPP,DRCC=@DRCC,CRPP=@CRPP,CRCC=@CRCC
, AMOUNTDR = @AMOUNTDR , AMOUNTCR = @AMOUNTCR , UNIT = @UNIT , UNITPRICE = @UNITPRICE , QUANTITY = @QUANTITY , FEEDESCRIPTION = @FEEDESCRIPTION , SORT = @SORT
WHERE GID = @GID ");
if ( bodyList ! = null )
{
foreach ( var enumValue in bodyList )
{
if ( enumValue . TEMPLATEID = = "*" )
{
cmdInsert . Parameters . Clear ( ) ;
db . AddInParameter ( cmdInsert , "@GID" , DbType . String , Guid . NewGuid ( ) . ToString ( ) ) ;
db . AddInParameter ( cmdInsert , "@TEMPLATEID" , DbType . String , headData . GID ) ;
db . AddInParameter ( cmdInsert , "@FEENAME" , DbType . String , enumValue . FEENAME ) ;
db . AddInParameter ( cmdInsert , "@FEEENAME" , DbType . String , enumValue . FEEENAME ) ;
db . AddInParameter ( cmdInsert , "@CURRENCY" , DbType . String , enumValue . CURRENCY ) ;
db . AddInParameter ( cmdInsert , "@EXCHANGERATE" , DbType . Decimal , enumValue . EXCHANGERATE ) ;
db . AddInParameter ( cmdInsert , "@DRPP" , DbType . Decimal , enumValue . DRPP ) ;
db . AddInParameter ( cmdInsert , "@DRCC" , DbType . Decimal , enumValue . DRCC ) ;
db . AddInParameter ( cmdInsert , "@CRPP" , DbType . Decimal , enumValue . CRPP ) ;
db . AddInParameter ( cmdInsert , "@CRCC" , DbType . Decimal , enumValue . CRCC ) ;
db . AddInParameter ( cmdInsert , "@AMOUNTDR" , DbType . Decimal , enumValue . AMOUNTDR ) ;
db . AddInParameter ( cmdInsert , "@AMOUNTCR" , DbType . Decimal , enumValue . AMOUNTCR ) ;
db . AddInParameter ( cmdInsert , "@UNITPRICE" , DbType . Decimal , enumValue . UNITPRICE ) ;
db . AddInParameter ( cmdInsert , "@UNIT" , DbType . String , enumValue . UNIT ) ;
db . AddInParameter ( cmdInsert , "@QUANTITY" , DbType . Decimal , enumValue . QUANTITY ) ;
db . AddInParameter ( cmdInsert , "@FEEDESCRIPTION" , DbType . String , enumValue . FEEDESCRIPTION ) ;
db . AddInParameter ( cmdInsert , "@REMARK" , DbType . String , enumValue . REMARK ) ;
db . AddInParameter ( cmdInsert , "@SORT" , DbType . Int32 , enumValue . SORT ) ;
db . AddInParameter ( cmdInsert , "@CREATEUSER" , DbType . String , userid ) ;
db . AddInParameter ( cmdInsert , "@CREATETIME" , DbType . String , DateTime . Now . ToString ( ) ) ;
db . ExecuteNonQuery ( cmdInsert , tran ) ;
}
else {
cmdUpdate . Parameters . Clear ( ) ;
db . AddInParameter ( cmdUpdate , "@GID" , DbType . String , enumValue . GID ) ;
db . AddInParameter ( cmdUpdate , "@FEENAME" , DbType . String , enumValue . FEENAME ) ;
db . AddInParameter ( cmdUpdate , "@FEEENAME" , DbType . String , enumValue . FEEENAME ) ;
db . AddInParameter ( cmdUpdate , "@CURRENCY" , DbType . String , enumValue . CURRENCY ) ;
db . AddInParameter ( cmdUpdate , "@EXCHANGERATE" , DbType . Decimal , enumValue . EXCHANGERATE ) ;
db . AddInParameter ( cmdUpdate , "@DRPP" , DbType . Decimal , enumValue . DRPP ) ;
db . AddInParameter ( cmdUpdate , "@DRCC" , DbType . Decimal , enumValue . DRCC ) ;
db . AddInParameter ( cmdUpdate , "@CRPP" , DbType . Decimal , enumValue . CRPP ) ;
db . AddInParameter ( cmdUpdate , "@CRCC" , DbType . Decimal , enumValue . CRCC ) ;
db . AddInParameter ( cmdUpdate , "@AMOUNTDR" , DbType . Decimal , enumValue . AMOUNTDR ) ;
db . AddInParameter ( cmdUpdate , "@AMOUNTCR" , DbType . Decimal , enumValue . AMOUNTCR ) ;
db . AddInParameter ( cmdUpdate , "@UNIT" , DbType . String , enumValue . UNIT ) ;
db . AddInParameter ( cmdUpdate , "@UNITPRICE" , DbType . Decimal , enumValue . UNITPRICE ) ;
db . AddInParameter ( cmdUpdate , "@QUANTITY" , DbType . Decimal , enumValue . QUANTITY ) ;
db . AddInParameter ( cmdUpdate , "@FEEDESCRIPTION" , DbType . String , enumValue . FEEDESCRIPTION ) ;
db . AddInParameter ( cmdInsert , "@REMARK" , DbType . String , enumValue . REMARK ) ;
db . AddInParameter ( cmdUpdate , "@SORT" , DbType . Int32 , enumValue . SORT ) ;
db . ExecuteNonQuery ( cmdUpdate , tran ) ;
}
}
}
tran . Commit ( ) ;
}
catch ( Exception )
{
tran . Rollback ( ) ;
result . Success = false ;
result . Message = "保存出现错误,请重试或联系系统管理员" ;
return result ;
}
}
result . Success = true ;
result . Message = "保存成功" ;
return result ;
}
public static DBResult DeleteTemplateDetail ( string GID )
{
var result = new DBResult ( ) ;
Database db = DatabaseFactory . CreateDatabase ( ) ;
using ( var conn = db . CreateConnection ( ) )
{
conn . Open ( ) ;
var tran = conn . BeginTransaction ( ) ;
try
{
var cmdDelete = db . GetSqlStringCommand ( "delete from code_fee_template_agentdetail where GID='" + GID + "'" ) ;
db . ExecuteNonQuery ( cmdDelete , tran ) ;
tran . Commit ( ) ;
}
catch ( Exception )
{
tran . Rollback ( ) ;
result . Success = false ;
result . Message = "保存出现错误,请重试或联系系统管理员" ;
return result ;
}
}
result . Success = true ;
result . Message = "保存成功" ;
return result ;
}
public static DBResult DeleteDetail ( CodeFeeTemplateAgent headData )
{
var result = new DBResult ( ) ;
Database db = DatabaseFactory . CreateDatabase ( ) ;
using ( var conn = db . CreateConnection ( ) )
{
conn . Open ( ) ;
var tran = conn . BeginTransaction ( ) ;
try
{
var cmd = db . GetSqlStringCommand ( "delete from code_fee_template_agent where GID='" + headData . GID + "'" ) ;
db . ExecuteNonQuery ( cmd , tran ) ;
var cmdDelete = db . GetSqlStringCommand ( "delete from code_fee_template_agentdetail where TEMPLATEID='" + headData . GID + "'" ) ;
db . ExecuteNonQuery ( cmdDelete , tran ) ;
tran . Commit ( ) ;
}
catch ( Exception )
{
tran . Rollback ( ) ;
result . Success = false ;
result . Message = "保存出现错误,请重试或联系系统管理员" ;
return result ;
}
}
result . Success = true ;
result . Message = "保存成功" ;
return result ;
}
# endregion
public static List < FeeUnit > GetFeeUnit ( string strCondition )
{
var strSql = new StringBuilder ( ) ;
strSql . Append ( "SELECT CTN from code_ctn" ) ;
if ( ! string . IsNullOrEmpty ( strCondition ) )
{
strSql . Append ( " where " + strCondition ) ;
}
Database db = DatabaseFactory . CreateDatabase ( ) ;
var evList = new List < FeeUnit > ( ) ;
using ( IDataReader reader = db . ExecuteReader ( CommandType . Text , strSql . ToString ( ) ) )
{
while ( reader . Read ( ) )
{
FeeUnit Data = new FeeUnit ( ) ;
Data . UNIT = Convert . ToString ( reader [ "CTN" ] ) ;
evList . Add ( Data ) ;
}
reader . Close ( ) ;
}
var data = new FeeUnit ( ) ;
data . UNIT = "单票" ;
evList . Add ( data ) ;
var data1 = new FeeUnit ( ) ;
data1 . UNIT = "重量" ;
evList . Add ( data1 ) ;
var data2 = new FeeUnit ( ) ;
data2 . UNIT = "尺码" ;
evList . Add ( data2 ) ;
var data3 = new FeeUnit ( ) ;
data3 . UNIT = "TEU" ;
evList . Add ( data3 ) ;
var data4 = new FeeUnit ( ) ;
data4 . UNIT = "箱型" ;
evList . Add ( data4 ) ;
var data5 = new FeeUnit ( ) ;
data5 . UNIT = "CBM" ;
evList . Add ( data5 ) ;
var data6 = new FeeUnit ( ) ;
data6 . UNIT = "BILL" ;
evList . Add ( data6 ) ;
var data7 = new FeeUnit ( ) ;
data7 . UNIT = "件数" ;
evList . Add ( data7 ) ;
var data8 = new FeeUnit ( ) ;
data8 . UNIT = "车" ;
evList . Add ( data8 ) ;
var data9 = new FeeUnit ( ) ;
data9 . UNIT = "自然箱" ;
evList . Add ( data9 ) ;
var data10 = new FeeUnit ( ) ;
data10 . UNIT = "小时" ;
evList . Add ( data10 ) ;
var data11 = new FeeUnit ( ) ;
data11 . UNIT = "个" ;
evList . Add ( data11 ) ;
var data12 = new FeeUnit ( ) ;
data11 . UNIT = "计费吨" ;
evList . Add ( data12 ) ;
return evList ;
}
#region 参照部分
public static string GetRangDAStr ( string tb , string userid , string usercode , string orgcode )
{
string str = "" ;
var strSql = new StringBuilder ( ) ;
strSql . Append ( "SELECT " ) ;
strSql . Append ( " VISIBLERANGE,OPERATERANGE " ) ;
strSql . Append ( " from VW_User_Authority " ) ;
strSql . Append ( " where [NAME]='modCodeFeeTemplateSet' and USERID='" + userid + "' and ISDELETE=0" ) ;
string visiblerange = "4" ;
string operaterange = "4" ;
Database db = DatabaseFactory . CreateDatabase ( ) ;
using ( IDataReader reader = db . ExecuteReader ( CommandType . Text , strSql . ToString ( ) ) )
{
while ( reader . Read ( ) )
{
visiblerange = Convert . ToString ( reader [ "VISIBLERANGE" ] ) ;
operaterange = Convert . ToString ( reader [ "OPERATERANGE" ] ) ;
break ;
}
reader . Close ( ) ;
}
if ( visiblerange = = "4" )
{
str = "1=2" ;
}
else if ( visiblerange = = "3" )
{
if ( tb = = "index" )
{
str = " CREATEUSER='" + userid + "'" ;
}
else
{
str = " UPPER(CORPID)='" + orgcode + "'" ;
}
}
else if ( visiblerange = = "2" )
{
if ( tb = = "index" )
{
str = " UPPER(CORPID)='" + orgcode + "'" ;
}
else
{
str = " UPPER(CORPID)='" + orgcode + "'" ;
}
}
else if ( visiblerange = = "1" )
{
str = " UPPER(CORPID)='" + orgcode + "'" ;
}
return str ;
}
# endregion
}
}