using System ;
using System.Data ;
using System.Collections.Generic ;
using System.Text ;
using DSWeb.MvcShipping.Models.MsCodeDuiTemplate ;
using Microsoft.Practices.EnterpriseLibrary.Data ;
using DSWeb.Areas.CommMng.Models ;
using HcUtility.Comm ;
namespace DSWeb.MvcShipping.DAL.MsCodeDuiTemplate
{
public class MsCodeDuiTempldateDAL
{
#region Inquery DataList
static public List < CodeDuiTemplate > GetDataList ( string strCondition , string userid , string usercode , string orgcode , string sort = null )
{
/ *
var rangstr = GetRangDAStr ( "" , userid , usercode , orgcode ) ;
if ( ! string . IsNullOrEmpty ( rangstr ) )
{
if ( ! string . IsNullOrEmpty ( strCondition ) )
{
strCondition = strCondition + " and " + rangstr ;
}
else
{
strCondition = rangstr ;
}
}
* /
var strSql = new StringBuilder ( ) ;
strSql . Append ( "SELECT " ) ;
strSql . Append ( "DM_ID" ) ;
strSql . Append ( ",NAME,ENAME" ) ;
strSql . Append ( ",REMARKS,INPUTBY,INPUTDATE" ) ;
strSql . Append ( " from code_dui_format " ) ;
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 CodeDuiTemplate GetData ( string condition , string userid , string usercode , string orgcode )
{
CodeDuiTemplate data = null ;
var list = GetDataList ( condition , userid , usercode , orgcode ) ;
if ( list . Count > 0 )
data = list [ 0 ] ;
if ( data = = null )
{
data = new CodeDuiTemplate ( ) ;
}
return data ;
}
private static List < CodeDuiTemplate > SetData ( StringBuilder strSql )
{
var headList = new List < CodeDuiTemplate > ( ) ;
Database db = DatabaseFactory . CreateDatabase ( ) ;
using ( IDataReader reader = db . ExecuteReader ( CommandType . Text , strSql . ToString ( ) ) )
{
while ( reader . Read ( ) )
{
CodeDuiTemplate data = new CodeDuiTemplate ( ) ;
#region Set DB data to Object
data . DM_ID = Convert . ToString ( reader [ "DM_ID" ] ) ;
data . NAME = Convert . ToString ( reader [ "NAME" ] ) ;
data . ENAME = Convert . ToString ( reader [ "ENAME" ] ) ;
data . REMARKS = Convert . ToString ( reader [ "REMARKS" ] ) ;
data . INPUTBY = Convert . ToString ( reader [ "INPUTBY" ] ) ;
if ( reader [ "INPUTDATE" ] ! = DBNull . Value )
data . INPUTDATE = Convert . ToDateTime ( reader [ "INPUTDATE" ] ) ;
# endregion
headList . Add ( data ) ;
}
reader . Close ( ) ;
}
return headList ;
}
# endregion
#region 明细表
static public List < CodeDuiTemplateFee > GetBodyList ( string strCondition )
{
var strSql = new StringBuilder ( ) ;
strSql . Append ( "SELECT " ) ;
strSql . Append ( "DM_ID,FEETYPE,FEENAME,CURRENCY,DISPLAYNAME,FEECODE" ) ;
strSql . Append ( ",(select EnumValueName from tSysEnumValue where LangId=0 and EnumTypeID=99020 and EnumValueID=code_dui_format_fee.FeeType) as FeeType_Ref" ) ;
strSql . Append ( " from code_dui_format_fee " ) ;
if ( strCondition . Trim ( ) ! = String . Empty )
{
strSql . Append ( " where " + strCondition ) ;
}
return SetBodyData ( strSql ) ;
}
private static List < CodeDuiTemplateFee > SetBodyData ( StringBuilder strSql )
{
var bodyList = new List < CodeDuiTemplateFee > ( ) ;
Database db = DatabaseFactory . CreateDatabase ( ) ;
using ( IDataReader reader = db . ExecuteReader ( CommandType . Text , strSql . ToString ( ) ) )
{
while ( reader . Read ( ) )
{
CodeDuiTemplateFee data = new CodeDuiTemplateFee ( ) ;
#region Set DB data to Object
data . DM_ID = Convert . ToString ( reader [ "DM_ID" ] ) ;
data . FeeType = Convert . ToDecimal ( reader [ "FEETYPE" ] ) ;
data . FeeTypeRef = Convert . ToString ( reader [ "FeeType_Ref" ] ) ;
data . FeeName = Convert . ToString ( reader [ "FEENAME" ] ) ;
data . Currency = Convert . ToString ( reader [ "CURRENCY" ] ) ;
data . DisplayName = Convert . ToString ( reader [ "DISPLAYNAME" ] ) ;
data . FeeCode = Convert . ToString ( reader [ "FEECODE" ] ) ;
# endregion
bodyList . Add ( data ) ;
}
reader . Close ( ) ;
}
return bodyList ;
}
static public List < CodeDuiTemplateSort > GetSortList ( string strCondition )
{
var strSql = new StringBuilder ( ) ;
strSql . Append ( "SELECT " ) ;
strSql . Append ( "DM_ID,SORTTYPE,SORTNUM,FIELDNAME" ) ;
strSql . Append ( ",(CASE SORTTYPE WHEN 1 THEN '升序' else '降序' end) as SORTTYPEref " ) ;
strSql . Append ( " from code_dui_format_sort " ) ;
if ( strCondition . Trim ( ) ! = String . Empty )
{
strSql . Append ( " where " + strCondition ) ;
}
return SetSortData ( strSql ) ;
}
private static List < CodeDuiTemplateSort > SetSortData ( StringBuilder strSql )
{
var bodyList = new List < CodeDuiTemplateSort > ( ) ;
Database db = DatabaseFactory . CreateDatabase ( ) ;
using ( IDataReader reader = db . ExecuteReader ( CommandType . Text , strSql . ToString ( ) ) )
{
while ( reader . Read ( ) )
{
CodeDuiTemplateSort data = new CodeDuiTemplateSort ( ) ;
#region Set DB data to Object
data . DM_ID = Convert . ToString ( reader [ "DM_ID" ] ) ;
data . SortType = Convert . ToDecimal ( reader [ "SORTTYPE" ] ) ;
data . SortNum = Convert . ToDecimal ( reader [ "SORTNUM" ] ) ;
data . FieldName = Convert . ToString ( reader [ "FIELDNAME" ] ) ;
data . SortTypeRef = Convert . ToString ( reader [ "SORTTYPEref" ] ) ;
# endregion
bodyList . Add ( data ) ;
}
reader . Close ( ) ;
}
return bodyList ;
}
static public List < CodeDuiFeeName > GetFieldList ( string strCondition )
{
var strSql = new StringBuilder ( ) ;
strSql . Append ( "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME='v_op_bill'" ) ;
return SetFieldData ( strSql ) ;
}
private static List < CodeDuiFeeName > SetFieldData ( StringBuilder strSql )
{
var bodyList = new List < CodeDuiFeeName > ( ) ;
Database db = DatabaseFactory . CreateDatabase ( ) ;
using ( IDataReader reader = db . ExecuteReader ( CommandType . Text , strSql . ToString ( ) ) )
{
while ( reader . Read ( ) )
{
CodeDuiFeeName data = new CodeDuiFeeName ( ) ;
#region Set DB data to Object
data . FieldName = Convert . ToString ( reader [ "COLUMN_NAME" ] ) ;
# endregion
bodyList . Add ( data ) ;
}
reader . Close ( ) ;
}
return bodyList ;
}
public static DBResult SaveDetail ( CodeDuiTemplate headData , List < CodeDuiTemplateFee > bodyList , List < CodeDuiTemplateSort > workList )
{
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_dui_format_fee where DM_ID='" + headData . DM_ID + "'" ) ;
db . ExecuteNonQuery ( cmdDelete , tran ) ;
var cmdDeletework = db . GetSqlStringCommand ( "delete from code_dui_format_sort where DM_ID='" + headData . DM_ID + "'" ) ;
db . ExecuteNonQuery ( cmdDeletework , tran ) ;
var cmdInsert =
db . GetSqlStringCommand (
@ "insert into code_dui_format_fee (DM_ID, FEETYPE, FEENAME,CURRENCY)
values ( @DM_ID , @FEETYPE , @FEENAME , @CURRENCY ) ");
if ( bodyList ! = null )
{
foreach ( var enumValue in bodyList )
{
cmdInsert . Parameters . Clear ( ) ;
db . AddInParameter ( cmdInsert , "@DM_ID" , DbType . String , headData . DM_ID ) ;
db . AddInParameter ( cmdInsert , "@FEETYPE" , DbType . Int32 , enumValue . FeeType ) ;
db . AddInParameter ( cmdInsert , "@FEENAME" , DbType . String , enumValue . FeeName ) ;
db . AddInParameter ( cmdInsert , "@CURRENCY" , DbType . String , enumValue . Currency ) ;
db . ExecuteNonQuery ( cmdInsert , tran ) ;
}
}
if ( workList ! = null )
{
var cmdInsertwork =
db . GetSqlStringCommand (
@ "insert into code_dui_format_sort (DM_ID, SORTTYPE,SORTNUM,FIELDNAME)
values ( @DM_ID , @SORTTYPE , @SORTNUM , @FIELDNAME ) ");
foreach ( var enumValue in workList )
{
cmdInsertwork . Parameters . Clear ( ) ;
db . AddInParameter ( cmdInsertwork , "@DM_ID" , DbType . String , headData . DM_ID ) ;
db . AddInParameter ( cmdInsertwork , "@SORTTYPE" , DbType . Int32 , enumValue . SortType ) ;
db . AddInParameter ( cmdInsertwork , "@SORTNUM" , DbType . Int32 , enumValue . SortNum ) ;
db . AddInParameter ( cmdInsertwork , "@FIELDNAME" , DbType . String , enumValue . FieldName ) ;
db . ExecuteNonQuery ( cmdInsertwork , 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 ( CodeDuiTemplate 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_dui_format where DM_ID='" + headData . DM_ID + "'" ) ;
db . ExecuteNonQuery ( cmd , tran ) ;
var cmdDelete = db . GetSqlStringCommand ( "delete from code_dui_format_fee where DM_ID='" + headData . DM_ID + "'" ) ;
db . ExecuteNonQuery ( cmdDelete , tran ) ;
var cmdDeletework = db . GetSqlStringCommand ( "delete from code_dui_format_sort where DM_ID='" + headData . DM_ID + "'" ) ;
db . ExecuteNonQuery ( cmdDeletework , tran ) ;
tran . Commit ( ) ;
}
catch ( Exception )
{
tran . Rollback ( ) ;
result . Success = false ;
result . Message = "保存出现错误,请重试或联系系统管理员" ;
return result ;
}
}
result . Success = true ;
result . Message = "保存成功" ;
return result ;
}
# endregion
#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]='modTruckPj' 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 = " UserCode='" + usercode + "'" ;
}
else
{
str = " UPPER(OrgCode)='" + orgcode + "'" ;
}
}
else if ( visiblerange = = "2" )
{
if ( tb = = "index" )
{
str = " UPPER(OrgCode)='" + orgcode + "'" ;
}
else
{
str = " UPPER(OrgCode)='" + orgcode + "'" ;
}
}
else if ( visiblerange = = "1" )
{
str = " UPPER(OrgCode)='" + orgcode + "'" ;
}
return str ;
}
# endregion
}
}