You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
BookingHeChuan/Myshipping.Application/Service/Fee/FeeRecordService.cs

147 lines
5.1 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.EventBus;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Myshipping.Application.Entity;
using Myshipping.Application.Service.Fee.Dto;
using Myshipping.Core;
using Myshipping.Core.Service;
using SqlSugar;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Myshipping.Application
{
/// <summary>
/// 费用录入
/// </summary>
[ApiDescriptionSettings("Application", Name = "FeeRecord", Order = 1)]
public class FeeRecordService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<FeeRecord> _repRecord;
private readonly ILogger<FeeRecordService> _logger;
private readonly ISysCacheService _cache;
private readonly ISysUserRoleService _sysUserRoleService;
private readonly IEventPublisher _publisher;
public FeeRecordService(ILogger<FeeRecordService> logger,
ISysCacheService cache,
IEventPublisher publisher,
SqlSugarRepository<FeeRecord> repRecord,
ISysUserRoleService sysUserRoleService)
{
_logger = logger;
_cache = cache;
_sysUserRoleService = sysUserRoleService;
_publisher = publisher;
_repRecord = repRecord;
}
/// <summary>
/// 费用记录查询
/// </summary>
[HttpGet("/FeeRecord/Query")]
public async Task<List<FeeRecordDto>> Query([FromQuery] FeeRecordInputDto input)
{
List<FeeRecord> feeRecords = await _repRecord.AsQueryable()
.Where(r => r.BussId == input.BussId)
.OrderBy(r => new { r.FeeType, r.Sort, r.CreatedTime })
.ToListAsync();
// 如果费用中含有机密费用,再判断是否有查询的权限
if (feeRecords.Any(r => !r.IsOpen))
{
bool canReviewSecretFee = await _sysUserRoleService.IsUserInRole(UserManager.UserId, CommonConst.SECRET_FEE_REVIEW);
if (!canReviewSecretFee)
{
feeRecords.RemoveAll(r => !r.IsOpen && r.CreatedUserId != UserManager.UserId);
}
}
var result = feeRecords.Adapt<List<FeeRecordDto>>();
return result;
}
/// <summary>
/// 费用录入保存
/// </summary>
[HttpPost("/FeeRecord/Save")]
[SqlSugarUnitOfWork]
public async Task Save(FeeRecordSaveDto input)
{
input.SaveList ??= new List<FeeRecordDto>();
input.DeleteList ??= new List<FeeRecordDto>();
var waitInsertList = input.SaveList.Where(r => r.Id == 0).ToList();
var waitUpdateList = input.SaveList.Where(r => r.Id != 0 && r.MarkModify).ToList();
#region 校验
foreach (var item in waitInsertList.Concat(waitUpdateList))
{
// 人民币汇率必须为1其它外币汇率不能为空
if (item.ExchangeRate == 0) throw Oops.Bah("汇率不能为空或0请调整");
if (string.IsNullOrEmpty(item.Currency)) throw Oops.Bah("币别不能为空");
if (item.Currency?.ToUpper() == "RMB")
{
if (item.ExchangeRate != 1) throw Oops.Bah("币别为人民币时汇率需为1请调整");
}
}
#endregion
// 删除
if (input.DeleteList.Any())
{
var waitDeleteListTemp = input.DeleteList.Select(r => r.Id).ToList();
await _repRecord.UpdateAsync(r => waitDeleteListTemp.Contains(r.Id), r => new FeeRecord() { IsDeleted = true });
}
// 新增
if (waitInsertList.Any())
{
var waitInsertListTemp = waitInsertList.Adapt<List<FeeRecord>>();
await _repRecord.InsertAsync(waitInsertListTemp);
}
// 更新
if (waitUpdateList.Any())
{
await _repRecord.AsUpdateable(waitUpdateList).UpdateColumns(r => new
{
r.FeeCode,
r.FeeName,
r.CustomerType,
r.CustomerName,
r.FeeDescription,
r.Currency,
r.ExchangeRate,
r.TaxRate,
r.Tax,
r.Unit,
r.UnitPrice,
r.Quantity,
r.Amount,
r.NoTaxAmount,
r.Remark,
r.IsOpen,
r.IsAdvancedPay,
r.Sort,
r.IsInvoice
}).ExecuteCommandAsync();
}
}
}
}