Zxd.Core/code/Zxd.Domain/ScreenRecordDomain.cs

230 lines
12 KiB
C#
Raw Permalink 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zxd.Domain.Dto.Resource;
namespace Zxd.Domain
{
public class ScreenRecordDomain : IScreenRecordDomain
{
private readonly IBaseRepository<ZxdDbContext> _repository;
public ScreenRecordDomain(IBaseRepository<ZxdDbContext> repository)
{
_repository = repository;
}
public async Task<bool> ScreenRecord(ScreenRecord postData)
{
Log.Information("ScreenRecord" + postData.ToJson());
if (string.IsNullOrWhiteSpace(postData.MasterId))
throw new Exception("主控ID不能为空");
if (string.IsNullOrWhiteSpace(postData.MasterIP))
throw new Exception("主控IP不能为空");
if (string.IsNullOrWhiteSpace(postData.SlaveId))
throw new Exception("被控ID不能为空");
if (string.IsNullOrWhiteSpace(postData.SlaveIP))
throw new Exception("被控IP不能为空");
postData.CTime = DateTime.Now;
int maxRetryCount = 3;
int retryCount = 0;
try
{
var screenRecord = await _repository.GetRepository<ScreenRecord>().FirstOrDefaultAsync(x => x.MasterDeviceCode == postData.MasterDeviceCode && x.SlaveDeviceCode == postData.SlaveDeviceCode && x.InitConnectTime == postData.InitConnectTime);
if (screenRecord != null)
{
screenRecord.RealEndTime = postData.RealEndTime;
await _repository.GetRepository<ScreenRecord>().UpdateAsync(screenRecord, x => new { x.RealEndTime });
}
else
{
var result = await _repository.GetRepository<ScreenRecord>().InsertAsync(postData);
}
return true;
}
catch (Exception ex)
{
Log.Error($"ScreenRecordERR:{postData.ToJson()}reason:{ex.Message}");
return false;
//await Task.Delay(500);
}
return true;
/*while (retryCount < maxRetryCount)
{
try
{
var screenRecord = await _repository.GetRepository<ScreenRecord>().FirstOrDefaultAsync(x => x.MasterDeviceCode == postData.MasterDeviceCode && x.SlaveDeviceCode == postData.SlaveDeviceCode && x.InitConnectTime == postData.InitConnectTime);
if (screenRecord != null)
{
screenRecord.RealEndTime = postData.RealEndTime;
await _repository.GetRepository<ScreenRecord>().UpdateAsync(screenRecord, x => new { x.RealEndTime });
}
else
{
var result = await _repository.GetRepository<ScreenRecord>().InsertAsync(postData);
}
return true;
}
catch (Exception ex)
{
Log.Information($"录屏数据传入出错。参数:{postData.ToJson()}。原因:{ex.Message}");
retryCount++;
await Task.Delay(500);
}
}
throw new Exception("存储失败");*/
}
public async Task<bool> FileRecord(ScreenRecordFile postData)
{
if (string.IsNullOrWhiteSpace(postData.MasterDeviceCode))
throw new Exception("主控Code不能为空");
if (string.IsNullOrWhiteSpace(postData.SlaveDeviceCode))
throw new Exception("被控Code不能为空");
if (!postData.InitConnectTime.HasValue)
throw new Exception("连接时长不能为空");
if (string.IsNullOrWhiteSpace(postData.FileUrl))
throw new Exception("文件路径不能为空");
var screenRecord = await _repository.GetRepository<ScreenRecord>().FirstOrDefaultAsync(x => x.MasterDeviceCode == postData.MasterDeviceCode && x.SlaveDeviceCode == postData.SlaveDeviceCode && x.InitConnectTime == postData.InitConnectTime);
if (screenRecord == null)
throw new Exception("录屏数据不存在");
screenRecord.FileUrl = postData.FileUrl;
screenRecord.UTime = DateTime.Now;
await _repository.GetRepository<ScreenRecord>().UpdateAsync(screenRecord);
return true;
}
/// <summary>
/// 获取录屏列表接口
/// </summary>
/// <param name="postData"></param>
/// <returns></returns>
public async Task<PageResult<ScreenRecordResponse>> GetScreenRecordList(ScreenRecordRequest postData)
{
var result = new List<ScreenRecordResponse>();
var query = _repository.GetRepository<ScreenRecord>().Query()
.Select(x => new ScreenRecordResponse
{
Id = x.id,
MasterUserName = x.MasterId,
SlaveUserName = x.SlaveId,
InitConnectTime = x.InitConnectTime,
RealStartTime = x.RealStartTime,
RealEndTime = x.RealEndTime,
FileUrl = x.FileUrl
});
var employees = from a in _repository.GetRepository<Employee>().Query()
join b in _repository.GetRepository<EMPLOYEE_PHONE_BIND>().Query() on a.employee_id equals b.eid
select new
{
Eid = a.employee_id,
Name = a.employee_name,
UserName = b.appusername,
AppId = a.appid
};
query = query.If(postData.InitConnectStartTime.HasValue, x => x.Where(y => y.InitConnectTime >= postData.InitConnectStartTime.Value))
.If(postData.InitConnectEndTime.HasValue, x => x.Where(y => y.InitConnectTime <= postData.InitConnectEndTime.Value.AddDays(1)));
//录屏主控人或被控人都可以是员工或客户
//因此筛选条件需要判断主控人与被控人一起判断
if (!string.IsNullOrWhiteSpace(postData.AppId))
{
var userNames = employees.Where(x => x.AppId == postData.AppId).Select(x => x.UserName).ToList();
query = query.Where(x => userNames.Contains(x.MasterUserName) || userNames.Contains(x.SlaveUserName));
}
if (postData.Eid.HasValue)
{
var userNames = employees.Where(x => x.Eid == postData.Eid).Select(x => x.UserName).ToList();
query = query.If(postData.Type == (int)ScreenRecordType., x => x.Where(y => userNames.Contains(y.MasterUserName)))
.If(postData.Type == (int)ScreenRecordType., x => x.Where(y => userNames.Contains(y.SlaveUserName)))
.If(!postData.Type.HasValue, x => x.Where(y => userNames.Contains(y.MasterUserName) || userNames.Contains(y.SlaveUserName)));
}
if (!string.IsNullOrWhiteSpace(postData.Name))
{
var userNames = await _repository.GetRepository<WX_SZZYORDER>().Query().Where(x => x.CNAME == postData.Name).Select(x => x.SOFTUSERNAME).ToListAsync();
query = query.If(postData.Type == (int)ScreenRecordType., x => x.Where(y => userNames.Contains(y.MasterUserName)))
.If(postData.Type == (int)ScreenRecordType., x => x.Where(y => userNames.Contains(y.SlaveUserName)))
.If(!postData.Type.HasValue, x => x.Where(y => userNames.Contains(y.MasterUserName) || userNames.Contains(y.SlaveUserName)));
}
if (!string.IsNullOrWhiteSpace(postData.UserName))
{
query = query.If(postData.Type == (int)ScreenRecordType., x => x.Where(y => y.MasterUserName == postData.UserName))
.If(postData.Type == (int)ScreenRecordType., x => x.Where(y => y.SlaveUserName == postData.UserName))
.If(!postData.Type.HasValue, x => x.Where(y => y.MasterUserName == postData.UserName || y.SlaveUserName == postData.UserName));
}
if (!string.IsNullOrWhiteSpace(postData.ResId))
{
var userNames = await _repository.GetRepository<SOFT_USER>().Query().Where(x => x.RESID == postData.ResId).Select(x => x.USERNAME).ToListAsync();
query = query.If(postData.Type == (int)ScreenRecordType., x => x.Where(y => userNames.Contains(y.MasterUserName)))
.If(postData.Type == (int)ScreenRecordType., x => x.Where(y => userNames.Contains(y.SlaveUserName)))
.If(!postData.Type.HasValue, x => x.Where(y => userNames.Contains(y.MasterUserName) || userNames.Contains(y.SlaveUserName)));
}
var total = await query.CountAsync();
result = await query
.OrderByDescending(x => x.Id)
.Skip((postData.PageIndex - 1) * postData.PageSize)
.Take(postData.PageSize)
.ToListAsync();
if (result != null && result.Any())
{
var userNames = result.Select(x => x.MasterUserName).ToList();
userNames.AddRange(result.Select(x => x.SlaveUserName).ToList());
userNames = userNames.Distinct().ToList();
var softUsers = await _repository.GetRepository<SOFT_USER>().Query().Where(x => userNames.Contains(x.USERNAME)).ToListAsync();
var employee = employees.Where(x => userNames.Contains(x.UserName)).ToList();
var orders = await _repository.GetRepository<WX_SZZYORDER>().Query().Where(x => userNames.Contains(x.SOFTUSERNAME) && x.ISTEST == 0 && !string.IsNullOrWhiteSpace(x.CNAME))
.OrderByDescending(x => x.CTIME).Select(x => new { Cname = x.CNAME, USERNAME = x.SOFTUSERNAME }).ToListAsync();
foreach (var item in result)
{
item.MasterResId = softUsers.FirstOrDefault(x => x.USERNAME == item.MasterUserName)?.RESID;
item.SlaveResId = softUsers.FirstOrDefault(x => x.USERNAME == item.SlaveUserName)?.RESID;
item.MasterUmid = softUsers.FirstOrDefault(x => x.USERNAME == item.MasterUserName)?.UMID;
item.SlaveUmid = softUsers.FirstOrDefault(x => x.USERNAME == item.SlaveUserName)?.UMID;
item.MasterName = orders.FirstOrDefault(x => x.USERNAME == item.MasterUserName)?.Cname;
item.SlaveName = orders.FirstOrDefault(x => x.USERNAME == item.SlaveUserName)?.Cname;
var time = item.RealEndTime - item.RealStartTime;
item.RealLongTime = time.HasValue ? $"{(int)time.Value.TotalMinutes}min{time.Value.TotalSeconds % 60}ss" : string.Empty;
var masterEmployee = employee.FirstOrDefault(x => x.UserName == item.MasterUserName);
var slaveEmployee = employee.FirstOrDefault(x => x.UserName == item.SlaveUserName);
item.MasterEid = masterEmployee?.Eid;
item.MasterEidStr = masterEmployee != null ? $"{masterEmployee?.Eid}-{masterEmployee?.Name}" : string.Empty;
//item.MasterName = masterEmployee?.Name;
item.SlaveEid = slaveEmployee?.Eid;
item.SlaveEidStr = slaveEmployee != null ? $"{slaveEmployee?.Eid}-{slaveEmployee?.Name}" : string.Empty;
//item.SlaveName = slaveEmployee?.Name;
}
}
return new PageResult<ScreenRecordResponse>(postData.PageIndex, postData.PageSize, total, result);
}
}
}