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

409 lines
17 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 Crm.Core.Domain.Dto.Live;
using Crm.Core.Domain.Dto.LivePlayback;
using Crm.Core.Domain.Impl;
using Crm.Core.Entity.Crm;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Crm.Core.Domain.Config.SystemEnums;
namespace Crm.Core.Domain
{
internal class LiveDomain : ILiveDomain
{
private readonly IHttpClient _httpClient;
private readonly IConfiguration _configuration;
private readonly IOracleRepository<CrmDbContext> _repository;
private readonly ICacheDomain _cacheDomain;
private readonly SystemConfig _systemConfig;
public LiveDomain(IHttpClient httpClient,
IConfiguration configuration,
ICacheDomain cacheDomain,
IOracleRepository<CrmDbContext> repository)
{
_httpClient = httpClient;
_configuration = configuration;
_cacheDomain = cacheDomain;
_systemConfig = _configuration.GetSection("SystemConfig").Get<SystemConfig>();
_repository = repository;
}
public async Task<LivePageDto<LiveUserSummayDataDto>> GetLiveUserSummayPageDto(LiveSearchUserSummayPageDto dto)
{
if (dto.Deptid == 0) return new LivePageDto<LiveUserSummayDataDto>();
return await GetLive<LivePageDto<LiveUserSummayDataDto>, LiveSearchUserSummayPageDto>(_systemConfig.GetLiveSummaryUrl(), dto);
}
public async Task<LivePageDto<LiveUserRealTimeDataDto>> GetLiveUserRealTimePageDto(LiveSearchUserRealTimePageDto dto)
{
if (dto.Deptid == 0) return new LivePageDto<LiveUserRealTimeDataDto>();
return await GetLive<LivePageDto<LiveUserRealTimeDataDto>, LiveSearchUserRealTimePageDto>(_systemConfig.GetLiveRealtimeUrl(), dto);
}
public async Task<LivePageDto<LiveUserWeworkDataDto>> GetLiveUserWeworkPageDto(LiveSearchUserWeworkPageDto dto)
{
if (dto.Deptid == 0) return new LivePageDto<LiveUserWeworkDataDto>();
return await GetLive<LivePageDto<LiveUserWeworkDataDto>, LiveSearchUserWeworkPageDto>(_systemConfig.GetLiveWeworkUsersUrl(), dto);
}
public async Task<LiveUserStatDto> GetLiveUserStat(LiveSearchUserSummayPageDto dto)
{
if (dto.Deptid == 0) return new LiveUserStatDto();
return await GetLive<LiveUserStatDto, LiveSearchUserSummayPageDto>(_systemConfig.GetLiveUserStatUrl(), dto);
}
public async Task<LiveWeworkStatDto> GetLiveWeworkStat(LiveSearchUserWeworkPageDto dto)
{
if (dto.Deptid == 0) return new LiveWeworkStatDto();
return await GetLive<LiveWeworkStatDto, LiveSearchUserWeworkPageDto>(_systemConfig.GetLiveWeworkStatUrl(), dto);
}
public async Task<LivePlaybackPageDto<LivePlaybackDetailDto, LivePlaybackDetailStatDto>> GetLivePlaybackDetailPageDto(LivePlaybackDetailPageDto dto)
{
if (dto.Deptid == 0) return new LivePlaybackPageDto<LivePlaybackDetailDto, LivePlaybackDetailStatDto>();
if (string.IsNullOrEmpty(dto.ActDateFrom))
{
throw new ApiException("请选择回放日期开始日期!");
}
else if (!string.IsNullOrEmpty(dto.ActDateTo))
{
var day = DiffDay(dto.ActDateFrom, dto.ActDateTo);
if (day > 31)
{
throw new ApiException("回放日期选择不能超过一个月,请重新选择!");
}
}
return await GetLive<LivePlaybackPageDto<LivePlaybackDetailDto, LivePlaybackDetailStatDto>, LivePlaybackDetailPageDto>(_systemConfig.GetLivePlaybackDetailUrl(), dto);
}
public async Task<LivePlaybackPageDto<LivePlaybackSummayDto, LivePlaybackSummayStatDto>> GetLivePlaybackSummayPageDto(LivePlaybackSummayPageDto dto)
{
if (dto.Deptid == 0) return new LivePlaybackPageDto<LivePlaybackSummayDto, LivePlaybackSummayStatDto>();
if (string.IsNullOrEmpty(dto.ActDateFrom))
{
throw new ApiException("请选择回放日期开始日期!");
}
else if (!string.IsNullOrEmpty(dto.ActDateTo))
{
var day = DiffDay(dto.ActDateFrom, dto.ActDateTo);
if (day > 31)
{
throw new ApiException("回放日期选择不能超过一个月,请重新选择!");
}
}
else
{
var day = DiffDay(dto.ActDateFrom, DateTime.Now.ToString("yyyy-MM-dd"));
if (day > 31)
{
throw new ApiException("回放日期选择不能超过一个月,请重新选择!");
}
}
return await GetLive<LivePlaybackPageDto<LivePlaybackSummayDto, LivePlaybackSummayStatDto>, LivePlaybackSummayPageDto>(_systemConfig.GetLivePlaybackSummaryUrl(), dto);
}
private async Task<T> GetLive<T, D>(string url, D dto)
where T : new()
where D : LiveSearchPageDto
{
if (dto.ExcludeDeptid == null)
{
var deptmentIds = await _cacheDomain.GetDeptmentIds();
var deptmentIds2= await _cacheDomain.GetSpecialDeptmentIdsByRightCode("G373");
deptmentIds.AddRange(deptmentIds2);
if (deptmentIds == null || !deptmentIds.Any() )
{
return new T();
}
dto.Deptids = string.Join(",", deptmentIds);
}
if (typeof(D) != typeof(LivePlaybackDetailPageDto) && typeof(D) != typeof(LivePlaybackSummayPageDto) && string.IsNullOrEmpty(dto.LivingDateFrom))
{
throw new ApiException("请选择直播日期开始日期!");
}
if (!string.IsNullOrEmpty(dto.LivingDateFrom))
{
if (!string.IsNullOrEmpty(dto.LivingDateTo))
{
var day = DiffDay(dto.LivingDateFrom, dto.LivingDateTo);
if (day > 31)
{
throw new ApiException("直播日期选择不能超过一个月,请重新选择!");
}
}
else
{
var day = DiffDay(dto.LivingDateFrom, DateTime.Now.ToString("yyyy-MM-dd"));
if (day > 31)
{
throw new ApiException("直播日期选择不能超过一个月,请重新选择!");
}
}
}
var eid = await _cacheDomain.GetCurrentEid();
Log.Information($"{url} 查询事业部:{dto.Deptids}");
// 处理筛选的事业部
if (!string.IsNullOrEmpty(dto.Txt_groupIds) && decimal.TryParse(dto.Txt_groupIds, out decimal salesGroupId))
{
var sales = await _cacheDomain.GetSalesDeptList();
if (!sales.Any(x => x.SalesGroupId == salesGroupId))
{
return new T();
}
dto.GroupIds = string.Join(",", sales.First(x => x.SalesGroupId == salesGroupId).GroupIds);
}
else if (!string.IsNullOrEmpty(dto.Txt_deptId))
{
var sales = await _cacheDomain.GetSalesDeptList();
var selesDepts = dto.Txt_deptId.Split(",");
var selesDeptIds = new List<decimal>();
foreach (var selesDept in selesDepts)
{
if (decimal.TryParse(selesDept, out decimal salesDeptId))
{
if (!sales.Any(x => x.SalesGroupId == salesDeptId))
{
continue;
}
selesDeptIds.Add(salesDeptId);
}
}
var groupIds = new List<decimal>();
sales.Where(x => x.SalesGroupId.HasValue && selesDeptIds.Contains(x.SalesGroupId.Value)).ToList().ForEach(x => groupIds.AddRange(x.GroupIds));
dto.GroupIds = string.Join(",", groupIds);
if (string.IsNullOrEmpty(dto.GroupIds))
{
var businessLineInfo = await _cacheDomain.GetBusinessLineByDeptMentIds(new Dto.Customer.GroupOrDeptIdDto { DeptId = dto.Txt_deptId });
dto.Deptids = businessLineInfo.deptId.ToString();
}
}
// 处理筛选员工ID
if (dto.Eid != null && dto.UserId != null && dto.Eid != dto.UserId)
{
return new T();
}
if (dto.Eid != null && dto.UserId == null)
{
var level = await _cacheDomain.LevelSSO(eid);
switch (level)
{
case RoleLevel.Self:
if (dto.Eid != eid) return new T();
break;
case RoleLevel.Dept:
var eids = await _cacheDomain.GetSalesDeptsForEid(eid);
if (!eids.Contains(dto.Eid.Value)) return new T();
break;
default:
break;
}
}
dto.Eid = dto.UserId == null ? dto.Eid : dto.UserId;
var dtoStr = JsonSerializer.Serialize(dto);
var param = JsonSerializer.Deserialize<Dictionary<string, object>>(dtoStr);
try
{
var response = await _httpClient.GetAsync<ApiResult<T>>($"{url}", param);
if (response.Code == 0)
{
return response.Data;
}
}
catch (Exception ex)
{
Log.Error(ex, $"{url} 请求出错param: {dtoStr}");
throw new ApiException("大数据接口请求报错!");
}
return new T();
}
private static int DiffDay(string dt1, string dt2)
{
var d1 = DateTime.Parse(dt1);
var d2 = DateTime.Parse(dt2);
var ts = d2 - d1;
return (int)ts.TotalDays;
}
public async Task<List<CourseDto>> GetCourses(decimal? deptid)
{
try
{
if (deptid == 0)
{
return new List<CourseDto>();
}
var list = new List<CourseDto>();
var deptmentIds = await _cacheDomain.GetDeptmentIds();
var deptmentIds2 = await _cacheDomain.GetSpecialDeptmentIdsByRightCode("G373");
deptmentIds.AddRange(deptmentIds2);
var deptments = await _cacheDomain.GetDeptments();
deptmentIds = deptmentIds.AsEnumerable().If(deptid != null, x => x.Where(x => x == deptid)).ToList();
Log.Information($"deptmentIds:" + JsonHelper.ToJson(deptmentIds));
foreach (var deptmentId in deptmentIds)
{
var response = await _httpClient.GetAsync<RetResult<List<CourseDto>>>($"{_systemConfig.GetCourseUrl()}?deptid={deptmentId}");
if (response.Ret == 0)
{
var deptment = deptments.FirstOrDefault(x => x.Id == deptmentId);
foreach (var item in response.Data)
{
item.DepartmentId = (int)deptmentId;
item.Title = deptment == null ? item.Title : $"{item.Title}({deptment.Title}ID{item.Id})";
item.Deptname = deptment?.Title;
item.CourseName = item.Title;
}
list.AddRange(response.Data);
}
else
{
Log.Error($"获取课程接口报错:{response.Message}");
}
}
return list;
}
catch (Exception ex)
{
Log.Error(ex, $"{_systemConfig.GetCourseUrl()} 请求出错");
throw new ApiException("获取课程接口报错!");
}
}
public async Task<dynamic> GetDeptMent()
{
var groups = await _repository.GetRepository<BAS_INNERGROUP>().Query()
.Where(x => x.DEPARTMENT_ID != null)
.Select(x => new
{
id = x.DEPARTMENT_ID,
title = x.GNAME
}).ToListAsync();
return groups;
}
public async Task<dynamic> GetBusinessLine()
{
try
{
var response = await _httpClient.GetAsync<RetResult<dynamic>>(_systemConfig.GetBusinessLineUrl());
if (response.Ret == 0)
{
return response.Data;
}
else
{
Log.Error($"获取业务线接口报错:{response.Message}");
}
}
catch (Exception ex)
{
Log.Error(ex, $"{_systemConfig.GetDeptMentUrl()} 请求出错");
throw new ApiException("获取业务线接口报错!");
}
return new Dictionary<int, string>();
}
public async Task<dynamic> GetInnerusers(int? groupid)
{
if (groupid == null)
{
return new List<dynamic>();
}
var users = await _repository.GetRepository<BAS_INNERUSER>().Query()
.Include(x => x.BAS_INNERUSERGROUP)
.Include(x => x.BAS_INNERUSERGROUP.BAS_INNERGROUP)
.Where(x => x.BAS_INNERUSERGROUP.BAS_INNERGROUP.DEPARTMENT_ID == groupid)
.Select(x => new
{
id = x.EID,
title = $"{x.UNAME}-{x.EID}"
})
.Distinct()
.ToListAsync();
return users;
}
public async Task<List<LiveCourseSummaryDto>> GetLiveCourseSummary(LiveSearchCourseSummaryDto dto)
{
var dtoStr = JsonSerializer.Serialize(dto);
var param = JsonSerializer.Deserialize<Dictionary<string, object>>(dtoStr);
try
{
var response = await _httpClient.GetAsync<ApiResult<List<LiveCourseSummaryDto>>>($"{_systemConfig.GetLiveCourseSummaryUrl()}", param);
if (response.Code == 0)
{
return response.Data;
}
}
catch (Exception ex)
{
Log.Error(ex, $"{_systemConfig.GetLiveUserStatUrl()} 请求出错param: {dtoStr}");
throw new ApiException("大数据接口请求报错!");
}
return new List<LiveCourseSummaryDto>();
}
public async Task<List<LiveCourseProductsDto>> GetLiveCourseProduct(LiveSearchCourseProductsDto dto)
{
try
{
dto.Deptids = String.Join(",", await _cacheDomain.GetDeptmentIds());
var dtoStr = JsonSerializer.Serialize(dto);
var param = JsonSerializer.Deserialize<Dictionary<string, object>>(dtoStr);
var t = _systemConfig.GetProductsUrl();
var response = await _httpClient.GetAsync<RetResult<List<LiveCourseProductsDto>>>(_systemConfig.GetProductsUrl(), param);
if (response.Ret == 0)
{
return response.Data;
}
else
{
Log.Error($"获取业务线接口报错:{response.Message}");
}
}
catch (Exception ex)
{
Log.Error(ex, $"{_systemConfig.GetDeptMentUrl()} 请求出错");
throw new ApiException("获取业务线接口报错!");
}
return new List<LiveCourseProductsDto>();
}
public async Task<List<LiveScheduleDto>> GetLiveScheduleTree(decimal? deptid)
{
var courses = await GetCourses(deptid);
var schedules = new List<LiveScheduleDto>();
foreach (var dept in courses.Select(x => new { x.Deptname, x.DepartmentId }).Distinct())
{
schedules.Add(new LiveScheduleDto
{
Deptid = dept.DepartmentId,
Value = dept.DepartmentId,
Name = dept.Deptname,
Children = new List<LiveScheduleDto>()
});
}
foreach (var course in courses)
{
var schedule = schedules.Find(x => x.Deptid == course.DepartmentId);
schedule.Children.Add(new LiveScheduleDto
{
Deptid = course.DepartmentId,
Value = course.Id,
Name = course.Title
});
}
return schedules;
}
}
}