186 lines
7.5 KiB
C#
186 lines
7.5 KiB
C#
using System.Text;
|
|
using Dlhis.Canteen.Order.Request;
|
|
using Dlhis.Canteen.Order.Response;
|
|
using Dlhis.Canteen.Voucher.Requests;
|
|
using Dlhis.Canteen.Voucher.Response;
|
|
using Dlhis.Constants;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
using Dlhis.Entity.Canteen.Entities;
|
|
using Dlhis.Entity.Canteen.Entities.EMealVoucher;
|
|
using Dlhis.Entity.Canteen.Enums;
|
|
using Kurisu.AspNetCore.Authentication.Abstractions;
|
|
using Kurisu.AspNetCore.CustomClass;
|
|
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.Voucher.Services.Implements;
|
|
|
|
public class EMealVoucherService : IEMealVoucherService
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
private readonly ICurrentUser _currentUser;
|
|
|
|
public EMealVoucherService(IDbContext dbContext, ICurrentUser currentUser)
|
|
{
|
|
_dbContext = dbContext;
|
|
_currentUser = currentUser;
|
|
}
|
|
|
|
|
|
/// <inheritdoc />
|
|
public async Task<Pagination<EMealVoucherPageResponse>> GetPageAsync(EMealVoucherPageRequest request)
|
|
{
|
|
return await _dbContext.Queryable<EMealVoucherEntity>()
|
|
.WhereIF(request.Search.IsPresent(), x => x.No == request.Search ||
|
|
x.Code == request.Search ||
|
|
x.Title.Contains(request.Search))
|
|
.WhereIF(request.SearchDateTimePeriod.HasValue(), x => x.Date >= request.SearchDateTimePeriod.Start && x.Date <= request.SearchDateTimePeriod.End)
|
|
.OrderBy(x => x.Date, OrderByType.Desc)
|
|
.Select(x => new EMealVoucherPageResponse
|
|
{
|
|
No = x.No,
|
|
Title = x.Title,
|
|
Code = x.Code,
|
|
Phone = x.Phone,
|
|
CurrentFactories = x.Factories.ToString(),
|
|
Date = x.Date,
|
|
CostId = x.CostId,
|
|
Source = x.Source,
|
|
Reason = x.Reason,
|
|
CurrentShifts = x.Shifts.ToString(),
|
|
Details = SqlFunc.Subqueryable<EMealVoucherDetailEntity>().Where(p => p.No == x.No).ToList(p => new EMealVoucherDetailResponse
|
|
{
|
|
Id = p.Id,
|
|
Name = p.Name,
|
|
Phone = p.Phone,
|
|
Start = p.Start,
|
|
End = p.End,
|
|
Balance = p.Balance,
|
|
OriginBalance = p.OriginBalance,
|
|
})
|
|
})
|
|
.ToPageListAsync(request);
|
|
}
|
|
|
|
public async Task<Pagination<EMealVoucherMyPageResponse>> GetMyEMealVoucherPageAsync(EMealVoucherPageRequest request)
|
|
{
|
|
var code = _currentUser.GetUserClaim(UserClaimConstants.JinkoCode);
|
|
|
|
return await _dbContext.Queryable<EMealVoucherEntity>()
|
|
.Where(x => x.Code == code)
|
|
.OrderBy(x => x.Id, OrderByType.Desc)
|
|
.Select<EMealVoucherMyPageResponse>()
|
|
.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 = QrCodeConstants.EMealVoucher + 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;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<Pagination<BalanceRecordEMealVoucherResponse>> GetEMealVoucherPageAsync(BalanceRecordEMealVoucherPageRequest request)
|
|
{
|
|
var tenantId = _currentUser.GetStringTenantId();
|
|
|
|
return await _dbContext.Queryable<BalanceRecordEntity>()
|
|
.LeftJoin<BusinessPlaceEntity>((x, bPlace) => x.BusinessPlaceCode == bPlace.Code)
|
|
.LeftJoin<DepartmentEntity>((x, bPlace, department) => x.DepartmentNo == department.No)
|
|
.Where(x => x.Status == BalanceRecordStatusEnumType.Valid && x.Type == BalanceRecordTypeEnumType.Out) //有效,消费数据
|
|
.WhereIF(request.SearchDateTimePeriod.HasValue(), x => x.CostDate >= request.SearchDateTimePeriod.Start && x.CostDate <= request.SearchDateTimePeriod.End) //成本日期
|
|
.Where(x => x.Source == BalanceRecordSourceEnumType.EMealVoucher)
|
|
.Where(x => SqlFunc.Subqueryable<EMealVoucherEntity>().Where(p => p.Factory == tenantId).Where(p => p.Code == x.UserCode).Any() || x.TenantId == tenantId)
|
|
.Select((x, bPlace, department) => new BalanceRecordEMealVoucherResponse
|
|
{
|
|
No = x.No,
|
|
UserNo = x.UserNo,
|
|
Name = x.Name,
|
|
UserCode = x.UserCode,
|
|
DepartmentNo = x.DepartmentNo,
|
|
DepartmentName = department.Name,
|
|
CostDate = x.CostDate,
|
|
Remark = x.Remark,
|
|
Source = x.Source,
|
|
Type = x.Type,
|
|
Amount = SqlFunc.Abs(x.Amount),
|
|
BusinessPlaceName = bPlace.Name,
|
|
ShiftType = x.ShiftType,
|
|
DeviceInfo = x.DeviceUniqueNo,
|
|
CostId = x.CostId,
|
|
OriginTenantId = x.OriginTenantId,
|
|
Status = x.Status,
|
|
CostDateTime = x.CostDateTime,
|
|
Factory = x.TenantId,
|
|
Werks = x.Werks
|
|
})
|
|
.ToPageListAsync(request);
|
|
}
|
|
} |