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; using System; using System.Text.RegularExpressions; using NPOI.Util; using StackExchange.Profiling.Internal; namespace Myshipping.Core.Service { /// /// 客户服务 /// [ApiDescriptionSettings(Name = "DjyCustomer", Order = 1)] public class DjyCustomerService : IDjyCustomerService, IDynamicApiController, ITransient { private readonly SqlSugarRepository _rep; private readonly SqlSugarRepository _repContact; private readonly SqlSugarRepository _repAddr; private readonly ILogger _logger; public DjyCustomerService(SqlSugarRepository rep, SqlSugarRepository repContact, ILogger logger, SqlSugarRepository addrRepContact) { _rep = rep; _repContact = repContact; _logger = logger; _repAddr = addrRepContact; } /// /// 分页查询客户 /// /// /// [HttpGet("/DjyCustomer/page")] public async Task Page([FromQuery] QueryDjyCustomerInput input) { var entities = await _rep.AsQueryable().Filter(null, true) .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())) .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(); } /// /// 增加客户 /// /// /// [HttpPost("/DjyCustomer/add"), SqlSugarUnitOfWork] public async Task Add(AddDjyCustomerInput input) { //var find = await _rep.FirstOrDefaultAsync(u => u.CodeName == input.CodeName); //if (find != null) //{ // throw Oops.Oh($"助记码已存在:{input.CodeName}"); //} var entity = input.Adapt(); 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>(); contactList.ForEach(x => x.CustomerId = entity.Id); await _repContact.InsertAsync(contactList); //地址 var addrList = input.Addrs.Adapt>(); addrList.ForEach(x => x.CustomerId = entity.Id); await _repAddr.InsertAsync(addrList); return entity.Id; } /// /// 更新客户 /// /// /// [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); } //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(); 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(); await _repContact.DeleteAsync(x => x.CustomerId == input.Id); var contactList = input.Contacts.Adapt>(); contactList.ForEach(x => x.CustomerId = entity.Id); await _repContact.InsertAsync(contactList); //地址 await _repAddr.DeleteAsync(x => x.CustomerId == input.Id); var addrList = input.Addrs.Adapt>(); addrList.ForEach(x => x.CustomerId = entity.Id); await _repAddr.InsertAsync(addrList); return entity.Id; } /// /// 删除客户 /// /// /// [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 _repAddr.DeleteAsync(x => x.CustomerId == input.Id); await _rep.DeleteAsync(entity); } } /// /// 获取客户 /// /// /// [HttpGet("/DjyCustomer/detail")] public async Task 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(); custOut.Contacts = _repContact.AsQueryable().Filter(null, true).Where(x => x.CustomerId == input.Id).OrderBy(x => x.Sort).ToList().Adapt>(); custOut.Addrs = _repAddr.AsQueryable().Filter(null, true).Where(x => x.CustomerId == input.Id).ToList().Adapt>(); return custOut; } /// /// 根据客户id,获取所有联系人 /// /// /// [HttpGet("/DjyCustomer/contacts")] public async Task> ListContact(long customerId) { var list = await _repContact.Where(x => x.CustomerId == customerId).OrderBy(x => x.Sort).ToListAsync(); return list.Adapt>(); } ///// ///// 获取客户列表 ///// ///// ///// //[HttpGet("/DjyCustomer/list")] //public async Task List([FromQuery] QueryDjyCustomerInput input) //{ // return await _rep.ToListAsync(); //} /// /// 下拉筛选 /// /// 关键字 /// 类型,参考字典参数:客户属性(djy_cust_prop) /// [HttpGet("/DjyCustomer/Suggest")] public async Task Suggest(string keyword, string type) { //List userlist = await DataFilterExtensions.GetDataScopeIdList(); var entities = await _rep.AsQueryable().Filter(null, true) .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(); } /// /// 检索往来单位(包含车队) /// /// 检索值 /// 检索类型数组(可传多个) fleet-车队;customs_broker-报关行 /// 默认最大行数 /// 返回回执 [HttpGet("/DjyCustomer/QuerytDjyCustomerInfo")] public async Task> QuerytDjyCustomerInfo([FromQuery] string queryItem, [FromQuery] string[] queryType, [FromQuery] int top = 40) { List list = new List(); 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>(); 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>(); 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>(); } if (!string.IsNullOrWhiteSpace(l.Addr)) { if (l.Addrs == null) l.Addrs = new List(); 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> 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(x.code, x.name)).ToList(); } } }