feat tickets

This commit is contained in:
2025-02-11 16:33:21 +08:00
parent c9df417799
commit 964e033583
33 changed files with 473 additions and 28 deletions

View File

@@ -0,0 +1,50 @@
using Dlhis.Canteen.Tickets.Request;
using Dlhis.Canteen.Tickets.Response;
using Dlhis.Canteen.Tickets.Services;
using Kurisu.AspNetCore.DataAccess.Dto;
using Kurisu.AspNetCore.Utils.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Dlhis.Canteen.Tickets.Controllers;
/// <summary>
/// 券
/// </summary>
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class TicketsController : ControllerBase
{
private readonly INamedResolver _namedResolver;
public TicketsController(INamedResolver namedResolver)
{
_namedResolver = namedResolver;
}
/// <summary>
/// 获取分页数据(按券类型)
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
[HttpGet("page")]
public async Task<Pagination<TicketPageResponse>> GetPageAsync(TicketPageRequest request)
{
return await _namedResolver.GetService<ITicketService>(request.Type.ToString()).GetPageAsync(request);
}
/// <summary>
/// 创建券
/// </summary>
/// <param name="type">券类型,需要后端支持</param>
[AllowAnonymous]
[HttpPost("{type:required}/create")]
public async Task CreateAsync(string type)
{
var srv = _namedResolver.GetService<ITicketService>(type);
srv.ThrowIfNull("不支持的券类型");
await srv.CreateAsync();
}
}

View File

@@ -0,0 +1,19 @@
using System.ComponentModel.DataAnnotations;
using Dlhis.Entity.Canteen.Enums;
using Kurisu.AspNetCore.DataAccess.Dto;
namespace Dlhis.Canteen.Tickets.Request;
public class TicketPageRequest : PageDto
{
/// <summary>
/// 类型
/// </summary>
[EnumDataType(typeof(TicketEnumType))]
public TicketEnumType Type { get; set; }
/// <summary>
/// 工号
/// </summary>
public string UserCode { get; set; }
}

View File

@@ -0,0 +1,41 @@
using Dlhis.Entity.Canteen.Enums;
namespace Dlhis.Canteen.Tickets.Response;
public class TicketPageResponse
{
/// <summary>
/// 券类型
/// </summary>
public TicketEnumType Type { get; set; }
/// <summary>
/// 工号
/// </summary>
public string UserCode { get; set; }
/// <summary>
/// 用户名称
/// </summary>
public string UserName { get; set; }
/// <summary>
/// 初始余额
/// </summary>
public decimal OriginalAmount { get; set; }
/// <summary>
/// 当前余额
/// </summary>
public decimal Amount { get; set; }
/// <summary>
/// 消费码
/// </summary>
public string QrCode { get; set; }
/// <summary>
/// 备注
/// </summary>
public string Remark { get; set; }
}

View File

@@ -0,0 +1,6 @@
namespace Dlhis.Canteen.Tickets.Services;
public interface IBirthdayTicketService : ITicketService
{
}

View File

@@ -0,0 +1,24 @@
using Dlhis.Canteen.Tickets.Request;
using Dlhis.Canteen.Tickets.Response;
using Kurisu.AspNetCore.DataAccess.Dto;
namespace Dlhis.Canteen.Tickets.Services;
/// <summary>
/// 券
/// </summary>
public interface ITicketService : IScopeDependency
{
/// <summary>
/// 分页数据
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
Task<Pagination<TicketPageResponse>> GetPageAsync(TicketPageRequest request);
/// <summary>
/// 创建
/// </summary>
/// <returns></returns>
Task CreateAsync();
}

View File

@@ -0,0 +1,65 @@
using System.Text;
using Dlhis.Constants;
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Canteen.Entities;
using Dlhis.Entity.Canteen.Enums;
using Kurisu.AspNetCore.DataAccess.Entity;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using Kurisu.AspNetCore.Utils.Extensions;
using SqlSugar;
namespace Dlhis.Canteen.Tickets.Services.Implements;
[Service(nameof(TicketEnumType.Birthday))]
public class BirthdayTicketService : BaseTicketService, IBirthdayTicketService
{
/// <inheritdoc />
public BirthdayTicketService(IDbContext dbContext) : base(dbContext)
{
}
/// <inheritdoc />
public override async Task CreateAsync()
{
await DbContext.IgnoreAsync<ITenantId>(async () =>
{
var today = DateTime.Today;
var userCodes = await DbContext.Queryable<GainfoEntity>()
.Where(x => SqlFunc.DateIsSame(SqlFunc.ToDate(x.Birthday), today, DateType.Day))
.Select(x => new { x.Code, x.Factory })
.ToListAsync();
const decimal amount = 30;
var todayStr = today.ToString("yyyy-MM-dd");
foreach (var item in userCodes)
{
var qrCode = GetQrCode(item.Code + todayStr);
if (!await DbContext.Queryable<TicketEntity>().Where(x => x.Type == TicketEnumType.Birthday && x.UserCode == item.Code && x.QrCode == qrCode).AnyAsync())
{
await DbContext.InsertAsync(new TicketEntity
{
Type = TicketEnumType.Birthday,
UserCode = item.Code,
OriginalAmount = amount,
Amount = amount,
QrCode = qrCode,
Remark = todayStr + "生日券",
TenantId = item.Factory
});
}
}
});
}
private static string GetQrCode(string data)
{
var qrCode = QrCodeConstants.Birthday + data.ToArray().Select(x =>
{
var b = Encoding.ASCII.GetBytes(x.ToString());
int i = b[0];
return Convert.ToString(i);
}).Join(string.Empty);
return qrCode;
}
}

View File

@@ -0,0 +1,32 @@
using Dlhis.Canteen.Tickets.Request;
using Dlhis.Canteen.Tickets.Response;
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Canteen.Entities;
using Kurisu.AspNetCore.DataAccess.Dto;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using Kurisu.AspNetCore.Utils.Extensions;
namespace Dlhis.Canteen.Tickets.Services.Implements;
public abstract class BaseTicketService : ITicketService
{
protected IDbContext DbContext { get; set; }
protected BaseTicketService(IDbContext dbContext)
{
DbContext = dbContext;
}
public async Task<Pagination<TicketPageResponse>> GetPageAsync(TicketPageRequest request)
{
return await DbContext.Queryable<TicketEntity>()
.LeftJoin<GainfoEntity>((x, ga) => x.UserCode == ga.Code)
.Where(x => x.Type == request.Type)
.WhereIF(request.UserCode.IsPresent(), x => x.UserCode == request.UserCode)
.Select((x, ga) => new TicketPageResponse { UserName = ga.Name }, true)
.ToPageListAsync(request);
}
public abstract Task CreateAsync();
}