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

549 lines
24 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.Core.Entity;
2 years ago
using System.Collections.Generic;
using Furion.FriendlyException;
using Furion.Logging;
using Microsoft.Extensions.Logging;
using System;
using System.Text.RegularExpressions;
using NPOI.Util;
using StackExchange.Profiling.Internal;
using Microsoft.AspNetCore.Authorization;
7 months ago
using static NPOI.HSSF.Util.HSSFColor;
7 months ago
using TinyPinyin;
using Myshipping.Core.Const;
2 years ago
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 SqlSugarRepository<DjyCustomerAddr> _repAddr;
private readonly ISysCacheService _cache;
private readonly ISysDataUserMenu _sysDataUserMenuService;
private readonly ILogger<DjyCustomerService> _logger;
public DjyCustomerService(SqlSugarRepository<DjyCustomer> rep,
SqlSugarRepository<DjyCustomerContact> repContact,
ILogger<DjyCustomerService> logger,
SqlSugarRepository<DjyCustomerAddr> addrRepContact,
ISysCacheService cache,
ISysDataUserMenu sysDataUserMenuService)
{
_rep = rep;
2 years ago
_repContact = repContact;
_logger = logger;
_repAddr = addrRepContact;
_cache = cache;
_sysDataUserMenuService = sysDataUserMenuService;
}
/// <summary>
/// 分页查询客户
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/DjyCustomer/page")]
public async Task<dynamic> Page([FromQuery] QueryDjyCustomerInput input)
{
var queryable = _rep.AsQueryable().Filter(null, true);
// 判断是否启用了委托单位查看控制权限
var tenantParamList = await _cache.GetAllTenantParam();
var isEnableCustomerAuthority = tenantParamList.FirstOrDefault(x => x.TenantId == UserManager.TENANT_ID
&& x.ParaCode == TenantParamCode.IS_ENABLE_CUSTOMER_AUTHORITY)?.ItemCode == "YES";
if (isEnableCustomerAuthority)
{
List<long> userList = await _sysDataUserMenuService.GetDataScopeList(MenuConst.MenuDjyCustomer);
if (userList?.Any() == true)
{
List<string> userListStr = userList.Select(x => x.ToString()).ToList();
queryable.Where(u => userList.Contains((long)u.CreatedUserId)
|| userListStr.Contains(u.OPID)
|| userListStr.Contains(u.SALEID)
|| userListStr.Contains(u.DOCID)
|| userListStr.Contains(u.CUSTSERVICEID));
}
}
var entities = await queryable
.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()))
2 years ago
.WhereIF(!string.IsNullOrWhiteSpace(input.ServiceItem), u => u.ServiceItem.Contains(input.ServiceItem.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.SALE), u => u.SALE.Contains(input.SALE.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.SearchValue), u => u.ShortName.Contains(input.SearchValue.Trim()) || u.CodeName.Contains(input.SearchValue.Trim()))
.Where(u => u.TenantId == UserManager.TENANT_ID) //权限范围目前不支持根据模块设置,客户模块暂时写死本公司数据
.ToPagedListAsync(input.PageNo, input.PageSize);
return entities.XnPagedResult();
}
/// <summary>
/// 增加客户
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/DjyCustomer/add"), SqlSugarUnitOfWork]
public async Task<long> Add(AddDjyCustomerInput input)
{
1 year ago
//var find = await _rep.FirstOrDefaultAsync(u => u.CodeName == input.CodeName);
//if (find != null)
//{
// throw Oops.Oh($"助记码已存在:{input.CodeName}");
//}
var entity = input.Adapt<DjyCustomer>();
1 year ago
if (entity.Level == "R3" && !string.IsNullOrEmpty(entity.CUSTSERVICE))
{
throw Oops.Bah("等级为R3客服为空");
}
if ((entity.Level == "R1" || entity.Level == "R2") && string.IsNullOrEmpty(entity.CUSTSERVICE))
{
throw Oops.Bah("等级为R1/R2客服必填");
}
await _rep.InsertAsync(entity);
var contactList = input.Contacts.Adapt<List<DjyCustomerContact>>();
2 years ago
contactList.ForEach(x => x.CustomerId = entity.Id);
await _repContact.InsertAsync(contactList);
//地址
var addrList = input.Addrs.Adapt<List<DjyCustomerAddr>>();
addrList.ForEach(x => x.CustomerId = entity.Id);
await _repAddr.InsertAsync(addrList);
return entity.Id;
}
/// <summary>
/// 更新客户
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/DjyCustomer/edit"), SqlSugarUnitOfWork]
public async Task<long> Update(UpdateDjyCustomerInput input)
{
2 years ago
var find = await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
if (find == null)
{
throw Oops.Oh(ErrorCode.D1002);
}
1 year ago
//find = await _rep.FirstOrDefaultAsync(u => u.CodeName == input.CodeName && u.Id != input.Id);
//if (find != null)
//{
// throw Oops.Oh($"助记码已存在:{input.CodeName}");
//}
var entity = input.Adapt<DjyCustomer>();
1 year ago
if (entity.Level == "R3" && !string.IsNullOrEmpty(entity.CUSTSERVICE))
{
throw Oops.Bah("等级为R3客服为空");
}
if ((entity.Level == "R1" || entity.Level == "R2") && string.IsNullOrEmpty(entity.CUSTSERVICE))
{
throw Oops.Bah("等级为R1/R2客服必填");
}
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
8 months ago
//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);
var dbContact = await _repContact.AsQueryable().Where(x => x.CustomerId == input.Id).ToListAsync();
foreach (var dtoUser in input.Contacts)
{
if (dtoUser.Id > 0)
{
var contact = dbContact.First(x => x.Id == dtoUser.Id);
dtoUser.Adapt(contact);
contact.CustomerId = entity.Id;
await _repContact.UpdateAsync(contact);
dbContact.Remove(contact);
}
else
{
var contact = dtoUser.Adapt<DjyCustomerContact>();
contact.CustomerId = entity.Id;
await _repContact.InsertAsync(contact);
}
}
foreach (var contact in dbContact)
{
await _repContact.DeleteAsync(contact);
}
//地址
await _repAddr.DeleteAsync(x => x.CustomerId == input.Id);
var addrList = input.Addrs.Adapt<List<DjyCustomerAddr>>();
addrList.ForEach(x => x.CustomerId = entity.Id);
await _repAddr.InsertAsync(addrList);
return entity.Id;
}
/// <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);
2 years ago
if (entity == null)
{
throw Oops.Oh(ErrorCode.D1002);
}
else
{
await _repContact.DeleteAsync(x => x.CustomerId == input.Id);
await _repAddr.DeleteAsync(x => x.CustomerId == input.Id);
2 years ago
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.AsQueryable().Filter(null, true).FirstAsync(u => u.Id == input.Id);
if (cust == null)
{
throw Oops.Oh(ErrorCode.D1002);
}
var custOut = cust.Adapt<DjyCustomerOutput>();
custOut.Contacts = _repContact.AsQueryable().Filter(null, true).Where(x => x.CustomerId == input.Id).OrderBy(x => x.Sort).ToList().Adapt<List<DjyCustomerContactOutput>>();
custOut.Addrs = _repAddr.AsQueryable().Filter(null, true).Where(x => x.CustomerId == input.Id).ToList().Adapt<List<DjyCustomerAddrInput>>();
return custOut;
}
2 years ago
/// <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();
//}
2 years ago
/// <summary>
/// 下拉筛选
/// </summary>
/// <param name="keyword">关键字</param>
/// <param name="type">类型参考字典参数客户属性djy_cust_prop</param>
/// <returns></returns>
[HttpGet("/DjyCustomer/Suggest")]
public async Task<dynamic> Suggest(string keyword, string type)
{
var queryable = _rep.AsQueryable().Filter(null, true);
// 判断是否启用了委托单位查看控制权限
var tenantParamList = await _cache.GetAllTenantParam();
var isEnableCustomerAuthority = tenantParamList.FirstOrDefault(x => x.TenantId == UserManager.TENANT_ID
&& x.ParaCode == TenantParamCode.IS_ENABLE_CUSTOMER_AUTHORITY)?.ItemCode == "YES";
if (isEnableCustomerAuthority)
{
List<long> userList = await _sysDataUserMenuService.GetDataScopeList(MenuConst.MenuDjyCustomer);
if (userList?.Any() == true)
{
List<string> userListStr = userList.Select(x => x.ToString()).ToList();
queryable.Where(u => userList.Contains((long)u.CreatedUserId)
|| userListStr.Contains(u.OPID)
|| userListStr.Contains(u.SALEID)
|| userListStr.Contains(u.DOCID)
|| userListStr.Contains(u.CUSTSERVICEID));
}
}
var entities = await queryable
.Where(x => x.TenantId == UserManager.TENANT_ID)
.WhereIF(!string.IsNullOrWhiteSpace(type), u => u.PropString.Contains(type))
.WhereIF(!string.IsNullOrWhiteSpace(keyword), u => u.CodeName.Contains(keyword) || u.ShortName.Contains(keyword))
.ToPagedListAsync(1, 10);
return entities.XnPagedResult();
}
2 years ago
/// <summary>
/// 检索往来单位(包含车队)
/// </summary>
/// <param name="queryItem">检索值</param>
/// <param name="queryType">检索类型数组(可传多个) fleet-车队customs_broker-报关行</param>
/// <param name="top">默认最大行数</param>
/// <returns>返回回执</returns>
[HttpGet("/DjyCustomer/QuerytDjyCustomerInfo")]
public async Task<List<DjyCustomerOutput>> QuerytDjyCustomerInfo([FromQuery] string queryItem, [FromQuery] string[] queryType, [FromQuery] int top = 40)
{
List<DjyCustomerOutput> list = new List<DjyCustomerOutput>();
try
{
var entityList = await _rep.AsQueryable()
.WhereIF(!string.IsNullOrWhiteSpace(queryItem), x =>
x.CodeName.Contains(queryItem) || x.ShortName.Contains(queryItem) || x.FullName.Contains(queryItem))
.WhereIF(queryType.Length > 0, x =>
x.PropString.Contains(queryType.FirstOrDefault()))
.Take(top).OrderBy(a => a.FullName)
.ToListAsync();
if (entityList.Count > 0)
{
list = entityList.Adapt<List<DjyCustomerOutput>>();
var custsArg = list.Select(a => a.Id).ToArray();
var contactList = _repContact.AsQueryable()
.Where(a => custsArg.Contains(a.CustomerId.Value)).ToList();
var addrList = _repAddr.AsQueryable()
.Where(a => custsArg.Contains(a.CustomerId)).ToList();
list = list.GroupJoin(contactList, l => l.Id, r => r.CustomerId.Value, (l, r) =>
{
var curList = r.ToList();
if (curList.Count > 0)
l.Contacts = curList.Adapt<List<DjyCustomerContactOutput>>();
return l;
}).ToList();
if (addrList.Count > 0)
{
list = list.GroupJoin(addrList, l => l.Id, r => r.CustomerId, (l, r) =>
{
var curList = r.ToList();
if (curList.Count > 0)
{
l.Addrs = curList.Adapt<List<DjyCustomerAddrInput>>();
}
if (!string.IsNullOrWhiteSpace(l.Addr))
{
if (l.Addrs == null)
l.Addrs = new List<DjyCustomerAddrInput>();
l.Addrs.Add(new DjyCustomerAddrInput
{
CustomerId = l.Id,
Addr = l.Addr,
});
}
return l;
}).ToList();
}
}
}
catch (Exception ex)
{
_logger.LogError("检索车队异常请求queryItem={0}queryType={1}top={2} 异常:{3}", queryItem, queryType, top, ex.Message);
throw Oops.Bah("检索车队异常,异常{msg}", ex.Message);
}
return list;
}
public async Task<List<(string codeName, string shortName)>> QueryDjyCustomerByProp(string propStringCode)
{
string propStringStr = string.Concat('[', propStringCode, ']');
var entityList = await _rep.AsQueryable()
.Where(x => x.PropString.Contains(propStringStr))
.Select(x => new { code = x.CodeName, name = x.ShortName })
.ToListAsync();
return entityList.Select(x => new ValueTuple<string, string>(x.code, x.name)).ToList();
}
/// <summary>
/// 批量更新客户信息
/// </summary>
[HttpPost("/DjyCustomer/BatchUpdate")]
public async Task BatchUpdate(BatchUpdateInput input)
{
if (input.CustomnerIdList == null || input.CustomnerIdList.Count == 0)
{
throw Oops.Oh("客户列表为空");
}
var updateable = _rep.Context.Updateable<DjyCustomer>();
if (input.IsSetCUSTSERVICE)
{
updateable.SetColumns(x => x.CUSTSERVICE == input.CUSTSERVICE);
updateable.SetColumns(x => x.CUSTSERVICEID == input.CUSTSERVICEID);
}
if (input.IsSetSALE)
{
updateable.SetColumns(x => x.SALE == input.SALE);
updateable.SetColumns(x => x.SALEID == input.SALEID);
}
if (input.IsSetOP)
{
updateable.SetColumns(x => x.OP == input.OP);
updateable.SetColumns(x => x.OPID == input.OPID);
}
if (input.IsSetDOC)
{
updateable.SetColumns(x => x.DOC == input.DOC);
updateable.SetColumns(x => x.DOCID == input.DOCID);
}
if (input.IsSetBUSINESS)
{
updateable.SetColumns(x => x.BUSINESS == input.BUSINESS);
updateable.SetColumns(x => x.BUSINESSID == input.BUSINESSID);
}
if (input.IsSetCUSTSERVICE || input.IsSetSALE || input.IsSetOP || input.IsSetDOC || input.IsSetBUSINESS)
{
await updateable.Where(x => input.CustomnerIdList.Contains(x.Id)).ExecuteCommandAsync();
}
}
#region 接收客户端租户、用户的数据到运营端委托单位
/// <summary>
/// 接收客户端租户、用户的数据到运营端委托单位
/// </summary>
/// <returns></returns>
[HttpPost("/DjyCustomer/RecCustomerUserSync"), AllowAnonymous, ApiUser(ApiCode = "DjyBookingReceiveApi")]
public async Task RecCustomerUserSync(DjyCustomerFromCustomerDto dto)
{
var cust = await _rep.AsQueryable().Filter(null, true).FirstAsync(x => x.CustSysId == dto.Id);
if (cust == null)
{
var shortName = dto.Name;
var idx = shortName.IndexOf("有限公司");
if (idx > -1)
{
shortName = shortName.Substring(0, idx);
}
cust = new DjyCustomer()
{
CustSysId = dto.Id,
FullName = dto.Name,
Email = dto.Email,
Tel = dto.Phone,
7 months ago
CodeName = PinyinHelper.GetPinyinInitials(shortName),
ShortName = shortName,
DjyGid = dto.DjyGid,
PropString = "[consignor]"
};
await _rep.InsertAsync(cust);
}
var dbContactList = await _repContact.AsQueryable().Filter(null, true).Where(x => x.CustomerId == cust.Id).ToListAsync();
foreach (var usr in dto.UserList)
{
7 months ago
DjyCustomerContact contact = dbContactList.FirstOrDefault(x => x.CustSysId == usr.Id);
//不存在,新增
7 months ago
if (contact == null)
{
7 months ago
contact = new DjyCustomerContact()
{
CustomerId = cust.Id,
Name = usr.Name,
Email = usr.Email,
Tel = usr.Phone,
CustSysId = usr.Id,
DjyGid = usr.DjyGid
};
await _repContact.InsertAsync(contact);
}
7 months ago
else
{
contact.CustomerId = cust.Id;
contact.Name = usr.Name;
contact.Email = usr.Email;
contact.Tel = usr.Phone;
contact.CustSysId = usr.Id;
contact.DjyGid = usr.DjyGid;
await _repContact.UpdateAsync(contact);
dbContactList.Remove(contact);
}
}
//删除没有的用户
foreach (var contact in dbContactList)
{
await _repContact.DeleteAsync(contact);
}
}
#endregion
}
}