using System ;
using System.Data ;
using System.Collections.Generic ;
using System.Text ;
using DSWeb.MvcShipping.Models.MsCompanysDept ;
using Microsoft.Practices.EnterpriseLibrary.Data ;
using DSWeb.EntityDA ;
using DSWeb.Areas.CommMng.Models ;
using HcUtility.Comm ;
using System.Data.SqlClient ;
using DSWeb.DataAccess ;
using HcUtility.Core ;
namespace DSWeb.MvcShipping.DAL.MsCompanysDeptDAL
{
public class MsCompanysDeptDAL
{
#region 查询
static public List < MsCompanysDept > GetDataList ( string strCondition , string userid , string usercode , string companyid , string sort = null )
{
var strSql = new StringBuilder ( ) ;
strSql . Append ( "SELECT *,CASE WHEN ISNULL((select count(1) ct from user_baseinfo WHERE DEPTNAME=sys_dept.DEPTNAME),0)>0 THEN '1' ELSE '0' END ISUSE from sys_Dept where 1=1" ) ;
if ( ! string . IsNullOrEmpty ( strCondition ) )
{
strSql . Append ( " and " + strCondition ) ;
}
/ /
var sortstring = DatasetSort . Getsortstring ( sort ) ;
if ( ! string . IsNullOrEmpty ( sortstring ) )
{
strSql . Append ( " order by " + sortstring ) ;
}
else
{
strSql . Append ( " order by DEPTNO,MODIFIEDTIME" ) ;
}
return SetData ( strSql ) ;
}
static public MsCompanysDept GetData ( string condition , string companyid )
{
var strSql = new StringBuilder ( ) ;
strSql . Append ( "SELECT *,CASE WHEN ISNULL((select count(1) ct from user_baseinfo WHERE DEPTNAME=sys_dept.DEPTNAME),0)>0 THEN '1' ELSE '0' END ISUSE from sys_Dept where 1=1" ) ;
if ( ! string . IsNullOrEmpty ( condition ) )
{
strSql . Append ( " and " + condition ) ;
}
var list = SetData ( strSql ) ;
if ( list . Count > 0 )
return list [ 0 ] ;
return new MsCompanysDept ( ) ;
}
private static List < MsCompanysDept > SetData ( StringBuilder strSql )
{
var headList = new List < MsCompanysDept > ( ) ;
Database db = DatabaseFactory . CreateDatabase ( ) ;
using ( IDataReader reader = db . ExecuteReader ( CommandType . Text , strSql . ToString ( ) ) )
{
while ( reader . Read ( ) )
{
MsCompanysDept data = new MsCompanysDept ( ) ;
#region Set DB data to Object
data . GID = ( reader [ "GID" ] = = null ? "" : Convert . ToString ( reader [ "GID" ] ) ) ; //惟一编号
data . LINKID = ( reader [ "LINKID" ] = = null ? "" : Convert . ToString ( reader [ "LINKID" ] ) ) ; //公司关联id
data . DEPTNO = ( reader [ "DEPTNO" ] = = null ? "" : Convert . ToString ( reader [ "DEPTNO" ] ) ) ; //代码
data . DEPTNAME = ( reader [ "DEPTNAME" ] = = null ? "" : Convert . ToString ( reader [ "DEPTNAME" ] ) ) ; //名称
data . FINANCESOFTCODE = ( reader [ "FINANCESOFTCODE" ] = = null ? "" : Convert . ToString ( reader [ "FINANCESOFTCODE" ] ) ) ; //FINANCESOFTCODE
data . MANAGE1 = ( reader [ "MANAGE1" ] = = null ? "" : Convert . ToString ( reader [ "MANAGE1" ] ) ) ; //经理
data . MANAGE2 = ( reader [ "MANAGE2" ] = = null ? "" : Convert . ToString ( reader [ "MANAGE2" ] ) ) ; //副经理
data . REMARK = ( reader [ "REMARK" ] = = null ? "" : Convert . ToString ( reader [ "REMARK" ] ) ) ; //备注
data . CREATEUSER = ( reader [ "CREATEUSER" ] = = null ? "" : Convert . ToString ( reader [ "CREATEUSER" ] ) ) ; //CREATEUSER
if ( data . CREATETIME ! = null )
data . CREATETIME = Convert . ToDateTime ( reader [ "CREATETIME" ] ) ; //创建时间
data . MODIFIEDUSER = ( reader [ "MODIFIEDUSER" ] = = null ? "" : Convert . ToString ( reader [ "MODIFIEDUSER" ] ) ) ; //更改操作人gid
if ( data . MODIFIEDTIME ! = null )
data . MODIFIEDTIME = Convert . ToDateTime ( reader [ "MODIFIEDTIME" ] ) ; //更改操作时间
data . ISUSE = Convert . ToString ( reader [ "ISUSE" ] ) ;
# endregion
headList . Add ( data ) ;
}
reader . Close ( ) ;
}
return headList ;
}
# endregion
#region 删除
public static DBResult Delete ( MsCompanysDept headData , string LINKID )
{
DBResult result = new DBResult ( ) ;
T_ALL_DA T_ALL_DA = new EntityDA . T_ALL_DA ( ) ;
string strSql = "SELECT count(*) as inum from [user] where GID in (select USERID from [user_baseinfo] where DEPTNAME='" + headData . DEPTNAME . ToString ( ) . Trim ( ) + "') and GID in (select USERID from [user_company] where COMPANYID='" + LINKID . Trim ( ) + "')" ;
int inum = int . Parse ( T_ALL_DA . GetStrSQL ( "inum" , strSql ) ) ;
if ( inum > 0 )
{
result . Success = false ;
result . Message = "此部门拥有人员信息,请先删除!" ;
return result ;
}
/ /
Database db = DatabaseFactory . CreateDatabase ( ) ;
using ( var conn = db . CreateConnection ( ) )
{
conn . Open ( ) ;
var tran = conn . BeginTransaction ( ) ;
try
{
var cmdDelete = db . GetSqlStringCommand ( "delete from [sys_dept] where GID='" + 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
}
}