using System ;
using System.Data ;
using System.Collections.Generic ;
using System.Text ;
using DSWeb.MvcShipping.Models.MsSysParamSet ;
using Microsoft.Practices.EnterpriseLibrary.Data ;
using DSWeb.Areas.CommMng.Models ;
using HcUtility.Comm ;
using DSWeb.SoftMng.Common ;
using DSWeb.Common.DB ;
using System.Linq ;
using System.Data.Entity.Migrations ;
namespace DSWeb.MvcShipping.DAL.MsSysParamSet
{
public class MsSysParamSetDAL
{
#region Inquery DataList
static public List < SysParamSet > GetDataList ( string strCondition , string sort = null )
{
var strSql = new StringBuilder ( ) ;
strSql . Append ( "SELECT " ) ;
strSql . Append ( "GID,PARAMNAME,PARAMTYPE,PARAMDESCRIPTION,PARAMVALUE,MODIFIEDUSER,MODIFIEDTIME,FIELDTYPE" ) ;
strSql . Append ( ",(CASE WHEN PARAMNAME='CWSTARTUSING' THEN (SELECT TOP 1 STARTNAME FROM cw_design_startusing WHERE GID=sys_param_set.PARAMVALUE) ELSE " ) ;
strSql . Append ( "(select top 1 DISPVALUE from sys_parameter_value where PARAMNAME=sys_param_set.PARAMNAME and PARAMVALUE=sys_param_set.PARAMVALUE) END) DISPVALUE" ) ;
//strSql.Append(",COMPANYID,PARAMVALUE2,PARAMVALUE3,PARAMVALUE4");
strSql . Append ( " from sys_param_set " ) ;
if ( ! string . IsNullOrEmpty ( strCondition ) )
{
strSql . Append ( " where " + strCondition ) ;
}
var sortstring = DatasetSort . Getsortstring ( sort ) ;
if ( ! string . IsNullOrEmpty ( sortstring ) & & sortstring . Trim ( ) ! = "" )
{
strSql . Append ( " order by " + sortstring ) ;
}
else {
strSql . Append ( " order by PARAMTYPE,PARAMNAME" ) ;
}
return SetData ( strSql ) ;
}
/// <summary>
/// 从系统参数中获取属性值
/// </summary>
/// <param name="condition"></param>
/// <returns></returns>
static public SysParamSet GetData ( string condition )
{
SysParamSet data = null ;
var list = GetDataList ( condition ) ;
if ( list . Count > 0 )
data = list [ 0 ] ;
if ( data = = null )
{
data = new SysParamSet ( ) ;
}
return data ;
}
static public SysParamSet GetDataByName ( string NameOrDescription )
{
SysParamSet data = null ;
var condition = $"PARAMNAME='{NameOrDescription}' or PARAMDESCRIPTION='{NameOrDescription}'" ;
var list = GetDataList ( condition ) ;
if ( list . Count > 0 )
data = list [ 0 ] ;
if ( data = = null )
{
data = new SysParamSet ( ) ;
}
return data ;
}
/// <summary>
/// 参数为 sys_param_set.PARAMNAME
/// 返回值为 sys_param_set.PARAMVALUE
/// </summary>
/// <param name="PARAMNAME"></param>
/// <returns></returns>
static public string GetSysParamValue ( string PARAMNAME )
{
var param = GetData ( "PARAMNAME = '" + PARAMNAME + "'" ) ;
if ( param = = null ) return "" ;
return param . PARAMVALUE ;
}
static public SysParamSet GetSysParam ( string PARAMNAME )
{
return GetData ( "PARAMNAME = '" + PARAMNAME + "'" ) ;
}
public static void SaveSysParamValue ( string PARAMNAME , string DESCRIPTION , string value ) {
var cdc = new CommonDataContext ( ) ;
var paramitem = cdc . sys_param_set . FirstOrDefault ( x = > x . PARAMNAME = = PARAMNAME ) ;
if ( paramitem ! = null & & ! string . IsNullOrWhiteSpace ( paramitem . PARAMNAME ) ) {
paramitem . PARAMVALUE = value ;
cdc . sys_param_set . AddOrUpdate ( paramitem ) ;
cdc . SaveChanges ( ) ;
}
else {
var newrec = new sys_param_set_md ( ) ;
newrec . GID = Guid . NewGuid ( ) . ToString ( ) ;
newrec . PARAMTYPE = "操作管理" ;
newrec . PARAMNAME = PARAMNAME ;
newrec . PARAMDESCRIPTION = DESCRIPTION ;
newrec . PARAMVALUE = value ;
cdc . sys_param_set . Add ( newrec ) ;
cdc . SaveChanges ( ) ;
}
}
private static List < SysParamSet > SetData ( StringBuilder strSql )
{
var headList = new List < SysParamSet > ( ) ;
Database db = DatabaseFactory . CreateDatabase ( ) ;
using ( IDataReader reader = db . ExecuteReader ( CommandType . Text , strSql . ToString ( ) ) )
{
while ( reader . Read ( ) )
{
SysParamSet data = new SysParamSet ( ) ;
#region Set DB data to Object
data . GID = Convert . ToString ( reader [ "GID" ] ) ;
data . PARAMNAME = Convert . ToString ( reader [ "PARAMNAME" ] ) ;
data . PARAMTYPE = Convert . ToString ( reader [ "PARAMTYPE" ] ) ;
data . PARAMDESCRIPTION = Convert . ToString ( reader [ "PARAMDESCRIPTION" ] ) ;
data . PARAMVALUE = Convert . ToString ( reader [ "PARAMVALUE" ] ) ;
data . DISPVALUE = Convert . ToString ( reader [ "DISPVALUE" ] ) ;
data . MODIFIEDUSER = Convert . ToString ( reader [ "MODIFIEDUSER" ] ) ;
data . FIELDTYPE = Convert . ToString ( reader [ "FIELDTYPE" ] ) ;
if ( data . FIELDTYPE = = "STR" )
data . DISPVALUE = data . PARAMVALUE ;
if ( data . FIELDTYPE = = "PWD" )
{
data . DISPVALUE = "******" ;
data . PARAMVALUE = "******" ;
}
# endregion
headList . Add ( data ) ;
}
reader . Close ( ) ;
}
return headList ;
}
public static DBResult SaveDetail ( List < SysParamSet > bodyList , string userid )
{
var result = new DBResult ( ) ;
Database db = DatabaseFactory . CreateDatabase ( ) ;
using ( var conn = db . CreateConnection ( ) )
{
conn . Open ( ) ;
var tran = conn . BeginTransaction ( ) ;
try
{
var cmdUpdate =
db . GetSqlStringCommand (
@"update sys_param_set set PARAMVALUE=@PARAMVALUE,MODIFIEDUSER=@MODIFIEDUSER,MODIFIEDTIME=@MODIFIEDTIME where GID=@GID " ) ;
if ( bodyList ! = null )
{
foreach ( var enumValue in bodyList )
{
cmdUpdate . Parameters . Clear ( ) ;
db . AddInParameter ( cmdUpdate , "@GID" , DbType . String , enumValue . GID ) ;
db . AddInParameter ( cmdUpdate , "@PARAMVALUE" , DbType . String , enumValue . PARAMVALUE ) ;
db . AddInParameter ( cmdUpdate , "@MODIFIEDUSER" , DbType . String , userid ) ;
db . AddInParameter ( cmdUpdate , "@MODIFIEDTIME" , DbType . String , DateTime . Now . ToString ( "yyyy-MM-dd" ) ) ;
db . ExecuteNonQuery ( cmdUpdate , tran ) ;
}
}
tran . Commit ( ) ;
}
catch ( Exception )
{
tran . Rollback ( ) ;
result . Success = false ;
result . Message = "保存出现错误,请重试或联系系统管理员" ;
return result ;
}
}
result . Success = true ;
result . Message = "保存成功" + result . Message ;
return result ;
}
# endregion
#region SysParamSet枚举值
static public List < SysParamValue > GetParamValueList ( string strCondition , string sort = null )
{
var strSql = new StringBuilder ( ) ;
if ( strCondition = = "PARAMNAME='CWSTARTUSING'" )
{
strSql . Append ( "SELECT " ) ;
strSql . Append ( "GID,'CWSTARTUSING' PARAMNAME,'财务管理' PARAMTYPE,'生成凭证固定账套' PARAMDESCRIPTION,STARTNAME DISPVALUE,GID PARAMVALUE" ) ;
strSql . Append ( " from cw_design_startusing " ) ;
strSql . Append ( " order by CREATETIME" ) ;
}
else
{
strSql . Append ( "SELECT " ) ;
strSql . Append ( "GID,PARAMNAME,PARAMTYPE,PARAMDESCRIPTION,DISPVALUE,PARAMVALUE" ) ;
strSql . Append ( " from sys_parameter_value " ) ;
if ( ! string . IsNullOrEmpty ( strCondition ) )
{
strSql . Append ( " where " + strCondition ) ;
}
var sortstring = DatasetSort . Getsortstring ( sort ) ;
if ( ! string . IsNullOrEmpty ( sortstring ) & & sortstring . Trim ( ) ! = "" )
{
strSql . Append ( " order by " + sortstring ) ;
}
else
{
strSql . Append ( " order by PARAMTYPE,PARAMNAME" ) ;
}
}
return SetParamValueData ( strSql ) ;
}
private static List < SysParamValue > SetParamValueData ( StringBuilder strSql )
{
var headList = new List < SysParamValue > ( ) ;
Database db = DatabaseFactory . CreateDatabase ( ) ;
using ( IDataReader reader = db . ExecuteReader ( CommandType . Text , strSql . ToString ( ) ) )
{
while ( reader . Read ( ) )
{
SysParamValue data = new SysParamValue ( ) ;
#region Set DB data to Object
data . GID = Convert . ToString ( reader [ "GID" ] ) ;
data . PARAMNAME = Convert . ToString ( reader [ "PARAMNAME" ] ) ;
data . DISPVALUE = Convert . ToString ( reader [ "DISPVALUE" ] ) ;
data . PARAMDESCRIPTION = Convert . ToString ( reader [ "PARAMDESCRIPTION" ] ) ;
data . PARAMVALUE = Convert . ToString ( reader [ "PARAMVALUE" ] ) ;
# endregion
headList . Add ( data ) ;
}
reader . Close ( ) ;
}
return headList ;
}
# endregion
}
}