/// <summary> /// 获取订单金额可以获得的积分V2 /// </summary> /// <param name="context"></param> /// <returns></returns> private string ScoreGetRateV2(HttpContext context) { ScoreConfig scoreConfig = bllScore.GetScoreConfig(); if (scoreConfig != null) { string data = context.Request["data"]; decimal productFee = 0; //商品总价格 不包含邮费 OrderRequestModel orderRequestModel; //订单模型 try { orderRequestModel = ZentCloud.Common.JSONHelper.JsonToModel <OrderRequestModel>(data); } catch (Exception ex) { resp.errcode = 1; resp.errmsg = "JSON格式错误,请检查。错误信息:" + ex.Message; return(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); } #region 格式检查 if (orderRequestModel.skus == null) { resp.errcode = 1; resp.errmsg = "参数skus 不能为空"; return(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); } //相关检查 #endregion #region 商品检查 订单详情生成 ///订单详情 List <WXMallOrderDetailsInfo> detailList = new List <WXMallOrderDetailsInfo>();//订单详情 orderRequestModel.skus = orderRequestModel.skus.Distinct().ToList(); foreach (var sku in orderRequestModel.skus) { //先检查库存 ProductSku productSku = bllMall.GetProductSku(sku.sku_id); if (productSku == null) { resp.errcode = 1; resp.errmsg = "SKU不存在,请检查"; return(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); } WXMallProductInfo productInfo = bllMall.GetProduct(productSku.ProductId.ToString()); if (productInfo.IsOnSale == "0") { resp.errcode = 1; resp.errmsg = string.Format("{0}已下架", productInfo.PName); return(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); } WXMallOrderDetailsInfo detailModel = new WXMallOrderDetailsInfo(); detailModel.PID = productInfo.PID; detailModel.TotalCount = sku.count; detailModel.OrderPrice = bllMall.GetSkuPrice(productSku); detailModel.ProductName = productInfo.PName; detailModel.SkuId = productSku.SkuId; detailModel.SkuShowProp = bllMall.GetProductShowProperties(productSku.SkuId); detailList.Add(detailModel); } #endregion productFee = detailList.Sum(p => p.OrderPrice * p.TotalCount).Value;//商品费用 //物流费用旧的 // orderInfo.Transport_Fee = bllMall.CalcFreight(orderRequestModel.skus.Sum(p => p.count)); #region 运费计算 FreightModel freightModel = new FreightModel(); freightModel.receiver_province_code = orderRequestModel.receiver_province_code; freightModel.receiver_city_code = orderRequestModel.receiver_city_code; freightModel.receiver_dist_code = orderRequestModel.receiver_dist_code; freightModel.skus = orderRequestModel.skus; decimal freight = 0; string freightMsg = ""; if (!bllMall.CalcFreight(freightModel, out freight, out freightMsg)) { resp.errcode = 1; resp.errmsg = freightMsg; return(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); } WXMallOrderInfo orderInfo = new WXMallOrderInfo();//订单信息 orderInfo.Transport_Fee = freight; #endregion #region 优惠券计算 decimal discountAmount = 0;//优惠金额 bool canUseCardCoupon = false; string msg = ""; if (orderRequestModel.cardcoupon_id > 0)//有优惠券 { discountAmount = bllMall.CalcDiscountAmount(orderRequestModel.cardcoupon_id.ToString(), data, bllMall.GetCurrUserID(), out canUseCardCoupon, out msg); if (!canUseCardCoupon) { resp.errcode = 1; resp.errmsg = msg; return(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); } } //优惠券计算 #endregion //积分计算 decimal scoreExchangeAmount = 0;///积分抵扣的金额 if (orderRequestModel.use_score > 0) { scoreExchangeAmount = Math.Round(orderRequestModel.use_score / (scoreConfig.ExchangeScore / scoreConfig.ExchangeAmount), 2); } //计算 orderInfo.Product_Fee = productFee; orderInfo.TotalAmount = orderInfo.Product_Fee + orderInfo.Transport_Fee; orderInfo.TotalAmount -= discountAmount; //折扣金额 orderInfo.PayableAmount = orderInfo.TotalAmount - freight; orderInfo.TotalAmount -= scoreExchangeAmount; //积分抵现金 return(ZentCloud.Common.JSONHelper.ObjectToJson(new { errcode = 0, errmsg = orderInfo.PayableAmount })); } else { return(ZentCloud.Common.JSONHelper.ObjectToJson(new { errcode = 1, errmsg = "尚未配置订单获取积分比例" })); } }
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; currentUserInfo = bllUser.GetCurrentUserInfo(); string data = context.Request["data"]; decimal productFee = 0; //商品总价格 不包含邮费 OrderRequestModel orderRequestModel = new OrderRequestModel(); //订单模型 try { orderRequestModel = ZentCloud.Common.JSONHelper.JsonToModel <OrderRequestModel>(data); } catch (Exception ex) { resp.errcode = 1; resp.errmsg = "JSON格式错误,请检查。错误信息:" + ex.Message; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); } WXMallOrderInfo orderInfo = new WXMallOrderInfo();//订单表 orderInfo.InsertDate = DateTime.Now; orderInfo.OrderUserID = currentUserInfo.UserID; orderInfo.WebsiteOwner = bllMall.WebsiteOwner; orderInfo.Transport_Fee = 0; orderInfo.OrderID = bllMall.GetGUID(BLLJIMP.TransacType.AddMallOrder); orderInfo.OrderMemo = orderRequestModel.buyer_memo; orderInfo.MyCouponCardId = orderRequestModel.cardcoupon_id.ToString(); orderInfo.UseScore = orderRequestModel.use_score; orderInfo.Status = "待付款"; orderInfo.OrderType = 1; if (orderRequestModel.pay_type == "WEIXIN")//微信支付 { orderInfo.PaymentType = 2; } else if (orderRequestModel.pay_type == "ALIPAY")//支付宝支付 { orderInfo.PaymentType = 1; } #region 格式检查 //相关检查 if (orderRequestModel.skus == null) { resp.errcode = 1; resp.errmsg = "参数skus 不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } //相关检查 #endregion #region 商品检查 订单详情生成 ///订单详情 List <WXMallOrderDetailsInfo> detailList = new List <WXMallOrderDetailsInfo>();//订单详情 orderRequestModel.skus = orderRequestModel.skus.Distinct().ToList(); foreach (var sku in orderRequestModel.skus) { //先检查库存 ProductSku productSku = bllMall.GetProductSku(sku.sku_id); if (productSku == null) { resp.errcode = 1; resp.errmsg = "SKU不存在"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } WXMallProductInfo productInfo = bllMall.GetProduct(productSku.ProductId.ToString()); if (productInfo.IsOnSale == "0") { resp.errcode = 1; resp.errmsg = string.Format("{0}已下架", productInfo.PName); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } if (bllMall.GetSkuCount(productSku) < sku.count) { resp.errcode = 1; resp.errmsg = string.Format("{0}{1}库存余量为{2},库存不足,请减少购买数量", productInfo.PName, bllMall.GetProductShowProperties(productSku.SkuId), productSku.Stock); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } if (bllMall.IsPromotionTime(productSku)) { if (sku.count > productSku.PromotionStock) { resp.errcode = 1; resp.errmsg = string.Format("{0}{1}特卖库存余量为{2},库存不足,请减少购买数量", productInfo.PName, bllMall.GetProductShowProperties(productSku.SkuId), productSku.PromotionStock); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } } WXMallOrderDetailsInfo detailModel = new WXMallOrderDetailsInfo(); detailModel.OrderID = orderInfo.OrderID; detailModel.PID = productInfo.PID; detailModel.TotalCount = sku.count; detailModel.OrderPrice = bllMall.GetSkuPrice(productSku); detailModel.ProductName = productInfo.PName; detailModel.SkuId = productSku.SkuId; detailModel.SkuShowProp = bllMall.GetProductShowProperties(productSku.SkuId); detailList.Add(detailModel); } #endregion productFee = detailList.Sum(p => p.OrderPrice * p.TotalCount).Value;//商品费用 //物流费用 #region 运费计算 FreightModel freightModel = new FreightModel(); freightModel.skus = orderRequestModel.skus; decimal freight = 0;//运费 string freightMsg = ""; if (!bllMall.CalcFreight(freightModel, out freight, out freightMsg)) { resp.errcode = 1; resp.errmsg = freightMsg; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } orderInfo.Transport_Fee = freight; #endregion #region 优惠券计算 decimal discountAmount = 0;//优惠金额 bool canUseCardCoupon = false; string msg = ""; if (orderRequestModel.cardcoupon_id > 0)//有优惠券 { discountAmount = bllMall.CalcDiscountAmount(orderRequestModel.cardcoupon_id.ToString(), data, currentUserInfo.UserID, out canUseCardCoupon, out msg); if (!canUseCardCoupon) { resp.errcode = 1; resp.errmsg = msg; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } if (discountAmount > productFee + orderInfo.Transport_Fee) { resp.errcode = 1; resp.errmsg = "优惠券可优惠金额超过了订单总金额"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } } //优惠券计算 #endregion #region 积分计算 decimal scoreExchangeAmount = 0;///积分抵扣的金额 //积分计算 if (orderRequestModel.use_score > 0) { if (currentUserInfo.TotalScore < orderRequestModel.use_score) { resp.errcode = 1; resp.errmsg = "积分不足"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } ScoreConfig scoreConfig = bllScore.GetScoreConfig(); scoreExchangeAmount = Math.Round(orderRequestModel.use_score / (scoreConfig.ExchangeScore / scoreConfig.ExchangeAmount), 2); } #region 驿氪积分同步 #endregion //积分计算 #endregion //合计计算 orderInfo.Product_Fee = productFee; orderInfo.TotalAmount = orderInfo.Product_Fee + orderInfo.Transport_Fee; orderInfo.TotalAmount -= discountAmount; //优惠券优惠金额 orderInfo.TotalAmount -= scoreExchangeAmount; //积分优惠金额 orderInfo.PayableAmount = orderInfo.TotalAmount - freight; //应付金额 if ((productFee + orderInfo.Transport_Fee - discountAmount - scoreExchangeAmount) < orderInfo.TotalAmount) { resp.errcode = 1; resp.errmsg = "积分兑换金额不能大于订单总金额,请减少积分兑换"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } ZentCloud.ZCBLLEngine.BLLTransaction tran = new ZCBLLEngine.BLLTransaction(); try { if (!this.bllMall.Add(orderInfo, tran)) { tran.Rollback(); resp.errcode = 1; resp.errmsg = "提交失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } #region 更新优惠券使用状态 //优惠券 if (orderRequestModel.cardcoupon_id > 0 && (canUseCardCoupon == true))//有优惠券且已经成功使用 { MyCardCoupons myCardCoupon = bllCardCoupon.GetMyCardCoupon(orderRequestModel.cardcoupon_id, currentUserInfo.UserID); myCardCoupon.UseDate = DateTime.Now; myCardCoupon.Status = 1; if (!bllCardCoupon.Update(myCardCoupon, tran)) { tran.Rollback(); resp.errcode = 1; resp.errmsg = "更新优惠券状态失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } } //优惠券 #endregion #region 积分抵扣 //积分扣除 if (orderRequestModel.use_score > 0) { currentUserInfo.TotalScore -= orderRequestModel.use_score; if ( bllMall.Update(currentUserInfo, string.Format(" TotalScore-={0}", orderRequestModel.use_score), string.Format(" AutoID={0}", currentUserInfo.AutoID)) < 0) { tran.Rollback(); resp.errcode = 1; resp.errmsg = "更新用户积分失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } //积分记录 WXMallScoreRecord scoreRecord = new WXMallScoreRecord(); scoreRecord.InsertDate = DateTime.Now; scoreRecord.OrderID = orderInfo.OrderID; scoreRecord.Score = orderRequestModel.use_score; scoreRecord.Type = 1; scoreRecord.UserId = currentUserInfo.UserID; scoreRecord.WebsiteOwner = bllMall.WebsiteOwner; scoreRecord.Remark = "积分抵扣"; if (!bllMall.Add(scoreRecord)) { tran.Rollback(); resp.errcode = 1; resp.errmsg = "插入积分记录失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } //积分记录 } //积分扣除 #endregion #region 插入订单详情页及更新库存 foreach (var item in detailList) { ProductSku productSku = bllMall.GetProductSku((int)(item.SkuId)); WXMallProductInfo productInfo = bllMall.GetProduct(productSku.ProductId.ToString()); if (!this.bllMall.Add(item, tran)) { tran.Rollback(); resp.errcode = 1; resp.errmsg = "提交失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } //更新 SKU库存 productSku.Stock -= item.TotalCount; if (bllMall.IsPromotionTime(productSku)) { productSku.PromotionStock -= item.TotalCount; } if (!bllMall.Update(productSku, tran)) { tran.Rollback(); resp.errcode = 1; resp.errmsg = "更新SKU失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } } #endregion tran.Commit();//提交订单事务 } catch (Exception ex) { //回滚事物 tran.Rollback(); resp.errcode = 1; resp.errmsg = ex.Message; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(new { errcode = 0, errmsg = "ok", order_id = orderInfo.OrderID })); }