Zxd.Core/code/Zxd.Core.Domain/CustomerDomain.cs

338 lines
14 KiB
C#
Raw 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 MySqlConnector;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zxd.Core.Domain.Dto.Wework;
using Zxd.EntityFramework;
using static Zxd.Domain.Impl.ICustomerDomain;
namespace Zxd.Domain
{
public class CustomerDomain : ICustomerDomain
{
private readonly IConfiguration _configuration;
private readonly IHttpClient _httpClient;
private readonly ICacheDomain _cacheDomain;
private readonly IBaseRepository<ZxdDbContext> _repository;
private readonly IBaseRepository<DncmsDbContext> _dncmsbaseRepository;
public CustomerDomain(IConfiguration configuration,
IHttpClient httpClient,
ICacheDomain cacheDomain,
IBaseRepository<DncmsDbContext> dncmsbaseRepository,
IBaseRepository<ZxdDbContext> repository)
{
_configuration = configuration;
_httpClient = httpClient;
_cacheDomain = cacheDomain;
_repository = repository;
_dncmsbaseRepository = dncmsbaseRepository;
}
/// <summary>
/// 添加标签数据
/// </summary>
/// <param name="resid"></param>
/// <param name="tag"></param>
/// <returns></returns>
/// <exception cref="ApiException"></exception>
public async Task<bool> AddTag(string resid, string tag,int ceid)
{
tag = tag.Trim();
resid = resid.Trim();
if (string.IsNullOrEmpty(tag))
throw new ApiException("参数错误");
if (string.IsNullOrEmpty(resid))
throw new ApiException("参数错误");
var info = await _repository.GetRepository<Res_Tag>().Query().Where(m => m.resid == resid && m.tag == tag && m.isdelete==0).FirstOrDefaultAsync();
if (info != null)
throw new ApiException("已存在此标签");
await _repository.GetRepository<Res_Tag>().InsertAsync(new Res_Tag()
{
resid = resid,
tag = tag,
ceid = ceid
});
return true;
}
public async Task<bool> DelTag(int id,int deleteeid)
{
var info = await _repository.GetRepository<Res_Tag>().Query().Where(m => m.id == id).FirstOrDefaultAsync();
if (info == null)
throw new ApiException("找不到这个标签");
info.deleteeid = deleteeid;
info.isdelete = 1;
await _repository.GetRepository<Res_Tag>().UpdateAsync(info);
return true;
}
/// <summary>
/// 获取标签列表
/// </summary>
/// <param name="resid"></param>
/// <returns></returns>
public async Task<List<Res_Tag>> GetTag(string resid)
{
resid = resid.Trim();
return await _repository.GetRepository<Res_Tag>().Query().Where(m => m.resid == resid && m.isdelete==0).ToListAsync();
}
public async Task<List<string>> GetUsernames(string? resid)
{
if (string.IsNullOrEmpty(resid)) return new List<string>();
var usernames = await _repository.GetRepository<RES_CUSTOMERUSER>().Query()
.Where(x => x.RESID == resid)
.Select(x => x.USERNAME)
.ToListAsync();
return usernames;
}
public async Task<List<userRet>> GetUsernamesByOrderid(string orderidListIn)
{
if (orderidListIn == null) return new List<userRet> { };
var orderidList = orderidListIn.Split(";").ToList();
if (orderidList.Count == 0) return new List<userRet>();
var usernamesList = await _repository.GetRepository<WX_SZZYORDER>().Query()
.Where(x => orderidList.Contains(x.ORDERID.ToString()))
.Select(x => new userRet { orderid = x.ORDERID.ToString(), softwarename = x.SOFTUSERNAME })
.ToListAsync();
var res = new List<userRet>();
orderidList.ForEach(orderid =>
{
var findOder = usernamesList.Find(u => u.orderid == orderid);
if (findOder != null)
{
res.Add(findOder);
}
});
return res;
}
public async Task<CreateCustomerRes> CreateCustomer(string MOBILE, string ResId, string CustomerFrom)
{
CreateCustomerRes res = new CreateCustomerRes();
var customer = await _repository.GetRepository<RES_CUSTOMER>().Query().FirstOrDefaultAsync(x => x.RESID == ResId);
if (customer == null)
{
//开始创建
var mobile = MOBILE.Replace("+86", "");
if (mobile.StartsWith("01") && mobile.Length == 12)
{
mobile = mobile.Substring(1);
}
var systemConfig = _configuration.GetSection("SystemConfig").Get<SystemConfig>();
var key = systemConfig.GetAccessKey(systemConfig.CRMClientKey);
var CNumber = SecurityHelper.EncyptData(mobile, key);
var param = new List<MySqlParameter>
{
new MySqlParameter() { ParameterName = "p_CNumber", DbType = DbType.String, Value =CNumber},
new MySqlParameter() { ParameterName = "p_ResId", DbType = DbType.String, Value = ResId },
new MySqlParameter() { ParameterName = "p_CustomerFrom", DbType = DbType.String, Value = CustomerFrom }
};
param.Add(!string.IsNullOrEmpty(mobile) ? new MySqlParameter() { ParameterName = "p_ECNumber", DbType = DbType.String, Value = NumberFormat(mobile) } : new MySqlParameter() { ParameterName = "p_ECNumber", DbType = DbType.String, Value = DBNull.Value });
var i = await _repository.ExecuteSqlCommandNonQueryAsync(System.Data.CommandType.StoredProcedure, "res_ResgisterCustomer", param.ToArray());
res.code = CCenum.;
}
else
{
res.code = CCenum.;
}
return res;
}
public async Task<List<WwUserFollowOutDto>> GetFollow(List<WwUserFollowInDto> queryList)
{
var extuseridList = queryList.Select(s => s.extuserid).ToList();
// 依据appid userid extuserid 找出表中对应的数据并根据subscribe判断是否关注
var temp = await _dncmsbaseRepository.GetRepository<WeworkExternalUser>().Query()
.Where(w => extuseridList.Contains(w.Externaluserid))
.Select(x => new WwUserFollowOutDto()
{
appid = x.Appid,
userid = x.Userid,
extuserid = x.Externaluserid,
isFollow = x.subscribe
})
.ToListAsync();
List<WwUserFollowOutDto> res = new List<WwUserFollowOutDto>();
queryList.ForEach(q =>
{
var t = temp.Find(f => f.appid == q.appid && f.userid == q.userid && f.extuserid == q.extuserid);
if (t != null)
{
res.Add(new WwUserFollowOutDto
{
appid = t.appid,
userid = t.userid,
extuserid = t.extuserid,
isFollow = t.isFollow
});
}
});
return res;
}
public async Task<List<SalesLeadDto>> GetSalesLeadList(string? resId)
{
var systemConfig = _configuration.GetSection("SystemConfig").Get<SystemConfig>();
var result = new List<SalesLeadDto>();
var url = systemConfig.SalesLeadUrl;
var key = systemConfig.GetAccessKey(systemConfig.CRMClientKey);
var reslist = await _repository.GetRepository<RES_RESOURCEMOBILE>().Query()
.Where(x => x.RESID == resId).ToListAsync();
var mobiles = new List<string>();
foreach (var res in reslist)
{
if (res == null) continue;
mobiles.Add(SecurityHelper.DecyptData(res.MOBILE, key));
}
if (mobiles == null || !mobiles.Any())
{
throw new ApiException("暂无销售线索!", -1);
}
var parameter = await _cacheDomain.GetValueParameter("ISVR_Saleclus_Get");
if (string.IsNullOrEmpty(parameter)) return result;
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;
result.Add(new SalesLeadDto
{
Name = name,
Appid = saleRelation.Appid,
Appuserid = saleRelation.Appuserid,
Url = $"{url}?appid={saleRelation.Appid}&appuserid={saleRelation.Appuserid}"
});
}
return result;
}
public string NumberFormat(string number)
{
if (string.IsNullOrEmpty(number) || number.Length <= 6)
{
return number;
}
return number.Substring(0, 3) + new string('*', number.Length - 6) + number.Substring(number.Length - 3);
}
/// <summary>
///
/// </summary>
/// <param name="resId"></param>
/// <param name="DeptCode"></param>
/// <returns></returns>
public async Task<List<ExUserModel>> GetWorkWXUserSelectAsync(string? resId, string? DeptCode)
{
if (string.IsNullOrEmpty(resId))
{
return new List<ExUserModel>();
}
var customerQuery = _repository.GetRepository<RES_CUSTOMER>().Query();
var allCustomer = (from a in customerQuery
join b in customerQuery on a.CUSTOMERID equals b.CUSTOMERID
where b.RESID == resId
select a.RESID).ToList();
var data = await _repository.GetRepository<WW_EXTUSER_RESID>().Query()
.Where(x => allCustomer.Contains(x.Resid) || x.Resid == resId)
.ToListAsync();
var result = new List<ExUserModel>();
var unameList = data.Select(n => n.Userid).Distinct().ToList();
foreach (var name in unameList)
{
ExUserModel newModel = new ExUserModel
{
Userid = name,
};
var names = data.Where(n => n.Userid == name).OrderByDescending(n => n.Ctime).ToList();
var seftItem = names.FirstOrDefault(n => n.Deptcode == DeptCode);
if (seftItem != null)
{
newModel.Ctime = seftItem.Ctime;
newModel.Deptcode = seftItem.Deptcode;
newModel.Resid = seftItem.Resid;
}
else
{
newModel.Ctime = names.FirstOrDefault().Ctime;
newModel.Deptcode = names.FirstOrDefault().Deptcode;
newModel.Resid = names.FirstOrDefault().Resid;
}
result.Add(newModel);
}
return result.OrderByDescending(n => n.Ctime).ToList();
}
public async Task<List<string>> ExtUserBandGetAsync(string? resId)
{
if (string.IsNullOrEmpty(resId))
{
return new List<string>();
}
var customerQuery = _repository.GetRepository<RES_CUSTOMER>().Query();
var allCustomer = (from a in customerQuery
join b in customerQuery on a.CUSTOMERID equals b.CUSTOMERID
where b.RESID == resId
select a.RESID).ToList();
var userList = await _repository.GetRepository<WW_EXTUSER_RESID>().Query().Where(n => allCustomer.Contains(n.Resid) || n.Resid == resId).ToListAsync();
List<string> result = new List<string>();
foreach (var item in userList)
{
result.Add(item.Userid);
}
result = result.Distinct().ToList();
return result;
}
public async Task<List<string>> GetResidByExtuser(List<string> extUser)
{
if (!extUser.Any()) return new List<string>();
var resids = await _repository.GetRepository<WW_EXTUSER_RESID>().Query()
.Where(x => extUser.Contains(x.Userid))
.Select(x => x.Userid + ":" + x.Resid)
.ToListAsync();
return resids;
}
public async Task<IList<string>> GetPhoneByUnionid(string unionid)
{
IList<string> mobiles = new List<string>();
var unionlist = await _repository.GetRepository<Soft_Userinfo_Sub>().Query()
.Where(x => x.unionid == unionid || x.appuserid == unionid).ToListAsync();
if (unionlist.Any())
{
mobiles = await _repository.GetRepository<Soft_Userinfo_Sub>().Query()
.Where(x => x.cid == unionlist.First().cid && !string.IsNullOrEmpty(x.mobile)).Select(m => m.mobile).Distinct().ToListAsync();
}
return mobiles;
}
}
public class ExtUserItem
{
public string userid { get; set; }
}
}