基本信息
源码名称:mvc4 PetsStore微软实例源码下载
源码大小:2.47M
文件格式:.zip
开发语言:C#
更新时间:2014-07-25
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PetsStore.EntitiesRepositories;
using PetsStore.Models;
using PetsStore.ViewModelServices;
using PetsStore.ViewModel;

namespace PetsStore.Controllers
{
    [Authorize]
    public class UserManagerController : Controller
    {
        MyMembership myMembership = new MyMembership();
        UserRepository userRepository = new UserRepository();
        CartRepository cartRepository = new CartRepository();
        UserManagerService userManagerService = new UserManagerService();
        ShippingRepository shippingRepository = new ShippingRepository();
        OrderRepository orderRepository = new OrderRepository();
        OrderDetailsRepository orderDetailsRepository = new OrderDetailsRepository();
        TransactionRepository transactionRepository = new TransactionRepository();
        PetRepository petRepository = new PetRepository();
        ReviewRepository reviewRepository = new ReviewRepository();
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult LogOut()
        {
            myMembership.UserLogOut();
            return RedirectToAction("Index", "Home");
        }
        [HttpPost]
        public ActionResult AddCart(Cart cart)
        {
            cart.UserID = userRepository.GetByUserName(HttpContext.User.Identity.Name).ID;
            if (ModelState.IsValid)
            {
                
                if (cartRepository.Insert(cart))
                {
                    return Json(true);
                }
                return Json(false);
            }
            else
            {
                return Json(false);
            }
        }

        public ActionResult MyCarts()
        {
            var username = User.Identity.Name;
            return View(userManagerService.getUserManager_MyCarts(username));
        }
        [HttpPost]
        public ActionResult UpdateQuantity(int CartID, int Quantity)
        {
            var user = userRepository.GetByUserName(HttpContext.User.Identity.Name);
            cartRepository.UpdateCartQuantity(CartID, Quantity);
            var cart = cartRepository.GetByCartID(CartID);
            return Json(new AjaxCart { Quantity = cart.Quantity, Amount = cart.Pet.Price * cart.Quantity, CartAmount = user.Carts.Sum(r => r.Quantity * r.Pet.Price) });
        }
        [HttpPost]
        public ActionResult DeleteCart(int id)
        {
            var user = userRepository.GetByUserName(HttpContext.User.Identity.Name);
            
            if (cartRepository.DeleteCart(id))
            {
                return Json(new AjaxCart { IsSuccess = true, CartAmount = user.Carts.Sum(r => r.Pet.Price * r.Quantity) });
            }
            return Json(new AjaxCart { IsSuccess = false, CartAmount = user.Carts.Sum(r => r.Pet.Price * r.Quantity) });
        }
        [HttpPost]
        public ActionResult ConfirmationOrder(List<int> checkcartid)
        {
            var user = userRepository.GetByUserName(HttpContext.User.Identity.Name);
            UserManager_CreateOrder userManager_CreateOrder = new UserManager_CreateOrder();
            userManager_CreateOrder.User = user;
            userManager_CreateOrder.Shippings = shippingRepository.GetByUserID(user.ID);
            List<OrderDetail> orderdetails = new List<OrderDetail>();
            if(checkcartid!=null)
            {
                foreach (int cartid in checkcartid)
                {
                    var cart = cartRepository.GetByCartID(cartid);
                    var orderdetail = new OrderDetail()
                    {
                        PetID = cart.PetID,
                        Quantity = cart.Quantity,
                        Pet=cart.Pet
                    };
                    orderdetails.Add(orderdetail);
                }
            }
            userManager_CreateOrder.OrderDetails = orderdetails;
            return View(userManager_CreateOrder);
        }
        [HttpPost]
        public ActionResult CreateOrder(int selectShpping, List<int> PetID, string Message,int[] Quantity)
        {
            string orderCode = DateTime.Now.ToString("yyyyMMddhhmmss")   new Random().Next(1000, 9999).ToString();
            int userID = userRepository.GetByUserName(HttpContext.User.Identity.Name).ID;
            Order order = new Order
            {
                DeleteForUser = false,
                Message = Message,
                OrderCode = orderCode,
                GeneratedTime = DateTime.Now,
                OrderStatus = true,
                PaymentStatus = false,
                ShippingStatus = "未发货",
                ShippingID = selectShpping,
                UserID = userID
            };
            if (orderRepository.Insert(order))
            {
                List<OrderDetail> orderdetails = new List<OrderDetail>();
                Order orderfromdata = orderRepository.GetByOrderCode(orderCode);
                int i = 0;
                foreach (int petID in PetID)
                {
                    OrderDetail orderDetail = new OrderDetail
                    {
                        PetID = petID,
                        Quantity = Quantity[i],
                        OrderID = orderfromdata.ID
                    };
                    orderdetails.Add(orderDetail);

                    //冲购物车中删除该宠物
                    Cart cart = cartRepository.GetByPetIDAndUserID(petID, userID);
                    cartRepository.DeleteCart(cart.ID);
                    i  = 1;
                }
                if (orderDetailsRepository.Insert(orderdetails))
                {


                    return RedirectToAction("MyOrders");
                }
                return Content("失败");
            }
            return Content("失败");
        }
        public ActionResult AddressBooks()
        {
            UserManager_AddressBooks usermanager_AddressBooks = userManagerService.GetUserManager_AddressBooks(shippingRepository);
            return View(usermanager_AddressBooks);
        }
        [HttpPost]
        public ActionResult AddressBooks(Shipping shipping)
        {
            if (ModelState.IsValid)
            {
                if (shippingRepository.Insert(shipping))
                {
                    return RedirectToAction("AddressBooks");
                }
                else
                {
                    ModelState.AddModelError("", "失败");
                    return View(userManagerService.GetUserManager_AddressBooks(shippingRepository));
                }
            }
            else
            {
                return View(userManagerService.GetUserManager_AddressBooks(shippingRepository));
            }
        }

