using Myshipping.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Linq;
using System.Threading.Tasks;
using Myshipping.Application.Entity;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using Furion.FriendlyException;
using Myshipping.Core.Entity;
namespace Myshipping.Application
{
///
/// 订舱关系人服务
///
[ApiDescriptionSettings("Application", Name = "BookingOrderContact", Order = 1)]
public class BookingOrderContactService : IBookingOrderContactService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _rep;
private readonly SqlSugarRepository _repOrder;
private readonly SqlSugarRepository _repContact;
private readonly ILogger _logger;
public BookingOrderContactService(SqlSugarRepository rep,
SqlSugarRepository repOrder,
SqlSugarRepository repContact,
ILogger logger)
{
_rep = rep;
_repOrder = repOrder;
_repContact = repContact;
_logger = logger;
}
///
/// 分页查询订舱关系人
///
///
///
[HttpGet("/BookingOrderContact/page")]
public async Task Page([FromQuery] QueryBookingOrderContactInput input)
{
if (input.BookingId <= 0)
{
throw Oops.Oh($"查询订舱关系人失败,请先保存订舱数据再编辑订舱关系人");
}
var entities = await _rep.AsQueryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.RoleCode), u => u.RoleCode.Contains(input.RoleCode.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name.Contains(input.Name.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Tel), u => u.Tel.Contains(input.Tel.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Email), u => u.Email.Contains(input.Email.Trim()))
.WhereIF(input.BookingId != 0, u => u.BookingId == input.BookingId)
.ToPagedListAsync(input.PageNo, input.PageSize);
return entities.XnPagedResult();
}
///
/// 增加订舱关系人
///
///
///
[HttpPost("/BookingOrderContact/add")]
public async Task Add(AddBookingOrderContactInput input)
{
var entity = input.Adapt();
await _rep.InsertAsync(entity);
}
///
/// 更新订舱关系人
///
///
///
[HttpPost("/BookingOrderContact/edit")]
public async Task Update(UpdateBookingOrderContactInput input)
{
var entity = input.Adapt();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
///
/// 删除订舱关系人
///
///
///
[HttpPost("/BookingOrderContact/delete")]
public async Task Delete(GetBookingOrderContactInput input)
{
var entity = await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
await _rep.DeleteAsync(entity);
}
///
/// 获取订舱关系人
///
///
///
[HttpGet("/BookingOrderContact/detail")]
public async Task Get([FromQuery] GetBookingOrderContactInput input)
{
return await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
}
///
/// 根据订舱ID获取关系人
///
///
///
[HttpGet("/BookingOrderContact/listbyorder")]
public async Task> ListByOrder(long bookingId)
{
var list = await _rep.Where(x => x.BookingId == bookingId).ToListAsync();
return list.Adapt>();
}
///
/// 批量保存关系人(同时新增、删除、修改)
///
///
///
[HttpPost("/BookingOrderContact/savebatch"), SqlSugarUnitOfWork]
public async Task SaveBatch(List input, long bookingId)
{
if (bookingId <= 0)
{
throw Oops.Oh($"保存关系人失败,请先保存订舱数据再编辑往来单位联系人");
}
_rep.Delete(x => x.BookingId == bookingId);
if (input != null && input.Count > 0)
{
foreach (var item in input)
{
await _rep.InsertAsync(item.Adapt());
}
}
//自动增加到该客户的联系人
var bookObj = _repOrder.FirstOrDefault(x => x.Id == bookingId);
if (bookObj.CUSTOMERID.HasValue && bookObj.CUSTOMERID.Value > 0)
{
var dbContacts = _repContact.Where(x => x.CustomerId == bookObj.CUSTOMERID.Value).ToList();
//不存在的,增加
foreach (var item in input.Where(x => dbContacts.Count(cc => cc.Name == x.Name) == 0))
{
var ct = item.Adapt();
ct.CustomerId = bookObj.CUSTOMERID.Value;
await _repContact.InsertAsync(ct);
}
}
}
/////
///// 获取订舱关系人列表
/////
/////
/////
//[HttpGet("/BookingOrderContact/list")]
//public async Task List([FromQuery] QueryBookingOrderContactInput input)
//{
// return await _rep.ToListAsync();
//}
}
}