Ejemplo n.º 1
0
        public ActionResult DeleteData(string type, FormCollection c)
        {
            try
            {
                var t    = Type.GetType(type);
                var data = t.Assembly.CreateInstance(t.FullName);

                data.CopyProperty(c.ToDic());

                var f  = typeof(CommonService);
                var mi = f.GetMethods().First(a => a.IsGenericMethod && a.Name == "Delete").MakeGenericMethod(t);
                if (mi == null)
                {
                    throw new Exception("类" + type + "查无Delete方法");
                }

                string msg = mi.Invoke(new CommonService(), new object[] { data, context, UserCache.CurrentUser, true }).ToString();
                return(Json(new { validate = msg.IsNullOrTrimEmpty(), msg = msg }));
            }
            catch (Exception e)
            {
                return(Json(new { validate = false, msg = e.Message }));
            }
        }
        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);
        }