106 lines
3.7 KiB
C#
106 lines
3.7 KiB
C#
using System.Text;
|
|
using Dlhis.Canteen.EMealVoucher.Request;
|
|
using Dlhis.Canteen.EMealVoucher.Response;
|
|
using Dlhis.Entity.Canteen.Entities;
|
|
using Kurisu.AspNetCore.Authentication.Abstractions;
|
|
using Kurisu.AspNetCore.DataAccess.Dto;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
using SqlSugar;
|
|
|
|
namespace Dlhis.Canteen.EMealVoucher.Services.Implements;
|
|
|
|
public class EMealVoucherService : IEMealVoucherService
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
private readonly ICurrentUser _currentUser;
|
|
|
|
public EMealVoucherService(IDbContext dbContext, ICurrentUser currentUser)
|
|
{
|
|
_dbContext = dbContext;
|
|
_currentUser = currentUser;
|
|
}
|
|
|
|
|
|
public async Task<Pagination<EMealVoucherPageResponse>> GetMyEMealVoucherPageAsync(EMealVoucherPageRequest request)
|
|
{
|
|
var code = _currentUser.GetUserClaim("jinko-code");
|
|
code = "0123456";
|
|
return await _dbContext.Queryable<EMealVoucherEntity>()
|
|
.Where(x => x.Code == code)
|
|
.OrderBy(x => x.Id, OrderByType.Desc)
|
|
.Select<EMealVoucherPageResponse>()
|
|
.ToPageListAsync(request);
|
|
}
|
|
|
|
|
|
public async Task<List<EMealVoucherDetailResponse>> GetDetailsByNoAsync(string no)
|
|
{
|
|
return await _dbContext.Queryable<EMealVoucherDetailEntity>()
|
|
.Where(x => x.No == no)
|
|
.Select<EMealVoucherDetailResponse>()
|
|
.ToListAsync();
|
|
}
|
|
|
|
|
|
public async Task<string> GetQrCodeSignatureAsync(long detailId)
|
|
{
|
|
return await GenerateSignatureAsync(detailId, false);
|
|
}
|
|
|
|
public async Task RefreshSignatureAsync(long detailId)
|
|
{
|
|
await GenerateSignatureAsync(detailId, true);
|
|
}
|
|
|
|
private async Task<string> GenerateSignatureAsync(long detailId, bool isRefresh)
|
|
{
|
|
var eMealVoucherDetail = await _dbContext.Queryable<EMealVoucherDetailEntity>().Where(x => x.Id == detailId).SingleAsync();
|
|
eMealVoucherDetail.ThrowIfNull("数据不存在");
|
|
if (eMealVoucherDetail.Signature.IsPresent())
|
|
{
|
|
if (isRefresh)
|
|
{
|
|
await _dbContext.Deleteable<EMealVoucherDetailQrCodeEntity>().Where(x => x.Signature == eMealVoucherDetail.Signature).ExecuteCommandAsync();
|
|
}
|
|
else
|
|
{
|
|
return eMealVoucherDetail.Signature;
|
|
}
|
|
}
|
|
|
|
eMealVoucherDetail.Signature = Guid.NewGuid().ToString().Replace("-", "") + DateTimeOffset.Now.ToUnixTimeMilliseconds();
|
|
await _dbContext.UpdateAsync(eMealVoucherDetail);
|
|
|
|
return eMealVoucherDetail.Signature;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<string> GetQrCodeAsync(string signature)
|
|
{
|
|
var eMealVoucherDetail = await _dbContext.Queryable<EMealVoucherDetailEntity>().Where(x => x.Signature == signature).SingleAsync();
|
|
eMealVoucherDetail.ThrowIfNull("无效地址");
|
|
var eMealVoucher = await _dbContext.Queryable<EMealVoucherEntity>().Where(x => x.No == eMealVoucherDetail.No).SingleAsync();
|
|
|
|
var ts = DateTimeOffset.Now.ToUnixTimeMilliseconds().ToString().Reverse().Join(string.Empty);
|
|
var originData = string.Concat(eMealVoucher.No, ":", ts, ":", eMealVoucher.Code, ":", eMealVoucherDetail.IdNo);
|
|
|
|
var qrCode = "02" + originData.ToArray().Select(x =>
|
|
{
|
|
var b = Encoding.ASCII.GetBytes(x.ToString());
|
|
int i = b[0];
|
|
return Convert.ToString(i);
|
|
}).Join(string.Empty);
|
|
|
|
|
|
await _dbContext.InsertAsync(new EMealVoucherDetailQrCodeEntity
|
|
{
|
|
Signature = signature,
|
|
OriginData = originData,
|
|
QrCode = qrCode
|
|
});
|
|
|
|
return qrCode;
|
|
}
|
|
} |