/// <summary> /// 计算运费 /// </summary> /// <param name="store"></param> /// <returns></returns> private int GetFreight(QiyeStore store, List <QiyeGoodsCart> cartList, int getWay, string goodCarIdStr, int aid, WxAddress address, ref string msg) { int friPrice = 0; QiyeStoreSwitchModel config = JsonConvert.DeserializeObject <QiyeStoreSwitchModel>(store.SwitchConfig); int qtySum = cartList.Sum(x => x.Count); switch (getWay) { case (int)miniAppOrderGetWay.到店自取: break; case (int)miniAppOrderGetWay.商家配送: DeliveryFeeResult deliueryResult = DeliveryTemplateBLL.SingleModel.GetQiyeFee(goodCarIdStr, aid, address.provinceName, address.cityName, ref msg); if (msg.Length > 0) { return(0); } friPrice = deliueryResult.Fee; break; } return(friPrice); }
/// <summary> /// 储值支付 /// </summary> /// <param name="orderObj"></param> /// <param name="order"></param> /// <param name="aid"></param> /// <param name="saveMoneyUser"></param> /// <param name="sbUpdateGoodCartSql"></param> /// <param name="reductionCart"></param> /// <param name="orderId"></param> /// <returns></returns> public object PayOrderByChuzhi(object orderObj, int aid, SaveMoneySetUser saveMoneyUser, TransactionModel tran, ref int orderId) { QiyeGoodsOrder order = (QiyeGoodsOrder)orderObj; //储值支付 扣除预存款金额并生成消费记录 //添加储值日志 SaveMoneySetUserLog userLog = new SaveMoneySetUserLog() { AppId = saveMoneyUser.AppId, UserId = order.UserId, MoneySetUserId = saveMoneyUser.Id, Type = -1, BeforeMoney = saveMoneyUser.AccountMoney, AfterMoney = saveMoneyUser.AccountMoney - order.BuyPrice, ChangeMoney = order.BuyPrice, ChangeNote = $" 购买商品,订单号:{order.OrderNum} ", CreateDate = DateTime.Now, State = 1, AId = order.AId, }; tran.Add(Utils.BuildAddSqlS(userLog, "OrderId", "(select LAST_INSERT_ID())", SaveMoneySetUserLogBLL.SingleModel.TableName, SaveMoneySetUserLogBLL.SingleModel.arrProperty)); //储值扣费 tran.Add($" update SaveMoneySetUser set AccountMoney = AccountMoney - {order.BuyPrice} where id = {saveMoneyUser.Id} ; "); if (!ExecuteTransactionDataCorect(tran.sqlArray, tran.ParameterArray)) { log4net.LogHelper.WriteInfo(this.GetType(), "储值支付:操作失败" + Newtonsoft.Json.JsonConvert.SerializeObject(tran)); return(""); } QiyeStore store = QiyeStoreBLL.SingleModel.GetModel(order.StoreId); if (store != null) { //清除商品缓存 QiyeGoodsBLL.SingleModel.RemoveEntGoodListCache(store.Aid); } orderId = 1; order = GetModelByOrderNum(order.OrderNum); if (order == null) { log4net.LogHelper.WriteInfo(this.GetType(), "储值支付:生成订单失败"); return(""); } //发送支付成功通知给用户 object orderData = TemplateMsg_Miniapp.QiyeGetTemplateMessageData(order, SendTemplateMessageTypeEnum.企业智推版订单支付成功通知); TemplateMsg_Miniapp.SendTemplateMessage(order.UserId, SendTemplateMessageTypeEnum.企业智推版订单支付成功通知, TmpType.企业智推版, orderData); //发送支付成功公众号通知给商家 TemplateMsg_Gzh.SendQiyePaySuccessTemplateMessage(order); return(new { postdata = order.OrderNum, orderid = 0, dbOrder = order.Id }); }
/// <summary> /// 微信支付 /// </summary> /// <param name="orderObj"></param> /// <param name="cityMorder"></param> /// <returns></returns> public object PayOrder(object orderObj, CityMorders cityMorder, TransactionModel tran, ref int orderId) { CityMordersBLL cityMordersBLL = new CityMordersBLL(); QiyeGoodsOrder order = (QiyeGoodsOrder)orderObj; cityMorder.payment_free = order.BuyPrice; if (tran == null || tran.sqlArray.Count() <= 0) { log4net.LogHelper.WriteInfo(this.GetType(), "独立模板生成订单失败,sql为空"); return(""); } TransactionModel tranModel = new TransactionModel(); tranModel.Add(cityMordersBLL.BuildAddSql(cityMorder)); foreach (string sqlitem in tran.sqlArray) { tranModel.Add(sqlitem); } if (!ExecuteTransactionDataCorect(tranModel.sqlArray, tranModel.ParameterArray)) { log4net.LogHelper.WriteInfo(this.GetType(), "独立模板生成订单失败" + JsonConvert.SerializeObject(tran)); return(""); } QiyeStore store = QiyeStoreBLL.SingleModel.GetModel(order.StoreId); if (store != null) { //清除商品缓存 QiyeGoodsBLL.SingleModel.RemoveEntGoodListCache(store.Aid); } order = GetModelByOrderNum(order.OrderNum); if (order == null) { log4net.LogHelper.WriteInfo(this.GetType(), $"独立模板生成订单失败,获取不到生成的订单【{order.OrderNum}】"); return(""); } if (order.OrderId <= 0) { log4net.LogHelper.WriteInfo(this.GetType(), "独立模板生成订单OrderId失败" + JsonConvert.SerializeObject(tranModel)); return(""); } cityMorder.Id = order.OrderId; orderId = order.OrderId; //为0不需进入生成微信预支付订单的流程(免费订单) if (order.BuyPrice == 0) { PayResult payresult = new PayResult(); new CityMordersBLL(payresult, cityMorder).QiyePayOrder(0, order); return(new { orderid = order.Id, dbOrder = order.Id }); } else //生成微信预支付订单 { return(new { orderid = order.OrderId, dbOrder = order.Id }); } }
// GET: public ActionResult Index() { int aid = Context.GetRequestInt("appId", 0); if (aid <= 0) { aid = Context.GetRequestInt("aid", 0); } XcxAppAccountRelation xcxrelation = XcxAppAccountRelationBLL.SingleModel.GetModel(aid); AccountRelation accountrelation = AccountRelationBLL.SingleModel.GetModelByAccountId(xcxrelation.AccountId.ToString()); QiyeStore qiyeStore = QiyeStoreBLL.SingleModel.GetModelByAId(aid); if (qiyeStore == null) { qiyeStore = new QiyeStore(); qiyeStore.Aid = aid; qiyeStore.AddTime = DateTime.Now; qiyeStore.UpdateTime = DateTime.Now; qiyeStore.SwitchModel = new QiyeStoreSwitchModel(); qiyeStore.SwitchConfig = JsonConvert.SerializeObject(qiyeStore.SwitchModel); int id = Convert.ToInt32(QiyeStoreBLL.SingleModel.Add(qiyeStore)); if (id <= 0) { return(View("PageError", new QiyeReturnMsg() { Msg = "初始化数据失败!", code = "500" })); } } return(View()); }
/// <summary> /// 企业智推版运费 /// </summary> /// <param name="goodcartids"></param> /// <param name="aid"></param> /// <param name="province"></param> /// <param name="city"></param> /// <param name="msg"></param> /// <returns></returns> public DeliveryFeeResult GetQiyeFee(string goodcartids, int aid, string province, string city, ref string msg) { DeliveryFeeResult deliueryResult = new DeliveryFeeResult(); //购物车 List <QiyeGoodsCart> cartList = QiyeGoodsCartBLL.SingleModel.GetListByIds(goodcartids); if (cartList == null || cartList.Count <= 0) { msg = "运费:购物车数据为空"; return(deliueryResult); } //商品 string goodsIds = string.Join(",", cartList.Select(s => s.GoodsId)); List <QiyeGoods> goodsList = QiyeGoodsBLL.SingleModel.GetListByIds(goodsIds); if (goodsList == null || goodsList.Count <= 0) { msg = "运费:找不到商品数据"; return(deliueryResult); } QiyeStore store = QiyeStoreBLL.SingleModel.GetModelByAId(aid); if (store == null) { msg = "运费:无效店铺"; return(deliueryResult); } DeliveryFeeSumMethond sumMethod; PlatStoreSwitchModel config = string.IsNullOrWhiteSpace(store.SwitchConfig) ? new PlatStoreSwitchModel() : JsonConvert.DeserializeObject <PlatStoreSwitchModel>(store.SwitchConfig); if (config.enableDeliveryTemplate) { if (!Enum.TryParse(config.deliveryFeeSumMethond.ToString(), out sumMethod)) { msg = "运费:无效运费模板"; return(deliueryResult); } List <DeliveryProduct> productInfo = new List <DeliveryProduct>(); foreach (QiyeGoods goodItem in goodsList) { List <QiyeGoodsCart> tempCartList = cartList.Where(w => w.GoodsId == goodItem.Id).ToList(); if (tempCartList == null || tempCartList.Count <= 0) { msg = "运费:无效购物车数据"; return(deliueryResult); } productInfo.Add(new DeliveryProduct { TemplateId = goodItem.TemplateId, Name = goodItem.Name, Count = tempCartList.Sum(s => s.Count), }); } deliueryResult = GetDeliveryFeeCommon(productInfo, province, city, sumMethod); if (deliueryResult == null) { msg = "运费:无效运费模板"; } else if (!deliueryResult.InRange) { msg = deliueryResult.Message; } } return(deliueryResult); }
/// <summary> /// 基本验证 /// </summary> /// <param name="userId"></param> /// <param name="wxaddressjson"></param> /// <param name="userInfo"></param> /// <param name="xcxRelation"></param> /// <param name="address"></param> /// <param name="msg"></param> private void CheckBaseData(string jsonData, int userId, ref QiyeGoodsOrder postData, ref C_UserInfo userInfo, ref List <QiyeGoodsCart> cartList, ref XcxAppAccountRelation xcxRelation, ref WxAddress address, ref QiyeStore store, ref string msg, int orderType) { postData = JsonConvert.DeserializeObject <QiyeGoodsOrder>(jsonData); if (postData == null) { msg = "基本验证:订单请求数据不能为空"; return; } userInfo = C_UserInfoBLL.SingleModel.GetModel(userId); if (userInfo == null) { msg = "基本验证:无效用户"; return; } xcxRelation = XcxAppAccountRelationBLL.SingleModel.GetModelByAppid(userInfo.appId); if (xcxRelation == null) { msg = "基本验证:无效模板"; return; } store = QiyeStoreBLL.SingleModel.GetModelByAId(xcxRelation.Id); if (store == null) { msg = $"基本验证:无效店铺"; return; } if (postData.GetWay == (int)miniAppOrderGetWay.到店自取) { if (store.SwitchConfigModel != null && !store.SwitchConfigModel.SwitchReceiving) { msg = $"基本验证:店铺暂不支持到店自取"; return; } } if (string.IsNullOrEmpty(postData.CartIds)) { msg = "基本验证:无效购物车数据"; return; } List <string> goodCarIdList = postData.CartIds.Split(',').ToList(); if (postData.CartIds.Substring(postData.CartIds.Length - 1, 1) == ",") { postData.CartIds = postData.CartIds.Substring(0, postData.CartIds.Length - 1); } cartList = QiyeGoodsCartBLL.SingleModel.GetListByIds(postData.CartIds); if (cartList == null || cartList.Count <= 0) { msg = "基本验证:找不到购物车记录"; return; } //地址验证 if (string.IsNullOrWhiteSpace(postData.WxAddresJson)) { msg = "基本验证:无效地址"; return; } address = JsonConvert.DeserializeObject <WxAddress>(postData.WxAddresJson); if (address == null) { msg = "基本验证:地址信息不存在"; return; } if (string.IsNullOrWhiteSpace(postData.AccepterName)) { msg = "基本验证:未输入收/提货人姓名"; return; } if (string.IsNullOrWhiteSpace(postData.AccepterTelePhone)) { msg = "基本验证:未输入收/提货电话或格式不正确"; return; } }
/// <summary> /// 生成订单 /// </summary> /// <param name="appid"></param> /// <param name="openid"></param> /// <param name="goodCarIdStr">购物车ID集合字符串:格式为(id1,id2)</param> /// <param name="order"></param> /// <returns></returns> public string AddOrder(int userId, string jsonData, ref TransactionModel tran, ref object orderObj, int orderType) { string msg = ""; //基本验证 QiyeGoodsOrder postData = new QiyeGoodsOrder(); C_UserInfo userInfo = new C_UserInfo(); List <QiyeGoodsCart> cartList = new List <QiyeGoodsCart>(); XcxAppAccountRelation xcxRelation = new XcxAppAccountRelation(); WxAddress address = new WxAddress(); QiyeStore store = new QiyeStore(); CheckBaseData(jsonData, userId, ref postData, ref userInfo, ref cartList, ref xcxRelation, ref address, ref store, ref msg, orderType); if (msg.Length > 0) { return(msg); } //检测库存 CheckGoods(cartList, ref msg); if (msg.Length > 0) { return(msg); } //优惠价格处理 int afterDiscountPrice = 0; int beforeDiscountPrice = 0; int vipDiscountPrice = 0; int couponPrice = 0; DiscountPrice(postData.CouponLogId, ref cartList, userInfo.Id, ref beforeDiscountPrice, ref afterDiscountPrice, ref vipDiscountPrice, ref couponPrice, ref msg); if (msg.Length > 0) { return(msg); } if (afterDiscountPrice > 999999999) { return("商品价格有误"); } //运费 int friPrice = GetFreight(store, cartList, postData.GetWay, postData.CartIds, store.Aid, address, ref msg); if (msg.Length > 0) { return(msg); } //订单 QiyeGoodsOrder order = new QiyeGoodsOrder(); order.AccepterName = postData.AccepterName; order.AccepterTelePhone = postData.AccepterTelePhone; order.ZipCode = address.postalCode; order.Address = postData.Address; order.Message = postData.Message; order.TemplateType = (int)TmpType.企业智推版; order.StoreId = store.Id; order.UserId = userInfo.Id; order.AddTime = DateTime.Now; order.AId = xcxRelation.Id; order.QtyCount = cartList.Sum(x => x.Count); order.BuyMode = postData.BuyMode; order.GetWay = postData.GetWay; order.AppId = userInfo.appId; order.SumPrice = beforeDiscountPrice; order.VipReducedPrice = vipDiscountPrice; order.ReducedPrice = beforeDiscountPrice - afterDiscountPrice; order.FreightPrice = friPrice; order.CouponPrice = couponPrice; order.BuyPrice = afterDiscountPrice + friPrice; order.BuyPrice = order.BuyPrice <= 0 ? 0 : order.BuyPrice; order.OrderNum = CommonCore.CreateOrderNum(cartList[0].Id.ToString()); if (order.BuyMode == (int)miniAppBuyMode.微信支付) { order.State = (int)QiyeOrderState.待付款; switch (order.GetWay) { case (int)miniAppOrderGetWay.到店自取: //4位随机取物码 System.Random Random = new System.Random(); order.LadingCode = Random.Next(0, 9999).ToString("0000"); break; } } else if (order.BuyMode == (int)miniAppBuyMode.储值支付) { order.PayTime = DateTime.Now; switch (order.GetWay) { case (int)miniAppOrderGetWay.到店自取: order.State = (int)QiyeOrderState.待自取; //4位随机取物码 System.Random Random = new System.Random(); order.LadingCode = Random.Next(0, 9999).ToString("0000"); break; case (int)miniAppOrderGetWay.商家配送: order.State = (int)QiyeOrderState.待发货; break; } } //新增订单sql tran.Add(Utils.BuildAddSqlS(order, "OrderId", "(select LAST_INSERT_ID())", base.TableName, base.arrProperty)); //修改优惠券状态 if (postData.CouponLogId > 0) { tran.Add($" update CouponLog set OrderId = (select last_insert_id()),State = 1 where id ={postData.CouponLogId}"); } //tran.Add(base.BuildAddSql(order)); //修改购物车订单 foreach (QiyeGoodsCart cartitem in cartList) { tran.Add($" update QiyeGoodsCart set OrderId = (select last_insert_id()),State = 1,Discount = {cartitem.Discount},price={cartitem.Price},OriginalPrice={cartitem.OriginalPrice} where id ={cartitem.Id} and state = 0; "); } //修改库存 UpdateGoodsStock(cartList, ref tran, ref msg, true); orderObj = order; return(msg); }