✨
This commit is contained in:
102
StopShopping.Services/Implementions/ReplyService.cs
Normal file
102
StopShopping.Services/Implementions/ReplyService.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
using System.Data.Common;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using StopShopping.EF;
|
||||
using StopShopping.Services.Extensions;
|
||||
using StopShopping.Services.Models;
|
||||
using StopShopping.Services.Models.Req;
|
||||
using StopShopping.Services.Models.Resp;
|
||||
|
||||
namespace StopShopping.Services.Implementions;
|
||||
|
||||
public class ReplyService : IReplyService
|
||||
{
|
||||
public ReplyService(
|
||||
IClaimsService claimsService,
|
||||
StopShoppingContext dbContext,
|
||||
ILogger<ReplyService> logger)
|
||||
{
|
||||
_claimsService = claimsService;
|
||||
_dbContext = dbContext;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
private readonly IClaimsService _claimsService;
|
||||
private readonly StopShoppingContext _dbContext;
|
||||
private readonly ILogger<ReplyService> _logger;
|
||||
|
||||
public async Task<ApiResponse<List<Reply>>> GetRepliesAsync(RequestIdParams model)
|
||||
{
|
||||
var request = await _dbContext.Requests
|
||||
.Include(r => r.Replies)
|
||||
.ThenInclude(r => r.Product)
|
||||
.Include(r => r.Replies)
|
||||
.ThenInclude(r => r.User)
|
||||
.AsNoTracking()
|
||||
.Where(r => r.Id == model.RequestId && !r.Deleted)
|
||||
.FirstOrDefaultAsync();
|
||||
if (null == request)
|
||||
return new ApiResponse<List<Reply>>().Failed("此需求已不存在,请刷新重试");
|
||||
|
||||
var replies = request.Replies
|
||||
.Where(r => !r.Rejected)
|
||||
.Select(r => new Reply
|
||||
{
|
||||
Amount = r.Amount,
|
||||
Id = r.Id,
|
||||
Memo = r.Memo,
|
||||
ProductId = r.ProductId,
|
||||
ProductName = r.Product.Name,
|
||||
Replier = r.User.NickName,
|
||||
ReplyTime = r.ReplyTime.ToFormatted(),
|
||||
UnitPrice = r.Product.UnitPrice,
|
||||
MinimumUnit = r.Product.MinimumUnit
|
||||
}).ToList();
|
||||
|
||||
return new ApiResponse<List<Reply>>(replies);
|
||||
}
|
||||
|
||||
public async Task<ApiResponse> ReplyAsync(ReplyParams model)
|
||||
{
|
||||
var userId = _claimsService.GetCurrentUserId();
|
||||
|
||||
using var trans = await _dbContext.Database.BeginTransactionAsync();
|
||||
try
|
||||
{
|
||||
var request = await _dbContext.Requests
|
||||
.Where(r => r.Id == model.RequestId && !r.Deleted)
|
||||
.FirstOrDefaultAsync();
|
||||
if (null == request)
|
||||
return ApiResponse.Failed("此需求已不存在,请刷新重试");
|
||||
|
||||
var status = (RequestStatus)request.Status;
|
||||
if (!status.CanReply())
|
||||
return ApiResponse.Failed("此需求已完成,请尝试其他需求");
|
||||
|
||||
request.Status = (short)RequestStatus.Replied;
|
||||
|
||||
EF.Models.Reply reply = new()
|
||||
{
|
||||
Amount = model.Amount,
|
||||
Memo = model.Memo,
|
||||
Price = model.Price,
|
||||
ProductId = model.ProductId,
|
||||
RequestId = model.RequestId,
|
||||
UserId = userId
|
||||
};
|
||||
|
||||
await _dbContext.Replies.AddAsync(reply);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
await trans.CommitAsync();
|
||||
}
|
||||
catch (DbException ex)
|
||||
{
|
||||
await trans.RollbackAsync();
|
||||
_logger.LogError(ex, "提交竞标失败");
|
||||
return ApiResponse.Failed("服务器错误,请刷新重试");
|
||||
}
|
||||
|
||||
return ApiResponse.Succed();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user