예제 #1
0
        public ActionResult AddToCart()
        {
            UserMessage UserMes = new UserMessage();

            UserMes = Session["UserMessage"] as UserMessage;
            //UserMes.id = 35;
            SingleOrder myOrder = new SingleOrder();

            List <TB_ORDER> ShopCart = new List <TB_ORDER>();

            //dbOrd = new Orders();
            ShopCart = dbOrd.Database.SqlQuery <TB_ORDER>(
                "SELECT * FROM TB_ORDER WHERE U_ID = " + UserMes.id + " AND S_ID = " + productMes.s_id + " AND STATE = 1").ToList();
            if (ShopCart.Count == 0)
            {
                string  sql         = "SELECT ORDERZIZENG.nextval FROM dual";
                decimal nextOrderID = dbOrd.Database.SqlQuery <decimal>(sql).ToList().First();

                myOrder.contain.O_ID = nextOrderID;
                myOrder.contain.P_ID = productMes.id;
                myOrder.contain.S_ID = productMes.s_id;
                myOrder.contain.NUM  = Convert.ToInt16(Request.Form["points"]);

                myOrder.order.ID          = nextOrderID;
                myOrder.order.S_ID        = productMes.s_id;
                myOrder.order.U_ID        = UserMes.id;
                myOrder.order.STATE       = 1;
                myOrder.order.TIME        = DateTime.Now;
                myOrder.order.TOTAL_PRICE = productMes.new_price * myOrder.contain.NUM;
                if (ModelState.IsValid)
                {
                    dbOrd.TB_ORDER.Add(myOrder.order);
                    dbOrd.SaveChanges();
                    dbCon.TB_CONTAINS.Add(myOrder.contain);
                    dbCon.SaveChanges();
                    //  UpdateProductNum(dbCon.Database.SqlQuery<TB_CONTAINS>("select * from TB_CONTAINS WHERE O_ID = " + nextOrderID).ToList());
                }
            }
            else
            {
                short ProductNum = Convert.ToInt16(Request.Form["points"]);
                myOrder.order = ShopCart.First();
                List <TB_CONTAINS> Contain = new List <TB_CONTAINS>();
                Contain = dbCon.Database.SqlQuery <TB_CONTAINS>(
                    "SELECT * FROM TB_CONTAINS WHERE O_ID = " + myOrder.order.ID + " AND P_ID = " + productMes.id).ToList();
                if (Contain.Count == 0)
                {
                    myOrder.contain.O_ID = myOrder.order.ID;
                    myOrder.contain.P_ID = productMes.id;
                    myOrder.contain.S_ID = productMes.s_id;
                    myOrder.contain.NUM  = ProductNum;
                    if (ModelState.IsValid)
                    {
                        dbCon.TB_CONTAINS.Add(myOrder.contain);
                        dbCon.SaveChanges();
                    }
                }
                else
                {
                    myOrder.contain      = Contain.First();
                    myOrder.contain.NUM += ProductNum;
                    if (ModelState.IsValid)
                    {
                        dbCon.Entry(myOrder.contain).State = EntityState.Modified;
                        dbCon.SaveChanges();
                    }
                }
                myOrder.order.TIME         = DateTime.Now;
                myOrder.order.TOTAL_PRICE += productMes.new_price * ProductNum;
                if (ModelState.IsValid)
                {
                    dbOrd.Entry(myOrder.order).State = EntityState.Modified;
                    dbOrd.SaveChanges();
                }
            }
            return(RedirectToAction("Index", "ShoppingCart"));
        }