Exemplo n.º 1
0
        public ActionResult UpdateResourceTaocan(ResourceTaocanModel model)
        {
            bool ret = false;
            resourceMgt = new ResourceManagement(User.Identity.GetUserId<int>());
            List<BResource> resources = resourceMgt.FindResources(model.ResoucedId, null, 0, out total);
            if (resources == null || resources.Count == 0)
            {
                ViewBag.Message = "资源信息丢失";
                return View("Error");
            }
            BResource resource = resources[0];

            if (ModelState.IsValid)
            {
                KMBit.DAL.Resource_taocan taocan = null;
                if(model.Id>0)
                {
                    int total = 0;
                    List<BResourceTaocan> ts = resourceMgt.FindResourceTaocans(model.Id, 0, 0, out total);
                    if (total == 1)
                    {
                        taocan = ts[0].Taocan;
                    }
                }else
                {
                    taocan = new DAL.Resource_taocan();
                    taocan.CreatedBy = User.Identity.GetUserId<int>();
                    taocan.Created_time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                    taocan.Quantity = model.Quantity;
                    taocan.Resource_id = model.ResoucedId;
                }
                taocan.Serial = model.Serial;
                taocan.Area_id = model.Province != null ? (int)model.Province : 0;
                taocan.City_id = model.City!=null ?(int)model.City:0;
                taocan.NumberProvinceId = model.NumberProvince != null ? (int)model.NumberProvince : 0;
                taocan.NumberCityId = model.NumberCity != null ? (int)model.NumberCity : 0;
                taocan.Enabled = model.Enabled;
                taocan.Purchase_price = model.PurchasePrice;
                taocan.EnableDiscount = model.EnabledDiscount;
                taocan.Resource_Discount = model.Discount;
                taocan.Sale_price = model.SalePrice;
                taocan.Sp_id = model.SP != null ? (int)model.SP : 0;               
                taocan.UpdatedBy = User.Identity.GetUserId<int>();
                taocan.Updated_time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                try
                {
                    if (model.Id <= 0)
                    {
                        ret = resourceMgt.CreateResourceTaocan(taocan);
                    }
                    else
                    {
                        ret = resourceMgt.UpdateResourceTaocan(taocan);
                    }
                }
                catch(KMBitException kex)
                {
                    KMLogger.GetLogger().Error(kex);
                    ViewBag.Message = kex.Message;
                    return View("Error");
                }                
                catch(Exception ex)
                {
                    KMLogger.GetLogger().Fatal(ex);
                    ViewBag.Message = "未知错误,请联系系统管理员";
                    return View("Error");
                }
                if (ret)
                {
                    return Redirect("/Admin/ViewResourceTaoCan?resourceId=" + model.ResoucedId);
                }
                else
                {
                    ViewBag.Message = "未知错误,请联系系统管理员";
                    return View("Error");
                }
            }else
            {
                var errors = ModelState
                .Where(x => x.Value.Errors.Count > 0)
                .Select(x => new { x.Key, x.Value.Errors })
                .ToArray();
                ViewBag.Message = "资源套餐创建失败";
                return View("Error");
            }
            List<KMBit.DAL.Area> provinces = null;
            List<KMBit.DAL.Sp> sps = null;
            provinces = resourceMgt.GetAreas(0);
            sps = resourceMgt.GetSps();
            ViewBag.Resource = resource;
            ViewBag.Provinces = new SelectList(provinces, "Id", "Name");
            ViewBag.Cities = new SelectList(resourceMgt.GetAreas(model.Province != null ? (int)model.Province : 0), "Id", "Name");
            ViewBag.SPs = new SelectList(sps, "Id", "Name");

            return View("CreateResourceTaocan",model);
        }