public ResultMessage <CustomerAttributeModel> GetCustomerAttribute(int customerId, string key)
        {
            var attributes = _attributeService.GetAttributeByCustomerId(customerId);
            var attribute  = attributes.FirstOrDefault(a => a.Key == key);

            return(new ResultMessage <CustomerAttributeModel>(attribute.MapTo <CustomerAttributeModel>()));
        }
Esempio n. 2
0
        public static void SaveCustomerAttribute <TPropType>(this Customer customer, string key, TPropType value,
                                                             ICustomerAttributeService _customerAttributeService)
        {
            if (customer == null || customer.Id == 0)
            {
                throw new ArgumentNullException("customer");
            }

            var props     = _customerAttributeService.GetAttributeByCustomerId(customer.Id);
            var attribute = new CustomerAttribute();

            if (props != null &&
                props.Count > 0 &&
                props.FirstOrDefault(ga =>
                                     ga.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase)) != null)
            {
                attribute = props.FirstOrDefault(ga =>
                                                 ga.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase));

                attribute.Value = value.ToString();
            }
            else
            {
                attribute.CustomerId = customer.Id;
                attribute.Key        = key;
                attribute.Value      = value.ToString();
            }
            _customerAttributeService.SaveAttribute(attribute);
        }
Esempio n. 3
0
        /// <summary>
        /// 获取用户的属性值
        /// </summary>
        /// <typeparam name="TPropType"></typeparam>
        /// <param name="customer"></param>
        /// <param name="key"></param>
        /// <param name="_customerAttributeService"></param>
        /// <returns></returns>
        public static TPropType GetCustomerAttributeValue <TPropType>(this Customer customer, string key,
                                                                      ICustomerAttributeService _customerAttributeService)
        {
            if (customer == null)
            {
                throw new ArgumentNullException("customer");
            }

            var props = _customerAttributeService.GetAttributeByCustomerId(customer.Id);

            if (props == null)
            {
                return(default(TPropType));
            }
            if (props.Count == 0)
            {
                return(default(TPropType));
            }

            var prop = props.FirstOrDefault(ga =>
                                            ga.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase)); //should be culture invariant

            if (prop == null || string.IsNullOrEmpty(prop.Value))
            {
                return(default(TPropType));
            }

            return(CommonHelper.To <TPropType>(prop.Value));
        }