crm4.0添加属性

/// <summary>
    /// CRM 4.0
    /// </summary>
    public class AttrCollection : System.Collections.IEnumerable
    {
        private List<Property> list = new List<Property>();
        // 实体名称
        private string entityName;
        public string EntityName
        {
            get { return entityName; }
            set { entityName = value; }
        }

        private ActionMode action = ActionMode.Create;
        public ActionMode Action
        {
            get { return action; }
            set { action = value; }
        }

        //主键
        private KeyProperty key;
        public KeyProperty Key
        {
            get { return key; }
            private set { key = value; }
        }

        public int Length {
            get { return list.Count; }
        }

        public void Clear()
        {
            list.Clear();
        }

        public void Add(Property p)
        {
            list.Add(p);
        }

        public void AddKey(string attrName,string value)
        {
            AddKey(attrName, new Guid(value.ToUpper()));
        }

        public void AddKey(string attrName, Guid value)
        {
            KeyProperty p = new KeyProperty() { Name = attrName, Value = new Key() { Value = value } };
            Add(p);
            key = p;
        }

        public void AddLookup(string attrName,Guid value,string lookName)
        {
            if (value!=Guid.Empty && IsNotNull(value))
            {
                LookupProperty p = new LookupProperty() { Name = attrName, Value = new Lookup() { name = lookName } };
                p.Value.Value = value;
                Add(p);
            }
            else
            {
                if (Action == ActionMode.Update)
                {
                    LookupProperty p = new LookupProperty() { Name = attrName, Value = new Lookup() { name = lookName } };
                    p.Value.IsNull = true;
                    p.Value.IsNullSpecified = true;
                    Add(p);
                }
            }
        }

        public void AddLookup(string attrName, string value, string lookName)
        {
            Guid v = Guid.Empty;
            if (IsNotNull(value)) { v = new Guid(value.ToUpper()); }
            AddLookup(attrName, v, lookName);
        }

        public void AddMoney(string attrName, decimal value,decimal defaultvalue)
        {
            if (value > defaultvalue)
            {
                CrmMoneyProperty p = new CrmMoneyProperty() { Name = attrName, Value = new CrmMoney(value) };
                Add(p);
            }
            else
            {
                if (Action == ActionMode.Update)
                {
                    CrmMoneyProperty p = new CrmMoneyProperty() { Name = attrName, Value = new CrmMoney() };
                    p.Value.IsNull = true;
                    p.Value.IsNullSpecified = true;
                    Add(p);
                }
            }
        }

        public void AddMoney(string attrName, decimal value)
        {
            AddMoney(attrName, value, 0);
        }

        public void AddString(string attrName,string value)
        {
            if (IsNotNull(value))
            {
                StringProperty p = new StringProperty() { Name = attrName,Value = value };
                Add(p);
            }
            else
            {
                if (Action == ActionMode.Update)
                {
                    StringProperty p = new StringProperty() { Name = attrName,Value = string.Empty };
                    Add(p);
                }
            }
        }

        public void AddNumber(string attrName,int value)
        {
            AddNumber(attrName, value, 0);
        }

        public void AddNumber(string attrName, int value,int defaultvalue)
        {
            if (value > defaultvalue)
            {
                CrmNumberProperty p = new CrmNumberProperty() { Name = attrName, Value = new CrmNumber() { Value = value } };
                Add(p);
            }
            else {
                if (Action == ActionMode.Update)
                {
                    CrmNumberProperty p = new CrmNumberProperty() { Name = attrName, Value = new CrmNumber() { } };
                    p.Value.IsNull = true;
                    p.Value.IsNullSpecified = true;
                    Add(p);
                }
            }
        }

        public void AddOwnerId(string value)
        {
            Guid v = Guid.Empty;
            if (IsNotNull(value)) { v = new Guid(value.ToUpper()); }
            AddOwnerId("ownerid", v);
        }

        public void AddOwnerId(string attrName,string value)
        {
            Guid v = Guid.Empty;
            if (IsNotNull(value)) { v = new Guid(value.ToUpper()); }
            AddOwnerId(attrName, v);
        }

        public void AddOwnerId(Guid value)
        {
            AddOwnerId("ownerid", value);
        }

        public void AddOwnerId(string attrName,Guid value)
        {
            if (value!=Guid.Empty && IsNotNull(value))
            {
                OwnerProperty p = new OwnerProperty() { Name = attrName, Value = new Owner() { Value = value } };
                Add(p);
            }
            else
            {
                if (Action == ActionMode.Update)
                {
                    OwnerProperty p = new OwnerProperty() { Name = attrName, Value = new Owner() { } };
                    p.Value.IsNull = true;
                    p.Value.IsNullSpecified = true;
                    Add(p);
                }  
            }
        }

        public void AddDateTime(string attrName,string value)
        {
            if (IsNotNull(value))
            {
                CrmDateTimeProperty p = new CrmDateTimeProperty() { Name = attrName, Value = new CrmDateTime() { Value = value } };
                Add(p);
            }
            else {
                if (Action == ActionMode.Update)
                {
                    CrmDateTimeProperty p = new CrmDateTimeProperty() { Name = attrName, Value = new CrmDateTime() { } };
                    p.Value.IsNull = true;
                    p.Value.IsNullSpecified = true;
                    Add(p);
                }
            }
        }

        public void AddFloat(string attrName,double value,double defaultvalue)
        {
            if (value > defaultvalue)
            {
                CrmFloatProperty p = new CrmFloatProperty() { Name = attrName, Value = new CrmFloat() { Value = value } };
                Add(p);
            }
            else
            {
                if (Action == ActionMode.Update) {
                    CrmFloatProperty p = new CrmFloatProperty() { Name = attrName, Value = new CrmFloat() { } };
                    p.Value.IsNull = true;
                    p.Value.IsNullSpecified = true;
                    Add(p);
                }
            }
        }

        public void AddFloat(string attrName, double value)
        {
            AddFloat(attrName, value, 0);
        }

        public void AddPickList(string attrName, int value, int defaultvalue)
        {
            if (value > defaultvalue)
            {
                PicklistProperty p = new PicklistProperty() { Name = attrName, Value = new Picklist() { name = attrName } };
                p.Value.Value = value;
                Add(p);
            }
            else
            {
                if (Action == ActionMode.Update) {
                    PicklistProperty p = new PicklistProperty() { Name = attrName, Value = new Picklist() { name = attrName } };
                    p.Value.IsNull = true;
                    p.Value.IsNullSpecified = true;
                    Add(p);
                }
            }
        }

        public void AddPickList(string attrName, int value)
        {
            AddPickList(attrName, value, 0);
        }

        public void AddCustomer(string attrName,Guid value)
        {
            if (value != Guid.Empty)
            {
                CustomerProperty p = new CustomerProperty() { Name = attrName, Value = new Customer() { name = "account" } };
                p.Value.Value = value;
                Add(p);
            }
            else {
                if (Action == ActionMode.Update)
                {
                    CustomerProperty p = new CustomerProperty() { Name = attrName, Value = new Customer() { name = "account" } };
                    p.Value.IsNull = true;
                    p.Value.IsNullSpecified = true;
                    Add(p);
                }
            }
        }

        public void AddCustomer(string attrName, Guid value,string lookName)
        {
            if (value != Guid.Empty)
            {
                CustomerProperty p = new CustomerProperty() { Name = attrName, Value = new Customer() { name = lookName } };
                p.Value.Value = value;
                Add(p);
            }
            else
            {
                if (Action == ActionMode.Update)
                {
                    CustomerProperty p = new CustomerProperty() { Name = attrName, Value = new Customer() { name = lookName } };
                    p.Value.IsNull = true;
                    p.Value.IsNullSpecified = true;
                    Add(p);
                }
            }
        }

        public void AddCustomer(string attrName,string value)
        {
            Guid v = Guid.Empty;
            if (IsNotNull(value)) { v = new Guid(value.ToUpper()); }
            AddCustomer(attrName, v);
        }

        public void AddCustomer(string attrName, string value,string lookName)
        {
            Guid v = Guid.Empty;
            if (IsNotNull(value)) { v = new Guid(value.ToUpper()); }
            AddCustomer(attrName, v, lookName);
        }

        public void AddState(string attrName, string value)
        {
            if (IsNotNull(value))
            {
                StateProperty p = new StateProperty() { Name = attrName, Value = value };
                Add(p);
            }
            else {
                if (Action == ActionMode.Update) {
                    StateProperty p = new StateProperty() { Name = attrName,Value = string.Empty }; 
                    Add(p);
                }
            }
        }

        public void AddStatus(string attrName, int value)
        {
            if (value > 0)
            {
                StatusProperty p = new StatusProperty() { Name = attrName, Value = new Status() { name = "statuscode" } };
                p.Value.Value = value;
                Add(p);
            }
            else {
                if (Action == ActionMode.Update) {
                    StatusProperty p = new StatusProperty() { Name = attrName, Value = new Status() { name = "statuscode" } };
                    p.Value.IsNull = true;
                    p.Value.IsNullSpecified = true;
                    Add(p);
                }
            }
        }

        public System.Collections.IEnumerator GetEnumerator()
        {
            return list.GetEnumerator();
        }

        public Property[] GetPropertys()
        {
            return list.ToArray();
        }

        private bool IsNotNull(object o)
        {
            return o != null && o != DBNull.Value && !string.IsNullOrEmpty(o.ToString());
        }
    }

    public enum ActionMode
    {
        Create = 1 ,
        Update = 2
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值