ComplianceServer/code/Hg.Internal.Domain/CustomerDomain.cs

144 lines
6.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Hg.Core.EntityFramework;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Channels;
using System.Threading.Tasks;
namespace Hg.Internal.Domain
{
internal class CustomerDomain : ICustomerDomain
{
private readonly IBaseRepository<HgCrmDbContext> _hgCrmRepository;
private readonly IBaseRepository<HgCrmInternalDbContext> _hgCrmInternalDbContext;
private readonly IServiceProvider _serviceProvider;
private readonly IHttpClient _httpClient;
private readonly IMapper _mapper;
private readonly IConfiguration _configuration;
private readonly IRedisManager _redisManager;
private readonly SystemConfig _systemConfig;
private readonly ICacheDomain _cacheDomain;
public CustomerDomain(IConfiguration configuration,
IServiceProvider serviceProvider,
IBaseRepository<HgCrmDbContext> hgCrmRepository,
IBaseRepository<HgCrmInternalDbContext> hgCrmInternalDbContext,
IHttpClient httpClient,
IMapper mapper,
IRedisManager redisManager,
ICacheDomain cacheDomain
)
{
_systemConfig = configuration.GetSection("SystemConfig").Get<SystemConfig>();
_serviceProvider = serviceProvider;
_configuration = configuration;
_hgCrmRepository = hgCrmRepository;
_hgCrmInternalDbContext = hgCrmInternalDbContext;
_httpClient = httpClient;
_mapper = mapper;
_redisManager = redisManager;
_cacheDomain = cacheDomain;
}
public async Task<List<CustomerMoblieDto>> GetCustomerMobliesByResid(string? resid)
{
var data = new List<CustomerMoblieDto>();
var param = new OracleParameter[] {
new OracleParameter() { ParameterName = "v_resid", OracleDbType = OracleDbType.Char, Value = resid },
new OracleParameter(){ ParameterName="v_tab", OracleDbType = OracleDbType.RefCursor, Direction = ParameterDirection.Output}
};
var result = await _hgCrmInternalDbContext.ExecuteSqlCommandAsync<CustomerMoblieDto>(CommandType.StoredProcedure, "PACK_CUSTOMER.GetCustomerByResid", param);
data = result.GroupBy(x => new
{
x.Mobile,
x.Resid,
x.CustomerId,
x.Lastnum3
}).Select(x =>
new CustomerMoblieDto
{
Mobile = x.Key.Mobile,
Resid = x.Key.Resid,
CustomerId = x.Key.CustomerId,
Lastnum3 = x.Key.Lastnum3,
IsPrimarynum = false,
CompanyName = string.Join(",", x.Select(y => y.CompanyName)),
CompanyCode = string.Join(",", x.Select(y => y.CompanyCode))
}).ToList();
return data;
}
public async Task<ComplaintCustomerInfoDto> GetCustomerDetailsByResid(string? resid)
{
var param = new OracleParameter[] {
new OracleParameter() { ParameterName = "v_resid", OracleDbType = OracleDbType.Char, Value = resid },
new OracleParameter(){ ParameterName="v_tab", OracleDbType = OracleDbType.RefCursor, Direction = ParameterDirection.Output}
};
var deptmentsIn = await _cacheDomain.GetDeptments(true);
Log.Information(deptmentsIn.ToJson());
var result = await _hgCrmInternalDbContext.ExecuteSqlCommandAsync<CustomerDetailsDto>(CommandType.StoredProcedure, "PACK_CUSTOMER.GetCustomerByResid", param);
// 过滤id为0的部门合规除外即DepartmentId=7的部门
var deptments = deptmentsIn.Where(x => x.Id != 0 || x.DepartmentId == 7).Select(l => new ComplaintCustomerInfoDto.ComplaintCustomerDeptInfo
{
appid = l.Appid,
deptid = l.Id,
companycode = l.Code,
deptname = l.Title
});
var companyGroup = result.GroupBy(x => new
{
x.resid,
x.CustomerId,
x.cname,
x.appid
});
var deptmentsOut = new List<ComplaintCustomerInfoDto.ComplaintCustomerDeptInfo>();
foreach (var dept in deptments)
{
if (companyGroup.Where(c => c.Key.appid == dept.appid).Any())
{
deptmentsOut.Add(dept);
}
else if (dept.deptid == 0)
{
// 过滤之后还有id为0的数据进来即为需要加上的数据例如上面提到 DepartmentId=7的合规部门
deptmentsOut.Add(dept);
};
}
var user = companyGroup.FirstOrDefault(u => u.Key.cname != "" && u.Key.cname != null);
if (user == null && companyGroup.Count() > 0) user = companyGroup.ElementAt(0);
var data = new ComplaintCustomerInfoDto { cname = user.Key.cname, resid = user.Key.resid, eid = null, deptInfos = deptmentsOut };
return data;
}
public async Task<List<string>> GetRelationCustomersByResid(string? resid)
{
var result = new List<string>();
var param = new OracleParameter[] {
new OracleParameter() { ParameterName = "v_resid", OracleDbType = OracleDbType.Char, Value = resid },
new OracleParameter(){ ParameterName="v_tab", OracleDbType = OracleDbType.RefCursor, Direction = ParameterDirection.Output}
};
var data = await _hgCrmInternalDbContext.ExecuteSqlCommandAsync<CustomerMoblieDto>(CommandType.StoredProcedure, "PACK_CUSTOMER.GetCustomerByResid", param);
if (data != null)
{
result = data.Select(x => x.Resid).Distinct().ToList();
}
return result;
}
}
}