コード例 #1
0
        public CustomerAddress SaveCustomerAddress(FormCollection c, string userid)
        {
            CustomerAddress address = new CustomerAddress();

            address.CopyProperty(c.ToDic());

            if (address.CustomerId.IsNullOrTrimEmpty() == false)
            {
                if (address.Id.IsNullOrTrimEmpty() == false)
                {
                    address = context.CustomerAddress.SingleOrDefault(a => a.Id.Equals(address.Id) && a.IsDeleted != 1);
                    if (address == null)
                    {
                        return(null);
                    }
                    address.CopyProperty(c.ToDic());
                    address.ModifiedBy = userid;
                    address.IsDeleted  = 0;
                    address.ModifiedOn = DateTime.Now;
                }
                else
                {
                    address.Id = FCake.Core.Common.DataHelper.GetSystemID();
                    context.CustomerAddress.Add(address);
                    address.IsDeleted = 0;
                    address.CreatedBy = userid;
                    address.CreatedOn = DateTime.Now;
                }

                if (address.IsDef == 0)
                {
                    string id = address.Id;
                    //把之前的默认地址先更新为非默认
                    var ar = context.CustomerAddress.Where(p => p.IsDef == 0 && p.Id.Equals(id) == false && p.CustomerId == address.CustomerId && p.IsDeleted == 0);
                    foreach (var x in ar)
                    {
                        x.IsDef = 1;
                    }
                }

                //若该客户从无地址则设为默认
                var cusAddrs = context.CustomerAddress.Where(a => a.CustomerId == address.CustomerId && a.IsDeleted != 1);
                if (cusAddrs.Any() == false)
                {
                    address.IsDef = 0;
                }

                if (address.LogisticsSiteId.IsNullOrTrimEmpty() == false)
                {
                    var siteAddr = context.LogisticsSite.SingleOrDefault(l => l.Id.Equals(address.LogisticsSiteId) && l.IsDeleted != 1);
                    address.Province = siteAddr.SiteProvince;
                    address.City     = siteAddr.SiteCity;
                    address.Area     = siteAddr.SiteArea;
                    address.Address  = siteAddr.SiteAddress;
                }

                context.SaveChanges();
                return(address);
            }

            return(null);
        }