using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.DirectoryServices; using System.Linq; using System.Text; using System.Threading.Tasks; using Zxd.Core.Domain.Dto.ImportanceItem; using Zxd.Core.Domain.Response; using Zxd.Domain.Sso; using Zxd.Entity.Action; namespace Zxd.Core.Domain { internal class ImportanceItemDomain : IImportanceItemDomain { private readonly IBaseRepository _hgActionRepository; private readonly IDeptmentDomain _deptmentDomain; private readonly IHttpClient _httpClient; private readonly SystemConfig _systemConfig; public ImportanceItemDomain(IBaseRepository hgActionRepository, IDeptmentDomain deptmentDomain, IHttpClient httpClient, IConfiguration configuration) { _hgActionRepository = hgActionRepository; _deptmentDomain = deptmentDomain; _httpClient = httpClient; _systemConfig = configuration.GetSection("SystemConfig").Get(); } public async Task> GetCustomerBehaviorLog(SearchCustomerBehaviorLogDto dto) { var deptments = await _deptmentDomain.GetDeptments(); if (string.IsNullOrEmpty(dto.Resid) && !dto.Uid.HasValue && (string.IsNullOrEmpty(dto.Appid) || string.IsNullOrEmpty(dto.Appuserid))) { throw new ApiException("参数不能为空!"); } //var customerids = await _hgActionRepository.GetRepository().Query() // .If(!string.IsNullOrEmpty(dto.Resid), x => x.Where(x => x.resid == dto.Resid)) // .If(dto.Uid.HasValue, x => x.Where(x => x.uid == dto.Uid)) // .If(!string.IsNullOrEmpty(dto.Appid) && !string.IsNullOrEmpty(dto.Appuserid), x => x.Where(x => x.appid == dto.Appid && x.appuserid == dto.Appuserid)) // .Select(b => b.customerid).Where(x => x > 0).Distinct().ToListAsync(); //var uids = await _hgActionRepository.GetRepository().Query() // .Where(a => customerids.Contains(a.customerid)).Select(a => a.uid) // .Where(x => x > 0).Distinct().ToListAsync(); var userlist = await GetUserList(dto.Appid, dto.Appuserid); var uids = userlist.Select(x => x.Uid).ToList(); var query = _hgActionRepository.GetRepository().Query() .Where(x => uids.Contains(x.uid.Value)) .If(dto.Eventid.HasValue, x => x.Where(x => x.neweventid == dto.Eventid)); var data = await query .Select(x => new CustomerBehaviorLogDto { Id = x.id, Deptid = x.deptid, ActTime = x.act_time, Eventid = x.neweventid.Value, Eventname = x.neweventname, Sceneidname = x.eventname, Scenetypename = x.eventtypename, Content = x.Content }) .OrderByDescending(x => x.Id) .ToListAsync(); foreach (var item in data) { var deptment = deptments.FirstOrDefault(x => x.Id == item.Deptid); if (deptment != null) { item.Deptname = deptment.Title; } } return data; } public async Task> GetCustomerBehaviorStatistics(SearchCustomerBehaviorLogDto dto) { //var customerids = await _hgActionRepository.GetRepository().Query() // .If(!string.IsNullOrEmpty(dto.Resid), x => x.Where(x => x.resid == dto.Resid)) // .If(dto.Uid.HasValue, x => x.Where(x => x.uid == dto.Uid)) // .If(!string.IsNullOrEmpty(dto.Appid) && !string.IsNullOrEmpty(dto.Appuserid), x => x.Where(x => x.appid == dto.Appid && x.appuserid == dto.Appuserid)) // .Select(b => b.customerid).Where(x => x > 0).Distinct().ToListAsync(); //var uids = await _hgActionRepository.GetRepository().Query() // .Where(a => customerids.Contains(a.customerid)).Select(a => a.uid) // .Where(x => x > 0).Distinct().ToListAsync(); var userlist = await GetUserList(dto.Appid, dto.Appuserid); var uids = userlist.Select(x => x.Uid).ToList(); var query = _hgActionRepository.GetRepository().Query() .Where(x => uids.Contains(x.uid.Value)) .GroupBy(x => new { x.neweventid, x.eventtypename }).Select(x => new CustomerBehaviorStatisticsDto { Eventid = x.Key.neweventid.Value, EventName = x.Key.eventtypename, Count = x.Count() }); var data = await query.ToListAsync(); return data; } private async Task> GetUserList(string? appid, string? appuserid) { var data = new { where = JsonHelper.ToJson(new { appid, appuserid }) }; var url = _systemConfig.GetUserList(); var result = await _httpClient.PostAsync>(url, data); if (result.Ret == 0) { return result.List; } return new List(); } } }