100 lines
4.2 KiB
C#
100 lines
4.2 KiB
C#
using Zxd.Core.Domain.Dto.Wework;
|
|
using Zxd.Entity.Dncms;
|
|
|
|
namespace Zxd.Domain
|
|
{
|
|
public class DeptmentDomain : IDeptmentDomain
|
|
{
|
|
private readonly IBaseRepository<DncmsbaseDbContext> _repository;
|
|
private readonly IBaseRepository<CompanyBaseConfDbContext> _companyBaseConfRepository;
|
|
private readonly IRedisManager _redisManager;
|
|
|
|
public DeptmentDomain(IBaseRepository<DncmsbaseDbContext> repository,
|
|
IBaseRepository<CompanyBaseConfDbContext> companyBaseConfRepository,
|
|
IRedisManager redisManager)
|
|
{
|
|
_repository = repository;
|
|
_companyBaseConfRepository = companyBaseConfRepository;
|
|
_redisManager = redisManager;
|
|
}
|
|
|
|
public async Task<List<DeptmentDto>> GetDeptments(bool? IsDept = null)
|
|
{
|
|
var key = CacheKeys.DeptmentList;
|
|
var data = new List<DeptmentDto>();
|
|
if (await _redisManager.ExistsAsync(key))
|
|
{
|
|
data = await _redisManager.GetListAsync<DeptmentDto>(key);
|
|
data = data.AsQueryable().If(IsDept != null, y => y.Where(x => x.IsDept.HasValue && x.IsDept.Value == IsDept))
|
|
.ToList();
|
|
return data;
|
|
}
|
|
var query = from a in _companyBaseConfRepository.GetRepository<DepartmentCrmconf>().Query()
|
|
join b in _companyBaseConfRepository.GetRepository<Zxd.Entity.CompanyBaseConf.Department>().Query() on a.DepartmentId equals b.Id
|
|
into temp
|
|
from t1 in temp.DefaultIfEmpty()
|
|
select new { a, t1 };
|
|
|
|
var deptments = await query.ToListAsync();
|
|
var deptids = deptments.Where(x => x.a.Saledeptid != null).Select(x => x.a.Saledeptid).ToList();
|
|
var deptmentList = await _repository.GetRepository<Deptment>().Query()
|
|
.Include(x => x.DeptmentCampainIds)
|
|
.Where(x => deptids.Contains(x.Id))
|
|
.ToListAsync();
|
|
foreach (var deptment in deptments)
|
|
{
|
|
var item = new DeptmentDto()
|
|
{
|
|
Id = deptment.a.Saledeptid.HasValue ? deptment.a.Saledeptid.Value : 0,
|
|
Title = deptment.t1 == null ? deptment.a.Title : deptment.t1.DepartmentName,
|
|
Code = deptment.a.CompanyCode,
|
|
Appid = deptment.a.Appid,
|
|
DepartmentId = deptment.a.DepartmentId,
|
|
IsDept = deptment.a.IsDept,
|
|
CompanyCode = deptment.a.CompanyCode,
|
|
SortNo = deptment.a.SortNo,
|
|
DeptmentCampains = new List<DeptmentCampainDto>()
|
|
};
|
|
if (deptmentList.Count == 0 || !deptmentList.Any(x => x.Id == deptment.a.Saledeptid))
|
|
{
|
|
data.Add(item);
|
|
continue;
|
|
}
|
|
var dept = deptmentList.FirstOrDefault(n => n.Id == deptment.a.Saledeptid);
|
|
if (dept != null)
|
|
{
|
|
item.GroupId = dept.Groupid;
|
|
foreach (var deptmentCampain in dept.DeptmentCampainIds)
|
|
{
|
|
item.DeptmentCampains.Add(new DeptmentCampainDto
|
|
{
|
|
EndCampainId = deptmentCampain.EndCampainId,
|
|
StartCampainId = deptmentCampain.StartCampainId
|
|
});
|
|
}
|
|
}
|
|
data.Add(item);
|
|
}
|
|
|
|
await _redisManager.SetAsync(key, data, TimeSpan.FromDays(1));
|
|
data = data.If(IsDept != null, x => x.Where(x => x.IsDept.HasValue && x.IsDept.Value == IsDept)).ToList();
|
|
return data;
|
|
}
|
|
|
|
public async Task<DeptmentDto?> GetDeptmentByChannel(int channel)
|
|
{
|
|
var depts = await GetDeptments();
|
|
foreach (var dept in depts)
|
|
{
|
|
foreach (var ch in dept.DeptmentCampains)
|
|
{
|
|
if (channel >= ch.StartCampainId && channel <= ch.EndCampainId)
|
|
{
|
|
return dept;
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
} |