Esempio n. 1
0
        public object Do_PreOrder(BaseApi baseApi)
        {
            List <PreOrderParam> preOrderParamList = JsonConvert.DeserializeObject <List <PreOrderParam> >(baseApi.param.ToString());

            if (preOrderParamList == null || preOrderParamList.Count == 0)
            {
                throw new ApiException(CodeMessage.InvalidParam, "InvalidParam");
            }

            PreOrder preOrder = new PreOrder();
            OrderDao orderDao = new OrderDao();

            string memberId = Utils.GetMemberID(baseApi.token);
            Store  store    = orderDao.GetStoreByMemberId(memberId);

            if (store == null)
            {
                throw new ApiException(CodeMessage.BindStoreFirst, "BindStoreFirst");
            }
            preOrder.addr = store.storeAddr;

            string[] goodsIds = new string[preOrderParamList.Count];
            for (int i = 0; i < preOrderParamList.Count; i++)
            {
                goodsIds[i] = preOrderParamList[i].goodsId;
            }
            List <Goods> goodsList = orderDao.GetGoodsByGoodsIds(goodsIds);

            int total = 0;
            List <PreOrderGoods> list = new List <PreOrderGoods>();

            foreach (Goods goods in goodsList)
            {
                var preOrderParam = preOrderParamList.Find
                                    (
                    item => item.goodsId.Equals(goods.goodsId)
                                    );
                if (preOrderParam == null)
                {
                    throw new ApiException(CodeMessage.InvalidGoods, "InvalidGoods");
                }
                if (preOrderParam.goodsNum < 0)
                {
                    throw new ApiException(CodeMessage.ErrorNum, "ErrorNum");
                }
                if (preOrderParam.goodsNum <= goods.goodsStock)
                {
                    total += goods.goodsPrice * preOrderParam.goodsNum;
                    PreOrderGoods preOrderGoods = new PreOrderGoods
                    {
                        cartId     = preOrderParam.cartId,
                        goodsNum   = preOrderParam.goodsNum,
                        goodsId    = goods.goodsId,
                        goodsImg   = goods.goodsImg,
                        goodsName  = goods.goodsName,
                        goodsPrice = goods.goodsPrice,
                    };
                    list.Add(preOrderGoods);
                }
                else
                {
                    throw new ApiException(CodeMessage.NotEnoughGoods, "NotEnoughGoods");
                }
            }
            preOrder.list       = list;
            preOrder.total      = total;
            preOrder.storeCode  = store.storeCode;
            preOrder.preOrderId = Guid.NewGuid().ToString();

            Utils.SetCache(preOrder.preOrderId, preOrder, 0, 5, 0);
            return(preOrder);
        }
Esempio n. 2
0
        public object Do_StartQBuyGoods(BaseApi baseApi)
        {
            StartQBuyGoodsParam startQBuyGoodsParam = JsonConvert.DeserializeObject <StartQBuyGoodsParam>(baseApi.param.ToString());

            if (startQBuyGoodsParam == null)
            {
                throw new ApiException(CodeMessage.InvalidParam, "InvalidParam");
            }

            ActiveDao activeDao = new ActiveDao();
            var       qBuyGoods = activeDao.GetQbuyGoodsByQBuyIdAndQBuyGoodsId(startQBuyGoodsParam.qBuyCode, startQBuyGoodsParam.qBuyGoodsId);

            PreOrder  preOrder  = new PreOrder();
            OrderDao  orderDao  = new OrderDao();
            MemberDao memberDao = new MemberDao();
            string    memberId  = Utils.GetMemberID(baseApi.token);
            Store     store     = orderDao.GetStoreByMemberId(memberId);

            if (store == null)
            {
                throw new ApiException(CodeMessage.BindStoreFirst, "BindStoreFirst");
            }
            preOrder.addr = store.storeAddr;

            string[] goodsIds = new string[1];
            goodsIds[0] = qBuyGoods.goodsId;

            List <Goods> goodsList = orderDao.GetGoodsByGoodsIds(goodsIds);

            int total = 0;
            List <PreOrderGoods> list = new List <PreOrderGoods>();

            foreach (Goods goods in goodsList)
            {
                if (qBuyGoods.goodsId != goods.goodsId)
                {
                    throw new ApiException(CodeMessage.InvalidGoods, "InvalidGoods");
                }
                if (Convert.ToInt32(qBuyGoods.num) < 0)
                {
                    throw new ApiException(CodeMessage.ErrorNum, "ErrorNum");
                }
                if (Convert.ToInt32(qBuyGoods.num) <= goods.goodsStock)
                {
                    total += Convert.ToInt32(qBuyGoods.price) * Convert.ToInt32(qBuyGoods.num);
                    PreOrderGoods preOrderGoods = new PreOrderGoods
                    {
                        goodsNum   = Convert.ToInt32(qBuyGoods.num),
                        goodsId    = goods.goodsId,
                        goodsImg   = goods.goodsImg,
                        goodsName  = goods.goodsName,
                        goodsPrice = Convert.ToInt32(qBuyGoods.price),
                    };
                    list.Add(preOrderGoods);
                }
                else
                {
                    throw new ApiException(CodeMessage.NotEnoughGoods, "NotEnoughGoods");
                }
            }
            preOrder.list      = list;
            preOrder.total     = total;
            preOrder.storeCode = store.storeCode;

            MemberInfo memberInfo = memberDao.GetMemberInfo(memberId);

            if (memberInfo.heart < Convert.ToInt32(total))
            {
                throw new ApiException(CodeMessage.NotEnoughHearts, "NotEnoughHearts");
            }

            string orderCode = preOrder.storeCode + memberId.PadLeft(6, '0') + DateTime.Now.ToString("yyyyMMddHHmmss");

            if (!activeDao.updateQBuy(startQBuyGoodsParam.qBuyCode, orderCode))
            {
                throw new ApiException(CodeMessage.PayForOrderError, "PayForOrderError");
            }
            if (!orderDao.InsertOrder(memberId, orderCode, preOrder, startQBuyGoodsParam.qBuyCode, preOrder.addr, 0))
            {
                throw new ApiException(CodeMessage.CreateOrderError, "CreateOrderError");
            }
            Order order = orderDao.GetOrderInfoByCode(orderCode);

            if (!orderDao.PayForOrder(memberId, order, memberInfo.heart))
            {
                throw new ApiException(CodeMessage.PayForOrderError, "PayForOrderError");
            }
            return("");
        }