public static Entity ClassToEntity < T> ( this T t) where T : new ( )
{
string entityName = t. GetType ( ) . GetField ( "CRMName" ) . GetValue ( null ) . ToString ( ) ;
Entity entity = new Entity ( entityName) ;
HelpAttribute helpAttr = null ;
List< PropertyInfo> propertys = t. GetType ( ) . GetProperties ( ) . ToList < PropertyInfo> ( ) ;
foreach ( PropertyInfo pInfo in propertys)
{
object value = pInfo. GetValue ( t, null ) ;
object [ ] HelpAttributes = pInfo. GetCustomAttributes ( typeof ( HelpAttribute ) , false ) ;
if ( HelpAttributes. Length != 0 && HelpAttributes. Length == 1 )
{
helpAttr = HelpAttributes[ 0 ] as HelpAttribute ;
if ( helpAttr. ISID)
{
#region 根据ID判断是否更新
if ( value == null )
{
throw new ApplicationException ( "未提供" + pInfo. Name + "的值,因此该字段为唯一值" ) ;
}
if ( value != null && value . ToString ( ) . Trim ( ) . Length == 0 )
{
throw new ApplicationException ( "未提供" + pInfo. Name + "的值,因此该字段为唯一值" ) ;
}
QueryExpression queryEntityQE = new QueryExpression
{
EntityName = entityName,
ColumnSet = new ColumnSet ( helpAttr. CRMColName. ToLower ( ) ) ,
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression
{
AttributeName = helpAttr. CRMColName. ToLower ( ) ,
Operator = ConditionOperator. Equal,
Values = { value }
}
}
}
} ;
List< Entity> hasRecord = CrmService. OrgService ( ) . RetrieveMultiple ( queryEntityQE) . Entities. ToList < Entity> ( ) ;
if ( hasRecord. Count > 1 )
{
throw new ApplicationException ( "数据异常:" + pInfo. Name + "的值重复!" ) ;
}
if ( hasRecord. Count == 1 )
{
entity. Id = hasRecord[ 0 ] . Id;
}
#endregion
}
if ( helpAttr. CRMColName != "" )
BuildEntityAttru ( entity, helpAttr, value , pInfo) ;
}
}
return entity;
}
public static Entity BuildEntityAttru ( Entity entity, HelpAttribute helpAttr, object value , PropertyInfo pro)
{
try
{
switch ( helpAttr. CrmType)
{
case CRMTYPE. Text:
entity[ helpAttr. CRMColName. ToLower ( ) ] = value ;
break ;
case CRMTYPE. OptionSet:
{
if ( value != null && value . ToString ( ) . Trim ( ) != "" )
entity[ helpAttr. CRMColName. ToLower ( ) ] = new OptionSetValue ( int . Parse ( value . ToString ( ) ) ) ;
}
break ;
case CRMTYPE. Monty:
entity[ helpAttr. CRMColName. ToLower ( ) ] = value == null ? null : value . ToString ( ) . Trim ( ) . Length == 0 ? null : new Money ( Decimal. Parse ( value . ToString ( ) ) ) ;
break ;
case CRMTYPE. LookUP:
{
if ( value != null && value . ToString ( ) . Trim ( ) != "" )
{
object lookUP = ParseEntityReference ( helpAttr, value . ToString ( ) ) ;
if ( lookUP != null )
entity[ helpAttr. CRMColName. ToLower ( ) ] = lookUP;
else
{
entity[ helpAttr. CRMColName. ToLower ( ) ] = null ;
}
}
else
{
entity[ helpAttr. CRMColName. ToLower ( ) ] = null ;
}
}
break ;
case CRMTYPE. LookUPWithValue:
{
if ( value != null && value . ToString ( ) . Trim ( ) != "" && ! Guid. Empty. Equals ( value ) )
{
entity[ helpAttr. CRMColName. ToLower ( ) ] = new EntityReference ( helpAttr. EntityName, new Guid ( value . ToString ( ) ) ) ;
}
else
{
entity[ helpAttr. CRMColName. ToLower ( ) ] = null ;
}
}
break ;
case CRMTYPE. Int:
entity[ helpAttr. CRMColName. ToLower ( ) ] = value == null ? value : value . ToString ( ) . Trim ( ) . Length == 0 ? 0 : int . Parse ( value . ToString ( ) ) ;
break ;
case CRMTYPE. Float:
entity[ helpAttr. CRMColName. ToLower ( ) ] = value == null ? value : value . ToString ( ) . Trim ( ) . Length == 0 ? 0.00 : float . Parse ( Convert. ToDecimal ( value ) . ToString ( "0.00" ) ) ;
break ;
case CRMTYPE. DateTime:
if ( value != null && value . ToString ( ) . Trim ( ) != "" )
{
entity[ helpAttr. CRMColName. ToLower ( ) ] = DateTime. Parse ( value . ToString ( ) ) ;
}
else
{
entity[ helpAttr. CRMColName. ToLower ( ) ] = null ;
}
break ;
case CRMTYPE. Bool:
entity[ helpAttr. CRMColName. ToLower ( ) ] = value == null ? false : value . ToString ( ) . Trim ( ) . Length == 0 ? false : ParseBool ( value . ToString ( ) ) ;
break ;
case CRMTYPE. SJZ:
entity[ helpAttr. CRMColName. ToLower ( ) ] = value == null ? decimal . Parse ( "0.00" ) : value . ToString ( ) . Trim ( ) . Length == 0 ? decimal . Parse ( "0.00" ) : ChangeToDecimal ( value , 2 ) ;
break ;
default :
break ;
}
}
catch ( Exception ex)
{
throw new ApplicationException ( "构造数据异常[实体:" + entity. LogicalName + "_属性:" + helpAttr. CRMColName + "_来源字段:" + pro. Name + "_值" + value . ToString ( ) + "]" + ex. Message) ;
}
return entity;
}
public static decimal ChangeToDecimal ( object d, int n)
{
string strDecimal = d. ToString ( ) ;
int index = strDecimal. IndexOf ( "." ) ;
if ( index == - 1 || strDecimal. Length < index + n + 1 )
{
strDecimal = string . Format ( "{0:F" + n + "}" , d) ;
}
else
{
int length = index;
if ( n != 0 )
{
length = index + n + 1 ;
}
strDecimal = strDecimal. Substring ( 0 , length) ;
}
return Decimal. Parse ( strDecimal) ;
}
public static bool ParseBool ( string value )
{
bool returnValue = false ;
if ( value == "是" ) returnValue = true ;
if ( value == "否" ) returnValue = false ;
if ( value . ToLower ( ) == "t" ) returnValue = true ;
if ( value . ToLower ( ) == "f" ) returnValue = false ;
if ( value . ToLower ( ) == "true" ) returnValue = true ;
if ( value . ToLower ( ) == "false" ) returnValue = false ;
if ( value . ToLower ( ) == "1" ) returnValue = true ;
if ( value . ToLower ( ) == "0" ) returnValue = false ;
if ( value . ToLower ( ) == "y" ) returnValue = true ;
if ( value . ToLower ( ) == "n" ) returnValue = false ;
return returnValue;
}
public static EntityReference ParseEntityReference ( HelpAttribute helpAttr, string value )
{
EntityReference entityRef = null ;
try
{
QueryExpression qe = new QueryExpression ( helpAttr. EntityName. ToLower ( ) . Trim ( ) ) ;
ConditionExpression ce = new ConditionExpression ( helpAttr. EntityIDCol, ConditionOperator. Equal, value . Trim ( ) ) ;
qe. ColumnSet. AddColumn ( helpAttr. EntityName. ToLower ( ) . Trim ( ) + "id" ) ;
qe. Criteria. AddCondition ( ce) ;
List< Entity> entity = CrmService. OrgService ( ) . RetrieveMultiple ( qe) . Entities. ToList < Entity> ( ) ;
if ( entity. Count == 0 ) return null ;
if ( entity. Count > 1 ) { throw new ApplicationException ( "获得到多个关联数据,相关数据重复[" + helpAttr. EntityIDCol + "]" ) ; }
Guid parentID = entity[ 0 ] . Id;
entityRef = new EntityReference ( helpAttr. EntityName. ToLower ( ) . Trim ( ) , parentID) ;
return entityRef;
}
catch ( Exception ex)
{
throw new ApplicationException ( ex. Message, ex) ;
}
}
[ AttributeUsage ( AttributeTargets. Property, AllowMultiple = true , Inherited = false ) ]
public class HelpAttribute : Attribute
{
protected string _CRMColName;
protected CRMTYPE _CrmType;
protected bool _CanNullInCRM;
protected string _EntityName;
protected string _EntityIDCol;
protected object _DefaultValue;
protected bool _ISID;
public string CRMColName { get { return _CRMColName; } }
public CRMTYPE CrmType { get { return _CrmType; } }
public bool CanNullInCRM { get { return _CanNullInCRM; } }
public string EntityName { get { return _EntityName; } }
public string EntityIDCol { get { return _EntityIDCol; } }
public object DefaultValue { get { return _DefaultValue; } }
public bool ISID { get { return _ISID; } }
public HelpAttribute ( string CRMColName, CRMTYPE CrmType, bool CanNullInCRM, string EntityName, string EntityIDCol, object DefaultValue, bool ISID)
{
this . _CRMColName = CRMColName;
this . _CrmType = CrmType;
this . _CanNullInCRM = CanNullInCRM;
this . _EntityName = EntityName;
this . _EntityIDCol = EntityIDCol;
this . _DefaultValue = DefaultValue;
this . _ISID = ISID;
}
public enum CRMTYPE : uint
{
LookUP = 1 ,
OptionSet = 2 ,
DateTime = 3 ,
Text = 4 ,
Monty = 5 ,
Int = 6 ,
Float = 7 ,
Bool = 8 ,
LookUPWithValue = 9 ,
SJZ = 10
}
}