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.Core/Service/DjyCustomer/DjyCustomerService.cs

174 lines
8.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 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.Core.Entity;
using System.Collections.Generic;
using Furion.FriendlyException;
using Furion.Logging;
using Microsoft.Extensions.Logging;
namespace Myshipping.Core.Service
{
/// <summary>
/// 客户服务
/// </summary>
[ApiDescriptionSettings(Name = "DjyCustomer", Order = 1)]
public class DjyCustomerService : IDjyCustomerService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<DjyCustomer> _rep;
private readonly SqlSugarRepository<DjyCustomerContact> _repContact;
private readonly ILogger<DjyCustomerService> _logger;
public DjyCustomerService(SqlSugarRepository<DjyCustomer> rep, SqlSugarRepository<DjyCustomerContact> repContact, ILogger<DjyCustomerService> logger)
{
_rep = rep;
_repContact = repContact;
_logger = logger;
}
/// <summary>
/// 分页查询客户
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/DjyCustomer/page")]
public async Task<dynamic> Page([FromQuery] QueryDjyCustomerInput input)
{
var entities = await _rep.AsQueryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.CodeName), u => u.CodeName.Contains(input.CodeName.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.ShortName), u => u.ShortName.Contains(input.ShortName.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.FullName), u => u.FullName.Contains(input.FullName.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Chief), u => u.Chief.Contains(input.Chief.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(!string.IsNullOrWhiteSpace(input.QQ), u => u.QQ.Contains(input.QQ.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.WebUrl), u => u.WebUrl.Contains(input.WebUrl.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Province), u => u.Province.Contains(input.Province.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.City), u => u.City.Contains(input.City.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Addr), u => u.Addr.Contains(input.Addr.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.FullNameEN), u => u.FullNameEN.Contains(input.FullNameEN.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.AddrEN), u => u.AddrEN.Contains(input.AddrEN.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.PropString), u => u.PropString.Contains(input.PropString.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.InvTitle), u => u.InvTitle.Contains(input.InvTitle.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.TaxNO), u => u.TaxNO.Contains(input.TaxNO.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.InvAddrTel), u => u.InvAddrTel.Contains(input.InvAddrTel.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.RMBBank), u => u.RMBBank.Contains(input.RMBBank.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.RmbAccount), u => u.RmbAccount.Contains(input.RmbAccount.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.USDBank), u => u.USDBank.Contains(input.USDBank.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.USDAccount), u => u.USDAccount.Contains(input.USDAccount.Trim()))
.ToPagedListAsync(input.PageNo, input.PageSize);
return entities.XnPagedResult();
}
/// <summary>
/// 增加客户
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/DjyCustomer/add"), SqlSugarUnitOfWork]
public async Task Add(AddDjyCustomerInput input)
{
var entity = input.Adapt<DjyCustomer>();
await _rep.InsertAsync(entity);
var contactList = input.Contacts.Adapt<List<DjyCustomerContact>>();
contactList.ForEach(x => x.CustomerId = entity.Id);
await _repContact.InsertAsync(contactList);
}
/// <summary>
/// 更新客户
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/DjyCustomer/edit"), SqlSugarUnitOfWork]
public async Task Update(UpdateDjyCustomerInput input)
{
var find = await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
if (find == null)
{
throw Oops.Oh(ErrorCode.D1002);
}
var entity = input.Adapt<DjyCustomer>();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
await _repContact.DeleteAsync(x => x.CustomerId == input.Id);
var contactList = input.Contacts.Adapt<List<DjyCustomerContact>>();
contactList.ForEach(x => x.CustomerId = entity.Id);
await _repContact.InsertAsync(contactList);
}
/// <summary>
/// 删除客户
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/DjyCustomer/delete"), SqlSugarUnitOfWork]
public async Task Delete(GetDjyCustomerInput input)
{
var entity = await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
if (entity == null)
{
throw Oops.Oh(ErrorCode.D1002);
}
else
{
await _repContact.DeleteAsync(x => x.CustomerId == input.Id);
await _rep.DeleteAsync(entity);
}
}
/// <summary>
/// 获取客户
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/DjyCustomer/detail")]
public async Task<DjyCustomerOutput> Detail([FromQuery] GetDjyCustomerInput input)
{
Log.Information($"查询客户详情:{input.Id}");
_logger.LogInformation($"查询客户详情:{input.Id}");
var cust = await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
if (cust == null)
{
throw Oops.Oh(ErrorCode.D1002);
}
var custOut = cust.Adapt<DjyCustomerOutput>();
custOut.Contacts = _repContact.Where(x => x.CustomerId == input.Id).OrderBy(x => x.Sort).ToList().Adapt<List<DjyCustomerContactOutput>>();
return custOut;
}
/// <summary>
/// 根据客户id获取所有联系人
/// </summary>
/// <param name="customerId"></param>
/// <returns></returns>
[HttpGet("/DjyCustomer/contacts")]
public async Task<List<DjyCustomerContactOutput>> ListContact(long customerId)
{
var list = await _repContact.Where(x => x.CustomerId == customerId).OrderBy(x => x.Sort).ToListAsync();
return list.Adapt<List<DjyCustomerContactOutput>>();
}
///// <summary>
///// 获取客户列表
///// </summary>
///// <param name="input"></param>
///// <returns></returns>
//[HttpGet("/DjyCustomer/list")]
//public async Task<dynamic> List([FromQuery] QueryDjyCustomerInput input)
//{
// return await _rep.ToListAsync();
//}
}
}