public LotPlayer() { CurShowNum = 0; sumScores = ScoreConfig.GetInst().GetConfigs(); InitLotData(); InitShow(); }
// GET: /<controller>/ public IActionResult Index() { ScoreConfig c = new ScoreConfig(); c.MaxScores = database.MaxHighScores; return(View("Index", c)); }
public ScoreConfig GetScoreConfig(string websiteOwner) { ScoreConfig scoreConfig = Get <ScoreConfig>(string.Format("WebsiteOwner='{0}'", websiteOwner)); if (scoreConfig == null) { scoreConfig = new ScoreConfig(); } return(scoreConfig); }
/// <summary> /// 获取用户的积分返利比例 /// </summary> /// <param name="userId"></param> /// <param name="websiteOwner"></param> /// <returns></returns> public decimal GetUserRebateScoreRate(string userId, string websiteOwner) { BLLDistribution bllDist = new BLLDistribution(); decimal rebateRate = 0; if (userId == null) { var defLevel = Get <UserLevelConfig>(string.Format(" [LevelType] = 'DistributionOnLine' AND [WebSiteOwner] = '{0}' AND [LevelNumber] = 0 ", websiteOwner )); if (defLevel != null) { if (decimal.TryParse(defLevel.RebateScoreRate, out rebateRate)) { rebateRate *= 0.01M; } } return(rebateRate); } var userInfo = bllUser.GetUserInfo(userId, websiteOwner); var userLevel = bllDist.GetUserLevel(userInfo); //获取积分返利比例 //先取会员级别、没有会员级别则取全局配置的规则 if (userLevel != null && userLevel.AutoId != null) { if (decimal.TryParse(userLevel.RebateScoreRate, out rebateRate)) { rebateRate *= 0.01M; } } else { ScoreConfig scoreConfig = GetScoreConfig(); if (scoreConfig != null) { if (scoreConfig.OrderScore == null) { rebateRate = 0; } else if (scoreConfig.OrderAmount > 0) { rebateRate = (decimal)scoreConfig.OrderScore / scoreConfig.OrderAmount; } } } return(rebateRate); }
public IActionResult Index(ScoreConfig config) { database.SetMaxScores(config.MaxScores); // Clear scores? if (config.ResetScores) { database.Clear(); database.SaveChangesAsync(); } return(RedirectToRoute(new { controller = "Home", action = "Index" })); }
public void ProcessRequest(HttpContext context) { ScoreConfig scoerConfig = bllScore.GetScoreConfig(); if (scoerConfig == null) { resp.errcode = 1; resp.errmsg = "没有设置积分配置"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } RequestModel requestModel = new RequestModel(); requestModel.order_amount = scoerConfig.OrderAmount; requestModel.order_score = scoerConfig.OrderScore; requestModel.exchange_amount = scoerConfig.ExchangeAmount; requestModel.exchange_score = scoerConfig.ExchangeScore; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(requestModel)); }
/// <summary> /// 获取订单金额可以获得的积分 /// </summary> /// <param name="context"></param> /// <returns></returns> private string ScoreGetRate(HttpContext context) { ScoreConfig scoreConfig = bllScore.GetScoreConfig(); //if (scoreConfig != null) //{ return(ZentCloud.Common.JSONHelper.ObjectToJson(new { errcode = 0, score = scoreConfig.OrderScore == null ? 0 : scoreConfig.OrderScore, amount = scoreConfig.OrderAmount })); //} //else //{ // return ZentCloud.Common.JSONHelper.ObjectToJson(new // { // errcode = 1, // errmsg = "尚未配置订单获取积分比例" // }); //} }
/// <summary> /// 获取积分兑换比例 /// </summary> /// <param name="context"></param> /// <returns></returns> private string ExchangeRate(HttpContext context) { ScoreConfig scoreConfig = bllScore.GetScoreConfig(); //if (scoreConfig!=null) //{ return(ZentCloud.Common.JSONHelper.ObjectToJson(new { errcode = 0, score = scoreConfig.ExchangeScore, amount = scoreConfig.ExchangeAmount })); //} //else //{ // return ZentCloud.Common.JSONHelper.ObjectToJson(new // { // errcode =1, // errmsg="尚未配置积分兑换比例" // }); //} }
private MainConfigManager() : base() { //新游戏配置开始处 _configMap.Add(typeof(ScoreConfig), ScoreConfig.GetInst().Initialize("ScoreCfg")); }
/// <summary> /// 修改积分配置 /// </summary> /// <param name="orderAmount">满多少元</param> /// <param name="orderScore">送多少积分</param> /// <param name="exchangeScore">多少积分</param> /// <param name="exchangeAmount">抵扣多少元</param> /// <returns></returns> public bool UpdateScoreConfig(string orderAmount, string orderScore, string exchangeScore, string exchangeAmount) { ScoreConfig model = GetScoreConfig(); if (model.AutoID > 0) { if (!string.IsNullOrEmpty(orderAmount)) { model.OrderAmount = int.Parse(orderAmount); } if (!string.IsNullOrEmpty(orderScore)) { model.OrderScore = int.Parse(orderScore); } if (!string.IsNullOrEmpty(exchangeAmount)) { model.ExchangeAmount = decimal.Parse(exchangeAmount); } if (!string.IsNullOrEmpty(exchangeScore)) { model.ExchangeScore = int.Parse(exchangeScore); } if (Update(model)) { return(true); } else { return(false); } } else { model = new ScoreConfig(); model.WebsiteOwner = WebsiteOwner; if (!string.IsNullOrEmpty(orderAmount)) { model.OrderAmount = int.Parse(orderAmount); } if (!string.IsNullOrEmpty(orderScore)) { model.OrderScore = int.Parse(orderScore); } if (!string.IsNullOrEmpty(exchangeAmount)) { model.ExchangeAmount = decimal.Parse(exchangeAmount); } if (!string.IsNullOrEmpty(exchangeScore)) { model.ExchangeScore = int.Parse(exchangeScore); } if (Add(model)) { return(true); } else { return(false); } } }
// Use this for initialization void Start() { scf = GameObject.FindWithTag("Config").GetComponent <ScoreConfig>(); }
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 })); }
public void ProcessRequest(HttpContext context) { string orderAmount = context.Request["order_amount"]; //订单金额 string orderScore = context.Request["order_score"]; //所得积分 string exchangeAmount = context.Request["exchange_amount"]; //积分兑换 金额 string exchangeScore = context.Request["exchange_score"]; //积分兑换 积分 ScoreConfig model = bllScore.GetScoreConfig(); if (model != null) { if (!string.IsNullOrEmpty(orderAmount)) { model.OrderAmount = int.Parse(orderAmount); } if (!string.IsNullOrEmpty(orderScore)) { model.OrderScore = int.Parse(orderScore); } if (!string.IsNullOrEmpty(exchangeAmount)) { model.ExchangeAmount = decimal.Parse(exchangeAmount); } if (!string.IsNullOrEmpty(exchangeScore)) { model.ExchangeScore = int.Parse(exchangeScore); } if (bllScore.Update(model)) { resp.errcode = 0; resp.errmsg = "ok"; resp.isSuccess = true; } else { resp.errcode = 1; resp.errmsg = "修改积分配置出错"; } } else { model = new ScoreConfig(); model.WebsiteOwner = bllScore.WebsiteOwner; if (!string.IsNullOrEmpty(orderAmount)) { model.OrderAmount = int.Parse(orderAmount); } if (!string.IsNullOrEmpty(orderScore)) { model.OrderScore = int.Parse(orderScore); } if (!string.IsNullOrEmpty(exchangeAmount)) { model.ExchangeAmount = decimal.Parse(exchangeAmount); } if (!string.IsNullOrEmpty(exchangeScore)) { model.ExchangeScore = int.Parse(exchangeScore); } if (bllScore.Add(model)) { resp.errcode = 0; resp.errmsg = "ok"; resp.isSuccess = true; } else { resp.errcode = 1; resp.errmsg = "添加积分出错"; } } context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); }
public void ProcessRequest(HttpContext context) { WebsiteInfo websiteInfo = bllMall.GetWebsiteInfoModelFromDataBase(); Open.HongWareSDK.Client hongWareClient = new Open.HongWareSDK.Client(websiteInfo.WebsiteOwner); Open.HongWareSDK.MemberInfo hongWeiWareMemberInfo = null; if (websiteInfo.IsUnionHongware == 1) { hongWeiWareMemberInfo = hongWareClient.GetMemberInfo(CurrentUserInfo.WXOpenId); if (hongWeiWareMemberInfo.member == null) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "您尚未绑定宏巍账号,请先绑定"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } } string data = context.Request["data"]; if (string.IsNullOrEmpty(data)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "data 参数必传"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } decimal productFee = 0; //商品总价格 不包含邮费 OrderModel orderRequestModel; //订单模型 try { orderRequestModel = ZentCloud.Common.JSONHelper.JsonToModel <OrderModel>(data); } catch (Exception ex) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "JSON格式错误,请检查。错误信息:" + ex.Message; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } WXMallOrderInfo orderInfo = new WXMallOrderInfo();//订单表 orderInfo.Address = orderRequestModel.receiver_address; orderInfo.Consignee = orderRequestModel.receiver_name; //orderInfo.ExpressCompanyName = orderRequestModel.express_company; orderInfo.InsertDate = DateTime.Now; orderInfo.OrderUserID = CurrentUserInfo.UserID; orderInfo.Phone = orderRequestModel.receiver_phone; orderInfo.WebsiteOwner = bllMall.WebsiteOwner; orderInfo.Transport_Fee = 0; orderInfo.OrderID = bllMall.GetGUID(BLLJIMP.TransacType.AddMallOrder); if (bllMall.WebsiteOwner != "mixblu") { orderInfo.OutOrderId = orderInfo.OrderID; } orderInfo.OrderMemo = orderRequestModel.buyer_memo; orderInfo.ReceiverProvince = orderRequestModel.receiver_province; orderInfo.ReceiverProvinceCode = orderRequestModel.receiver_province_code.ToString(); orderInfo.ReceiverCity = orderRequestModel.receiver_city; orderInfo.ReceiverCityCode = orderRequestModel.receiver_city_code.ToString(); orderInfo.ReceiverDist = orderRequestModel.receiver_dist; orderInfo.ReceiverDistCode = orderRequestModel.receiver_dist_code.ToString(); orderInfo.ZipCode = orderRequestModel.receiver_zip; orderInfo.MyCouponCardId = orderRequestModel.cardcoupon_id.ToString(); orderInfo.UseScore = orderRequestModel.use_score; orderInfo.UseAmount = orderRequestModel.use_amount; orderInfo.Status = "待付款"; orderInfo.OrderType = 2; orderInfo.LastUpdateTime = DateTime.Now; //if (!string.IsNullOrEmpty(orderRequestModel.sale_id)) //{ // long saleId = 0; // if (long.TryParse(orderRequestModel.sale_id, out saleId)) // { // orderInfo.SellerId = saleId; // } // else // { // } //} if (orderRequestModel.pay_type == "WEIXIN")//微信支付 { orderInfo.PaymentType = 2; } else if (orderRequestModel.pay_type == "ALIPAY")//支付宝支付 { orderInfo.PaymentType = 1; } #region 格式检查 //相关检查 if (string.IsNullOrEmpty(orderRequestModel.rule_id)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "rule_id 必传"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (string.IsNullOrEmpty(orderInfo.Consignee)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "收货人姓名不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (string.IsNullOrEmpty(orderInfo.Phone)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "收货人联系电话不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (string.IsNullOrEmpty(orderInfo.ReceiverProvince)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "省份名称不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (string.IsNullOrEmpty(orderInfo.ReceiverProvinceCode)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "省份代码不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (string.IsNullOrEmpty(orderInfo.ReceiverCity)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "城市名称不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (string.IsNullOrEmpty(orderInfo.ReceiverCityCode)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "城市代码不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (string.IsNullOrEmpty(orderInfo.ReceiverDist)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "城市区域名称不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (string.IsNullOrEmpty(orderInfo.ReceiverCityCode)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "城市区域代码不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (string.IsNullOrEmpty(orderInfo.Address)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "收货地址不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (orderRequestModel.skus == null) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "参数skus 不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } //相关检查 #endregion #region 分销关系建立 if (websiteInfo.DistributionRelationBuildMallOrder == 1) { if (!string.IsNullOrEmpty(orderRequestModel.sale_id)) { int saleId = 0; if (int.TryParse(orderRequestModel.sale_id, out saleId)) { orderInfo.SellerId = saleId; if (string.IsNullOrEmpty(CurrentUserInfo.DistributionOwner)) { var recommendUserInfo = bllUser.GetUserInfoByAutoID(saleId); if (recommendUserInfo != null) { var setUserDistributionOwnerResult = bllDis.SetUserDistributionOwner(CurrentUserInfo.UserID, recommendUserInfo.UserID, CurrentUserInfo.WebsiteOwner); if (setUserDistributionOwnerResult) { CurrentUserInfo.DistributionOwner = recommendUserInfo.UserID; CurrentUserInfo.Channel = recommendUserInfo.Channel; } } } } else { } } else { if (string.IsNullOrEmpty(CurrentUserInfo.DistributionOwner)) { CurrentUserInfo.DistributionOwner = bllUser.WebsiteOwner; bllUser.Update(CurrentUserInfo); } } } #endregion if (orderRequestModel.skus.Count > 1) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "只能购买一种商品"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (orderRequestModel.skus.Sum(p => p.count) > 1) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "团购商品只能购买一件"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } List <ProductGroupBuyRule> groupBuyList = new List <ProductGroupBuyRule>();//此商品的团购规则列表 #region 购买的商品 List <WXMallProductInfo> productList = new List <WXMallProductInfo>(); foreach (var sku in orderRequestModel.skus) { ProductSku productSku = bllMall.GetProductSku(sku.sku_id); if (productSku == null) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "sku_id 无效"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } // WXMallProductInfo productInfo = bllMall.GetProduct(productSku.ProductId.ToString()); //productList.Add(productInfo); //ProductSku productSku = bllMall.GetProductSku(sku.sku_id); //if (productSku == null) continue; if (productList.Count(p => p.PID == productSku.ProductId.ToString()) > 0) { continue; } WXMallProductInfo productInfo = bllMall.GetProduct(productSku.ProductId.ToString()); productList.Add(productInfo); groupBuyList = bllMall.GetProductGroupBuyRuleList(productSku.ProductId.ToString()); } if (groupBuyList.Count == 0) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "此商品不能团购"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (groupBuyList.SingleOrDefault(p => p.RuleId == int.Parse(orderRequestModel.rule_id)) == null) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "rule_id 错误,请检查"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } ProductGroupBuyRule groupBuyRule = groupBuyList.SingleOrDefault(p => p.RuleId == int.Parse(orderRequestModel.rule_id));//团购规则 // productList = productList.Distinct().ToList(); #endregion string cardCouponType = "";//优惠券类型 #region 检查优惠券是否可用 if (orderRequestModel.cardcoupon_id > 0) { var mycardCoupon = bllCardCoupon.GetMyCardCoupon(orderRequestModel.cardcoupon_id, CurrentUserInfo.UserID); if (mycardCoupon == null) { apiResp.code = 1; apiResp.msg = "无效的优惠券"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } var cardCoupon = bllCardCoupon.GetCardCoupon(mycardCoupon.CardId); if (cardCoupon == null) { apiResp.code = 1; apiResp.msg = "无效的优惠券"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } cardCouponType = cardCoupon.CardCouponType; #region 需要购买指定商品 if ((!string.IsNullOrEmpty(cardCoupon.Ex2)) && (cardCoupon.Ex2 != "0")) { if (productList.Count(p => p.PID == cardCoupon.Ex2) == 0) { var productInfo = bllMall.GetProduct(cardCoupon.Ex2); apiResp.code = 1; apiResp.msg = string.Format("此优惠券需要购买{0}时才可以使用", productInfo.PName); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } } #endregion #region 需要购买指定标签商品 if (!string.IsNullOrEmpty(cardCoupon.Ex8)) { if (productList.Where(p => p.Tags == "" || p.Tags == null).Count() == productList.Count)//全部商品都没有标签 { apiResp.code = 1; apiResp.msg = string.Format("使用此优惠券需要购买标签为{0}的商品", cardCoupon.Ex8); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } bool checkResult = true; foreach (var product in productList) { if (!string.IsNullOrEmpty(product.Tags)) { bool tempResult = false; foreach (string tag in product.Tags.Split(',')) { if (cardCoupon.Ex8.Contains(tag)) { tempResult = true; break; } } if (!tempResult) { checkResult = false; break; } } else//商品不包含标签 { checkResult = false; break; } } if (!checkResult) { apiResp.code = 1; apiResp.msg = string.Format("使用此优惠券需要购买标签为{0}的商品", cardCoupon.Ex8); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } } #endregion } #endregion var needUseScore = 0; //必须使用的积分 decimal needUseCash = 0; //必须使用的现金 #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) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "SKU不存在"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } WXMallProductInfo productInfo = productList.Single(p => p.PID == productSku.ProductId.ToString()); if (productInfo.IsOnSale == "0") { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = string.Format("{0}已下架", productInfo.PName); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (productSku.Stock < sku.count) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = string.Format("{0}{1}库存余量为{2},库存不足,请减少购买数量", productInfo.PName, bllMall.GetProductShowProperties(productSku.SkuId), productSku.Stock); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (productInfo.Score > 0)//必须使用的积分 { needUseScore += sku.count * productInfo.Score; } if (productInfo.IsCashPayOnly == 1)//必须使用的现金 { needUseCash += (Math.Round((decimal)(productSku.Price * (decimal)(groupBuyRule.HeadDiscount / 10)), 2)) * sku.count; } WXMallOrderDetailsInfo detailModel = new WXMallOrderDetailsInfo(); detailModel.OrderID = orderInfo.OrderID; detailModel.PID = productInfo.PID; detailModel.TotalCount = sku.count; detailModel.OrderPrice = Math.Round((decimal)(productSku.Price * (decimal)(groupBuyRule.HeadDiscount / 10)), 2);//四舍五入 detailModel.ProductName = productInfo.PName; detailModel.SkuId = productSku.SkuId; detailModel.SkuShowProp = bllMall.GetProductShowProperties(productSku.SkuId); detailModel.IsComplete = 1;//拼团的只要下单就算销量 detailList.Add(detailModel); } #endregion #region 纯积分购买 if (needUseScore > 0) { if ((CurrentUserInfo.TotalScore < needUseScore) || (orderRequestModel.use_score < needUseScore)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = string.Format("您需要使用{0}积分来兑换, 可用积分不足", needUseScore); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } } #endregion productFee = detailList.Sum(p => p.OrderPrice * p.TotalCount).Value;//商品费用 //物流费用 #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)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = freightMsg; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); 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) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = msg; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (cardCouponType == "MallCardCoupon_FreeFreight")//免邮券 { orderInfo.Transport_Fee = 0; } } //优惠券计算 #endregion #region 积分计算 decimal scoreExchangeAmount = 0;///积分抵扣的金额 //积分计算 if (orderRequestModel.use_score > 0) { #region 使用宏巍积分 if (websiteInfo.IsUnionHongware == 1) { CurrentUserInfo.TotalScore = hongWeiWareMemberInfo.member.point; } #endregion if (CurrentUserInfo.TotalScore < orderRequestModel.use_score) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "积分不足"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } ScoreConfig scoreConfig = bllScore.GetScoreConfig(); if (scoreConfig != null && scoreConfig.ExchangeAmount > 0) { scoreExchangeAmount = Math.Round(orderRequestModel.use_score / (scoreConfig.ExchangeScore / scoreConfig.ExchangeAmount), 2); } //scoreExchangeAmount = Math.Round(orderRequestModel.use_score / (scoreConfig.ExchangeScore / scoreConfig.ExchangeAmount), 2); } //积分计算 #endregion #region 使用账户余额 if (orderRequestModel.use_amount > 0) { if (!bllMall.IsEnableAccountAmountPay()) { apiResp.msg = "尚未启用余额支付功能"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } #region 使用宏巍余额 if (websiteInfo.IsUnionHongware == 1) { CurrentUserInfo.AccountAmount = (decimal)hongWeiWareMemberInfo.member.balance; } #endregion if (CurrentUserInfo.AccountAmount < orderRequestModel.use_amount) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "您的账户余额不足"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } } #endregion //合计计算 orderInfo.Product_Fee = productFee; orderInfo.TotalAmount = orderInfo.Product_Fee + orderInfo.Transport_Fee; orderInfo.TotalAmount -= discountAmount; //优惠券优惠金额 orderInfo.TotalAmount -= scoreExchangeAmount; //积分优惠金额 orderInfo.TotalAmount -= orderRequestModel.use_amount; //余额 orderInfo.PayableAmount = orderInfo.TotalAmount - freight; //应付金额 orderInfo.ScoreExchangAmount = scoreExchangeAmount; //优惠券抵扣金额 orderInfo.CardcouponDisAmount = discountAmount; //卡券抵扣金额 if ((productFee + orderInfo.Transport_Fee - discountAmount - scoreExchangeAmount) < orderInfo.TotalAmount) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "积分兑换金额不能大于订单总金额,请减少积分兑换"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (orderInfo.TotalAmount < 0) { orderInfo.TotalAmount = 0; } if (orderInfo.TotalAmount == 0) { orderInfo.PaymentStatus = 1; orderInfo.PayTime = DateTime.Now; orderInfo.Status = "待发货"; } orderInfo.HeadDiscount = groupBuyRule.HeadDiscount; orderInfo.MemberDiscount = groupBuyRule.MemberDiscount; orderInfo.PeopleCount = groupBuyRule.PeopleCount; orderInfo.ExpireDay = groupBuyRule.ExpireDay; if (orderInfo.TotalAmount < needUseCash) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = string.Format("最少需要支付{0}元" + needUseCash); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } ZentCloud.ZCBLLEngine.BLLTransaction tran = new ZCBLLEngine.BLLTransaction(); try { if (!this.bllMall.Add(orderInfo, tran)) { tran.Rollback(); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "提交失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); 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(); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "更新优惠券状态失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); 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), tran) < 0) { tran.Rollback(); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "更新用户积分失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } //积分记录 UserScoreDetailsInfo scoreRecord = new UserScoreDetailsInfo(); scoreRecord.AddTime = DateTime.Now; scoreRecord.Score = -orderRequestModel.use_score; scoreRecord.TotalScore = CurrentUserInfo.TotalScore; scoreRecord.ScoreType = "OrderSubmit"; scoreRecord.UserID = CurrentUserInfo.UserID; scoreRecord.AddNote = "微商城-开团使用积分"; scoreRecord.WebSiteOwner = CurrentUserInfo.WebsiteOwner; if (!bllMall.Add(scoreRecord, tran)) { tran.Rollback(); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "插入积分记录失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } #region 更新宏巍积分 if (websiteInfo.IsUnionHongware == 1) { if (!hongWareClient.UpdateMemberScore(hongWeiWareMemberInfo.member.mobile, CurrentUserInfo.WXOpenId, -orderRequestModel.use_score)) { tran.Rollback(); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "更新宏巍积分失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } } #endregion } //积分扣除 #endregion #region 余额抵扣 if (orderRequestModel.use_amount > 0 && bllMall.IsEnableAccountAmountPay()) { CurrentUserInfo.AccountAmount -= orderRequestModel.use_amount; if (bllMall.Update(CurrentUserInfo, string.Format(" AccountAmount={0}", CurrentUserInfo.AccountAmount), string.Format(" AutoID={0}", CurrentUserInfo.AutoID)) < 0) { tran.Rollback(); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "更新用户余额失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } UserScoreDetailsInfo scoreRecord = new UserScoreDetailsInfo(); scoreRecord.AddTime = DateTime.Now; scoreRecord.Score = -(double)orderRequestModel.use_amount; scoreRecord.TotalScore = (double)CurrentUserInfo.AccountAmount; scoreRecord.UserID = CurrentUserInfo.UserID; scoreRecord.AddNote = "拼团-开团使用余额"; scoreRecord.RelationID = orderInfo.OrderID; scoreRecord.WebSiteOwner = bllUser.WebsiteOwner; scoreRecord.ScoreType = "AccountAmount"; if (!bllMall.Add(scoreRecord)) { tran.Rollback(); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "插入余额记录失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } UserCreditAcountDetails record = new UserCreditAcountDetails(); record.WebsiteOwner = bllUser.WebsiteOwner; record.Operator = CurrentUserInfo.UserID; record.UserID = CurrentUserInfo.UserID; record.CreditAcount = -orderRequestModel.use_amount; record.SysType = "AccountAmount"; record.AddTime = DateTime.Now; record.AddNote = "账户余额变动-" + orderRequestModel.use_amount; if (!bllMall.Add(record)) { tran.Rollback(); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "插入余额记录失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } #region 更新宏巍余额 if (websiteInfo.IsUnionHongware == 1) { if (!hongWareClient.UpdateMemberBlance(hongWeiWareMemberInfo.member.mobile, CurrentUserInfo.WXOpenId, -(float)orderRequestModel.use_amount)) { tran.Rollback(); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "更新宏巍余额失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } } #endregion } #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(); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "提交失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } //更新 SKU库存 System.Text.StringBuilder sbUpdateStock = new StringBuilder();//更新库存sql sbUpdateStock.AppendFormat(" Update ZCJ_ProductSku Set Stock-={0} ", item.TotalCount); sbUpdateStock.AppendFormat(" Where SkuId={0} And Stock>0 ", productSku.SkuId); if (ZentCloud.ZCBLLEngine.BLLBase.ExecuteSql(sbUpdateStock.ToString(), tran) <= 0) { tran.Rollback(); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "提交订单失败,库存不足"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } } #endregion bllMall.DeleteShoppingCart(CurrentUserInfo.UserID, orderRequestModel.skus); tran.Commit();//提交订单事务 #region 宏巍通知 if (websiteInfo.IsUnionHongware == 1) { hongWareClient.OrderNotice(CurrentUserInfo.WXOpenId, orderInfo.OrderID); } #endregion #region 微信模板消息 try { string title = "订单已成功提交"; if (orderInfo.TotalAmount > 0) { title += ",请尽快付款"; } bllWeiXin.SendTemplateMessageNotifyComm(CurrentUserInfo, title, string.Format("订单号:{0}\\n订单金额:{1}元\\n收货人:{2}\\n电话:{3}", orderInfo.OrderID, orderInfo.TotalAmount, orderInfo.Consignee, orderInfo.Phone)); } catch { } #endregion } catch (Exception ex) { //回滚事物 tran.Rollback(); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "提交订单失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } apiResp.status = true; apiResp.msg = "ok"; apiResp.result = new { order_id = orderInfo.OrderID }; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); }
public void ProcessRequest(HttpContext context) { WebsiteInfo websiteInfo = bllMall.GetWebsiteInfoModelFromDataBase(); Open.HongWareSDK.Client hongWareClient = new Open.HongWareSDK.Client(websiteInfo.WebsiteOwner); Open.HongWareSDK.MemberInfo hongWeiWareMemberInfo = null; if (websiteInfo.IsUnionHongware == 1) { hongWeiWareMemberInfo = hongWareClient.GetMemberInfo(CurrentUserInfo.WXOpenId); if (hongWeiWareMemberInfo.member == null) { resp.errcode = 1; resp.errmsg = "您尚未绑定宏巍账号,请先绑定"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } } 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)); return; } 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); if (bllMall.WebsiteOwner != "mixblu") { orderInfo.OutOrderId = orderInfo.OrderID; } orderInfo.OrderMemo = orderRequestModel.buyer_memo; orderInfo.MyCouponCardId = orderRequestModel.cardcoupon_id.ToString(); orderInfo.UseScore = orderRequestModel.use_score; orderInfo.UseAmount = orderRequestModel.use_amount; orderInfo.Status = "待付款"; orderInfo.OrderType = 1; orderInfo.LastUpdateTime = DateTime.Now; 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; } if (orderRequestModel.skus.Count > 1) { resp.errcode = 1; resp.errmsg = "只能购买一个商品"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } //相关检查 #endregion #region 商品检查 订单详情生成 ///订单详情 List <WXMallOrderDetailsInfo> detailList = new List <WXMallOrderDetailsInfo>();//订单详情 orderRequestModel.skus = orderRequestModel.skus.Distinct().ToList(); #region 购买的商品 List <WXMallProductInfo> productList = new List <WXMallProductInfo>(); foreach (var sku in orderRequestModel.skus) { ProductSku productSku = bllMall.GetProductSku(sku.sku_id); if (productSku == null) { continue; } if (productList.Count(p => p.PID == productSku.ProductId.ToString()) > 0) { continue; } WXMallProductInfo productInfo = bllMall.GetProduct(productSku.ProductId.ToString()); productList.Add(productInfo); } //productList = productList.Distinct().ToList(); #endregion #region 检查优惠券是否可用 if (orderRequestModel.cardcoupon_id > 0) { var mycardCoupon = bllCardCoupon.GetMyCardCoupon(orderRequestModel.cardcoupon_id, CurrentUserInfo.UserID); if (mycardCoupon == null) { resp.errcode = 1; resp.errmsg = "无效的优惠券"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } var cardCoupon = bllCardCoupon.GetCardCoupon(mycardCoupon.CardId); if (cardCoupon == null) { resp.errcode = 1; resp.errmsg = "无效的优惠券"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } #region 需要购买指定商品 if ((!string.IsNullOrEmpty(cardCoupon.Ex2)) && (cardCoupon.Ex2 != "0")) { if (productList.Count(p => p.PID == cardCoupon.Ex2) == 0) { var productInfo = bllMall.GetProduct(cardCoupon.Ex2); resp.errcode = 1; resp.errmsg = string.Format("此优惠券需要购买{0}时才可以使用", productInfo.PName); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } } #endregion #region 需要购买指定标签商品 if (!string.IsNullOrEmpty(cardCoupon.Ex8)) { if (productList.Where(p => p.Tags == "" || p.Tags == null).Count() == productList.Count)//全部商品都没有标签 { resp.errcode = 1; resp.errmsg = string.Format("使用此优惠券需要购买标签为{0}的商品", cardCoupon.Ex8); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } bool checkResult = true; foreach (var product in productList) { if (!string.IsNullOrEmpty(product.Tags)) { bool tempResult = false; foreach (string tag in product.Tags.Split(',')) { if (cardCoupon.Ex8.Contains(tag)) { tempResult = true; break; } } if (!tempResult) { checkResult = false; break; } } else//商品不包含标签 { checkResult = false; break; } } if (!checkResult) { resp.errcode = 1; resp.errmsg = string.Format("使用此优惠券需要购买标签为{0}的商品", cardCoupon.Ex8); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } } #endregion } #endregion var needUseScore = 0; //必须使用的积分 decimal needUseCash = 0; //必须使用的现金 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()); WXMallProductInfo productInfo = productList.Single(p => p.PID == 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; } } if (productInfo.Score > 0)//必须使用的积分 { needUseScore += sku.count * productInfo.Score; } if (productInfo.IsCashPayOnly == 1)//必须使用的现金 { needUseCash += bllMall.GetSkuPrice(productSku) * sku.count; } 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 #region 纯积分购买 if (needUseScore > 0) { if ((CurrentUserInfo.TotalScore < needUseScore) || (orderRequestModel.use_score < needUseScore)) { resp.errcode = 1; resp.errmsg = string.Format("您需要使用{0}积分来兑换, 可用积分不足", needUseScore); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } } #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) { #region 使用宏巍积分 if (websiteInfo.IsUnionHongware == 1) { CurrentUserInfo.TotalScore = hongWeiWareMemberInfo.member.point; } #endregion if (CurrentUserInfo.TotalScore < orderRequestModel.use_score) { resp.errcode = 1; resp.errmsg = "积分不足"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } ScoreConfig scoreConfig = bllScore.GetScoreConfig(); if (scoreConfig == null) { resp.errcode = 1; resp.errmsg = "未配置积分兑换"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } if (scoreConfig.ExchangeScore == 0 || scoreConfig.ExchangeAmount == 0) { resp.errcode = 1; resp.errmsg = "未配置积分兑换"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } scoreExchangeAmount = Math.Round(orderRequestModel.use_score / (scoreConfig.ExchangeScore / scoreConfig.ExchangeAmount), 2); } //积分计算 #endregion #region 使用账户余额 if (orderRequestModel.use_amount > 0) { if (!bllMall.IsEnableAccountAmountPay()) { resp.errcode = 1; resp.errmsg = "尚未启用余额支付功能"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } #region 使用宏巍余额 if (websiteInfo.IsUnionHongware == 1) { CurrentUserInfo.AccountAmount = (decimal)hongWeiWareMemberInfo.member.balance; } #endregion if (CurrentUserInfo.AccountAmount < orderRequestModel.use_amount) { resp.errcode = 1; resp.errmsg = "您的账户余额不足"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } } #endregion //合计计算 orderInfo.Product_Fee = productFee; orderInfo.TotalAmount = orderInfo.Product_Fee + orderInfo.Transport_Fee; orderInfo.TotalAmount -= discountAmount; //优惠券优惠金额 orderInfo.TotalAmount -= scoreExchangeAmount; //积分优惠金额 orderInfo.TotalAmount -= orderRequestModel.use_amount; //余额 orderInfo.PayableAmount = orderInfo.TotalAmount; //应付金额 orderInfo.ScoreExchangAmount = scoreExchangeAmount; //优惠券抵扣金额 orderInfo.CardcouponDisAmount = discountAmount; //卡券抵扣金额 if ((productFee + orderInfo.Transport_Fee - discountAmount - scoreExchangeAmount) < orderInfo.TotalAmount) { resp.errcode = 1; resp.errmsg = "积分兑换金额不能大于订单总金额,请减少积分兑换"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } if (orderInfo.TotalAmount < 0) { orderInfo.TotalAmount = 0; } if (orderInfo.TotalAmount == 0) { orderInfo.PaymentStatus = 1; orderInfo.PayTime = DateTime.Now; orderInfo.Status = "待发货"; } if (orderInfo.TotalAmount < needUseCash) { resp.errcode = 1; resp.errmsg = string.Format("最少需要支付{0}元" + needUseCash); 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; } //积分记录 UserScoreDetailsInfo scoreRecord = new UserScoreDetailsInfo(); scoreRecord.AddTime = DateTime.Now; scoreRecord.Score = -orderRequestModel.use_score; scoreRecord.TotalScore = CurrentUserInfo.TotalScore; scoreRecord.ScoreType = "OrderSubmit"; scoreRecord.UserID = CurrentUserInfo.UserID; scoreRecord.AddNote = "微商城-下单使用积分"; scoreRecord.WebSiteOwner = CurrentUserInfo.WebsiteOwner; if (!bllMall.Add(scoreRecord)) { tran.Rollback(); resp.errcode = 1; resp.errmsg = "插入积分记录失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } #region 更新宏巍积分 if (websiteInfo.IsUnionHongware == 1) { if (!hongWareClient.UpdateMemberScore(hongWeiWareMemberInfo.member.mobile, CurrentUserInfo.WXOpenId, -orderRequestModel.use_score)) { tran.Rollback(); resp.errcode = 1; resp.errmsg = "更新宏巍积分失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } } #endregion //积分记录 } //积分扣除 #endregion #region 余额抵扣 if (orderRequestModel.use_amount > 0 && bllMall.IsEnableAccountAmountPay()) { CurrentUserInfo.AccountAmount -= orderRequestModel.use_amount; if (bllMall.Update(CurrentUserInfo, string.Format(" AccountAmount={0}", CurrentUserInfo.AccountAmount), string.Format(" AutoID={0}", CurrentUserInfo.AutoID)) < 0) { tran.Rollback(); resp.errcode = 1; resp.errmsg = "更新用户余额失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } UserScoreDetailsInfo scoreRecord = new UserScoreDetailsInfo(); scoreRecord.AddTime = DateTime.Now; scoreRecord.Score = -(double)orderRequestModel.use_amount; scoreRecord.TotalScore = (double)CurrentUserInfo.AccountAmount; scoreRecord.UserID = CurrentUserInfo.UserID; scoreRecord.AddNote = "账户余额变动-下单使用余额"; scoreRecord.RelationID = orderInfo.OrderID; scoreRecord.WebSiteOwner = bllUser.WebsiteOwner; scoreRecord.ScoreType = "AccountAmount"; if (!bllMall.Add(scoreRecord)) { tran.Rollback(); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "插入余额记录失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } UserCreditAcountDetails record = new UserCreditAcountDetails(); record.WebsiteOwner = bllUser.WebsiteOwner; record.Operator = CurrentUserInfo.UserID; record.UserID = CurrentUserInfo.UserID; record.CreditAcount = -orderRequestModel.use_amount; record.SysType = "AccountAmount"; record.AddTime = DateTime.Now; record.AddNote = "账户余额变动-" + orderRequestModel.use_amount; if (!bllMall.Add(record)) { tran.Rollback(); resp.errcode = 1; resp.errmsg = "插入余额记录失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } #region 更新宏巍余额 if (websiteInfo.IsUnionHongware == 1) { if (!hongWareClient.UpdateMemberBlance(hongWeiWareMemberInfo.member.mobile, CurrentUserInfo.WXOpenId, -(float)orderRequestModel.use_amount)) { tran.Rollback(); resp.errcode = 1; resp.errmsg = "更新宏巍余额失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } } #endregion } #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; } //productSku.Stock -= item.TotalCount; //if (bllMall.IsPromotionTime(productSku)) //{ // productSku.PromotionStock -= item.TotalCount; //} //if (ZentCloud.ZCBLLEngine.BLLBase.ExecuteSql(string.Format("update ZCJ_ProductSku set Stock={0},PromotionStock={1} where SkuId={2}", productSku.Stock, productSku.PromotionStock, productSku.SkuId), tran) <= 0) //{ // tran.Rollback(); // resp.errcode = 1; // resp.errmsg = "更新SKU失败"; // context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); // return; //} //更新 SKU库存 System.Text.StringBuilder sbUpdateStock = new StringBuilder();//更新库存sql sbUpdateStock.AppendFormat(" Update ZCJ_ProductSku Set Stock-={0} ", item.TotalCount); if (bllMall.IsPromotionTime(productSku)) { sbUpdateStock.AppendFormat(",PromotionStock-={0} ", item.TotalCount); } sbUpdateStock.AppendFormat(" Where SkuId={0} And Stock>0 ", productSku.SkuId); if (bllMall.IsPromotionTime(productSku)) { sbUpdateStock.AppendFormat(" And PromotionStock>0 ", item.TotalCount); } if (ZentCloud.ZCBLLEngine.BLLBase.ExecuteSql(sbUpdateStock.ToString(), tran) <= 0) { tran.Rollback(); resp.errcode = 1; resp.errmsg = "提交订单失败,库存不足"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } } #endregion tran.Commit();//提交订单事务 #region 宏巍通知 if (websiteInfo.IsUnionHongware == 1) { hongWareClient.OrderNotice(CurrentUserInfo.WXOpenId, orderInfo.OrderID); } #endregion } catch (Exception ex) { //回滚事物 tran.Rollback(); resp.errcode = 1; resp.errmsg = ex.ToString(); 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 })); }
/// <summary> /// 收货 /// </summary> private void ReceiptConfirm() { try { var orderList = bllMall.GetList <WXMallOrderInfo>(string.Format(" Status='已发货' And PayMentStatus=1 And DATEDIFF(day,DeliveryTime,GETDATE())>=8 And WebSiteOwner='{0}'", websiteOwner)); ToLog(string.Format("共获取到{0}笔订单,开始自动收货...", orderList.Count)); DateTime dtNow = DateTime.Now; int index = 1; int successCount = 0; foreach (var orderInfo in orderList) { ToLog(string.Format("正在处理第{0}笔订单,共{1}笔...", index, orderList.Count)); UserInfo orderUserInfo = bllUser.GetUserInfo(orderInfo.OrderUserID); ZentCloud.ZCBLLEngine.BLLTransaction tran = new ZentCloud.ZCBLLEngine.BLLTransaction(); try { orderInfo.Status = "交易成功"; if (bllMall.Update(orderInfo, " Status='交易成功'", string.Format(" OrderId='{0}'", orderInfo.OrderID), tran) <= 0) { tran.Rollback(); continue; } //增加积分 ScoreConfig scoreConfig = bllScore.Get <ScoreConfig>(string.Format(" WebsiteOwner='{0}'", websiteOwner)); if (scoreConfig.OrderAmount == 0) { scoreConfig.OrderAmount = 1; } int addScore = (int)(orderInfo.PayableAmount / (scoreConfig.OrderAmount / scoreConfig.OrderScore)); if (addScore > 0) { WXMallScoreRecord scoreRecord = new WXMallScoreRecord(); scoreRecord.InsertDate = DateTime.Now; scoreRecord.Remark = "微商城-交易完成获得积分"; scoreRecord.Score = addScore; scoreRecord.UserId = orderInfo.OrderUserID; scoreRecord.WebsiteOwner = websiteOwner; scoreRecord.OrderID = orderInfo.OrderID; scoreRecord.Type = 1; if (!bllMall.Add(scoreRecord, tran)) { tran.Rollback(); continue; } if (bllUser.Update(orderUserInfo, string.Format(" TotalScore+={0},HistoryTotalScore+={0}", addScore), string.Format(" AutoID={0}", orderUserInfo.AutoID), tran) <= 0) { tran.Rollback(); continue; } try { //驿氪同步 yiKeClient.BonusUpdate(orderUserInfo.Ex2, addScore, string.Format("订单交易成功获得{0}积分", addScore)); //驿氪同步 yiKeClient.ChangeStatus(orderInfo.OrderID, orderInfo.Status); } catch (Exception) { } } // //更新订单明细表状态 List <WXMallOrderDetailsInfo> orderDetailList = bllMall.GetOrderDetailsList(orderInfo.OrderID); foreach (var orderDetail in orderDetailList) { orderDetail.IsComplete = 1; if (bllMall.Update(orderDetail, "IsComplete=1", string.Format(" OrderId='{0}'", orderInfo.OrderID)) <= 0) { tran.Rollback(); continue; } } } catch (Exception ex) { tran.Rollback(); continue; } tran.Commit(); successCount++; // } ToLog("操作完成。耗时:" + (DateTime.Now - dtNow).TotalSeconds + "S"); WriteLog(string.Format("自动收货完成.耗时{0}秒,自动收货订单数量{1}", (DateTime.Now - dtNow).TotalSeconds, successCount)); } catch (Exception ex) { ToLog(ex.Message); WriteLog(ex.Message); } }
/// <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) { string data = context.Request["data"]; OrderModel orderRequestModel;//订单模型 try { orderRequestModel = JSONHelper.JsonToModel <OrderModel>(data); } catch (Exception ex) { apiResp.msg = "提交格式错误"; apiResp.code = (int)APIErrCode.OperateFail; bllMall.ContextResponse(context, apiResp); return; } WXMallProductInfo productInfo = bllMall.GetProduct(orderRequestModel.product_id.ToString()); if (productInfo == null) { apiResp.msg = "记录没有找到"; apiResp.code = (int)APIErrCode.IsNotFound; bllMall.ContextResponse(context, apiResp); return; } WXMallOrderInfo orderInfo = new WXMallOrderInfo();//订单表 orderInfo.ArticleCategoryType = productInfo.ArticleCategoryType; orderInfo.InsertDate = DateTime.Now; orderInfo.OrderUserID = CurrentUserInfo.UserID; orderInfo.WebsiteOwner = bllMall.WebsiteOwner; orderInfo.OrderMemo = orderRequestModel.buyer_memo; orderInfo.UseAmount = orderRequestModel.use_amount; if (orderRequestModel.receiver_id == 0) { orderInfo.Consignee = CurrentUserInfo.TrueName; orderInfo.Phone = CurrentUserInfo.Phone; } else { WXConsigneeAddress nUserAddress = bllMall.GetByKey <WXConsigneeAddress>("AutoID", orderRequestModel.receiver_id.ToString()); orderInfo.Consignee = nUserAddress.ConsigneeName; orderInfo.Phone = nUserAddress.Phone; orderInfo.Address = nUserAddress.Address; orderInfo.ZipCode = nUserAddress.ZipCode; orderInfo.ReceiverProvince = nUserAddress.Province; orderInfo.ReceiverProvinceCode = nUserAddress.ProvinceCode; orderInfo.ReceiverCity = nUserAddress.City; orderInfo.ReceiverCityCode = nUserAddress.CityCode; orderInfo.ReceiverDist = nUserAddress.Dist; orderInfo.ReceiverDistCode = nUserAddress.DistCode; } orderInfo.Transport_Fee = 0; orderInfo.Status = "待付款"; if (orderRequestModel.pay_type == "WEIXIN")//微信支付 { orderInfo.PaymentType = 2; } else if (orderRequestModel.pay_type == "ALIPAY")//支付宝支付 { orderInfo.PaymentType = 1; } if (orderRequestModel.skus == null || orderRequestModel.skus.Count == 0) { apiResp.msg = "Skus不能为空"; apiResp.code = (int)APIErrCode.OperateFail; bllMall.ContextResponse(context, apiResp); return; } #region 商品检查 订单详情生成 //订单详情 List <WXMallOrderDetailsInfo> detailList = new List <WXMallOrderDetailsInfo>(); //主商品订单明细 List <WXMallOrderDetailsInfo> detailAddedList = new List <WXMallOrderDetailsInfo>(); //增值服务订单明细 orderRequestModel.skus = orderRequestModel.skus.Distinct().ToList(); #region 购买的商品 foreach (var sku in orderRequestModel.skus) { ProductSku productSku = bllMall.GetProductSku(sku.sku_id); WXMallOrderDetailsInfo detailModel = new WXMallOrderDetailsInfo(); detailModel.TotalCount = sku.count; detailModel.OrderPrice = bllMall.GetSkuPrice(productSku); detailModel.SkuId = productSku.SkuId; detailModel.ArticleCategoryType = productSku.ArticleCategoryType; if (productSku.ArticleCategoryType.Contains("Added")) { WXMallProductInfo rproductInfo = bllMall.GetProduct(productSku.ProductId.ToString()); detailModel.PID = rproductInfo.PID; detailModel.ProductName = rproductInfo.PName; detailModel.Unit = rproductInfo.Unit; detailAddedList.Add(detailModel); } else { detailModel.PID = productInfo.PID; detailModel.ProductName = productInfo.PName; detailModel.StartDate = sku.start_date; detailModel.EndDate = sku.end_date; detailModel.Unit = productInfo.Unit; detailList.Add(detailModel); } } #endregion if (detailList.Count == 0) { apiResp.msg = "请选择预约时间"; apiResp.code = (int)APIErrCode.OperateFail; bllMall.ContextResponse(context, apiResp); return; } //已有订单详情 List <WXMallOrderDetailsInfo> oDetailList = bllMall.GetOrderDetailsList(null, productInfo.PID, productInfo.ArticleCategoryType, detailList.Min(p => p.StartDate), detailList.Max(p => p.EndDate)); List <string> hasOrderID_List = new List <string>(); string hasOrderIDs = ""; int maxCount = productInfo.Stock; foreach (var item in detailList) { List <WXMallOrderDetailsInfo> hasOrderDetailList = oDetailList.Where(p => !((item.StartDate >= p.EndDate && item.EndDate > p.EndDate) || (item.StartDate < p.StartDate && item.EndDate <= p.StartDate))).ToList(); if (hasOrderDetailList.Count >= maxCount) { hasOrderID_List.AddRange(hasOrderDetailList.Select(p => p.OrderID).Distinct()); } } if (hasOrderID_List.Count > 0) { hasOrderID_List = hasOrderID_List.Distinct().ToList(); hasOrderIDs = MyStringHelper.ListToStr(hasOrderID_List, "'", ","); int tempCount = 0; List <WXMallOrderInfo> tempList = bllMall.GetOrderList(0, 1, "", out tempCount, "预约成功", null, null, null, null, null, null, null, null, null, null, orderInfo.ArticleCategoryType, hasOrderIDs); if (tempCount >= maxCount) { apiResp.msg = "所选时间已有成功的预约"; apiResp.code = (int)APIErrCode.OperateFail; bllMall.ContextResponse(context, apiResp); return; } } //增值服务合并到主订单明细列表 detailList.AddRange(detailAddedList); //合计计算 orderInfo.Product_Fee = detailList.Sum(p => p.OrderPrice * p.TotalCount).Value; #region 积分计算 decimal scoreExchangeAmount = 0;//积分抵扣的金额 //积分计算 if (orderRequestModel.use_score > 0 && orderInfo.Product_Fee > 0) { orderInfo.UseScore = orderRequestModel.use_score; if (CurrentUserInfo.TotalScore < orderRequestModel.use_score) { apiResp.msg = "积分不足"; apiResp.code = (int)APIErrCode.OperateFail; bllMall.ContextResponse(context, apiResp); return; } ScoreConfig scoreConfig = bllScore.GetScoreConfig(); scoreExchangeAmount = Math.Round(orderRequestModel.use_score / (scoreConfig.ExchangeScore / scoreConfig.ExchangeAmount), 2); } //积分计算 #endregion #region 使用账户余额 if (orderRequestModel.use_amount > 0) { if (!bllMall.IsEnableAccountAmountPay()) { apiResp.msg = "未开启余额支付"; apiResp.code = (int)APIErrCode.OperateFail; bllMall.ContextResponse(context, apiResp); return; } if (CurrentUserInfo.AccountAmount < orderRequestModel.use_amount) { apiResp.msg = "您的账户余额不足"; apiResp.code = (int)APIErrCode.OperateFail; bllMall.ContextResponse(context, apiResp); return; } } #endregion orderInfo.TotalAmount = orderInfo.Product_Fee + orderInfo.Transport_Fee; orderInfo.TotalAmount -= scoreExchangeAmount; //积分优惠金额 orderInfo.TotalAmount -= orderRequestModel.use_amount; //余额抵扣金额 orderInfo.PayableAmount = orderInfo.TotalAmount - orderInfo.Transport_Fee; //应付金额 if ((orderInfo.Product_Fee + orderInfo.Transport_Fee - scoreExchangeAmount) < orderInfo.TotalAmount) { apiResp.msg = "积分兑换金额不能大于订单总金额,请减少积分兑换"; apiResp.code = (int)APIErrCode.OperateFail; bllMall.ContextResponse(context, apiResp); return; } if (orderInfo.TotalAmount < 0) { orderInfo.TotalAmount = 0; } if (orderInfo.TotalAmount == 0 && orderInfo.UseScore == 0) { orderInfo.Status = "待审核"; } else if (orderInfo.TotalAmount == 0 && (orderInfo.UseAmount > 0 || orderInfo.UseScore > 0)) { orderInfo.PaymentStatus = 1; orderInfo.PayTime = DateTime.Now; orderInfo.Status = "预约成功"; } #endregion //生成订单ID orderInfo.OrderID = bllMall.GetGUID(BLLJIMP.TransacType.AddMallOrder); BLLTransaction tran = new BLLTransaction(); if (!this.bllMall.Add(orderInfo, tran)) { tran.Rollback(); apiResp.msg = "提交失败"; apiResp.code = (int)APIErrCode.OperateFail; bllMall.ContextResponse(context, apiResp); return; } foreach (var item in detailList) { item.OrderID = orderInfo.OrderID; if (!this.bllMall.Add(item, tran)) { tran.Rollback(); apiResp.msg = "提交失败"; apiResp.code = (int)APIErrCode.OperateFail; bllMall.ContextResponse(context, apiResp); return; } } #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) , tran) < 0) { tran.Rollback(); apiResp.msg = "更新用户积分失败"; apiResp.code = (int)APIErrCode.OperateFail; bllMall.ContextResponse(context, apiResp); return; } //积分记录 UserScoreDetailsInfo scoreRecord = new UserScoreDetailsInfo(); scoreRecord.AddTime = DateTime.Now; scoreRecord.Score = -orderRequestModel.use_score; scoreRecord.TotalScore = CurrentUserInfo.TotalScore; scoreRecord.ScoreType = "OrderSubmit"; scoreRecord.UserID = CurrentUserInfo.UserID; scoreRecord.AddNote = "预约-下单使用积分"; scoreRecord.RelationID = orderInfo.OrderID; scoreRecord.WebSiteOwner = CurrentUserInfo.WebsiteOwner; if (!bllMall.Add(scoreRecord)) { tran.Rollback(); apiResp.msg = "插入积分记录失败"; apiResp.code = (int)APIErrCode.OperateFail; bllMall.ContextResponse(context, apiResp); return; } } //积分扣除 #endregion #region 余额抵扣 if (orderRequestModel.use_amount > 0 && bllMall.IsEnableAccountAmountPay()) { CurrentUserInfo.AccountAmount -= orderRequestModel.use_amount; if (bllMall.Update(CurrentUserInfo, string.Format(" AccountAmount={0}", CurrentUserInfo.AccountAmount), string.Format(" AutoID={0}", CurrentUserInfo.AutoID)) < 0) { tran.Rollback(); apiResp.msg = "更新用户余额失败"; apiResp.code = (int)APIErrCode.OperateFail; bllMall.ContextResponse(context, apiResp); return; } UserScoreDetailsInfo scoreRecord = new UserScoreDetailsInfo(); scoreRecord.AddTime = DateTime.Now; scoreRecord.Score = -(double)orderRequestModel.use_amount; scoreRecord.TotalScore = (double)CurrentUserInfo.AccountAmount; scoreRecord.UserID = CurrentUserInfo.UserID; scoreRecord.AddNote = "账户余额变动-下单使用余额"; scoreRecord.RelationID = orderInfo.OrderID; scoreRecord.WebSiteOwner = bllMall.WebsiteOwner; scoreRecord.ScoreType = "AccountAmount"; if (!bllMall.Add(scoreRecord)) { tran.Rollback(); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "插入余额记录失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } UserCreditAcountDetails record = new UserCreditAcountDetails(); record.WebsiteOwner = bllMall.WebsiteOwner; record.Operator = CurrentUserInfo.UserID; record.UserID = CurrentUserInfo.UserID; record.CreditAcount = -orderRequestModel.use_amount; record.SysType = "AccountAmount"; record.AddTime = DateTime.Now; record.AddNote = "账户余额变动-" + orderRequestModel.use_amount; if (!bllMall.Add(record)) { tran.Rollback(); apiResp.msg = "插入余额记录失败"; apiResp.code = (int)APIErrCode.OperateFail; bllMall.ContextResponse(context, apiResp); return; } } #endregion tran.Commit();//提交订单事务 if (orderInfo.Status == "预约成功") { int tempCount = 0; if (string.IsNullOrWhiteSpace(hasOrderIDs)) { hasOrderIDs = "'0'"; } List <WXMallOrderInfo> tempList = bllMall.GetOrderList(0, 1, "", out tempCount, "预约成功", null, null, null, null, null, null, null, null, null, null, orderInfo.ArticleCategoryType, hasOrderIDs); tempCount = tempCount + 1; //加上当前订单的数量 if (tempCount >= maxCount) { tempList = bllMall.GetColOrderListInStatus("'待付款','待审核'", hasOrderIDs, "OrderID,OrderUserID,UseScore", bllMall.WebsiteOwner); if (tempList.Count > 0) { string stopOrderIds = MyStringHelper.ListToStr(tempList.Select(p => p.OrderID).ToList(), "'", ","); tempList = tempList.Where(p => p.UseScore > 0).ToList(); foreach (var item in tempList) { UserInfo orderUserInfo = bllUser.GetUserInfo(orderInfo.OrderUserID, bllMall.WebsiteOwner);//下单用户信息 if (orderUserInfo == null) { continue; } orderUserInfo.TotalScore += item.UseScore; if (bllMall.Update(orderUserInfo, string.Format(" TotalScore+={0}", item.UseScore), string.Format(" UserID='{0}'", item.OrderUserID)) > 0) { UserScoreDetailsInfo scoreRecord = new UserScoreDetailsInfo(); scoreRecord.AddTime = DateTime.Now; scoreRecord.Score = item.UseScore; scoreRecord.TotalScore = orderUserInfo.TotalScore; scoreRecord.ScoreType = "OrderCancel"; scoreRecord.UserID = item.OrderUserID; scoreRecord.RelationID = item.OrderID; scoreRecord.AddNote = "预约-订单失败返还积分"; scoreRecord.WebSiteOwner = item.WebsiteOwner; bllMall.Add(scoreRecord); } } bllMall.Update(new WXMallOrderInfo(), string.Format("Status='{0}'", "预约失败"), string.Format("OrderID In ({0}) and WebsiteOwner='{1}'", stopOrderIds, bllMall.WebsiteOwner)); } } } //预约通知 bllWeiXin.SendTemplateMessageNotifyComm(CurrentUserInfo, orderInfo.Status, string.Format("预约:{2}\\n订单号:{0}\\n订单金额:{1}元", orderInfo.OrderID, orderInfo.TotalAmount, productInfo.PName)); apiResp.result = new { order_id = orderInfo.OrderID }; apiResp.msg = "提交完成"; apiResp.code = (int)APIErrCode.IsSuccess; apiResp.status = true; bllMall.ContextResponse(context, apiResp); }
public void ProcessRequest(HttpContext context) { WebsiteInfo websiteInfo = bllMall.GetWebsiteInfoModelFromDataBase(); Open.HongWareSDK.Client hongWareClient = new Open.HongWareSDK.Client(websiteInfo.WebsiteOwner); Open.HongWareSDK.MemberInfo hongWeiWareMemberInfo = null; if (websiteInfo.IsUnionHongware == 1) { hongWeiWareMemberInfo = hongWareClient.GetMemberInfo(CurrentUserInfo.WXOpenId); if (hongWeiWareMemberInfo.member == null) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "您尚未绑定宏巍账号,请先绑定"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } } string data = context.Request["data"]; if (string.IsNullOrEmpty(data)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "data 参数必传"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } decimal productFee = 0; //商品总价格 不包含邮费 OrderModel orderRequestModel; //订单模型 try { orderRequestModel = ZentCloud.Common.JSONHelper.JsonToModel <OrderModel>(data); } catch (Exception ex) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "JSON格式错误,请检查。错误信息:" + ex.Message; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (string.IsNullOrEmpty(orderRequestModel.order_id)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "order_id 必传"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } WXMallOrderInfo parentOrderInfo = bllMall.GetOrderInfo(orderRequestModel.order_id);//父订单 if (parentOrderInfo == null) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "订单不存在"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (parentOrderInfo.OrderUserID == CurrentUserInfo.UserID) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "团长不可以参加"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (parentOrderInfo.OrderType != 2) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "不是拼团订单"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (!string.IsNullOrEmpty(parentOrderInfo.GroupBuyParentOrderId)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "订单无效"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (parentOrderInfo.PaymentStatus == 0) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "团长订单未付款"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (bllMall.GetCount <WXMallOrderInfo>(string.Format("PaymentStatus=1 And GroupBuyParentOrderId='{0}' Or OrderId='{0}'", parentOrderInfo.OrderID)) >= parentOrderInfo.PeopleCount) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "团购人数已满"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (bllMall.GetCount <WXMallOrderInfo>(string.Format("GroupBuyParentOrderId='{0}' And OrderUserId='{1}' And PaymentStatus=0", parentOrderInfo.OrderID, CurrentUserInfo.UserID)) > 0) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "您还有未支付的订单,请先支付"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (DateTime.Now >= (((DateTime)parentOrderInfo.PayTime).AddDays(parentOrderInfo.ExpireDay))) { parentOrderInfo.GroupBuyStatus = "2"; bllMall.Update(parentOrderInfo); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "拼团已过期"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } #region 分销关系建立 if (websiteInfo.DistributionRelationBuildMallOrder == 1) { UserInfo orderUserInfo = bllUser.GetUserInfo(parentOrderInfo.OrderUserID, parentOrderInfo.WebsiteOwner); if (bllUser.IsDistributionMember(orderUserInfo)) { if (string.IsNullOrEmpty(CurrentUserInfo.DistributionOwner)) { var setUserDistributionOwnerResult = bllDis.SetUserDistributionOwner(CurrentUserInfo.UserID, orderUserInfo.UserID, CurrentUserInfo.WebsiteOwner); if (setUserDistributionOwnerResult) { CurrentUserInfo.DistributionOwner = orderUserInfo.UserID; CurrentUserInfo.Channel = orderUserInfo.Channel; } } } } #endregion WXMallOrderInfo orderInfo = new WXMallOrderInfo();//订单表 orderInfo.Address = orderRequestModel.receiver_address; orderInfo.Consignee = orderRequestModel.receiver_name; orderInfo.InsertDate = DateTime.Now; orderInfo.OrderUserID = CurrentUserInfo.UserID; orderInfo.Phone = orderRequestModel.receiver_phone; orderInfo.WebsiteOwner = bllMall.WebsiteOwner; orderInfo.Transport_Fee = 0; orderInfo.OrderID = bllMall.GetGUID(BLLJIMP.TransacType.AddMallOrder); if (bllMall.WebsiteOwner != "mixblu") { orderInfo.OutOrderId = orderInfo.OrderID; } orderInfo.OrderMemo = orderRequestModel.buyer_memo; orderInfo.ReceiverProvince = orderRequestModel.receiver_province; orderInfo.ReceiverProvinceCode = orderRequestModel.receiver_province_code.ToString(); orderInfo.ReceiverCity = orderRequestModel.receiver_city; orderInfo.ReceiverCityCode = orderRequestModel.receiver_city_code.ToString(); orderInfo.ReceiverDist = orderRequestModel.receiver_dist; orderInfo.ReceiverDistCode = orderRequestModel.receiver_dist_code.ToString(); orderInfo.ZipCode = orderRequestModel.receiver_zip; orderInfo.Status = "待付款"; orderInfo.OrderType = 2; orderInfo.GroupBuyParentOrderId = parentOrderInfo.OrderID; orderInfo.MyCouponCardId = orderRequestModel.cardcoupon_id.ToString(); orderInfo.UseScore = orderRequestModel.use_score; orderInfo.UseAmount = orderRequestModel.use_amount; orderInfo.LastUpdateTime = DateTime.Now; if (orderRequestModel.pay_type == "WEIXIN")//微信支付 { orderInfo.PaymentType = 2; } else if (orderRequestModel.pay_type == "ALIPAY")//支付宝支付 { orderInfo.PaymentType = 1; } #region 格式检查 if (string.IsNullOrEmpty(orderInfo.Consignee)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "收货人姓名不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (string.IsNullOrEmpty(orderInfo.Phone)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "收货人联系电话不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (string.IsNullOrEmpty(orderInfo.ReceiverProvince)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "省份名称不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (string.IsNullOrEmpty(orderInfo.ReceiverProvinceCode)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "省份代码不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (string.IsNullOrEmpty(orderInfo.ReceiverCity)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "城市名称不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (string.IsNullOrEmpty(orderInfo.ReceiverCityCode)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "城市代码不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (string.IsNullOrEmpty(orderInfo.ReceiverDist)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "城市区域名称不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (string.IsNullOrEmpty(orderInfo.ReceiverCityCode)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "城市区域代码不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (string.IsNullOrEmpty(orderInfo.Address)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "收货地址不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } //相关检查 #endregion #region 商品检查 订单详情生成 var needUseScore = 0; //必须使用的积分 decimal needUseCash = 0; //必须使用的现金 var parentOrderDetailList = bllMall.GetOrderDetailsList(parentOrderInfo.OrderID); //先检查库存 ProductSku productSku = bllMall.GetProductSku((int)parentOrderDetailList[0].SkuId); if (productSku == null) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "SKU不存在"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } WXMallProductInfo productInfo = bllMall.GetProduct(productSku.ProductId.ToString()); string cardCouponType = "";//优惠券类型 #region 检查优惠券是否可用 if (orderRequestModel.cardcoupon_id > 0) { var mycardCoupon = bllCardCoupon.GetMyCardCoupon(orderRequestModel.cardcoupon_id, CurrentUserInfo.UserID); if (mycardCoupon == null) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "无效的优惠券"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } var cardCoupon = bllCardCoupon.GetCardCoupon(mycardCoupon.CardId); if (cardCoupon == null) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "无效的优惠券"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } cardCouponType = cardCoupon.CardCouponType; #region 需要购买指定商品 if ((!string.IsNullOrEmpty(cardCoupon.Ex2)) && (cardCoupon.Ex2 != "0")) { if (productInfo.PID != cardCoupon.Ex2) { var productInfoCard = bllMall.GetProduct(cardCoupon.Ex2); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = string.Format("此优惠券需要购买{0}时才可以使用", productInfoCard.PName); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } } #endregion #region 需要购买指定标签商品 if (!string.IsNullOrEmpty(cardCoupon.Ex8)) { if (string.IsNullOrEmpty(productInfo.Tags))//全部商品都没有标签 { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = string.Format("使用此优惠券需要购买标签为{0}的商品", cardCoupon.Ex8); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } bool checkResult = true; if (!string.IsNullOrEmpty(productInfo.Tags)) { bool tempResult = false; foreach (string tag in productInfo.Tags.Split(',')) { if (cardCoupon.Ex8.Contains(tag)) { tempResult = true; break; } } if (!tempResult) { checkResult = false; } } else//商品不包含标签 { checkResult = false; } if (!checkResult) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = string.Format("使用此优惠券需要购买标签为{0}的商品", cardCoupon.Ex8); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } } #endregion } #endregion if (productInfo.IsOnSale == "0") { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = string.Format("{0}已下架", productInfo.PName); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (productSku.Stock < 1) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = string.Format("{0}{1}库存余量为{2},库存不足", productInfo.PName, bllMall.GetProductShowProperties(productSku.SkuId), productSku.Stock); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (productInfo.Score > 0)//必须使用的积分 { needUseScore = productInfo.Score; } if (productInfo.IsCashPayOnly == 1) //必须使用的现金 { needUseCash = Math.Round((decimal)(productSku.Price * (decimal)(parentOrderInfo.MemberDiscount / 10)), 2); //四舍五入 } WXMallOrderDetailsInfo detailModel = new WXMallOrderDetailsInfo(); detailModel.OrderID = orderInfo.OrderID; detailModel.PID = productInfo.PID; detailModel.TotalCount = 1; detailModel.OrderPrice = Math.Round((decimal)(productSku.Price * (decimal)(parentOrderInfo.MemberDiscount / 10)), 2);//四舍五入 detailModel.ProductName = productInfo.PName; detailModel.SkuId = productSku.SkuId; detailModel.SkuShowProp = bllMall.GetProductShowProperties(productSku.SkuId); detailModel.IsComplete = 1;//拼团的只要下单就算销量 #endregion #region 纯积分购买 if (needUseScore > 0) { if ((CurrentUserInfo.TotalScore < needUseScore) || (orderRequestModel.use_score < needUseScore)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = string.Format("您需要使用{0}积分来兑换, 可用积分不足", needUseScore); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } } #endregion productFee = (decimal)detailModel.OrderPrice; //物流费用 #region 运费计算 List <SkuModel> skus = new List <SkuModel>(); skus.Add(new SkuModel { sku_id = productSku.SkuId, count = 1 }); 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 = skus; decimal freight = 0;//运费 string freightMsg = ""; if (!bllMall.CalcFreight(freightModel, out freight, out freightMsg)) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = freightMsg; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); 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) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = msg; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (cardCouponType == "MallCardCoupon_FreeFreight")//免邮券 { orderInfo.Transport_Fee = 0; } } //优惠券计算 #endregion #region 积分计算 decimal scoreExchangeAmount = 0;///积分抵扣的金额 //积分计算 if (orderRequestModel.use_score > 0) { #region 使用宏巍积分 if (websiteInfo.IsUnionHongware == 1) { CurrentUserInfo.TotalScore = hongWeiWareMemberInfo.member.point; } #endregion orderInfo.UseScore = orderRequestModel.use_score; if (CurrentUserInfo.TotalScore < orderRequestModel.use_score) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "积分不足"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } ScoreConfig scoreConfig = bllScore.GetScoreConfig(); if (scoreConfig != null && scoreConfig.ExchangeAmount > 0) { scoreExchangeAmount = Math.Round(orderRequestModel.use_score / (scoreConfig.ExchangeScore / scoreConfig.ExchangeAmount), 2); } //scoreExchangeAmount = Math.Round(orderRequestModel.use_score / (scoreConfig.ExchangeScore / scoreConfig.ExchangeAmount), 2); } //积分计算 #endregion #region 使用账户余额 if (orderRequestModel.use_amount > 0) { if (!bllMall.IsEnableAccountAmountPay()) { apiResp.msg = "尚未启用余额支付功能"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } #region 使用宏巍余额 if (websiteInfo.IsUnionHongware == 1) { CurrentUserInfo.AccountAmount = (decimal)hongWeiWareMemberInfo.member.balance; } #endregion if (CurrentUserInfo.AccountAmount < orderRequestModel.use_amount) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "您的账户余额不足"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } } #endregion //合计计算 orderInfo.Product_Fee = productFee; orderInfo.TotalAmount = orderInfo.Product_Fee + orderInfo.Transport_Fee; orderInfo.TotalAmount -= discountAmount; //优惠券优惠金额 orderInfo.TotalAmount -= scoreExchangeAmount; //积分优惠金额 orderInfo.TotalAmount -= orderRequestModel.use_amount; //余额 orderInfo.PayableAmount = orderInfo.TotalAmount - freight; //应付金额 orderInfo.HeadDiscount = parentOrderInfo.HeadDiscount; orderInfo.MemberDiscount = parentOrderInfo.MemberDiscount; orderInfo.PeopleCount = parentOrderInfo.PeopleCount; orderInfo.ExpireDay = parentOrderInfo.ExpireDay; orderInfo.GroupBuyParentOrderId = parentOrderInfo.OrderID; orderInfo.ScoreExchangAmount = scoreExchangeAmount; //优惠券抵扣金额 orderInfo.CardcouponDisAmount = discountAmount; //卡券抵扣金额 if (orderInfo.TotalAmount <= 0) { orderInfo.TotalAmount = 0; orderInfo.PaymentStatus = 1; orderInfo.PayTime = DateTime.Now; orderInfo.Status = "待发货"; } if (orderInfo.TotalAmount < needUseCash) { apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = string.Format("最少需要支付{0}元" + needUseCash); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } ZentCloud.ZCBLLEngine.BLLTransaction tran = new ZCBLLEngine.BLLTransaction(); try { if (!this.bllMall.Add(orderInfo, tran)) { tran.Rollback(); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "提交失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); 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(); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "更新优惠券状态失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); 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), tran) < 0) { tran.Rollback(); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "更新用户积分失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } //积分记录 UserScoreDetailsInfo scoreRecord = new UserScoreDetailsInfo(); scoreRecord.AddTime = DateTime.Now; scoreRecord.Score = -orderRequestModel.use_score; scoreRecord.TotalScore = CurrentUserInfo.TotalScore; scoreRecord.ScoreType = "OrderSubmit"; scoreRecord.UserID = CurrentUserInfo.UserID; scoreRecord.AddNote = "微商城-参加团购使用积分"; scoreRecord.WebSiteOwner = CurrentUserInfo.WebsiteOwner; if (!bllMall.Add(scoreRecord, tran)) { tran.Rollback(); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "插入积分记录失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } #region 更新宏巍积分 if (websiteInfo.IsUnionHongware == 1) { if (!hongWareClient.UpdateMemberScore(hongWeiWareMemberInfo.member.mobile, CurrentUserInfo.WXOpenId, -orderRequestModel.use_score)) { tran.Rollback(); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "更新宏巍积分失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } } #endregion } //积分扣除 #endregion #region 余额抵扣 if (orderRequestModel.use_amount > 0 && bllMall.IsEnableAccountAmountPay()) { CurrentUserInfo.AccountAmount -= orderRequestModel.use_amount; if (bllMall.Update(CurrentUserInfo, string.Format(" AccountAmount={0}", CurrentUserInfo.AccountAmount), string.Format(" AutoID={0}", CurrentUserInfo.AutoID)) < 0) { tran.Rollback(); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "更新用户余额失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } UserScoreDetailsInfo scoreRecord = new UserScoreDetailsInfo(); scoreRecord.AddTime = DateTime.Now; scoreRecord.Score = -(double)orderRequestModel.use_amount; scoreRecord.TotalScore = (double)CurrentUserInfo.AccountAmount; scoreRecord.UserID = CurrentUserInfo.UserID; scoreRecord.AddNote = "拼团-参团使用余额"; scoreRecord.RelationID = orderInfo.OrderID; scoreRecord.WebSiteOwner = bllUser.WebsiteOwner; scoreRecord.ScoreType = "AccountAmount"; if (!bllMall.Add(scoreRecord)) { tran.Rollback(); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "插入余额记录失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } UserCreditAcountDetails record = new UserCreditAcountDetails(); record.WebsiteOwner = bllUser.WebsiteOwner; record.Operator = CurrentUserInfo.UserID; record.UserID = CurrentUserInfo.UserID; record.CreditAcount = -orderRequestModel.use_amount; record.SysType = "AccountAmount"; record.AddTime = DateTime.Now; record.AddNote = "账户余额变动-" + orderRequestModel.use_amount; if (!bllMall.Add(record)) { tran.Rollback(); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "插入余额记录失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } #region 更新宏巍余额 if (websiteInfo.IsUnionHongware == 1) { if (!hongWareClient.UpdateMemberBlance(hongWeiWareMemberInfo.member.mobile, CurrentUserInfo.WXOpenId, -(float)orderRequestModel.use_amount)) { tran.Rollback(); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "更新宏巍余额失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } } #endregion } #endregion #region 插入订单详情表及更新库存 if (!this.bllMall.Add(detailModel, tran)) { tran.Rollback(); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "提交失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } //更新 SKU库存 System.Text.StringBuilder sbUpdateStock = new StringBuilder();//更新库存sql sbUpdateStock.AppendFormat(" Update ZCJ_ProductSku Set Stock-={0} ", 1); sbUpdateStock.AppendFormat(" Where SkuId={0} And Stock>0 ", productSku.SkuId); if (ZentCloud.ZCBLLEngine.BLLBase.ExecuteSql(sbUpdateStock.ToString(), tran) <= 0) { tran.Rollback(); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "提交订单失败,库存不足"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } #endregion tran.Commit();//提交订单事务 #region 宏巍通知 if (websiteInfo.IsUnionHongware == 1) { hongWareClient.OrderNotice(CurrentUserInfo.WXOpenId, orderInfo.OrderID); } #endregion try { //团购完成取消其它未付款订单 if (parentOrderInfo.Ex10 == "1") { if (bllMall.GetCount <WXMallOrderInfo>(string.Format("PaymentStatus=1 And (GroupBuyParentOrderId='{0}')", parentOrderInfo.OrderID)) >= parentOrderInfo.PeopleCount) { bllMall.Update(new WXMallOrderInfo(), string.Format("Status='已取消'"), string.Format(" GroupBuyParentOrderId='{0}' And PaymentStatus=0", parentOrderInfo.OrderID)); parentOrderInfo.GroupBuyStatus = "1"; bllMall.Update(parentOrderInfo); } } else { if (bllMall.GetCount <WXMallOrderInfo>(string.Format("PaymentStatus=1 And (GroupBuyParentOrderId='{0}' Or OrderId='{0}')", parentOrderInfo.OrderID)) >= parentOrderInfo.PeopleCount) { bllMall.Update(new WXMallOrderInfo(), string.Format("Status='已取消'"), string.Format(" GroupBuyParentOrderId='{0}' And PaymentStatus=0", parentOrderInfo.OrderID)); parentOrderInfo.GroupBuyStatus = "1"; bllMall.Update(parentOrderInfo); } } #region 微信模板消息 string title = "订单已成功提交"; if (orderInfo.TotalAmount > 0) { title += ",请尽快付款"; } bllWeiXin.SendTemplateMessageNotifyComm(CurrentUserInfo, title, string.Format("订单号:{0}\\n订单金额:{1}元\\n收货人:{2}\\n电话:{3}", orderInfo.OrderID, orderInfo.TotalAmount, orderInfo.Consignee, orderInfo.Phone)); #endregion } catch { } } catch (Exception ex) { //回滚事物 tran.Rollback(); apiResp.code = (int)APIErrCode.OperateFail; apiResp.msg = "提交订单失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } apiResp.status = true; apiResp.msg = "ok"; apiResp.result = new { order_id = orderInfo.OrderID }; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); }
public void ProcessRequest(HttpContext context) { string orderSn = context.Request["order_sn"]; //订单号 string status = context.Request["status"]; //订单状态 1001 确认收货 1002取消订单 if (string.IsNullOrEmpty(orderSn)) { resp.code = (int)APIErrCode.PrimaryKeyIncomplete; resp.msg = "order_sn 参数必传"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } if (string.IsNullOrEmpty(status)) { resp.code = (int)APIErrCode.PrimaryKeyIncomplete; resp.msg = "status 参数必传"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } var orderInfo = bllMall.GetOrderInfoByOutOrderId(orderSn); if (orderInfo == null) { resp.code = (int)APIErrCode.PrimaryKeyIncomplete; resp.msg = "order_sn 不存在,请检查"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } UserInfo orderUserInfo = bllUser.GetUserInfo(orderInfo.OrderUserID, bllUser.WebsiteOwner); switch (status) { #region 确认收货 case "1001": //确认收货 if (orderInfo.Status == "交易成功") { resp.code = (int)APIErrCode.OperateFail; resp.msg = "重复操作"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } if (orderInfo.PaymentStatus == 0) { resp.code = (int)APIErrCode.OperateFail; resp.msg = "订单未付款"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } if (orderInfo.Status == "已取消") { resp.code = (int)APIErrCode.OperateFail; resp.msg = "订单已取消"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } ZentCloud.ZCBLLEngine.BLLTransaction tran = new ZCBLLEngine.BLLTransaction(); //交易成功处理 try { orderInfo.Status = "交易成功"; orderInfo.ReceivingTime = DateTime.Now; if (!bllMall.Update(orderInfo, tran)) { tran.Rollback(); resp.code = (int)APIErrCode.OperateFail; resp.msg = "更新订单状态失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } #region 交易成功加积分 //增加积分 ScoreConfig scoreConfig = bllScore.GetScoreConfig(); int addScore = 0; if (scoreConfig != null && scoreConfig.OrderAmount > 0 && scoreConfig.OrderScore > 0) { addScore = (int)(orderInfo.PayableAmount / (scoreConfig.OrderAmount / scoreConfig.OrderScore)); } if (addScore > 0) { orderUserInfo.TotalScore += addScore; UserScoreDetailsInfo scoreRecord = new UserScoreDetailsInfo(); scoreRecord.AddTime = DateTime.Now; scoreRecord.Score = addScore; scoreRecord.TotalScore = orderUserInfo.TotalScore; scoreRecord.ScoreType = "OrderSuccess"; scoreRecord.UserID = orderUserInfo.UserID; scoreRecord.AddNote = "微商城-交易成功获得积分"; scoreRecord.WebSiteOwner = bllMall.WebsiteOwner; if (!bllMall.Add(scoreRecord, tran)) { tran.Rollback(); resp.code = (int)APIErrCode.OperateFail; resp.msg = "插入积分记录表失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } if (bllUser.Update(orderUserInfo, string.Format(" TotalScore+={0},HistoryTotalScore+={0}", addScore), string.Format(" AutoID={0}", orderUserInfo.AutoID), tran) < 1) { tran.Rollback(); resp.code = (int)APIErrCode.OperateFail; resp.msg = "更新用户积分失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } } #endregion // #region 更新订单明细表状态 List <WXMallOrderDetailsInfo> orderDetailList = bllMall.GetOrderDetailsList(orderInfo.OrderID); foreach (var orderDetail in orderDetailList) { orderDetail.IsComplete = 1; if (!bllMall.Update(orderDetail)) { tran.Rollback(); resp.code = (int)APIErrCode.OperateFail; resp.msg = "更新订单明细表失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } } #endregion } catch (Exception ex) { tran.Rollback(); resp.code = (int)APIErrCode.OperateFail; resp.msg = ex.Message; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } tran.Commit(); resp.code = 0; resp.msg = "ok"; resp.status = true; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; //交易成功处理 #endregion #region 取消订单 case "1002": //取消订单 if (orderInfo.Status == "已取消") { resp.code = (int)APIErrCode.OperateFail; resp.msg = "重复操作"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } ZentCloud.ZCBLLEngine.BLLTransaction tranCancel = new ZCBLLEngine.BLLTransaction(); try { orderInfo.Status = "已取消"; if (!bllMall.Update(orderInfo, tranCancel)) { tranCancel.Rollback(); resp.code = (int)APIErrCode.OperateFail;; resp.msg = "更新订单状态失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } List <WXMallOrderDetailsInfo> orderDetailList = bllMall.GetOrderDetailsList(orderInfo.OrderID); foreach (var orderDetail in orderDetailList) { if (orderDetail.SkuId != null) { ProductSku sku = bllMall.GetProductSku((int)orderDetail.SkuId); if (sku != null) { if (bllMall.Update(sku, string.Format(" Stock+={0}", orderDetail.TotalCount), string.Format(" SkuId={0}", sku.SkuId), tranCancel) == 0) { tranCancel.Rollback(); resp.code = (int)APIErrCode.OperateFail;; resp.msg = "修改sku库存失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } } } } if (bllMall.Update(new WXMallOrderDetailsInfo(), "IsComplete=0", string.Format(" OrderId='{0}'", orderInfo.OrderID), tranCancel) <= 0) { tranCancel.Rollback(); resp.code = (int)APIErrCode.OperateFail;; resp.msg = "更新订单详情失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } #region 积分返还 if (orderInfo.UseScore > 0) //使用积分 积分返还 { orderUserInfo.TotalScore += orderInfo.UseScore; if (bllUser.Update(orderUserInfo, string.Format(" TotalScore+={0}", orderInfo.UseScore), string.Format(" AutoID={0}", orderUserInfo.AutoID)) < 0) { tranCancel.Rollback(); resp.code = (int)APIErrCode.OperateFail;; resp.msg = "积分返还失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } UserScoreDetailsInfo scoreRecord = new UserScoreDetailsInfo(); scoreRecord.AddTime = DateTime.Now; scoreRecord.Score = orderInfo.UseScore; scoreRecord.TotalScore = orderUserInfo.TotalScore; scoreRecord.ScoreType = "OrderCancel"; scoreRecord.UserID = orderUserInfo.UserID; scoreRecord.RelationID = orderInfo.OrderID; scoreRecord.WebSiteOwner = bllMall.WebsiteOwner; scoreRecord.AddNote = "微商城-订单取消返还积分"; if (!bllMall.Add(scoreRecord)) { tranCancel.Rollback(); resp.code = (int)APIErrCode.OperateFail;; resp.msg = "插入积分记录失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } } #endregion #region 优惠券返还 if (!string.IsNullOrEmpty(orderInfo.MyCouponCardId)) { var myCardCoupon = bllCardCoupon.GetMyCardCoupon(int.Parse(orderInfo.MyCouponCardId), orderUserInfo.UserID); if (myCardCoupon != null && myCardCoupon.Status == 1) { myCardCoupon.Status = 0; if (!bllCardCoupon.Update(myCardCoupon)) { tranCancel.Rollback(); resp.code = (int)APIErrCode.OperateFail;; resp.msg = "优惠券更新失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); } } } #endregion #region 账户余额返还 if (orderInfo.UseAmount > 0) { orderUserInfo.AccountAmount += orderInfo.UseAmount; if (bllMall.Update(orderUserInfo, string.Format(" AccountAmount={0}", orderUserInfo.AccountAmount), string.Format(" AutoID={0}", orderUserInfo.AutoID)) < 0) { tranCancel.Rollback(); resp.code = (int)APIErrCode.OperateFail;; resp.msg = "更新用户余额失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } UserScoreDetailsInfo scoreRecord = new UserScoreDetailsInfo(); scoreRecord.AddTime = DateTime.Now; scoreRecord.Score = (double)orderInfo.UseAmount; scoreRecord.TotalScore = (double)orderUserInfo.AccountAmount; scoreRecord.ScoreType = "AccountAmount"; scoreRecord.UserID = orderUserInfo.UserID; scoreRecord.RelationID = orderInfo.OrderID; scoreRecord.WebSiteOwner = bllMall.WebsiteOwner; scoreRecord.AddNote = "微商城-订单取消返还余额"; if (!bllMall.Add(scoreRecord)) { tranCancel.Rollback(); resp.code = (int)APIErrCode.OperateFail;; resp.msg = "插入余额记录失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } } #endregion tranCancel.Commit(); resp.code = 0; resp.msg = "ok"; resp.status = true; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } catch (Exception ex) { tranCancel.Rollback(); resp.code = (int)APIErrCode.OperateFail;; resp.msg = ex.Message; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } #endregion default: resp.code = (int)APIErrCode.OperateFail; resp.msg = "不合法的 status 值"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } }
public void ProcessRequest(HttpContext context) { string data = context.Request["data"]; decimal productFee = 0; //商品总价格 OrderModel orderRequestModel; //订单模型 try { orderRequestModel = ZentCloud.Common.JSONHelper.JsonToModel <OrderModel>(data); } catch (Exception ex) { apiResp.code = 1; apiResp.msg = "JSON格式错误,请检查.错误信息:" + ex.Message; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } #region 检查是否可以下单 if (string.IsNullOrEmpty(orderRequestModel.select_time_type)) { apiResp.code = 1; apiResp.msg = "请选择一种时间预约方式"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } int totalHours = 0; //预约小时数 if (orderRequestModel.select_time_type == "0") //直接选择开始时间结束时间方式 { if (string.IsNullOrEmpty(orderRequestModel.start_time) || string.IsNullOrEmpty(orderRequestModel.stop_time)) { apiResp.code = 1; apiResp.msg = "请选择开始时间与结束时间"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (DateTime.Parse(orderRequestModel.start_time) <= DateTime.Now) { apiResp.code = 1; apiResp.msg = "开始时间需要晚于当前时间"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (DateTime.Parse(orderRequestModel.stop_time) <= DateTime.Parse(orderRequestModel.start_time)) { apiResp.code = 1; apiResp.msg = "结束时间需要晚于开始时间"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } totalHours = (int)(DateTime.Parse(orderRequestModel.stop_time) - DateTime.Parse(orderRequestModel.start_time)).TotalHours; } else if (orderRequestModel.select_time_type == "1")//选择某一天的多个时间段的方式 { if (string.IsNullOrEmpty(orderRequestModel.date) || string.IsNullOrEmpty(orderRequestModel.date_time_ranges)) { apiResp.code = 1; apiResp.msg = "请选择预约时间段"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (DateTime.Parse(orderRequestModel.date) <= DateTime.Now) { apiResp.code = 1; apiResp.msg = "开始时间需要晚于当前时间"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } totalHours = orderRequestModel.date_time_ranges.Split(';').Count(); } if (!Check(orderRequestModel)) { apiResp.code = 1; apiResp.msg = "您所选的时间段已经被占用,请选择别的时间段"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } #endregion WXMallOrderInfo orderInfo = new WXMallOrderInfo();//订单表 orderInfo.OrderID = bllMall.GetGUID(BLLJIMP.TransacType.AddMallOrder); orderInfo.Consignee = orderRequestModel.receiver_name; orderInfo.InsertDate = DateTime.Now; orderInfo.OrderUserID = CurrentUserInfo.UserID; orderInfo.Phone = orderRequestModel.receiver_phone; orderInfo.WebsiteOwner = bllMall.WebsiteOwner; orderInfo.Transport_Fee = 0; orderInfo.OrderMemo = orderRequestModel.buyer_memo; orderInfo.MyCouponCardId = orderRequestModel.cardcoupon_id.ToString(); orderInfo.UseScore = orderRequestModel.use_score; orderInfo.Status = "待付款"; orderInfo.OrderType = 3; orderInfo.Ex3 = orderRequestModel.select_time_type; orderInfo.Ex4 = orderRequestModel.start_time; orderInfo.Ex5 = orderRequestModel.stop_time; orderInfo.Ex6 = orderRequestModel.date; orderInfo.Ex7 = orderRequestModel.date_time_ranges; orderInfo.LastUpdateTime = DateTime.Now; if (!string.IsNullOrEmpty(orderRequestModel.sale_id))//分销ID { long saleId = 0; if (long.TryParse(orderRequestModel.sale_id, out saleId)) { orderInfo.SellerId = saleId; } } if (orderRequestModel.pay_type == "WEIXIN")//微信支付 { orderInfo.PaymentType = 2; } else if (orderRequestModel.pay_type == "ALIPAY")//支付宝支付 { orderInfo.PaymentType = 1; } #region 格式检查 if (string.IsNullOrEmpty(orderInfo.Consignee)) { apiResp.code = 1; apiResp.msg = "收货人姓名不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (string.IsNullOrEmpty(orderRequestModel.receiver_phone)) { apiResp.code = 1; apiResp.msg = "联系手机号不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (orderRequestModel.skus == null) { apiResp.code = 1; apiResp.msg = "skus 参数不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } //相关检查 #endregion #region 商品检查 订单详情生成 ///订单详情 List <WXMallOrderDetailsInfo> detailList = new List <WXMallOrderDetailsInfo>();//订单详情 #region 购买的商品 List <WXMallProductInfo> productList = new List <WXMallProductInfo>(); foreach (var sku in orderRequestModel.skus) { ProductSku productSku = bllMall.GetProductSku(sku.sku_id); WXMallProductInfo productInfo = bllMall.GetProduct(productSku.ProductId.ToString()); productList.Add(productInfo); } productList = productList.Distinct().ToList(); #endregion #region 检查优惠券是否可用 if (orderRequestModel.cardcoupon_id > 0) { var mycardCoupon = bllCardCoupon.GetMyCardCoupon(orderRequestModel.cardcoupon_id, CurrentUserInfo.UserID); if (mycardCoupon == null) { apiResp.code = 1; apiResp.msg = "无效的优惠券"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } var cardCoupon = bllCardCoupon.GetCardCoupon(mycardCoupon.CardId); if (cardCoupon == null) { apiResp.code = 1; apiResp.msg = "无效的优惠券"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } #region 需要购买指定商品 if ((!string.IsNullOrEmpty(cardCoupon.Ex2)) && (cardCoupon.Ex2 != "0")) { if (productList.Count(p => p.PID == cardCoupon.Ex2) == 0) { var productInfo = bllMall.GetProduct(cardCoupon.Ex2); apiResp.code = 1; apiResp.msg = string.Format("此优惠券需要购买{0}时才可以使用", productInfo.PName); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } } #endregion #region 需要购买指定标签商品 if (!string.IsNullOrEmpty(cardCoupon.Ex8)) { if (productList.Where(p => p.Tags == "" || p.Tags == null).Count() == productList.Count)//全部商品都没有标签 { apiResp.code = 1; apiResp.msg = string.Format("使用此优惠券需要购买标签为{0}的商品", cardCoupon.Ex8); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } bool checkResult = false; foreach (var product in productList) { if (!string.IsNullOrEmpty(product.Tags)) { foreach (string tag in product.Tags.Split(',')) { if (cardCoupon.Ex8.Contains(tag)) { checkResult = true; break; } } } } if (!checkResult) { apiResp.code = 1; apiResp.msg = string.Format("使用此优惠券需要购买标签为{0}的商品", cardCoupon.Ex8); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } } #endregion } #endregion foreach (var sku in orderRequestModel.skus) { //先检查库存 ProductSku productSku = bllMall.GetProductSku(sku.sku_id); if (productSku == null) { apiResp.code = 1; apiResp.msg = "SKU不存在"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } WXMallProductInfo productInfo = bllMall.GetProduct(productSku.ProductId.ToString()); if (productInfo.IsOnSale == "0") { apiResp.code = 1; apiResp.msg = string.Format("{0}已下架", productInfo.PName); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } WXMallOrderDetailsInfo detailModel = new WXMallOrderDetailsInfo(); detailModel.OrderID = orderInfo.OrderID; detailModel.PID = productInfo.PID; detailModel.TotalCount = sku.count; detailModel.OrderPrice = bllMall.GetSkuPrice(productSku) * totalHours; detailModel.ProductName = productInfo.PName; detailModel.SkuId = productSku.SkuId; detailList.Add(detailModel); } #endregion productFee = detailList.Sum(p => p.OrderPrice * p.TotalCount).Value; //商品费用 orderInfo.Transport_Fee = 0; orderInfo.Product_Fee = productFee; orderInfo.TotalAmount = orderInfo.Product_Fee + orderInfo.Transport_Fee; #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) { apiResp.code = 1; apiResp.msg = msg; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (discountAmount > productFee) { apiResp.code = 1; apiResp.msg = "优惠券可优惠金额超过了订单总金额"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } orderInfo.CardcouponDisAmount = discountAmount; } #endregion #region 积分计算 decimal scoreExchangeAmount = 0;///积分抵扣的金额 //积分计算 if (orderRequestModel.use_score > 0) { if (CurrentUserInfo.TotalScore < orderRequestModel.use_score) { apiResp.code = 1; apiResp.msg = "积分不足"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } ScoreConfig scoreConfig = bllScore.GetScoreConfig(); scoreExchangeAmount = Math.Round(orderRequestModel.use_score / (scoreConfig.ExchangeScore / scoreConfig.ExchangeAmount), 2); } //积分计算 #endregion #region 合计计算 orderInfo.TotalAmount -= discountAmount; //优惠券优惠金额 orderInfo.TotalAmount -= scoreExchangeAmount; //积分优惠金额 orderInfo.PayableAmount = orderInfo.TotalAmount; //应付金额 if ((productFee - discountAmount - scoreExchangeAmount) < orderInfo.TotalAmount) { apiResp.code = 1; apiResp.msg = "积分兑换金额不能大于订单总金额,请减少积分兑换"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } #endregion if (orderInfo.TotalAmount < 0) { apiResp.code = 1; apiResp.msg = "付款金额不能小于0"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } if (orderInfo.TotalAmount == 0) { orderInfo.PaymentStatus = 1; orderInfo.PayTime = DateTime.Now; orderInfo.Status = "待发货"; } ZentCloud.ZCBLLEngine.BLLTransaction tran = new ZCBLLEngine.BLLTransaction(); try { if (!this.bllMall.Add(orderInfo, tran)) { tran.Rollback(); apiResp.code = 1; apiResp.msg = "提交失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); 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(); apiResp.code = 1; apiResp.msg = "更新优惠券状态失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); 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(); apiResp.code = 1; apiResp.msg = "更新用户积分失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } //积分记录 UserScoreDetailsInfo scoreRecord = new UserScoreDetailsInfo(); scoreRecord.AddTime = DateTime.Now; scoreRecord.Score = -orderRequestModel.use_score; scoreRecord.ScoreType = "OrderSubmit"; scoreRecord.UserID = CurrentUserInfo.UserID; scoreRecord.AddNote = "预约使用积分"; if (!bllMall.Add(scoreRecord)) { tran.Rollback(); apiResp.code = 1; apiResp.msg = "插入积分记录失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); 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(); apiResp.code = 1; apiResp.msg = "提交失败"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } } #endregion tran.Commit();//提交订单事务 } catch (Exception ex) { //回滚事物 tran.Rollback(); apiResp.code = 1; apiResp.msg = "提交订单失败,内部错误"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); return; } apiResp.status = true; apiResp.msg = "ok"; apiResp.result = new { order_id = orderInfo.OrderID }; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(apiResp)); }
public void ProcessRequest(HttpContext context) { try { string data = context.Request["data"]; decimal productFee = 0; //商品总价格 不包含邮费 decimal deviceFee = 0; //租金总金额,不包含押金 OrderModel orderRequestModel; //订单模型 try { orderRequestModel = ZentCloud.Common.JSONHelper.JsonToModel <OrderModel>(data); } catch (Exception ex) { resp.errcode = 1; resp.errmsg = "JSON格式错误,请检查.错误信息:" + ex.Message; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } if (string.IsNullOrEmpty(orderRequestModel.departure_date)) { resp.errcode = 1; resp.errmsg = "请选择出发日期"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } if (string.IsNullOrEmpty(orderRequestModel.backhome_date)) { resp.errcode = 1; resp.errmsg = "请选择回国日期"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } WXMallOrderInfo orderInfo = new WXMallOrderInfo(); //订单表 string orderId = bllMall.GetGUID(BLLJIMP.TransacType.AddMallOrder); orderInfo.OrderID = CreateOrderId(orderId); //内部订单号 orderInfo.OutOrderId = CreateOrderId(orderId); //外部订单号 orderInfo.Consignee = orderRequestModel.receiver_name; orderInfo.InsertDate = DateTime.Now; orderInfo.OrderUserID = CurrentUserInfo.UserID; orderInfo.Phone = orderRequestModel.receiver_phone; orderInfo.WebsiteOwner = bllMall.WebsiteOwner; orderInfo.Transport_Fee = 0; orderInfo.OrderMemo = orderRequestModel.buyer_memo; orderInfo.ZipCode = orderRequestModel.receiver_zip; orderInfo.MyCouponCardId = orderRequestModel.cardcoupon_id.ToString(); orderInfo.UseScore = orderRequestModel.use_score; orderInfo.Status = "待付款"; orderInfo.Email = orderRequestModel.email; orderInfo.Tel = orderRequestModel.receiver_tel; orderInfo.DeliveryType = orderRequestModel.delivery_type; orderInfo.Ex1 = bllMall.GetTime(long.Parse(orderRequestModel.departure_date)).ToString(); //出国时间 orderInfo.Ex2 = bllMall.GetTime(long.Parse(orderRequestModel.backhome_date)).ToString(); //回国时间 orderInfo.LastUpdateTime = DateTime.Now; if (!string.IsNullOrEmpty(orderRequestModel.sale_id)) //分销ID { long saleId = 0; if (long.TryParse(orderRequestModel.sale_id, out saleId)) { orderInfo.SellerId = saleId; } } if (orderRequestModel.pay_type == "WEIXIN")//微信支付 { orderInfo.PaymentType = 2; } else if (orderRequestModel.pay_type == "ALIPAY")//支付宝支付 { orderInfo.PaymentType = 1; } #region 格式检查 if (string.IsNullOrEmpty(orderInfo.Consignee)) { resp.errcode = 1; resp.errmsg = "收货人姓名不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } if (string.IsNullOrEmpty(orderRequestModel.receiver_phone)) { resp.errcode = 1; resp.errmsg = "收货人联系手机号不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } #region 快递 if (orderRequestModel.delivery_type == 0)//快递 { //相关检查 if (string.IsNullOrEmpty(orderRequestModel.receiver_province)) { resp.errcode = 1; resp.errmsg = "省份名称不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } if (string.IsNullOrEmpty(orderRequestModel.receiver_province_code)) { resp.errcode = 1; resp.errmsg = "省份代码不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } if (string.IsNullOrEmpty(orderRequestModel.receiver_city)) { resp.errcode = 1; resp.errmsg = "城市名称不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } if (string.IsNullOrEmpty(orderRequestModel.receiver_city_code)) { resp.errcode = 1; resp.errmsg = "城市代码不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } if (string.IsNullOrEmpty(orderRequestModel.receiver_dist)) { resp.errcode = 1; resp.errmsg = "城市区域名称不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } if (string.IsNullOrEmpty(orderRequestModel.receiver_dist_code)) { resp.errcode = 1; resp.errmsg = "城市区域代码不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } if (string.IsNullOrEmpty(orderRequestModel.receiver_address)) { resp.errcode = 1; resp.errmsg = "街道地址不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } orderInfo.ReceiverProvince = orderRequestModel.receiver_province; orderInfo.ReceiverProvinceCode = orderRequestModel.receiver_province_code; orderInfo.ReceiverCity = orderRequestModel.receiver_city; orderInfo.ReceiverCityCode = orderRequestModel.receiver_city_code; orderInfo.ReceiverDist = orderRequestModel.receiver_dist; orderInfo.ReceiverDistCode = orderRequestModel.receiver_dist_code; orderInfo.Address = orderRequestModel.receiver_address; orderInfo.ZipCode = orderRequestModel.receiver_zip; } #endregion #region 门自提 if (orderRequestModel.delivery_type == 1)//自提点 { if (string.IsNullOrEmpty(orderRequestModel.get_address_id)) { resp.errcode = 1; resp.errmsg = "自提点ID为必填项,请检查"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } if (string.IsNullOrEmpty(orderRequestModel.get_address_name)) { resp.errcode = 1; resp.errmsg = "自提点名称为必填项,请检查"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } //if (string.IsNullOrEmpty(orderRequestModel.ex7)) //{ // resp.errcode = 1; // resp.errmsg = "自提时间为必填项,请检查"; // context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); // return; //} orderInfo.Ex3 = orderRequestModel.get_address_id; orderInfo.Ex4 = orderRequestModel.get_address_name; orderInfo.Ex5 = bllMall.GetGetAddress(orderRequestModel.get_address_id).GetAddressLocation; //orderInfo.Ex7 = orderRequestModel.ex7;//自提时间 } #endregion if (orderRequestModel.skus == null) { resp.errcode = 1; resp.errmsg = "skus 参数不能为空"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } DateTime startTime = DateTime.Parse(bllMall.GetTime(long.Parse(orderRequestModel.departure_date)).ToString("yyyy/MM/dd")); DateTime returnTime = bllMall.GetTime(long.Parse(orderRequestModel.backhome_date)); if (returnTime <= startTime) { resp.errcode = 1; resp.errmsg = "回国日期不能晚于或等于出发日期"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } int day = (int)(returnTime - startTime).TotalDays + 1; if (day < 3) { resp.errcode = (int)BLLJIMP.Enums.APIErrCode.OperateFail; resp.errmsg = "起租最低为3天"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } //相关检查 #endregion #region 商品检查 订单详情生成 ///订单详情 List <WXMallOrderDetailsInfo> detailList = new List <WXMallOrderDetailsInfo>();//订单详情 //orderRequestModel.skus = orderRequestModel.skus.Distinct().ToList(); #region 购买的商品 List <WXMallProductInfo> productList = new List <WXMallProductInfo>(); foreach (var sku in orderRequestModel.skus) { ProductSku productSku = bllMall.GetProductSku(sku.sku_id); WXMallProductInfo productInfo = bllMall.GetProduct(productSku.ProductId.ToString()); productList.Add(productInfo); } productList = productList.Distinct().ToList(); #endregion #region 检查优惠券是否可用 if (orderRequestModel.cardcoupon_id > 0) { var mycardCoupon = bllCardCoupon.GetMyCardCoupon(orderRequestModel.cardcoupon_id, CurrentUserInfo.UserID); if (mycardCoupon == null) { resp.errcode = 1; resp.errmsg = "无效的优惠券"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } var cardCoupon = bllCardCoupon.GetCardCoupon(mycardCoupon.CardId); if (cardCoupon == null) { resp.errcode = 1; resp.errmsg = "无效的优惠券"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } #region 需要购买指定商品 if ((!string.IsNullOrEmpty(cardCoupon.Ex2)) && (cardCoupon.Ex2 != "0")) { if (productList.Count(p => p.PID == cardCoupon.Ex2) == 0) { var productInfo = bllMall.GetProduct(cardCoupon.Ex2); resp.errcode = 1; resp.errmsg = string.Format("此优惠券需要购买{0}时才可以使用", productInfo.PName); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } } #endregion #region 需要购买指定标签商品 if (!string.IsNullOrEmpty(cardCoupon.Ex8)) { if (productList.Where(p => p.Tags == "" || p.Tags == null).Count() == productList.Count)//全部商品都没有标签 { resp.errcode = 1; resp.errmsg = string.Format("使用此优惠券需要购买标签为{0}的商品", cardCoupon.Ex8); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } bool checkResult = false; foreach (var product in productList) { if (!string.IsNullOrEmpty(product.Tags)) { foreach (string tag in product.Tags.Split(',')) { if (cardCoupon.Ex8.Contains(tag)) { checkResult = true; break; } } } } if (!checkResult) { resp.errcode = 1; resp.errmsg = string.Format("使用此优惠券需要购买标签为{0}的商品", cardCoupon.Ex8); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } } #endregion } #endregion List <int> relationSkuList = new List <int>();//关联的SKU 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.IsDelete == 1) { resp.errcode = 1; resp.errmsg = string.Format("{0}已下架", productInfo.PName); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } 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 (!string.IsNullOrEmpty(productInfo.RelationProductId)) { WXMallProductInfo relationProductInfo = bllMall.GetProduct(productInfo.RelationProductId); var relationProductSkuList = bllMall.GetProductSkuList(int.Parse(relationProductInfo.PID)); if (orderRequestModel.skus.Where(p => p.sku_id == relationProductSkuList[0].SkuId).Count() == 0) { resp.errcode = 1; resp.errmsg = string.Format("{0}必须有关联商品下单", productInfo.PName); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } else { relationSkuList.Add(relationProductSkuList[0].SkuId);//关联的Sku商品不参与运费计算 } } //if (bllMall.IsLimitProductTime(productInfo, orderInfo.Ex1, orderInfo.Ex2)) //{ // resp.errcode = 1; // resp.errmsg = string.Format("{0} 暂时不能购买", productInfo.PName); // 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; if (!string.IsNullOrEmpty(productInfo.RelationProductId))// { //detailModel.OrderPrice = bllMall.GetSkuPrice(productSku) * day; //List<string> dateRange = new List<string>();//时间范围 //startTime = DateTime.Parse(startTime.ToString("yyyy/MM/dd")); //returnTime = DateTime.Parse(returnTime.ToString("yyyy/MM/dd")); //for (int i = 1; i < (returnTime - startTime).TotalDays; i++) //{ // dateRange.Add(startTime.AddDays(i).ToString("yyyy/MM/dd")); //} //dateRange.Add(startTime.ToString("yyyy/MM/dd")); //dateRange.Add(returnTime.ToString("yyyy/MM/dd")); //dateRange = dateRange.Distinct().ToList(); //detailModel.OrderPrice = 0; //foreach (var date in dateRange)// //{ detailModel.OrderPrice = bllMall.GetSkuPriceByDate(productSku, startTime.ToString("yyyy/MM/dd")) * day; //} deviceFee += (decimal)detailModel.OrderPrice * detailModel.TotalCount; } else//设备租金 { detailModel.OrderPrice = bllMall.GetSkuPrice(productSku); } detailModel.ProductName = productInfo.PName; detailModel.SkuId = productSku.SkuId; detailModel.ParentProductId = sku.parent_product_id; detailList.Add(detailModel); } #endregion productFee = detailList.Sum(p => p.OrderPrice * p.TotalCount).Value; //商品费用 #region 运费计算 List <ZentCloud.BLLJIMP.Model.API.Mall.SkuModel> skuList = new List <ZentCloud.BLLJIMP.Model.API.Mall.SkuModel>(); foreach (var item in orderRequestModel.skus) { ZentCloud.BLLJIMP.Model.API.Mall.SkuModel sku = new BLLJIMP.Model.API.Mall.SkuModel(); sku.sku_id = item.sku_id; sku.count = item.count; skuList.Add(sku); } decimal freight = 0; //运费 string freightMsg = ""; if (orderRequestModel.delivery_type == 0) //配送方式为快递时才计算运费 { FreightModel freightModel = new FreightModel(); freightModel.receiver_province_code = int.Parse(orderRequestModel.receiver_province_code); freightModel.receiver_city_code = int.Parse(orderRequestModel.receiver_city_code); freightModel.receiver_dist_code = int.Parse(orderRequestModel.receiver_dist_code); freightModel.skus = skuList; foreach (int relationSku in relationSkuList) { //关联SKU不参与运费计算 freightModel.skus = freightModel.skus.Where(p => p.sku_id != relationSku).ToList(); } 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 orderInfo.Product_Fee = productFee; orderInfo.TotalAmount = orderInfo.Product_Fee + orderInfo.Transport_Fee; #region 优惠券计算 decimal discountAmount = 0;//优惠金额 bool canUseCardCoupon = false; string msg = ""; if (orderRequestModel.cardcoupon_id > 0)//有优惠券 { discountAmount = bllMall.CalcDiscountAmountWifi(orderRequestModel.cardcoupon_id.ToString(), data, CurrentUserInfo.UserID, out canUseCardCoupon, deviceFee, 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; } orderInfo.CardcouponDisAmount = discountAmount; } #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); } //积分计算 #endregion #region 合计计算 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; } #endregion if (orderInfo.TotalAmount <= 0) { //resp.errcode = 1; //resp.errmsg = "付款金额不能小于0"; //context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); //return; orderInfo.TotalAmount = 0; orderInfo.PaymentStatus = 1; orderInfo.PayTime = DateTime.Now; orderInfo.Status = "待发货"; } 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; } //积分记录 UserScoreDetailsInfo scoreRecord = new UserScoreDetailsInfo(); scoreRecord.AddTime = DateTime.Now; scoreRecord.Score = -orderRequestModel.use_score; scoreRecord.TotalScore = CurrentUserInfo.TotalScore; scoreRecord.ScoreType = "OrderSubmit"; scoreRecord.UserID = CurrentUserInfo.UserID; scoreRecord.AddNote = "微商城-下单使用积分"; 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库存 if (ZentCloud.ZCBLLEngine.BLLBase.ExecuteSql(string.Format("update ZCJ_ProductSku set Stock-={0} where SkuId={1} And Stock>0", item.TotalCount, productSku.SkuId), tran) <= 0) { tran.Rollback(); resp.errcode = 1; resp.errmsg = "提交订单失败,库存不足"; context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } } #endregion bllMall.DeleteShoppingCart(CurrentUserInfo.UserID, skuList); bllLog.Add(BLLJIMP.Enums.EnumLogType.Mall, BLLJIMP.Enums.EnumLogTypeAction.Add, bllLog.GetCurrUserID(), "提交订单", orderInfo.OrderID); tran.Commit();//提交订单事务 } catch (Exception ex) { //回滚事物 tran.Rollback(); resp.errcode = 1; resp.errmsg = "提交订单失败,内部错误"; 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, total_amount = orderInfo.TotalAmount })); } catch (Exception ex) { resp.errcode = 1; resp.errmsg = ex.ToString(); context.Response.Write(ZentCloud.Common.JSONHelper.ObjectToJson(resp)); return; } }