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 _repository; private readonly ICacheDomain _cacheDomain; private readonly SystemConfig _systemConfig; public LiveDomain(IHttpClient httpClient, IConfiguration configuration, ICacheDomain cacheDomain, IOracleRepository repository) { _httpClient = httpClient; _configuration = configuration; _cacheDomain = cacheDomain; _systemConfig = _configuration.GetSection("SystemConfig").Get(); _repository = repository; } public async Task> GetLiveUserSummayPageDto(LiveSearchUserSummayPageDto dto) { if (dto.Deptid == 0) return new LivePageDto(); return await GetLive, LiveSearchUserSummayPageDto>(_systemConfig.GetLiveSummaryUrl(), dto); } public async Task> GetLiveUserRealTimePageDto(LiveSearchUserRealTimePageDto dto) { if (dto.Deptid == 0) return new LivePageDto(); return await GetLive, LiveSearchUserRealTimePageDto>(_systemConfig.GetLiveRealtimeUrl(), dto); } public async Task> GetLiveUserWeworkPageDto(LiveSearchUserWeworkPageDto dto) { if (dto.Deptid == 0) return new LivePageDto(); return await GetLive, LiveSearchUserWeworkPageDto>(_systemConfig.GetLiveWeworkUsersUrl(), dto); } public async Task GetLiveUserStat(LiveSearchUserSummayPageDto dto) { if (dto.Deptid == 0) return new LiveUserStatDto(); return await GetLive(_systemConfig.GetLiveUserStatUrl(), dto); } public async Task GetLiveWeworkStat(LiveSearchUserWeworkPageDto dto) { if (dto.Deptid == 0) return new LiveWeworkStatDto(); return await GetLive(_systemConfig.GetLiveWeworkStatUrl(), dto); } public async Task> GetLivePlaybackDetailPageDto(LivePlaybackDetailPageDto dto) { if (dto.Deptid == 0) return new LivePlaybackPageDto(); 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, LivePlaybackDetailPageDto>(_systemConfig.GetLivePlaybackDetailUrl(), dto); } public async Task> GetLivePlaybackSummayPageDto(LivePlaybackSummayPageDto dto) { if (dto.Deptid == 0) return new LivePlaybackPageDto(); 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, LivePlaybackSummayPageDto>(_systemConfig.GetLivePlaybackSummaryUrl(), dto); } private async Task GetLive(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(); 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(); 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>(dtoStr); try { var response = await _httpClient.GetAsync>($"{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> GetCourses(decimal? deptid) { try { if (deptid == 0) { return new List(); } var list = new List(); 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>>($"{_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 GetDeptMent() { var groups = await _repository.GetRepository().Query() .Where(x => x.DEPARTMENT_ID != null) .Select(x => new { id = x.DEPARTMENT_ID, title = x.GNAME }).ToListAsync(); return groups; } public async Task GetBusinessLine() { try { var response = await _httpClient.GetAsync>(_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(); } public async Task GetInnerusers(int? groupid) { if (groupid == null) { return new List(); } var users = await _repository.GetRepository().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> GetLiveCourseSummary(LiveSearchCourseSummaryDto dto) { var dtoStr = JsonSerializer.Serialize(dto); var param = JsonSerializer.Deserialize>(dtoStr); try { var response = await _httpClient.GetAsync>>($"{_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(); } public async Task> GetLiveCourseProduct(LiveSearchCourseProductsDto dto) { try { dto.Deptids = String.Join(",", await _cacheDomain.GetDeptmentIds()); var dtoStr = JsonSerializer.Serialize(dto); var param = JsonSerializer.Deserialize>(dtoStr); var t = _systemConfig.GetProductsUrl(); var response = await _httpClient.GetAsync>>(_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(); } public async Task> GetLiveScheduleTree(decimal? deptid) { var courses = await GetCourses(deptid); var schedules = new List(); 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() }); } 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; } } }