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; namespace GDZZ.Application { /// /// 邀请码表服务 /// [ApiDescriptionSettings("Application", Name = "InvitationCode", Order = 1)] public class InvitationCodeService : IInvitationCodeService, IDynamicApiController, ITransient { private readonly SqlSugarRepository _rep; private readonly SqlSugarRepository inviteUserPosrep; public InvitationCodeService(SqlSugarRepository rep, SqlSugarRepository inviteUserPosrep) { _rep = rep; 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() { var invitation = await this.inviteUserPosrep.Where(u => u.InviteUserID == UserManager.UserId).ToArrayAsync(); if (invitation.IsNullOrZero()) throw Oops.Oh("暂无邀请人"); return invitation.Count(); } /// /// 填写邀请码 /// /// /// [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("无此邀请码!"); this.inviteUserPosrep.Insert(new InviteUserPos() { InviteID = incode.Id, InviteUserID = (long)incode.CreatedUserId, UserID = UserManager.UserId, }); } /// /// 获取邀请码 /// /// /// [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; } } }