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.

168 lines
5.6 KiB

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
{
/// <summary>
/// 邀请码表服务
/// </summary>
[ApiDescriptionSettings("Application", Name = "InvitationCode", Order = 1)]
public class InvitationCodeService : IInvitationCodeService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<InvitationCode> _rep;
private readonly SqlSugarRepository<InviteUserPos> inviteUserPosrep;
private readonly SqlSugarRepository<SysUser> sysUserrep; //用户仓储
private readonly SqlSugarRepository<MiniRecharge> rechargeRep; //充值仓储
public InvitationCodeService(SqlSugarRepository<InvitationCode> rep,
SqlSugarRepository<MiniRecharge> rechargeRep,
SqlSugarRepository<SysUser> sysUserrep,
SqlSugarRepository<InviteUserPos> inviteUserPosrep)
{
_rep = rep;
this.sysUserrep = sysUserrep;
this.rechargeRep = rechargeRep;
this.inviteUserPosrep = inviteUserPosrep;
}
/// <summary>
/// 获取邀请码
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/InvitationCode/AddOrUpdate")]
public async Task AddOrUpdate(AddInvitationCodeInput input)
{
var entity = input.Adapt<InvitationCode>();
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();
}
}
/// <summary>
/// 获取邀请码详情
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/InvitationCode/Detail")]
public async Task<InvitationCode> Get([FromQuery] QueryeInvitationCodeInput input)
{
var invitation = await _rep.FirstOrDefaultAsync(u => u.CreatedUserId == UserManager.UserId);
if (invitation == null)
throw Oops.Oh("暂无邀请码");
return invitation;
}
/// <summary>
/// 获取邀请人数
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/InvitationCode/GetPNumber")]
public async Task<List<InvitationCodeOutput>> GetPNumber()
{
List<InvitationCodeOutput> invitationCodes = new List<InvitationCodeOutput>();
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;
}
/// <summary>
/// 填写邀请码
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[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);
}
/// <summary>
/// 获取邀请码
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/Mini/InvitationCode/GetNumber")]
public async Task<long> 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;
}
}
}