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
{
///
/// 费用录入
///
[ApiDescriptionSettings("Application", Name = "FeeRecord", Order = 1)]
public class FeeRecordService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _repRecord;
private readonly ILogger _logger;
private readonly ISysCacheService _cache;
private readonly ISysUserRoleService _sysUserRoleService;
private readonly IEventPublisher _publisher;
public FeeRecordService(ILogger logger,
ISysCacheService cache,
IEventPublisher publisher,
SqlSugarRepository repRecord,
ISysUserRoleService sysUserRoleService)
{
_logger = logger;
_cache = cache;
_sysUserRoleService = sysUserRoleService;
_publisher = publisher;
_repRecord = repRecord;
}
///
/// 费用记录查询
///
[HttpGet("/FeeRecord/Query")]
public async Task> Query([FromQuery] FeeRecordInputDto input)
{
List 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>();
return result;
}
///
/// 费用录入保存
///
[HttpPost("/FeeRecord/Save")]
[SqlSugarUnitOfWork]
public async Task Save(FeeRecordSaveDto input)
{
input.FeeRecordList ??= new List();
var waitDeleteList = input.FeeRecordList.Where(r => r.MarkDelete && r.Id != 0).ToList();
var waitInsertList = input.FeeRecordList.Where(r => !r.MarkDelete && r.Id == 0).ToList();
var waitUpdateList = input.FeeRecordList.Where(r => !r.MarkDelete && r.MarkModify && r.Id != 0).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 (waitDeleteList.Any())
{
var waitDeleteListTemp = waitDeleteList.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>();
await _repRecord.InsertAsync(waitInsertListTemp);
}
// 更新
if (waitUpdateList.Any())
{
await _repRecord.AsUpdateable(waitUpdateList).IgnoreColumns(r => new
{
r.Reason,
r.TenantId,
r.TenantName
}).ExecuteCommandAsync();
}
}
}
}