using System ;
using System.Data ;
using System.Collections.Generic ;
using System.Text ;
using DSWeb.MvcShipping.Models.MsCrmClient ;
using Microsoft.Practices.EnterpriseLibrary.Data ;
using DSWeb.Areas.CommMng.Models ;
using HcUtility.Comm ;
using DSWeb.EntityDA ;
namespace DSWeb.MvcShipping.DAL.MsCrmClient
{
public class MsCrmClientDAL
{
#region Inquery DataList
static public List < MsCrmClientVisit > GetVisitDataList ( string strCondition , int start , int limit , string userid , string username , string companyid , out int totel , string sort = null )
{
var rangstr = "" ;
rangstr = GetRangDAStr ( "index" , userid , username , companyid ) ;
if ( ! string . IsNullOrEmpty ( rangstr ) )
{
if ( ! string . IsNullOrEmpty ( strCondition ) )
{
strCondition = strCondition + " and " + rangstr ;
}
else
{
strCondition = rangstr ;
}
}
var strSql = new StringBuilder ( ) ;
strSql . Append ( "SELECT " ) ;
strSql . Append ( "[GID],[CLIENTGID],(SELECT SHORTNAME FROM info_client WHERE GID=CLIENTGID) SHORTNAME,[CONTACTNAME],[SALE],[VISITWAY],[VISITCONTENT],[VISITDATE],[NEXTACTION],[NEXTCONTENT]" ) ;
strSql . Append ( ",[NEXTDATE],[CREATEUSER],[CREATETIME],[MODIFIEDUSER],[MODIFIEDTIME],VISITTITLE,VISITSTATUS" ) ;
strSql . Append ( " from crm_info_client_visit " ) ;
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 VISITDATE DESC" ) ;
}
totel = getVisitTotalCount ( strCondition ) ;
return SetVisitData ( strSql ) ;
}
public static int getVisitTotalCount ( string strCondition )
{
StringBuilder strSql = new StringBuilder ( ) ;
strSql . Append ( "select count(*) " ) ;
strSql . Append ( " FROM crm_info_client_visit where 1=1 " ) ;
if ( ! string . IsNullOrEmpty ( strCondition ) )
{
strSql . Append ( " and " + strCondition ) ;
}
int cnt = 0 ;
Database db = DatabaseFactory . CreateDatabase ( ) ;
using ( IDataReader reader = db . ExecuteReader ( CommandType . Text , strSql . ToString ( ) ) )
{
while ( reader . Read ( ) )
{
cnt = Convert . ToInt32 ( reader [ 0 ] ) ;
}
}
return cnt ;
}
static public MsCrmClientVisit GetVisitData ( string condition )
{
var strSql = new StringBuilder ( ) ;
strSql . Append ( "SELECT " ) ;
strSql . Append ( "[GID],[CLIENTGID],(SELECT SHORTNAME FROM info_client WHERE GID=CLIENTGID) SHORTNAME,[CONTACTNAME],[SALE],[VISITWAY],[VISITCONTENT],[VISITDATE],[NEXTACTION],[NEXTCONTENT]" ) ;
strSql . Append ( ",[NEXTDATE],[CREATEUSER],[CREATETIME],[MODIFIEDUSER],[MODIFIEDTIME],VISITTITLE,VISITSTATUS" ) ;
strSql . Append ( " from crm_info_client_visit " ) ;
if ( ! string . IsNullOrEmpty ( condition ) )
{
strSql . Append ( " where " + condition ) ;
}
var list = SetVisitData ( strSql ) ;
if ( list . Count > 0 )
return list [ 0 ] ;
return new MsCrmClientVisit ( ) ;
}
private static List < MsCrmClientVisit > SetVisitData ( StringBuilder strSql )
{
var headList = new List < MsCrmClientVisit > ( ) ;
Database db = DatabaseFactory . CreateDatabase ( ) ;
using ( IDataReader reader = db . ExecuteReader ( CommandType . Text , strSql . ToString ( ) ) )
{
while ( reader . Read ( ) )
{
MsCrmClientVisit data = new MsCrmClientVisit ( ) ;
#region Set DB data to Object
data . GID = Convert . ToString ( reader [ "GID" ] ) ;
data . CLIENTGID = Convert . ToString ( reader [ "CLIENTGID" ] ) ;
data . SHORTNAME = Convert . ToString ( reader [ "SHORTNAME" ] ) ;
data . CONTACTNAME = Convert . ToString ( reader [ "CONTACTNAME" ] ) ;
data . SALE = Convert . ToString ( reader [ "SALE" ] ) ;
data . VISITWAY = Convert . ToString ( reader [ "VISITWAY" ] ) ;
data . VISITCONTENT = Convert . ToString ( reader [ "VISITCONTENT" ] ) ;
if ( reader [ "VISITDATE" ] ! = DBNull . Value )
data . VISITDATE = Convert . ToDateTime ( reader [ "VISITDATE" ] ) . ToString ( "yyyy-MM-dd" ) ; / /
data . NEXTACTION = Convert . ToString ( reader [ "NEXTACTION" ] ) ;
data . NEXTCONTENT = Convert . ToString ( reader [ "NEXTCONTENT" ] ) ;
if ( reader [ "NEXTDATE" ] ! = DBNull . Value )
data . NEXTDATE = Convert . ToDateTime ( reader [ "NEXTDATE" ] ) . ToString ( "yyyy-MM-dd HH:mm:ss" ) ; / /
data . VISITTITLE = Convert . ToString ( reader [ "VISITTITLE" ] ) ;
data . VISITSTATUS = Convert . ToString ( reader [ "VISITSTATUS" ] ) ;
data . CREATEUSER = Convert . ToString ( reader [ "CREATEUSER" ] ) ;
data . CREATETIME = Convert . ToString ( reader [ "CREATETIME" ] ) ;
# endregion
headList . Add ( data ) ;
}
reader . Close ( ) ;
}
return headList ;
}
# endregion
#region 权限范围
public static string GetRangDAStr ( string tb , string userid , string username , string companyid )
{
string str = "" ;
var strSql = new StringBuilder ( ) ;
strSql . Append ( "SELECT " ) ;
strSql . Append ( " VISIBLERANGE,OPERATERANGE,AUTHORITYID " ) ;
strSql . Append ( " from VW_User_Authority " ) ;
strSql . Append ( " where [NAME]='modCrmClientVisit' and USERID='" + userid + "' and ISDELETE=0" ) ;
string visiblerange = "4" ;
string operaterange = "4" ;
string AUTHORITYID = "" ;
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" ] ) ;
AUTHORITYID = Convert . ToString ( reader [ "AUTHORITYID" ] ) ;
break ;
}
reader . Close ( ) ;
}
if ( visiblerange = = "4" )
{
str = " SALE='" + username + "' or CREATEUSER='" + username + "' " ;
}
else if ( visiblerange = = "3" )
{
str = " SALE='" + username + "' or CREATEUSER='" + username + "' " ;
}
else if ( visiblerange = = "2" )
{
var rangeDa = new RangeDA ( ) ;
var deptid = rangeDa . GetDEPTGID ( userid ) ;
//str = " (SALE in (select showname from vw_user where deptgid='" + deptid + "') OR CREATEUSER in (select userid from vw_user where deptgid='" + deptid + "') )";
str = " exists( select 1 from vw_user where deptgid='" + deptid + "' and(showname=SALE OR USERID=CREATEUSER )) " ;
}
else if ( visiblerange = = "5" )
{
str = " 1=1 " ;
}
else if ( visiblerange = = "6" )
{
str = " EXISTS (select 1 from user_authority_range_op P left join [user] u on (u.GID=P.OPID) where (crm_info_client_visit.SALE=U.SHOWNAME OR crm_info_client_visit.CREATEUSER=p.OPID ) AND P.userid='" + userid + "' and P.AUTHORITYID='" + AUTHORITYID + "' and P.VISIBLERANGE=1) " ;
}
else if ( visiblerange = = "1" )
{
str = " 1=1 " ;
}
if ( str ! = "" )
{
str = "(" + str + ")" ;
}
return str ;
}
# endregion
}
}