using GDZZ.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Linq;
using System.Threading.Tasks;
using GDZZ.Application.Entity;
using GDZZ.Application.Help;
using Furion.DistributedIDGenerator;
using Furion.FriendlyException;
using System;
using GDZZ.Core.Entity;
using System.Collections.Generic;
using GDZZ.Application.Service.WXPay;
using GDZZ.Application.Service;
namespace GDZZ.Application
{
///
/// 邀请码表服务
///
[ApiDescriptionSettings("Application", Name = "InvitationCode", Order = 1)]
public class InvitationCodeService : IInvitationCodeService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _rep;
private readonly SqlSugarRepository inviteUserPosrep;
private readonly SqlSugarRepository sysUserrep; //用户仓储
private readonly SqlSugarRepository rechargeRep; //充值仓储
public InvitationCodeService(SqlSugarRepository rep,
SqlSugarRepository rechargeRep,
SqlSugarRepository sysUserrep,
SqlSugarRepository inviteUserPosrep)
{
_rep = rep;
this.sysUserrep = sysUserrep;
this.rechargeRep = rechargeRep;
this.inviteUserPosrep = inviteUserPosrep;
}
///
/// 获取邀请码
///
///
///
[HttpPost("/InvitationCode/AddOrUpdate")]
public async Task AddOrUpdate(AddInvitationCodeInput input)
{
var entity = input.Adapt();
var first = await this._rep.FirstOrDefaultAsync(x => x.InviteCode == entity.InviteCode);
if (first.IsNullOrZero())
{
entity.InviteCode = Utils.NextLong(0, 100000000);
entity.EnterpriseID = UserManager.UserId;
var incode = await this._rep.FirstOrDefaultAsync(x => x.InviteCode == entity.InviteCode);
if (incode.IsNullOrZero())
await _rep.InsertAsync(entity);
}
else
{
first.InviteCode = Utils.NextLong(0, 100000000);
await _rep.AsUpdateable(first).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
}
///
/// 获取邀请码详情
///
///
///
[HttpGet("/InvitationCode/Detail")]
public async Task Get([FromQuery] QueryeInvitationCodeInput input)
{
var invitation = await _rep.FirstOrDefaultAsync(u => u.CreatedUserId == UserManager.UserId);
if (invitation == null)
throw Oops.Oh("暂无邀请码");
return invitation;
}
///
/// 获取邀请人数
///
///
///
[HttpGet("/InvitationCode/GetPNumber")]
public async Task> GetPNumber()
{
List invitationCodes = new List();
var invitation = await this.inviteUserPosrep.Where(u => u.InviteUserID == UserManager.UserId).ToArrayAsync();
if (invitation.IsNullOrZero())
throw Oops.Oh("暂无邀请人");
foreach (var item in invitation)
{
var sysUser =await this.sysUserrep.FirstOrDefaultAsync(x => x.Id == item.UserID);
invitationCodes.Add(new InvitationCodeOutput()
{
Id = item.Id,
Name = sysUser.Name,
Avatar = sysUser.Avatar,
dateTime = item.CreatedTime,
});
}
return invitationCodes;
}
///
/// 填写邀请码
///
///
///
[HttpPost("/Mini/InvitationCode/Receive")]
public async Task Receive(AddInvitationCodeInput input)
{
var incode = await this._rep.FirstOrDefaultAsync(x => x.InviteCode == input.InviteCode);
if (incode.IsNullOrZero())
throw Oops.Oh("无此邀请码!");
UtilService utilService = new UtilService(this.rechargeRep);
var res = this.inviteUserPosrep.Insert(new InviteUserPos()
{
InviteID = incode.Id,
InviteUserID = (long)incode.CreatedUserId,
UserID = UserManager.UserId,
});
if (res < 1)
throw Oops.Oh("填写邀请码失败!");
//发放奖励
utilService.Reward(UserManager.UserId, 1);
}
///
/// 获取邀请码
///
///
///
[HttpGet("/Mini/InvitationCode/GetNumber")]
public async Task GetNumber()
{
var inviteUserPos = await this.inviteUserPosrep.FirstOrDefaultAsync(u => u.UserID == UserManager.UserId);
if (inviteUserPos.IsNullOrZero())
return 0;
var res = await this._rep.FirstOrDefaultAsync(x => x.Id == inviteUserPos.InviteID);
return res.InviteCode;
}
}
}