crm.core/code/Crm.Core.Domain/CustomerDomain.cs

215 lines
9.7 KiB
C#

using Crm.Core.Domain.Dto.CRM;
using Crm.Core.Domain.Dto.Customer;
using Crm.Core.Domain.Impl;
using Crm.Core.Entity.Crm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Xml.Linq;
namespace Crm.Core.Domain
{
public class CustomerDomain : ICustomerDomain
{
private readonly ICacheDomain _cacheDomain;
private readonly IHttpClient _httpClient;
private readonly IConfiguration _configuration;
private readonly IOracleRepository<CrmDbContext> _repository;
public CustomerDomain(ICacheDomain cacheDomain,
IHttpClient httpClient,
IConfiguration configuration,
IOracleRepository<CrmDbContext> repository)
{
_cacheDomain = cacheDomain;
_httpClient = httpClient;
_configuration = configuration;
_repository = repository;
}
/// <summary>
/// 获取销售线索列表
/// </summary>
/// <param name="resId"></param>
/// <returns></returns>
public async Task<List<SalesLeadDto>> GetSalesLeadList(string encyptDto)
{
var systemConfig = _configuration.GetSection("SystemConfig").Get<SystemConfig>();
var url = systemConfig.SalesLeadUrl;
var t = SecurityHelper.DecyptData(encyptDto, "dwj5rc8x");
saledsleadDesDto param = JsonHelper.FromJson<saledsleadDesDto>(t);
var resId = param.resid;
var content = "";
var jsonDto = "";
if (resId == null || resId == "")
{
jsonDto = JsonHelper.ToJson(new saledsleadDesDto { appid = param.appid, appuserid = param.appuserid, deptid = param.deptid, eid = param.eid, ext_flag = param.ext_flag });
content = HttpUtility.UrlEncode(SecurityHelper.EncyptData(jsonDto, "dwj5rc8x"));
return new List<SalesLeadDto> { new SalesLeadDto { Name="默认",Url = $"{url}?content={content}&clientid=WK_EXT" } };
}
var result = new List<SalesLeadDto>();
var key = systemConfig.GetAccessKey(systemConfig.CRMClientKey);
var userlist = await _repository.GetRepository<WW_EXTUSER_RESID>().QueryIncluding(x => x.WW_EXTUSER)
.Where(x => x.RESID == resId).ToListAsync();
foreach (var user in userlist)
{
if (user.WW_EXTUSER == null) continue;
var exinfo = JsonHelper.FromJson<ExternalUserDto>(user.WW_EXTUSER.EXINFO);
if (exinfo.External_contact == null) continue;
var contact = exinfo.External_contact;
var appid = user.WW_EXTUSER.CORPID != "com.dongniu" ? user.WW_EXTUSER.CORPID : user.WW_EXTUSER.CORPID;
jsonDto = "";
content = "";
jsonDto = JsonHelper.ToJson(new saledsleadDesDto { appid = appid, appuserid = contact.ExternalUserId, deptid = param.deptid, eid = param.eid, ext_flag = param.ext_flag });
content = HttpUtility.UrlEncode(SecurityHelper.EncyptData(jsonDto, "dwj5rc8x"));
result.Add(new SalesLeadDto
{
Name = contact.Name,
Appid = appid,
Appuserid = contact.ExternalUserId,
Url = $"{url}?content={content}&clientid=WK_EXT"
});
}
var customerId = await _repository.GetRepository<RES_CUSTOMER>().Query()
.Where(x => x.RESID == resId).Select(x => x.RESID).FirstOrDefaultAsync();
var resIds = await _repository.GetRepository<RES_CUSTOMER>().Query()
.Where(x => x.CUSTOMERID == customerId).Select(x => x.RESID).ToListAsync();
var customerUserList = await _repository.GetRepository<RES_CUSTOMERUSER>().Query()
.Where(x => resIds.Contains(x.RESID)).ToListAsync();
foreach (var customerUser in customerUserList)
{
var name = PhoneHelper.FormatPhoneUserName(customerUser.USERNAME);
jsonDto = "";
content = "";
jsonDto = JsonHelper.ToJson(new saledsleadDesDto { appid = "com.dongniu", appuserid = name, deptid = param.deptid, eid = param.eid, ext_flag = param.ext_flag });
content = HttpUtility.UrlEncode(SecurityHelper.EncyptData(jsonDto, "dwj5rc8x"));
result.Add(new SalesLeadDto
{
Name = name,
Url = $"{url}?content={content}&clientid=WK_EXT"
});
}
var parameter = await _cacheDomain.GetValueParameter("ISVR_Saleclus_Get");
if (string.IsNullOrEmpty(parameter)) return result;
var mobiles = await _repository.GetRepository<RES_RESOURCEMOBILE>().Query()
.Where(x => x.RESID == resId).Select(x => SecurityHelper.DecyptData(x.MOBILE, key)).ToListAsync();
if (mobiles == null || !mobiles.Any())
{
throw new ApiException("暂无销售线索!", -1);
}
var response = await _httpClient.PostAsync<SaleClusResult<SaleRelation>>(parameter, new { mobiles });
if (response.Ret != 0 || response.List == null)
{
return result;
}
foreach (var saleRelation in response.List)
{
if (result.Any(x => x.Appuserid == saleRelation.Appuserid && x.Appid == saleRelation.Appid))
{
continue;
}
var name = saleRelation.Appuserid.Length > 10 ?
saleRelation.Appuserid[..10] + "*****" :
saleRelation.Appuserid;
jsonDto = "";
content = "";
jsonDto = JsonHelper.ToJson(new saledsleadDesDto { appid = saleRelation.Appid, appuserid = saleRelation.Appuserid, deptid = param.deptid, eid = param.eid, ext_flag = param.ext_flag });
content = HttpUtility.UrlEncode(SecurityHelper.EncyptData(jsonDto, "dwj5rc8x"));
result.Add(new SalesLeadDto
{
Name = name,
Url = $"{url}?content={content}&clientid=WK_EXT"
});
}
return result;
}
public async Task<bool> BestCustomerCreate(BestCustomerDto model)
{
var result = true;
//resid为空 则根据 外部联系人id查找resid
if (string.IsNullOrWhiteSpace(model.ResId))
{
var userlist = await _repository.GetRepository<WW_EXTUSER_RESID>().Query().Where(x => x.USERID == model.OutUserId).ToListAsync();
model.ResId = string.Join(",", userlist.Select(n=>n.RESID));
}
if (string.IsNullOrWhiteSpace(model.ResId))
{
throw new Exception("该客户未关联客户id !");
}
try
{
var resids = model.ResId.Split(",").Distinct().ToList();
BAS_INNERUSER user = new BAS_INNERUSER();
if (model.Eid.HasValue)
{
user = await _repository.GetRepository<BAS_INNERUSER>().FirstOrDefaultAsync(n => n.EID == model.Eid);
}
var bestRepository = _repository.GetRepository<RES_CUSTOMER_BEST>();
var dbBestList = bestRepository.Query().Where(n => resids.Contains(n.RESID)).ToList();
foreach (var item in resids)
{
var dbModel = dbBestList.FirstOrDefault(n => n.RESID == item);
if (dbModel == null)
{
RES_CUSTOMER_BEST newModel = new RES_CUSTOMER_BEST
{
RESID = item,
REMARK = model.Remark,
CTIME = DateTime.Now,
CORPID = model.CropId,
OUTUSERID = model.OutUserId,
USERID = model.UserId,
CREATEID = user?.PKID,
CUSERNAME = user?.UNAME
};
await bestRepository.InsertAsync(newModel);
}
else
{
if (dbModel.CREATEID != user?.PKID)
{
throw new Exception($"该客户已被标记 !");
}
dbModel.UTIME = DateTime.Now;
dbModel.REMARK = model.Remark;
dbModel.CREATEID = user?.PKID;
dbModel.CUSERNAME = user?.UNAME;
await bestRepository.UpdateAsync(dbModel);
}
}
}
catch(Exception ex)
{
Log.Error($"插入优质客户失败{ex.Message}");
throw new Exception($"{ex.Message}");
}
return result;
}
public async Task<List<WwHhuserEidDto>> GetWwHhuserEids(List<decimal> eids, int deptid)
{
if (await _cacheDomain.SetApp(_repository, deptid))
{
var data = await _repository.GetRepository<WW_HHUSER_EID>().Query()
.Where(x => eids.Contains(x.EID.Value))
.Select(x => new WwHhuserEidDto
{
CORPID = x.CORPID,
EID = x.EID,
USERID = x.USERID,
}).ToListAsync();
return data;
}
return new List<WwHhuserEidDto>();
}
}
}