public ViewResult <VerificationCode> RandomCode() { var result = new ViewResult <VerificationCode>(); try { var model = new VerificationCode(); var code = Convert.ToBoolean(showRandomCode) ? RandomCodeHelper.CreateRandomCode(4) : "1234"; model.ValidaCode = PasswordHelper.GetMd5Hash(code.ToLower()); model.ValidateImage = RandomCodeHelper.CreateValidateGraphic(code); result.Data = model; } catch (Exception ex) { result.Status = -1; result.Message = ex.Message; Logger.WriteErrorLog(ex); } return(result); }
/// <summary> /// 订单确认收款 /// </summary> public static string ReceBusinessOrder(string OrderId, decimal realAmount) { try { String moblie = String.Empty; using (IDbConnection conn = new SqlConnection(ConnectionDb.GetBusinessConnection())) { var code = RandomCodeHelper.CreateRandomCode(5); string sql = "SELECT * FROM Tb_Charge_Receipt WHERE OrderId=@OrderId OR Id=@OrderId"; Tb_Charge_Receipt Receipt = conn.Query <Tb_Charge_Receipt>(sql, new { OrderId = OrderId }).SingleOrDefault(); if (Receipt.IsPay.ToString() == "已付款") { return("订单已付款"); } //订单确认收款,收款动作写在下面 Receipt.IsPay = "已付款"; Receipt.PayDate = DateTime.Now; Receipt.Method = "支付宝"; Receipt.RealAmount = realAmount; Receipt.ExtractionCode = code; //总价上面不包括运费 //Receipt.RealAmount = realAmount - Receipt.Freight; //获取商家的手机号 String businessMoblieSql = " SELECT BussMobileTel FROM Tb_System_BusinessCorp WHERE bussId=@bussId "; moblie = conn.QueryFirstOrDefault <String>(businessMoblieSql, new { bussId = Receipt.BussId }); // 对于没有使用优惠券的,直接设置Amount if (Receipt.IsUseCoupon <= 0) { //GetLog().Info(new { realAmount = realAmount, RealAmount = Receipt.RealAmount }); conn.Execute("UPDATE Tb_Charge_Receipt SET IsPay=@IsPay,PayDate=@PayDate,Method='支付宝',Amount=@RealAmount,RealAmount=@RealAmount WHERE Id=@Id", new { IsPay = Receipt.IsPay.ToString(), PayDate = Receipt.PayDate.ToString(), RealAmount = Receipt.RealAmount, Id = Receipt.Id.ToString() }); IEnumerable <Tb_Charge_ReceiptDetail> ReceiptDetails = conn.Query <Tb_Charge_ReceiptDetail>(@"SELECT * FROM Tb_Charge_ReceiptDetail WHERE ReceiptCode=@ReceiptCode", new { ReceiptCode = Receipt.Id }); foreach (Tb_Charge_ReceiptDetail item in ReceiptDetails) { Tb_Resources_Details resources = conn.Query <Tb_Resources_Details>("SELECT * FROM Tb_Resources_Details WHERE ResourcesID=@ResourcesID", new { ResourcesID = item.ResourcesID }).FirstOrDefault(); if (resources != null) { #region 根据购物车获取具体的某一个规格属性 ,并使用该规格属性上得价格 String sqlStr = @"SELECT A.Price,A.DiscountAmount,A.GroupBuyingPrice FROM Tb_ResourcesSpecificationsPrice AS A INNER JOIN (SELECT B.ResourcesID,C.PropertysId,C.SpecId FROM Tb_ShoppingCar AS B LEFT JOIN Tb_ShoppingDetailed AS C ON B.Id=C.ShoppingId WHERE B.Id IS NOT NULL AND B.Id=@Id) AS D ON D.ResourcesID=A.ResourcesID AND D.PropertysId =A.PropertyId AND D.SpecId=A.SpecId"; var praceInfo = conn.QueryFirstOrDefault(sqlStr, new { Id = item.ShoppingId }); if (null == praceInfo) { return(JSONHelper.FromString(false, "商品没有规格属性")); } resources.ResourcesSalePrice = (decimal)praceInfo.Price; resources.GroupBuyPrice = (decimal)praceInfo.GroupBuyingPrice; resources.ResourcesDisCountPrice = (decimal)praceInfo.DiscountAmount; #endregion decimal detailAmount = 0.0m; if (resources.IsGroupBuy == "是" && resources.GroupBuyEndDate.HasValue && resources.GroupBuyEndDate > DateTime.Now) { detailAmount = resources.GroupBuyPrice.Value * item.Quantity; } else { detailAmount = (resources.ResourcesSalePrice - resources.ResourcesDisCountPrice) * item.Quantity; } conn.Execute(@"UPDATE Tb_Charge_ReceiptDetail SET SalesPrice=@SalesPrice, DiscountPrice=@DiscountPrice, GroupPrice=@GroupPrice, DetailAmount=@DetailAmount, OffsetMoney=0 WHERE ResourcesID=@ResourcesID AND RpdCode=@RpdCode", new { SalesPrice = resources.ResourcesSalePrice, DiscountPrice = resources.ResourcesDisCountPrice, GroupPrice = (( resources.IsGroupBuy == "是" && resources.GroupBuyEndDate.HasValue && resources.GroupBuyEndDate > DateTime.Now && resources.GroupBuyStartDate.HasValue && resources.GroupBuyStartDate <= DateTime.Now ) ? resources.GroupBuyPrice : null), DetailAmount = detailAmount, ResourcesID = resources.ResourcesID, RpdCode = item.RpdCode }); // 减库存 //conn.Execute("UPDATE Tb_Resources_Details SET ResourcesCount=(ResourcesCount-@Count) WHERE ResourcesID=@ResourcesID", // new { Count = AppGlobal.StrToLong(item.Quantity.ToString()), ResourcesID = resources.ResourcesID }); #region 减库存,减少规格参数得库存 //获取当前订单得所有商品 sql = $@"SELECT B.ResourcesID,C.PropertysId,C.SpecId,A.ReceiptCode,Quantity FROM Tb_Charge_ReceiptDetail AS A INNER JOIN Tb_ShoppingCar AS B ON A.ShoppingId=B.Id INNER JOIN Tb_ShoppingDetailed AS C ON A.ShoppingId=C.ShoppingId WHERE A.RpdCode=@Id"; var detail = conn.Query(sql, new { Id = item.RpdCode }); //逐个减少各个规格得参数,增加销售数量 StringBuilder updateStr = new StringBuilder(); foreach (var model in detail) { updateStr.Append($"UPDATE Tb_ResourcesSpecificationsPrice SET Inventory= Inventory - {model.Quantity} ,SalesVolumes= SalesVolumes + {item.Quantity } WHERE BussId={Receipt.BussId} AND ResourcesID='{model.ResourcesID}' AND PropertyId= '{model.PropertysId}' AND SpecId='{model.SpecId}' ;"); } var count = conn.Execute(updateStr.ToString()); //然后重新核对商品得数量,增加销售数量 if (count == detail.Count()) { conn.Execute($"UPDATE Tb_Resources_Details SET ResourcesCount=(SELECT SUM(Inventory) AS Inventory FROM Tb_ResourcesSpecificationsPrice WHERE ResourcesID=@ResourcesID),ActualSalesVolumes = ActualSalesVolumes + {item.Quantity} WHERE ResourcesID =@ResourcesID", new { ResourcesID = resources.ResourcesID }); } #endregion } } } else { IEnumerable <Tb_Charge_ReceiptDetail> ReceiptDetails = conn.Query <Tb_Charge_ReceiptDetail>(@"SELECT * FROM Tb_Charge_ReceiptDetail WHERE ReceiptCode=@ReceiptCode", new { ReceiptCode = Receipt.Id }); IEnumerable <Tb_Resources_Details> resources = conn.Query <Tb_Resources_Details>(@"SELECT * FROM Tb_Resources_Details WHERE ResourcesID in(SELECT ResourcesID FROM Tb_Charge_ReceiptDetail WHERE ReceiptCode=@ReceiptCode)", new { ReceiptCode = Receipt.Id }); decimal totalAmount = 0.0m; // 商品总价,此处计算的是不算优惠券时的总价 // 此时Tb_Charge_ReceiptDetail.DetailAmount保存的是未计算优惠券时的价格,最终价格需要另行计算 foreach (Tb_Resources_Details item in resources) { foreach (Tb_Charge_ReceiptDetail receiptDetail in ReceiptDetails) { if (receiptDetail.ResourcesID == item.ResourcesID) { // 团购 if (item.IsGroupBuy == "是" && item.GroupBuyEndDate.HasValue && item.GroupBuyEndDate > DateTime.Now) { receiptDetail.DetailAmount = (item.GroupBuyPrice.Value * receiptDetail.Quantity); } else { receiptDetail.DetailAmount = (item.ResourcesSalePrice - item.ResourcesDisCountPrice) * receiptDetail.Quantity; } totalAmount += receiptDetail.DetailAmount.Value; break; } } } conn.Execute("UPDATE Tb_Charge_Receipt SET IsPay=@IsPay,PayDate=@PayDate,Method='支付宝',Amount=@Amount,RealAmount=@RealAmount WHERE Id=@Id", new { IsPay = Receipt.IsPay.ToString(), PayDate = Receipt.PayDate.ToString(), Amount = totalAmount, RealAmount = Receipt.RealAmount, Id = Receipt.Id.ToString() }); // 本次使用的抵扣券金额 //decimal couponMoney = totalAmount - realAmount; //decimal tempUsedCouponMoney = 0.0m;// 暂时没有用处,判断本次使用的抵扣券是否和订单详细记录里面使用的抵扣券价格一样 foreach (Tb_Resources_Details item in resources) { foreach (Tb_Charge_ReceiptDetail receiptDetail in ReceiptDetails) { if (receiptDetail.ResourcesID == item.ResourcesID) { // 更新订单详情表商品付费金额信息 conn.Execute(@"UPDATE Tb_Charge_ReceiptDetail SET SalesPrice=@SalesPrice, DiscountPrice=@DiscountPrice, GroupPrice=@GroupPrice, DetailAmount=@DetailAmount WHERE ResourcesID=@ResourcesID AND ReceiptCode=@ReceiptCode", new { SalesPrice = item.ResourcesSalePrice, DiscountPrice = item.ResourcesDisCountPrice, GroupPrice = ((item.IsGroupBuy == "是" && item.GroupBuyEndDate.HasValue && item.GroupBuyEndDate > DateTime.Now) ? item.GroupBuyPrice : null), DetailAmount = receiptDetail.DetailAmount.Value - receiptDetail.OffsetMoney.Value, ResourcesID = receiptDetail.ResourcesID, ReceiptCode = receiptDetail.ReceiptCode }); break; } } } } //支付成功生成核销码 conn.Execute("UPDATE Tb_Charge_Receipt SET ExtractionCode=@ExtractionCode WHERE Id=@Id", new { ExtractionCode = code, Id = Receipt.Id.ToString() }); //订单推送 //Orders.OrderInfoPush(Receipt.CorpId, Receipt.OrderId, moblie); return("success"); } } catch (Exception ex) { return(ex.Message); } }