        public ActionResult MyOrders()
        {
            var username = HttpContext.User.Identity.Name;
            var user = userRepository.GetByUserName(username);
            var orders = orderRepository.GetByUserID(user.ID);
            return View(orders);
        }
        
        [HttpPost]
        public ActionResult DeleteOrder(int id)
        {
            if (orderRepository.UserDeleteOrder(id))
            {
                return Json(true);
            }
            else
            {
                return Json(false);
            }
        }

        [HttpPost]
        public ActionResult CancelOrder(int id)
        {
            if (orderRepository.CancelOrder(id))
            {
                return Json(true);
            }
            return Json(false);
        }

        [HttpPost]
        public ActionResult Payment(int id)
        {
            
            if (orderRepository.Payment(id))
            {
                //添加到创建交易记录
                Order order = orderRepository.GetByOrderID(id);
                foreach (var orderDetail in order.OrderDetails)
                {
                    Transaction transaction = new Transaction()
                    {
                        Bid = orderDetail.Pet.Price,
                        PetID = orderDetail.PetID,
                        Quantity = orderDetail.Quantity,
                        TurnoverTime = orderDetail.Order.GeneratedTime,
                        UserID = orderDetail.Order.UserID
                    };
                    transactionRepository.Insert(transaction);
                }
                return Json(true);
            }
            return Json(false);
        }

        [HttpPost]
        public ActionResult ConfirmReceipt(int id)
        {
            if (orderRepository.ConfirmReceipt(id))
                return Json(true);
            return Json(false);
        }

        public ActionResult ShowOrder(int id)
        {
            return View(orderRepository.GetByOrderID(id));
        }

        public ActionResult PostComment(int PetID)
        {
            ViewBag.PetID=PetID;
            return View(petRepository.GetByID(PetID).Reviews);
        }
        [HttpPost]
        public ActionResult PostComment(Review review)
        {
            review.ReviewTime = DateTime.Now;
            review.UserID = userRepository.GetByUserName(HttpContext.User.Identity.Name).ID;
            var petid=review.PetID;
            if (reviewRepository.Insert(review))
            {

                return RedirectToAction("PostComment", new { PetID = review.PetID });
            }
            return View();
        }
    }
}