using System.Threading.Tasks;
using GxPress.Common.WechatPay;
using GxPress.Repository.Interface.Order;
using GxPress.Service.Interface.Order;
using GxPress.Repository.Interface.Media;
using GxPress.Repository.Interface;
using System;
using System.Collections.Generic;
using GxPress.Repository.Interface.VipEquity;
using GxPress.Common.Tools;
using System.Transactions;
using GxPress.EnumConst;
namespace GxPress.Service.Implement.Order
{
public class OrderService : IOrderService
{
private readonly IOrderRepository orderRepository;
private readonly IMediaRepository mediaRepository;
private readonly IUserRepository userRepository;
private readonly IVipEquityRepository vipEquityRepository;
public OrderService(IOrderRepository orderRepository, IMediaRepository mediaRepository, IUserRepository userRepository, IVipEquityRepository vipEquityRepository)
{
this.orderRepository = orderRepository;
this.mediaRepository = mediaRepository;
this.userRepository = userRepository;
this.vipEquityRepository = vipEquityRepository;
}
///
/// 创建微信订单
///
///
///
public async Task InsertWxOrderAsync(WxPayData data)
{
var attach = data.GetValue("attach").ToString();
if (attach.Split('_').Length > 1)
{
return true;
}
try
{
using (var Transaction = new TransactionScope())
{
var order = await orderRepository.GetOrderAsync(attach.ToString());
var user = await userRepository.GetAsync(order.UserId);
//获取用户数据
order.Price = order.PayWay == PayWayTypeConst.WeChatPay.GetHashCode() ? Decimal.Parse(data.GetValue("total_fee").ToString()) / 100 : Decimal.Parse(data.GetValue("total_fee").ToString());
order.TransactionId = data.GetValue("transaction_id").ToString();
order.OutTradeNo = data.GetValue("out_trade_no").ToString();
order.IsSuccess = true;
//如果是用户增加
if (order.OrderType == 2)
{
var vipEquity = await vipEquityRepository.GetAsync(order.MediaId);
user.IsVip = true;
if ((VipCardTypeConst)vipEquity.VipType == VipCardTypeConst.Month)
user.EndTime = DateTime.Now.AddMonths(1);
if ((VipCardTypeConst)vipEquity.VipType == VipCardTypeConst.Season)
user.EndTime = DateTime.Now.AddMonths(3);
if ((VipCardTypeConst)vipEquity.VipType == VipCardTypeConst.Year)
user.EndTime = DateTime.Now.AddYears(1);
user.AccumulativePrice = user.AccumulativePrice + order.Price;
await userRepository.UpdateAsync(user);
}
if (user.IsVip)
order.IsVip = true;
await orderRepository.UpdateAsync(order);
Transaction.Complete();
return true;
}
}
catch
{
return false;
}
}
///
/// 支付宝
///
///
///
public async Task InsertAliPayOrderAsync(Dictionary sArray)
{
var attach = sArray["out_trade_no"].ToString();
if (attach.Split('_').Length > 1)
return true;
//获取medio属性
var order = await orderRepository.GetOrderAsync(attach.ToString());
//获取用户数据
var user = await userRepository.GetAsync(order.UserId);
order.OutTradeNo = sArray["out_trade_no"].ToString();
order.Price = decimal.Parse(sArray["total_amount"].ToString());
order.TransactionId = sArray["trade_no"].ToString();
order.IsVip = user.IsVip;
order.PayWay = 2;
return await orderRepository.UpdateAsync(order);
}
}
}