using Furion;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.EventBus;
using Furion.FriendlyException;
using Furion.LinqBuilder;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Myshipping.Application.ConfigOption;
using Myshipping.Application.Entity;
using Myshipping.Application.Event;
using Myshipping.Application.Service.BookingOrder.Dto;
using Myshipping.Application.Service.BookingSlot.Dto;
using Myshipping.Application.Service.Fee.Dto;
using Myshipping.Core;
using Myshipping.Core.Service;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Yitter.IdGenerator;
namespace Myshipping.Application
{
///
/// 费用模板
///
[ApiDescriptionSettings("Application", Name = "FeeTemplate", Order = 1)]
public class FeeTemplateService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _repTemplate;
private readonly SqlSugarRepository _repTemplateDetail;
private readonly ILogger _logger;
private readonly ISysCacheService _cache;
private readonly IEventPublisher _publisher;
public FeeTemplateService(SqlSugarRepository repTemplate,
SqlSugarRepository repTemplateDetail,
ILogger logger,
ISysCacheService cache,
IEventPublisher publisher)
{
_repTemplate = repTemplate;
_repTemplateDetail = repTemplateDetail;
_logger = logger;
_cache = cache;
_publisher = publisher;
}
///
/// 费用模板查询
///
///
///
[HttpPost("/FeeTemplate/Page")]
public async Task Page(FeeTemplatePageInput input)
{
var entities = await _repTemplate.AsQueryable()
.WhereIF(!string.IsNullOrEmpty(input.OpType), u => u.OpType == input.OpType)
.WhereIF(!string.IsNullOrEmpty(input.FeeType), u => u.FeeType == input.FeeType)
.WhereIF(!string.IsNullOrEmpty(input.Name), u => u.Name.Contains(input.Name))
.ToPagedListAsync(input.PageNo, input.PageSize);
var result = entities.Adapt>();
return result.XnPagedResult();
}
///
/// 保存
///
///
///
[HttpPost("/FeeTemplate/Save")]
public async Task Save(FeeTemplateSaveDto input)
{
if (input == null)
{
throw Oops.Bah("请传入正常数据!");
}
FeeTemplate entity = null;
if (input.Id == 0)
{
entity = input.Adapt();
entity.Id = YitIdHelper.NextId();
await _repTemplate.InsertAsync(entity);
}
else
{
entity = await _repTemplate.AsQueryable().Filter(null, true).FirstAsync(x => x.Id == input.Id);
entity = input.Adapt(entity);
await _repTemplate.UpdateAsync(entity);
}
await _repTemplateDetail.DeleteAsync(x => x.TemplateID == entity.Id);
foreach (var det in input.Detail)
{
var detail = det.Adapt();
detail.Id = YitIdHelper.NextId();
detail.TemplateID = entity.Id;
await _repTemplateDetail.InsertAsync(detail);
}
var rtn = entity.Adapt();
rtn.Detail = input.Detail;
return rtn;
}
///
/// 获取详情
///
///
///
[HttpGet("/FeeTemplate/Detail")]
public async Task Detail(long id)
{
var entity = await _repTemplate.AsQueryable().Filter(null, true).FirstAsync(x => x.Id == id);
var detailList = await _repTemplateDetail.AsQueryable().Where(x => x.TemplateID == id).ToListAsync();
var rtn = entity.Adapt();
rtn.Detail = detailList.Adapt>();
return rtn;
}
///
/// 删除
///
///
///
[HttpPost("/FeeTemplate/Delete")]
public async Task Delete(List ids)
{
var list = await _repTemplate.AsQueryable().Filter(null, true).Where(x => ids.Contains(x.Id)).ToListAsync();
list.ForEach(x => x.IsDeleted = true);
await _repTemplate.AsUpdateable(list).ExecuteCommandAsync();
}
}
}