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.

162 lines
6.2 KiB
C#

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
{
/// <summary>
/// 订舱关系人服务
/// </summary>
[ApiDescriptionSettings("Application", Name = "BookingOrderContact", Order = 1)]
public class BookingOrderContactService : IBookingOrderContactService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<BookingOrderContact> _rep;
private readonly SqlSugarRepository<BookingOrder> _repOrder;
private readonly SqlSugarRepository<DjyCustomerContact> _repContact;
private readonly ILogger<BookingOrderContact> _logger;
public BookingOrderContactService(SqlSugarRepository<BookingOrderContact> rep,
SqlSugarRepository<BookingOrder> repOrder,
SqlSugarRepository<DjyCustomerContact> repContact,
ILogger<BookingOrderContact> logger)
{
_rep = rep;
_repOrder = repOrder;
_repContact = repContact;
_logger = logger;
}
/// <summary>
/// 分页查询订舱关系人
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/BookingOrderContact/page")]
public async Task<dynamic> Page([FromQuery] QueryBookingOrderContactInput input)
{
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();
}
/// <summary>
/// 增加订舱关系人
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/BookingOrderContact/add")]
public async Task Add(AddBookingOrderContactInput input)
{
var entity = input.Adapt<BookingOrderContact>();
await _rep.InsertAsync(entity);
}
/// <summary>
/// 更新订舱关系人
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/BookingOrderContact/edit")]
public async Task Update(UpdateBookingOrderContactInput input)
{
var entity = input.Adapt<BookingOrderContact>();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
/// <summary>
/// 删除订舱关系人
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/BookingOrderContact/delete")]
public async Task Delete(GetBookingOrderContactInput input)
{
var entity = await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
await _rep.DeleteAsync(entity);
}
/// <summary>
/// 获取订舱关系人
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/BookingOrderContact/detail")]
public async Task<BookingOrderContact> Get([FromQuery] GetBookingOrderContactInput input)
{
return await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
}
/// <summary>
/// 根据订舱ID获取关系人
/// </summary>
/// <param name="bookingId"></param>
/// <returns></returns>
[HttpGet("/BookingOrderContact/listbyorder")]
public async Task<List<BookingOrderContactOutput>> ListByOrder(long bookingId)
{
var list = await _rep.Where(x => x.BookingId == bookingId).ToListAsync();
return list.Adapt<List<BookingOrderContactOutput>>();
}
/// <summary>
/// 批量保存关系人(同时新增、删除、修改)
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/BookingOrderContact/savebatch"), SqlSugarUnitOfWork]
public async Task SaveBatch(List<UpdateBookingOrderContactInput> input, long bookingId)
{
_rep.Delete(x => x.BookingId == bookingId);
if (input != null && input.Count > 0)
{
foreach (var item in input)
{
await _rep.InsertAsync(item.Adapt<BookingOrderContact>());
}
}
//自动增加到该客户的联系人
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<DjyCustomerContact>();
ct.CustomerId = bookObj.CUSTOMERID.Value;
await _repContact.InsertAsync(ct);
}
}
}
///// <summary>
///// 获取订舱关系人列表
///// </summary>
///// <param name="input"></param>
///// <returns></returns>
//[HttpGet("/BookingOrderContact/list")]
//public async Task<dynamic> List([FromQuery] QueryBookingOrderContactInput input)
//{
// return await _rep.ToListAsync();
//}
}
}