You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

383 lines
16 KiB

using Znyc.CloudCar.Auth.HttpContextUser;
using Znyc.CloudCar.Configuration;
using Znyc.CloudCar.IRepository.Currency;
using Znyc.CloudCar.IRepository.User;
using Znyc.CloudCar.IServices.Currency;
using Znyc.CloudCar.Model.Dtos.Currency;
using Znyc.CloudCar.Model.Entities;
using Znyc.CloudCar.Model.ViewModels.ReportsCallBack;
using Znyc.CloudCar.Utility.Extensions;
using Znyc.CloudCar.Utility.Helper;
using EnumExtensions = Znyc.CloudCar.Utility.Extensions.EnumExtensions;
namespace Znyc.CloudCar.Services.Currency
{
/// <summary>
/// 用户云币服务
/// </summary>
public class CurrencyService : ICurrencyService
{
private readonly ICurrencyRepository _currencyRepository;
private readonly ICurrencyRecordRepository _currencyRecordRepository;
private readonly IHttpContextUser _httpContextUser;
private readonly IUserRepository _userRepository;
public CurrencyService(
ICurrencyRepository currencyRepository,
ICurrencyRecordRepository currencyRecordRepository,
IHttpContextUser httpContextUser,
IUserRepository userRepository
)
{
_currencyRepository = currencyRepository;
_currencyRecordRepository = currencyRecordRepository;
_httpContextUser = httpContextUser;
_userRepository = userRepository;
}
/// <summary>
/// 总云币
/// </summary>
/// <returns></returns>
public async Task<ResponseOutput> GetAsync()
{
CurrencyOutput currency = await _currencyRepository.GetAsync<CurrencyOutput>(x => x.UserId == _httpContextUser.Id);
ResponseOutput response = new ResponseOutput()
{
Data = currency,
Successed = true,
Code = 1
};
return response;
}
/// <summary>
/// 云币账单
/// </summary>
/// <param name="currencyType">0全部/1收入/2支出</param>
/// <param name="currentPage"></param>
/// <param name="pageSize"></param>
/// <returns></returns>
public async Task<ResponseOutput> PageAsync(int currencyType, int currentPage, int pageSize)
{
List<CurrencyRecordOutput> list = await _currencyRecordRepository.Select
.Where(x => x.UserId == _httpContextUser.Id)
.WhereIf(currencyType > 0, x => x.OperatingType == currencyType)
.Count(out long total)
.OrderByDescending(x => x.CreatedTime)
.Page(currentPage, pageSize)
.ToListAsync<CurrencyRecordOutput>();
foreach (CurrencyRecordOutput item in list)
{
item.Title = EnumExtensions
.ParseEnum(typeof(GlobalEnumVars.CurrencyType), item.CurrencyType.ToString()).ToDescription();
item.ScoreDescription = item.OperatingType == 1
? GlobalConstVars.CurrencyOperatingType.Add + item.OperatingCurrency
: GlobalConstVars.CurrencyOperatingType.Reduce + item.OperatingCurrency;
}
List<CurrencyRecordListOutput> currencyRecordListOutput = list
.GroupBy(x => x.CreatedTime.ToString("yyyy-MM"))
.Select(grp => new CurrencyRecordListOutput
{
Month = grp.Key.ToString(),
List = grp.ToList()
})
.ToList();
PageOutput<CurrencyRecordListOutput> data = new PageOutput<CurrencyRecordListOutput>
{
List = currencyRecordListOutput,
Total = total
};
ResponseOutput response = new ResponseOutput()
{
Data = data,
Successed = true,
Code = 1
};
return response;
}
/// <summary>
/// 首次登录加云币
/// </summary>
/// <returns></returns>
public async Task AddCurrencyForFirstLogin(long userId)
{
var currencyRecord = await _currencyRecordRepository.GetAsync(x => x.CurrencySoureObjectId == userId && x.CurrencyType == (int)GlobalEnumVars.CurrencyType.FirstLogin);
//不存在此记录
if (currencyRecord == null)
{
var currency = await _currencyRepository.GetAsync(x => x.UserId == userId);
if (currency == null)
{
await _currencyRepository.InsertAsync(new CurrencyEntity
{
UserId = userId,
OfficialCurrency = GlobalConstVars.Currency.FirstLogin,
AvailableCurrency = GlobalConstVars.Currency.FirstLogin
});
await _currencyRecordRepository.InsertAsync(new CurrencyRecordEntity
{
UserId = userId,
OperatingCurrency = GlobalConstVars.Currency.FirstLogin,
OperatingType = (int)GlobalEnumVars.OperatingType.Add,
CurrencyType = (int)GlobalEnumVars.CurrencyType.FirstLogin,
CurrencySoureObjectId = userId
});
}
}
}
/// <summary>
/// 充值加云币
/// </summary>
/// <param name="userId"></param>
/// <param name="orderId"></param>
/// <param name="credits"></param>
/// <returns></returns>
public async Task AddCurrencyByCharge(long userId, long orderId, int credits)
{
CurrencyRecordEntity currencyRecord = await _currencyRecordRepository.GetAsync(x =>
x.CurrencySoureObjectId == orderId && x.CurrencyType == (int)GlobalEnumVars.CurrencyType.BuyCurrency);
//不存在此记录
if (currencyRecord.IsNull())
{
CurrencyEntity currency = await _currencyRepository.GetAsync(x => x.UserId == userId);
currency.OfficialCurrency += credits;
currency.AvailableCurrency += credits;
await _currencyRepository.UpdateAsync(currency);
await _currencyRecordRepository.InsertAsync(new CurrencyRecordEntity
{
UserId = userId,
OperatingCurrency = credits,
OperatingType = (int)GlobalEnumVars.OperatingType.Add,
CurrencyType = (int)GlobalEnumVars.CurrencyType.BuyCurrency,
CurrencySoureObjectId = orderId
});
}
}
/// <summary>
/// 邀请新用户加云币
/// </summary>
/// <param name="userId"></param>
/// <param name="receiveUserId"></param>
/// <returns></returns>
public async Task AddCurrencyForNewUsers(long userId, long receiveUserId)
{
//判断是否多次被邀请
CurrencyRecordEntity currencyRecord = await _currencyRecordRepository.GetAsync(x =>
x.CurrencySoureObjectId == receiveUserId && x.CurrencyType == (int)GlobalEnumVars.CurrencyType.NewUsers);
if (currencyRecord.IsNull())
{
CurrencyEntity currency = await _currencyRepository.GetAsync(x => x.UserId == userId);
if (currency.IsNotNull())
{
currency.OfficialCurrency += GlobalConstVars.Currency.NewUsers;
currency.AvailableCurrency += GlobalConstVars.Currency.NewUsers;
await _currencyRepository.UpdateAsync(currency);
await _currencyRecordRepository.InsertAsync(new CurrencyRecordEntity
{
UserId = userId,
OperatingCurrency = GlobalConstVars.Currency.NewUsers,
OperatingType = (int)GlobalEnumVars.OperatingType.Add,
CurrencyType = (int)GlobalEnumVars.CurrencyType.NewUsers,
CurrencySoureObjectId = receiveUserId
});
}
}
}
#region 设备
/// <summary>
/// 刷新扣除云币
/// </summary>
/// <param name="equipmentId"></param>
/// <returns></returns>
public async Task<ResponseOutput> RefreshDeduct(long equipmentId)
{
ResponseOutput response = new ResponseOutput();
CurrencyEntity currency = await _currencyRepository.GetAsync(x => x.UserId == _httpContextUser.Id);
if (currency.AvailableCurrency < GlobalConstVars.Currency.Refresh)
{
response.Msg = $"您当前的云币不足,刷新需要消耗{GlobalConstVars.Currency.Refresh}云币,是否前往获取云币?";
response.Successed = false;
response.Code = 2;
return response;
}
//临时云币足够时优先扣除临时云币
if (currency.TemporarylCurrency >= GlobalConstVars.Currency.Refresh)
{
currency.TemporarylCurrency -= GlobalConstVars.Currency.Refresh;
}
//不够时组合扣除积分
else
{
currency.OfficialCurrency -= GlobalConstVars.Currency.Refresh - currency.TemporarylCurrency;
currency.TemporarylCurrency = GlobalConstVars.Currency.Default_TemporarylCredits;
}
currency.AvailableCurrency -= GlobalConstVars.Currency.Refresh;
await _currencyRepository.UpdateAsync(currency);
await _currencyRecordRepository.InsertAsync(new CurrencyRecordEntity
{
UserId = _httpContextUser.Id,
OperatingCurrency = GlobalConstVars.Currency.Refresh,
OperatingType = (int)GlobalEnumVars.OperatingType.Reduce,
CurrencyType = (int)GlobalEnumVars.CurrencyType.RefreshEquipment,
CurrencySoureObjectId = equipmentId
});
response.Successed = true;
response.Code = 1;
return response;
}
/// <summary>
/// 置顶扣除云币
/// </summary>
/// <param name="equipmentId"></param>
/// <returns></returns>
public async Task<ResponseOutput> TopDeduct(long equipmentId)
{
ResponseOutput response = new ResponseOutput();
CurrencyEntity currency = await _currencyRepository.GetAsync(x => x.UserId == _httpContextUser.Id);
if (currency.OfficialCurrency < GlobalConstVars.Currency.TopEquipment)
{
response.Msg = $"您当前的正式云币不足,置顶需要消耗{GlobalConstVars.Currency.TopEquipment}云币,是否前往获取云币?";
response.Successed = false;
response.Code = 2;
return response;
}
currency.OfficialCurrency -= GlobalConstVars.Currency.TopEquipment;
currency.AvailableCurrency -= GlobalConstVars.Currency.TopEquipment;
await _currencyRepository.UpdateAsync(currency);
await _currencyRecordRepository.InsertAsync(new CurrencyRecordEntity
{
UserId = _httpContextUser.Id,
OperatingCurrency = GlobalConstVars.Currency.TopEquipment,
OperatingType = (int)GlobalEnumVars.OperatingType.Reduce,
CurrencyType = (int)GlobalEnumVars.CurrencyType.TopEquipment,
CurrencySoureObjectId = equipmentId
});
response.Successed = true;
response.Code = 1;
return response;
}
/// <summary>
/// 获取电话扣除云币
/// </summary>
/// <param name="equipmentId"></param>
/// <returns></returns>
public async Task<ResponseOutput> GetPhoneDeduct(long equipmentId, int sellingPrice)
{
ResponseOutput response = new ResponseOutput();
int callPhoneCurrency = CommonHelper.GetCallPhoneCurrency(sellingPrice);
CurrencyEntity currency = await _currencyRepository.GetAsync(x => x.UserId == _httpContextUser.Id);
if (currency.AvailableCurrency < callPhoneCurrency)
{
response.Msg = $"您当前的云币不足,获取电话需要消耗{callPhoneCurrency}云币,是否前往获取云币?";
response.Successed = false;
response.Code = 2;
return response;
}
//临时云币足够时优先扣除临时云币
if (currency.TemporarylCurrency >= callPhoneCurrency)
{
currency.TemporarylCurrency -= callPhoneCurrency;
}
//不够时组合扣除积分
else
{
currency.OfficialCurrency -= callPhoneCurrency - currency.TemporarylCurrency;
currency.TemporarylCurrency = GlobalConstVars.Currency.Default_TemporarylCredits;
}
currency.AvailableCurrency -= callPhoneCurrency;
await _currencyRepository.UpdateAsync(currency);
await _currencyRecordRepository.InsertAsync(new CurrencyRecordEntity
{
UserId = _httpContextUser.Id,
OperatingCurrency = callPhoneCurrency,
OperatingType = (int)GlobalEnumVars.OperatingType.Reduce,
CurrencyType = (int)GlobalEnumVars.CurrencyType.CallPhone,
CurrencySoureObjectId = equipmentId
});
response.Successed = true;
response.Code = 1;
return response;
}
#endregion
/// <summary>
/// 购买优惠卡赠送云币
/// </summary>
/// <param name="userId"></param>
/// <param name="orderId"></param>
/// <param name="credits"></param>
/// <returns></returns>
public async Task AddCurrencyByBuyCard(long userId, long orderId, int credits)
{
CurrencyRecordEntity currencyRecord = await _currencyRecordRepository.GetAsync(x =>
x.CurrencySoureObjectId == orderId && x.CurrencyType == (int)GlobalEnumVars.CurrencyType.BuyCurrency);
//不存在此记录
if (currencyRecord.IsNull())
{
CurrencyEntity currency = await _currencyRepository.GetAsync(x => x.UserId == userId);
currency.OfficialCurrency += credits;
currency.AvailableCurrency += credits;
await _currencyRepository.UpdateAsync(currency);
await _currencyRecordRepository.InsertAsync(new CurrencyRecordEntity
{
UserId = userId,
OperatingCurrency = credits,
OperatingType = (int)GlobalEnumVars.OperatingType.Add,
CurrencyType = (int)GlobalEnumVars.CurrencyType.PreferentialCard,
CurrencySoureObjectId = orderId
});
}
} /// <summary>
/// 分享
/// </summary>
/// <param name="userId"></param>
/// <param name="shareType"></param>
/// <returns></returns>
public async Task<ResponseOutput> ShareAsync(string shareType, long userId)
{
ResponseOutput response = new ResponseOutput();
var user = _userRepository.GetAsync(userId);
if (user.IsNotNull())
{
switch (shareType.ToLower())
{
//邀请新用户
case "newusers":
await AddCurrencyForNewUsers(userId, _httpContextUser.Id);
break;
////每日分享
//case "dailyshare":
// await AddCurrencyForDailyShare(_user.Id);
// break;
default:
await AddCurrencyForNewUsers(userId, _httpContextUser.Id);
break;
}
}
response.Successed = true;
response.Code = 1;
return response;
}
}
}