282 lines
14 KiB
C#
282 lines
14 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Zxd.Core.Domain.Dto.WxResource;
|
|
|
|
namespace Zxd.Core.Domain
|
|
{
|
|
public class WxResourceDomain : IWxResourceDomain
|
|
{
|
|
private readonly IConfiguration _configuration;
|
|
private readonly ICacheDomain _cacheDomain;
|
|
private readonly SystemConfig _systemConfig;
|
|
private readonly IHttpClient _httpClient;
|
|
private readonly IBaseRepository<DncmsDbContext> _cmsRepository;
|
|
private readonly IMapper _mapper;
|
|
|
|
public WxResourceDomain(
|
|
IConfiguration configuration,
|
|
IHttpClient httpClient,
|
|
ICacheDomain cacheDomain,
|
|
IBaseRepository<DncmsDbContext> cmsRepository,
|
|
IMapper mapper)
|
|
{
|
|
_httpClient = httpClient;
|
|
_configuration = configuration;
|
|
_cacheDomain = cacheDomain;
|
|
_systemConfig = configuration.GetSection("SystemConfig").Get<SystemConfig>();
|
|
_cmsRepository = cmsRepository;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
public async Task<List<UserGroupDto>> GetUserGroupList(UserGroupQueryDto dto)
|
|
{
|
|
var url = $"{_systemConfig.GetUserGroupList()}?deptIds={dto.deptid}&page=1&limit=100000";
|
|
if (dto.cateKey.HasValue)
|
|
{
|
|
url += $"&cateKey={dto.cateKey}";
|
|
}
|
|
var response = await _httpClient.GetAsync<RetResult<GroupPageDto<UserGroupDto>>>(url);
|
|
if (response.Ret == 0)
|
|
{
|
|
return response.Data.TableData;
|
|
}
|
|
else
|
|
{
|
|
Log.Error($"获取分群列表接口报错:{response.Message}");
|
|
throw new ApiException($"获取分群列表接口报错:{response.Message}");
|
|
}
|
|
}
|
|
|
|
public async Task<ResourceCountPage> GetKfSourceCountByCrm(ResourceCountQueryDto dto)
|
|
{
|
|
ResourceCountPage res = new ResourceCountPage();
|
|
var url = $"{_systemConfig.GetKFSourceCount()}?appid={dto.appid}&userids={dto.userids}&fields=_appuserid,_appid,_headimgurl,_nickname¤tPage={dto.page}&pageSize={dto.limit}&status={dto.status}&subscribe={dto.subscribe}&group_ids={dto.groupids}";
|
|
var response = await _httpClient.GetAsync<RetResult<LivePageDto<ResourceCountReturnModel>>>(url);
|
|
if (response.Ret == 0)
|
|
{
|
|
res.Total = response.Data.TableData.Count();
|
|
res.Data = response.Data.TableData.ToList();
|
|
var userids = dto.userids.Split(",").ToList();
|
|
List<ResRelationModel> resRelationModels = new List<ResRelationModel>();
|
|
var groupid = 0;
|
|
Int32.TryParse(dto.groupids, out groupid);
|
|
var oldLogs = new List<ResourceFlowLog>();
|
|
var allOldFromLog = await _cmsRepository.GetRepository<ResourceFlowLog>().Query().Where(x => x.Appid == dto.appid && userids.Contains(x.FromUserid) && x.Status != ResourceFlowStatus.转移成功).ToListAsync();
|
|
if (groupid > 0)
|
|
{
|
|
oldLogs = allOldFromLog.Where(x => x.GroupId == groupid).ToList();
|
|
}
|
|
//最近一天的 不让重新分配
|
|
var notPassFromLog = allOldFromLog.Where(n => n.FlowTime >= DateTime.Now.AddDays(-1)).ToList();
|
|
oldLogs.AddRange(notPassFromLog);
|
|
foreach (var userid in userids)
|
|
{
|
|
url = $"{_systemConfig.GetKFSourceCount()}?appid={dto.appid}&userids={userid}&fields=_appuserid,_appid,_headimgurl,_nickname¤tPage={dto.page}&pageSize={dto.limit}&status={dto.status}&subscribe={dto.subscribe}&group_ids={dto.groupids}";
|
|
response = await _httpClient.GetAsync<RetResult<LivePageDto<ResourceCountReturnModel>>>(url);
|
|
if (response.Ret == 0)
|
|
{
|
|
var relation = response.Data.TableData;
|
|
foreach (var entity in relation)
|
|
{
|
|
ResRelationModel model = new ResRelationModel
|
|
{
|
|
_appid = entity._appid,
|
|
_appuserid = entity._appuserid,
|
|
_headimgurl = entity._headimgurl,
|
|
_nickname = entity._nickname,
|
|
_fromuserid = userid
|
|
};
|
|
var log = oldLogs.FirstOrDefault(n => n.Appuserid == model._appuserid && n.FromUserid == userid);
|
|
if (log != null)
|
|
{
|
|
model._done = true;
|
|
}
|
|
resRelationModels.Add(model);
|
|
}
|
|
}
|
|
}
|
|
resRelationModels = resRelationModels.ToList();
|
|
res.ResRelationModel = resRelationModels;
|
|
res.DoneModel = resRelationModels.Where(n => n._done).ToList();
|
|
res.RelationCount = resRelationModels.Count();
|
|
//去重
|
|
var distinctData = new List<ResourceCountReturnModel>();
|
|
List<string> containStr = new List<string>();
|
|
foreach (var entity in res.Data)
|
|
{
|
|
var key = $"{entity._appid}_{entity._appuserid}";
|
|
if (containStr.Contains(key))
|
|
{
|
|
continue;
|
|
}
|
|
containStr.Add(key);
|
|
distinctData.Add(entity);
|
|
}
|
|
res.Total = distinctData.Count();
|
|
res.Data = distinctData.ToList();
|
|
res.UseCount = distinctData.Where(n => resRelationModels.Where(n => !n._done).Select(n => n._appuserid).Contains(n._appuserid)).Count();
|
|
res.UseData = distinctData.Where(n => resRelationModels.Where(n => !n._done).Select(n => n._appuserid).Contains(n._appuserid)).ToList();
|
|
res.DoneCount = resRelationModels.Where(n => n._done).Count();
|
|
return res;
|
|
}
|
|
else
|
|
{
|
|
Log.Error($"获取分群列表接口报错:{response.Message}");
|
|
throw new ApiException($"获取分群列表接口报错:{response.Message}");
|
|
}
|
|
}
|
|
|
|
public async Task<LivePageDto<ResourceCountReturnModel>> GetKfSourceCount(ResourceCountQueryDto dto)
|
|
{
|
|
var url = $"{_systemConfig.GetKFSourceCount()}?appid={dto.appid}&userids={dto.userids}&fields=_appuserid,_appid,_headimgurl,_nickname¤tPage={dto.page}&pageSize={dto.limit}&status={dto.status}&subscribe={dto.subscribe}";
|
|
if (!string.IsNullOrWhiteSpace(dto.groupids))
|
|
{
|
|
url += $"&group_ids={dto.groupids}";
|
|
}
|
|
var response = await _httpClient.GetAsync<RetResult<LivePageDto<ResourceCountReturnModel>>>(url);
|
|
if (response.Ret == 0)
|
|
{
|
|
return response.Data;
|
|
}
|
|
else
|
|
{
|
|
Log.Error($"获取分群列表接口报错:{response.Message}");
|
|
throw new ApiException($"获取分群列表接口报错:{response.Message}");
|
|
}
|
|
}
|
|
|
|
public async Task SubmitSourceTask(ResourceConfigCreateDto dto)
|
|
{
|
|
//校验
|
|
if (dto.ToUser.Count == 0)
|
|
{
|
|
throw new Exception("接替者不能为空");
|
|
}
|
|
if (dto.FromUser.Count == 0)
|
|
{
|
|
throw new Exception("转移成员不能为空");
|
|
}
|
|
try
|
|
{
|
|
ResourceFlowConfig config = new ResourceFlowConfig
|
|
{
|
|
GroupId = dto.groupid,
|
|
Appid = dto.appid,
|
|
Eid = dto.eid,
|
|
Ename = dto.ename,
|
|
GroupName = dto.gname,
|
|
FlowCount = dto.flowCount,
|
|
Ctime = DateTime.Now,
|
|
Deptid = dto.deptId,
|
|
Status = ResourceConfigStatus.待执行
|
|
};
|
|
await _cmsRepository.GetRepository<ResourceFlowConfig>().InsertAsync(config);
|
|
List<ResourceFlowConfigFrom> fromList = new List<ResourceFlowConfigFrom>();
|
|
foreach (var item in dto.FromUser)
|
|
{
|
|
fromList.Add(new ResourceFlowConfigFrom
|
|
{
|
|
ConfigId = config.Id,
|
|
Eid = item.eid,
|
|
Ename = item.ename,
|
|
Userid = item.userid,
|
|
Ctime = DateTime.Now,
|
|
GroupId = dto.groupid,
|
|
});
|
|
}
|
|
List<ResourceFlowConfigTo> toList = new List<ResourceFlowConfigTo>();
|
|
foreach (var item in dto.ToUser)
|
|
{
|
|
toList.Add(new ResourceFlowConfigTo
|
|
{
|
|
ConfigId = config.Id,
|
|
Eid = item.eid,
|
|
Ename = item.ename,
|
|
FlowCount = item.flowCount,
|
|
Userid = item.userid,
|
|
Ctime = DateTime.Now,
|
|
GroupId = dto.groupid
|
|
});
|
|
}
|
|
await _cmsRepository.GetRepository<ResourceFlowConfigFrom>().BatchInsertAsync(fromList);
|
|
await _cmsRepository.GetRepository<ResourceFlowConfigTo>().BatchInsertAsync(toList);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error($"提交企微流转任务失败{ex.Message}");
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
public async Task<PageResult<SourceTaskModel>> GetSourceTaskPage(SearchSourceTaskDto dto)
|
|
{
|
|
//PageResult<SourceTaskModel> res = new PageResult<SourceTaskModel>();
|
|
var deptids = new List<int>();
|
|
if (!string.IsNullOrWhiteSpace(dto.deptids))
|
|
{
|
|
deptids = dto.deptids.Split(",").Select(n => Convert.ToInt32(n)).ToList();
|
|
}
|
|
var query = _cmsRepository.GetRepository<ResourceFlowConfig>().Query();
|
|
if (deptids.Any())
|
|
{
|
|
query = query.Where(n => deptids.Contains(n.Deptid));
|
|
}
|
|
var data = await query
|
|
.OrderByDescending(x => x.Id)
|
|
.Select(x => new SourceTaskModel
|
|
{
|
|
Id = x.Id,
|
|
GroupId = x.GroupId,
|
|
GroupName = x.GroupName,
|
|
Appid = x.Appid,
|
|
Eid = x.Eid,
|
|
Ename = x.Ename,
|
|
Date = x.Ctime.ToString("yyyy-MM-dd"),
|
|
FlowCount = x.FlowCount,
|
|
Deptid = x.Deptid,
|
|
SuccessCount = x.ResourceFlowLog.Where(n => n.Status == ResourceFlowStatus.转移成功).Count(),
|
|
})
|
|
.Skip((dto.PageIndex - 1) * dto.PageSize)
|
|
.Take(dto.PageSize)
|
|
.ToListAsync();
|
|
var total = await query.CountAsync();
|
|
return new PageResult<SourceTaskModel>(dto.PageIndex, dto.PageSize, total, data);
|
|
}
|
|
|
|
public async Task<List<ResourceFlowConfigFrom>> GetFromUserPage(SearchFromDto dto)
|
|
{
|
|
return await _cmsRepository.GetRepository<ResourceFlowConfigFrom>().Query().Where(n => n.ConfigId == dto.Id).ToListAsync();
|
|
}
|
|
|
|
public async Task<ToUserTotalModel> GetToUserPage(SearchToDto dto)
|
|
{
|
|
ToUserTotalModel result = new ToUserTotalModel();
|
|
|
|
List<ToUserModel> res = new List<ToUserModel>();
|
|
var toUserList = await _cmsRepository.GetRepository<ResourceFlowConfigTo>().Query().Where(n => n.ConfigId == dto.Id).ToListAsync();
|
|
var logList = await _cmsRepository.GetRepository<ResourceFlowLog>().Query().Where(n => n.ConfigId == dto.Id).ToListAsync();
|
|
foreach (var toUser in toUserList)
|
|
{
|
|
ToUserModel model = new ToUserModel
|
|
{
|
|
Eid = toUser.Eid,
|
|
Ename = toUser.Ename,
|
|
FlowCount = logList.Where(n => n.ToUserid == toUser.Userid).Select(n => n.Appuserid).Distinct().Count(),
|
|
Userid = toUser.Userid,
|
|
SuccessCount = logList.Where(n => n.ToUserid == toUser.Userid && n.Status == ResourceFlowStatus.转移成功).Select(n => n.Appuserid).Distinct().Count(),
|
|
NoneCount = logList.Where(n => n.ToUserid == toUser.Userid && n.Status == ResourceFlowStatus.无好友关系).Select(n => n.Appuserid).Distinct().Count(),
|
|
ForbidCount = logList.Where(n => n.ToUserid == toUser.Userid && n.Status == ResourceFlowStatus.成员禁用).Select(n => n.Appuserid).Distinct().Count(),
|
|
RefundCount = logList.Where(n => n.ToUserid == toUser.Userid && n.Status == ResourceFlowStatus.用户拒绝).Select(n => n.Appuserid).Distinct().Count(),
|
|
OtherCount = logList.Where(n => n.ToUserid == toUser.Userid && n.Status == ResourceFlowStatus.转移失败).Select(n => n.Appuserid).Distinct().Count(),
|
|
};
|
|
res.Add(model);
|
|
}
|
|
result.ToUserModel = res;
|
|
result.FailCount = res.Sum(n => n.RefundCount) + res.Sum(n => n.ForbidCount) + res.Sum(n => n.RefundCount) + res.Sum(n => n.OtherCount);
|
|
return result;
|
|
}
|
|
}
|
|
} |