using System ;
using System.Data ;
using System.Collections.Generic ;
using System.Text ;
using DSWeb.MvcShipping.Models.MsSysThirdPartyAccount ;
using Microsoft.Practices.EnterpriseLibrary.Data ;
using DSWeb.Areas.CommMng.Models ;
using HcUtility.Comm ;
using DSWeb.Common.DB ;
using HcUtility.Core ;
using System.Linq ;
namespace DSWeb.MvcShipping.DAL.MsSysThirdPartyAccount
{
public class MsSysThirdPartyAccountDAL
{
#region Inquery DataList
static public List < SysThirdPartyAccount > GetDataList ( string strCondition , string sort = null )
{
var strSql = new StringBuilder ( ) ;
strSql . Append ( "SELECT " ) ;
strSql . Append ( "GID,THIRDPARTY,CUSTOMERNAME,USERID,ACCOUNT,ACCOUNTPSW,MODIFIEDUSER,MODIFIEDTIME,CREATEUSER,CREATETIME" ) ;
strSql . Append ( ",(select top 1 SHOWNAME from [user] where GID=sys_thidparty_account.USERID) USERNAME" ) ;
strSql . Append ( " from sys_thidparty_account " ) ;
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 THIRDPARTY" ) ;
}
return SetData ( strSql ) ;
}
static public SysThirdPartyAccount GetData ( string condition )
{
SysThirdPartyAccount data = null ;
var list = GetDataList ( condition ) ;
if ( list . Count > 0 )
data = list [ 0 ] ;
if ( data = = null )
{
data = new SysThirdPartyAccount ( ) ;
}
return data ;
}
static public SysThirdPartyAccount GetDataByCustomername ( string USERID , string CUSTOMERNAME )
{
return GetData ( $" USERID='{USERID}' and CUSTOMERNAME='{CUSTOMERNAME}'" ) ;
}
private static List < SysThirdPartyAccount > SetData ( StringBuilder strSql )
{
var headList = new List < SysThirdPartyAccount > ( ) ;
Database db = DatabaseFactory . CreateDatabase ( ) ;
using ( IDataReader reader = db . ExecuteReader ( CommandType . Text , strSql . ToString ( ) ) )
{
while ( reader . Read ( ) )
{
SysThirdPartyAccount data = new SysThirdPartyAccount ( ) ;
#region Set DB data to Object
data . GID = Convert . ToString ( reader [ "GID" ] ) ;
data . USERID = Convert . ToString ( reader [ "USERID" ] ) ;
data . USERNAME = Convert . ToString ( reader [ "USERNAME" ] ) ;
data . THIRDPARTY = Convert . ToString ( reader [ "THIRDPARTY" ] ) ;
data . ACCOUNT = Convert . ToString ( reader [ "ACCOUNT" ] ) ;
data . ACCOUNTPSW = Convert . ToString ( reader [ "ACCOUNTPSW" ] ) ;
data . CREATEUSER = Convert . ToString ( reader [ "CREATEUSER" ] ) ;
data . MODIFIEDUSER = Convert . ToString ( reader [ "MODIFIEDUSER" ] ) ;
data . CREATETIME = Convert . ToString ( reader [ "CREATETIME" ] ) ;
data . MODIFIEDTIME = Convert . ToString ( reader [ "MODIFIEDTIME" ] ) ;
data . CUSTOMERNAME = Convert . ToString ( reader [ "CUSTOMERNAME" ] ) ;
# endregion
headList . Add ( data ) ;
}
reader . Close ( ) ;
}
return headList ;
}
public static DBResult SaveDetail ( List < SysThirdPartyAccount > 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 sys_thidparty_account (GID,USERID,CUSTOMERNAME,THIRDPARTY,ACCOUNT,ACCOUNTPSW,CREATEUSER,CREATETIME,MODIFIEDUSER,MODIFIEDTIME)
values ( @GID , @USERID , @CUSTOMERNAME , @THIRDPARTY , @ACCOUNT , @ACCOUNTPSW , @CREATEUSER , @CREATETIME , @MODIFIEDUSER , @MODIFIEDTIME ) ");
var cmdUpdate =
db . GetSqlStringCommand (
@"update sys_thidparty_account set USERID=@USERID,THIRDPARTY=@THIRDPARTY,ACCOUNT=@ACCOUNT,ACCOUNTPSW=@ACCOUNTPSW,MODIFIEDUSER=@MODIFIEDUSER,MODIFIEDTIME=@MODIFIEDTIME,CUSTOMERNAME=@CUSTOMERNAME where GID=@GID " ) ;
if ( bodyList ! = null )
{
foreach ( var enumValue in bodyList )
{
if ( enumValue . GID = = "*" | | enumValue . GID = = "" )
{
cmdInsert . Parameters . Clear ( ) ;
db . AddInParameter ( cmdInsert , "@GID" , DbType . String , Guid . NewGuid ( ) . ToString ( ) ) ;
db . AddInParameter ( cmdInsert , "@USERID" , DbType . String , enumValue . USERID ) ;
db . AddInParameter ( cmdInsert , "@THIRDPARTY" , DbType . String , enumValue . THIRDPARTY ) ;
db . AddInParameter ( cmdInsert , "@ACCOUNT" , DbType . String , enumValue . ACCOUNT ) ;
db . AddInParameter ( cmdInsert , "@ACCOUNTPSW" , DbType . String , enumValue . ACCOUNTPSW ) ;
db . AddInParameter ( cmdInsert , "@CREATEUSER" , DbType . String , userid ) ;
db . AddInParameter ( cmdInsert , "@CREATETIME" , DbType . String , DateTime . Now . ToString ( "yyyy-MM-dd HH:mm:ss" ) ) ;
db . AddInParameter ( cmdInsert , "@MODIFIEDUSER" , DbType . String , userid ) ;
db . AddInParameter ( cmdInsert , "@MODIFIEDTIME" , DbType . String , DateTime . Now . ToString ( "yyyy-MM-dd HH:mm:ss" ) ) ;
db . AddInParameter ( cmdInsert , "@CUSTOMERNAME" , DbType . String , enumValue . CUSTOMERNAME ) ;
db . ExecuteNonQuery ( cmdInsert , tran ) ;
}
else
{
cmdUpdate . Parameters . Clear ( ) ;
db . AddInParameter ( cmdUpdate , "@GID" , DbType . String , enumValue . GID ) ;
db . AddInParameter ( cmdUpdate , "@USERID" , DbType . String , enumValue . USERID ) ;
db . AddInParameter ( cmdUpdate , "@THIRDPARTY" , DbType . String , enumValue . THIRDPARTY ) ;
db . AddInParameter ( cmdUpdate , "@ACCOUNT" , DbType . String , enumValue . ACCOUNT ) ;
db . AddInParameter ( cmdUpdate , "@ACCOUNTPSW" , DbType . String , enumValue . ACCOUNTPSW ) ;
db . AddInParameter ( cmdUpdate , "@MODIFIEDUSER" , DbType . String , userid ) ;
db . AddInParameter ( cmdUpdate , "@MODIFIEDTIME" , DbType . String , DateTime . Now . ToString ( "yyyy-MM-dd HH:mm:ss" ) ) ;
db . AddInParameter ( cmdUpdate , "@CUSTOMERNAME" , DbType . String , enumValue . CUSTOMERNAME ) ;
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 ;
}
public static DBResult DeleteDetail ( List < SysThirdPartyAccount > headData )
{
var result = new DBResult ( ) ;
Database db = DatabaseFactory . CreateDatabase ( ) ;
using ( var conn = db . CreateConnection ( ) )
{
conn . Open ( ) ;
var tran = conn . BeginTransaction ( ) ;
try
{
if ( headData ! = null )
{
foreach ( var enumValue in headData )
{
var cmdDelete = db . GetSqlStringCommand ( "delete from sys_thidparty_account where GID='" + enumValue . 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 DBResult MakeSureField ( string THIRDPARTY , string USERID )
{
var result = new DBResult ( ) ;
var currList = GetDataList ( $" THIRDPARTY='{THIRDPARTY}' and USERID='{USERID}' " ) . ToList ( ) ;
if ( currList = = null | | currList . Count = = 0 )
{
currList = GetDataList ( $" THIRDPARTY='{THIRDPARTY}' and (isnull(USERID,'')='') " ) . ToList ( ) ;
}
if ( currList = = null | | currList . Count = = 0 )
{
result . SetErrorInfo ( $"没有找到第三方账号类型【{THIRDPARTY}】" ) ;
var newrec = new SysThirdPartyAccount ( ) ;
newrec . GID = Guid . NewGuid ( ) . ToString ( ) ;
newrec . USERID = USERID ;
newrec . THIRDPARTY = THIRDPARTY ;
newrec . ACCOUNT = "" ;
newrec . ACCOUNTPSW = "" ;
newrec . CREATEUSER = USERID ;
newrec . CREATETIME = System . DateTime . Now . ToString ( "yyyy-MM-dd HH:mm:ss.fff" ) ;
newrec . MODIFIEDUSER = USERID ;
newrec . MODIFIEDTIME = System . DateTime . Now . ToString ( "yyyy-MM-dd HH:mm:ss.fff" ) ;
newrec . CUSTOMERNAME = "" ;
newrec . DbOperationType = DbOperationType . DbotIns ;
newrec . ModelUIStatus = "I" ;
var modb = new ModelObjectDB ( ) ;
var _r = modb . Save ( newrec ) ;
var cdc = new CommonDataContext ( ) ;
var djyapiNameDic = new Dictionary < string , string >
{
{ "客户(往来单位)同步" , "/DataSync/SyncCustomer" } ,
{ "批量同步客户无返回值" , "/DataSync/SyncCustomerList" } ,
{ "批量同步船期无返回值" , "/DataSync/SyncVesselDateList" } ,
{ "批量删除船期" , "/DataSync/DeletaVesselDate" } ,
} ;
//第三方软件类型 受枚举类型96020的内容管理
var currEnumList = cdc . tSysEnumValue . Where ( x = > x . EnumTypeID = = 96020 ) . ToList ( ) ;
if ( ! currEnumList . Exists ( x = > x . EnumValueName = = THIRDPARTY ) )
{
var newenumValue = new tSysEnumValue_md ( ) ;
newenumValue . LangID = 0 ;
newenumValue . EnumTypeID = 96020 ;
newenumValue . EnumValueID = THIRDPARTY ;
newenumValue . EnumValueName = THIRDPARTY ;
if ( djyapiNameDic . ContainsKey ( THIRDPARTY ) )
{
newenumValue . EnumValueName_2 = djyapiNameDic [ THIRDPARTY ] ;
}
cdc . tSysEnumValue . Add ( newenumValue ) ;
cdc . SaveChanges ( ) ;
}
}
else
{
var _r = currList [ 0 ] ;
if ( string . IsNullOrWhiteSpace ( _r . ACCOUNT ) )
{
result . SetErrorInfo ( $"第三方账号类型【{THIRDPARTY}】目前没有设置值;" ) ;
}
else
{
result . OK ( "" , currList [ 0 ] ) ;
}
}
return result ;
}
}
}