using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using AutoMapper;
|
using SqlSugar;
|
using WIDESEAWCS_Core;
|
using WIDESEAWCS_Core.BaseRepository;
|
using WIDESEAWCS_Core.BaseServices;
|
using WIDESEAWCS_ITelescopicService;
|
using WIDESEAWCS_Model.Models;
|
using WIDESEAWCS_DTO;
|
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
|
using WIDESEAWCS_DTO.Telescopic;
|
using SqlSugar.DistributedSystem.Snowflake;
|
using System.IO;
|
using System.Net;
|
|
namespace WIDESEAWCS_TelescopicService
|
{
|
public class MaintenanceService : ServiceBase<Dt_Maintenance, IRepository<Dt_Maintenance>>, IMaintenanceService
|
{
|
public IRepository<Dt_Maintenance> Repository => BaseDal;
|
public readonly IRepository<Dt_MaintenanceTeam> _team;
|
private readonly IRepository<Dt_CustomIPaddress> _ipaddress;
|
private readonly IRepository<Sys_User> _user;
|
|
|
|
public MaintenanceService(IRepository<Dt_CustomIPaddress>ipaddress,IRepository<Dt_Maintenance> BaseDal, IRepository<Sys_User> user, IRepository<Dt_MaintenanceTeam> team) : base(BaseDal)
|
{
|
_user = user;
|
_team = team;
|
_ipaddress = ipaddress;
|
}
|
|
|
|
/// <summary>
|
/// 设置检修权限
|
/// </summary>
|
/// <param name="pagination"></param>
|
/// <returns></returns>
|
public WebResponseContent ShowMaintence(PaginationDTO pagination)
|
{
|
try
|
{
|
int totalCount = 0;
|
var sys = _user.Db.Queryable<Sys_User>().Where(x=>x.Enable==1);
|
var main = Db.Queryable<Dt_Maintenance>();
|
|
var query = sys.InnerJoin<Dt_Maintenance>((a, b) => a.UserName == b.UserAccount);
|
|
// 筛选搜索关键字(无论admin还是普通用户都适用)
|
if (!string.IsNullOrEmpty(pagination.searchKeyword))
|
{
|
query = query.Where((a, b) =>
|
a.UserTrueName.Contains(pagination.searchKeyword) ||
|
a.UserName.Contains(pagination.searchKeyword) ||
|
a.CardNumber.ToString().Contains(pagination.searchKeyword) ||
|
a.Userteam.Contains(pagination.searchKeyword)
|
);
|
}
|
|
// 如果不是管理员,根据部门过滤
|
if (pagination.account != "admin")
|
{
|
var currentUser = _user.Db.Queryable<Sys_User>()
|
.Where(u => u.UserName == pagination.account)
|
.First();
|
|
if (currentUser == null)
|
{
|
return new WebResponseContent { Status = false, Data = "未获取到用户信息" };
|
}
|
|
int? deptId = currentUser.Dept_Id;
|
|
query = query.Where((a, b) => a.Dept_Id == deptId);
|
}
|
// 执行分页查询
|
var result = query.Select((a, b) => new
|
{
|
b.ID,
|
a.UserTrueName,
|
a.UserName,
|
a.CardNumber,
|
a.Userteam,
|
a.HeadImageUrl,
|
b.IsPossible
|
}).ToPageList(pagination.pageIndex, pagination.pageSize, ref totalCount);
|
|
return new WebResponseContent
|
{
|
Status = true,
|
Data = new
|
{
|
TotalCount = totalCount,
|
PageIndex = pagination.pageIndex,
|
PageSize = pagination.pageSize,
|
Items = result
|
}
|
};
|
}
|
catch (Exception ex)
|
{
|
return new WebResponseContent { Status = false, Data = ex.Message };
|
}
|
}
|
|
|
|
|
|
/// <summary>
|
/// 人员监控(数字大屏)
|
/// </summary>
|
/// <param name="pageIndex"></param>
|
/// <param name="pageSize"></param>
|
/// <param name="searchKeyword"></param>
|
/// <returns></returns>
|
//public WebResponseContent PersonnelMonitoring(PaginationDTO pagination)
|
//{
|
// try
|
// {
|
// int totalCount = 0;
|
// var sys = _user.Db.Queryable<Sys_User>();
|
|
|
// var query = sys.InnerJoin<Dt_Maintenance>((a, b) => a.UserName == b.UserAccount)
|
// .RightJoin<Dt_CustomIPaddress>((a,b,c)=>b.IPAddress==c.IPaddress)
|
// .Where((a, b) => b.MaintenanceStatus==1); // 展示运行检修的
|
|
// // 搜索关键字
|
// if (!string.IsNullOrEmpty(pagination.searchKeyword))
|
// {
|
// query = query.Where((a, b) =>
|
// a.UserTrueName.Contains(pagination.searchKeyword) ||
|
// a.Userteam.Contains(pagination.searchKeyword));
|
// }
|
|
// var result = query
|
// .Select((a, b,c) => new
|
// {
|
// b.UserAccount,//账号
|
// a.UserTrueName,
|
// a.Userteam,//班组
|
// a.Unit,//单位
|
// a.RoleName,//角色名称
|
// //b.IPAddress,//站台
|
// IPAddress = c.Addressname ?? b.IPAddress, // 优先显示自定义名称
|
// b.ID,//id
|
// b.MaintenanceStatus,//检修状态
|
// b.CreateDate,//日期
|
// b.MaintenancStartTime,//开始时间
|
// })
|
// .ToPageList(pagination.pageIndex, pagination.pageSize, ref totalCount);
|
|
// return new WebResponseContent
|
// {
|
// Status = true,
|
// Data = new
|
// {
|
// TotalCount = totalCount,
|
// PageIndex = pagination.pageIndex,
|
// PageSize = pagination.pageSize,
|
// Items = result
|
// }
|
// };
|
// }
|
// catch (Exception ex)
|
// {
|
// return new WebResponseContent { Status = false, Data = ex.Message };
|
// }
|
//}
|
public WebResponseContent PersonnelMonitoring(PaginationDTO pagination)
|
{
|
try
|
{
|
int totalCount = 0;
|
|
var query = _user.Db.Queryable<Dt_Maintenance>()
|
.Where(b => b.MaintenanceStatus == 1)
|
.InnerJoin<Sys_User>((b, a) => b.UserAccount == a.UserName)
|
.LeftJoin<Dt_CustomIPaddress>((b, a, c) => b.IPAddress == c.IPaddress);
|
|
if (!string.IsNullOrEmpty(pagination.searchKeyword))
|
{
|
query = query.Where((b, a, c) =>
|
a.UserTrueName.Contains(pagination.searchKeyword) ||
|
a.Userteam.Contains(pagination.searchKeyword));
|
}
|
|
var result = query.Select((b, a, c) => new
|
{
|
b.UserAccount,
|
a.UserTrueName,
|
a.Userteam,
|
a.Unit,
|
a.RoleName,
|
IPAddress = c.Addressname ?? b.IPAddress ?? "无IP",
|
b.ID,
|
b.MaintenanceStatus,
|
b.CreateDate,
|
b.MaintenancStartTime
|
}).ToPageList(pagination.pageIndex, pagination.pageSize, ref totalCount);
|
|
return new WebResponseContent
|
{
|
Status = true,
|
Data = new { totalCount, pagination.pageIndex, pagination.pageSize, result }
|
};
|
}
|
catch (Exception ex)
|
{
|
return new WebResponseContent { Status = false, Message = ex.Message };
|
}
|
}
|
|
|
|
|
|
/// <summary>
|
/// 更新状态强制下线(添加了检修记录
|
/// </summary>
|
/// <param name="id">要强制谁下线</param>
|
/// <param name="account">登入人员账号</param>
|
/// <param name="LocalIP">地址ip</param>
|
/// <returns></returns>
|
public WebResponseContent RunOperation(int id, string account, string LocalIP)
|
{
|
try
|
{
|
// 1. 查找目标用户(要被强制下线的人)
|
var maint = BaseDal.QueryData().FirstOrDefault(x => x.ID == id);
|
if (maint==null)
|
{
|
return new WebResponseContent { Status = false, Message = "没有找到该用户" };
|
}
|
var sysuer = _user.QueryData(x => x.UserName == maint.UserAccount).FirstOrDefault();//user表
|
|
|
|
// 2. 查找当前登录用户(执行操作的人)
|
var currentUser = _user.QueryData(x => x.UserName == account).FirstOrDefault();
|
if (currentUser == null)
|
{
|
return new WebResponseContent { Status = false, Message = "当前登录用户无效" };
|
}
|
|
// 3. 权限检查:当前用户是 admin 或 当前用户是组长(IsLeader == 1)
|
bool isAdmin = account == "admin";
|
bool isLeader = currentUser.IsLeader == 1; // 假设 Sys_User 表有 IsLeader 字段
|
|
if (!isAdmin && !isLeader)
|
{
|
return new WebResponseContent { Status = false, Message = "你没有权限强制下线" };
|
}
|
|
// 4. 执行强制下线
|
maint.MaintenanceStatus = 0; // 改变状态为下线
|
BaseDal.UpdateData(maint);
|
|
var ip = LocalIP?.Replace("::ffff:", "") ?? ""; // 如果 LocalIP 是 null,ip 会是 ""
|
|
// 5. 记录操作日志
|
var recording = new Dt_MaintenanceTeam
|
{
|
Account = maint.UserAccount,
|
MaintenanceStatus = 0,
|
OperatorName = sysuer.UserTrueName, // 操作人姓名
|
TeamName = sysuer.RoleName, // 操作人角色
|
DistributionTime = DateTime.Now,
|
IPAddress = ip,
|
};
|
_team.AddData(recording);
|
|
return new WebResponseContent { Status = true, Message = "强制下线成功" };
|
}
|
catch (Exception ex)
|
{
|
return new WebResponseContent { Status = false, Message = "失败:" + ex.Message };
|
}
|
}
|
|
|
/// <summary>
|
/// 更改状态(暂时没有用到)
|
/// </summary>
|
/// <param name="id"></param>
|
/// <returns></returns>
|
public WebResponseContent ChangeTasState(int id)
|
{
|
try
|
{
|
var change = BaseDal.QueryFirst(x => x.ID == id);
|
//将数据库中的int 值转为 枚举型
|
if (!Enum.IsDefined(typeof(Maint), change.MaintenanceStatus))
|
{
|
return new WebResponseContent { Status = false, Message = "当前状态不合法,无法更新" };
|
}
|
|
Maint Status = (Maint)change.MaintenanceStatus; //int ->枚举
|
|
switch (Status)
|
{
|
case Maint.待开始:
|
change.MaintenanceStatus = (int)Maint.检修中;
|
change.MaintenancStartTime = DateTime.Now;
|
break;
|
case Maint.检修中:
|
change.MaintenanceStatus = (int)Maint.已完成;
|
break;
|
case Maint.已完成:
|
change.MaintenancEendTime = DateTime.Now;
|
return new WebResponseContent { Status = false, Message = "当前任务已完成" };
|
default:
|
return new WebResponseContent { Status = false, Message = "更新失败" };
|
}
|
return new WebResponseContent { Status = true, Data = change };
|
}
|
catch (Exception ex)
|
{
|
|
return new WebResponseContent { Status = false, Message = "错误:" + ex };
|
}
|
|
|
}
|
|
/// <summary>
|
/// 检修操作记录
|
/// </summary>
|
/// <param name="pageIndex">页数</param>
|
/// <param name="pageSize">一页多少个数据</param>
|
/// <returns></returns>
|
public WebResponseContent MaintenanceOperationRecord(PaginationDTO pagination)
|
{
|
try
|
{
|
int totalCount = 0;
|
var sys = _user.Db.Queryable<Sys_User>();
|
var main = Db.Queryable<Dt_Maintenance>();
|
|
//模糊查询
|
var query = sys.InnerJoin<Dt_Maintenance>((a, b) => a.UserName == b.UserAccount);
|
if (!string.IsNullOrEmpty(pagination.searchKeyword))
|
{
|
query = query.Where((a, b) =>
|
a.UserTrueName.Contains(pagination.searchKeyword)||
|
a.Userteam.Contains(pagination.searchKeyword)||
|
a.HeadImageUrl.Contains(pagination.searchKeyword)
|
);
|
}
|
//时间查询
|
if (pagination.startDate.HasValue && pagination.endDate.HasValue)
|
{
|
query = query.Where((a, b) => b.MaintenancStartTime >= pagination.startDate.Value && b.MaintenancStartTime <= pagination.endDate.Value);
|
}
|
//排序
|
if (!string.IsNullOrEmpty(pagination.sortField))
|
{
|
var isAcs = pagination.sortOrder?.ToLower() == "asc";
|
query = pagination.sortField.ToLower() switch
|
{
|
|
|
"maintenancstarttime" => isAcs ? query.OrderBy((a, b) => b.MaintenancStartTime) : query.OrderByDescending((a, b) => b.MaintenancStartTime),
|
|
"maintenancenendtime" => isAcs ? query.OrderBy((a, b) => b.MaintenancEendTime) : query.OrderByDescending((a, b) => b.MaintenancEendTime),
|
|
_ => query.OrderByDescending((a, b) => b.Creater) // 默认按创建时间降序
|
};
|
|
}
|
else
|
{
|
query=query.OrderByDescending((a, b) => b.Creater);
|
}
|
|
|
//返回结果
|
var result = query.Select((a, b) => new
|
{
|
a.UserTrueName,
|
a.Userteam,
|
a.HeadImageUrl,
|
a.Dept_Id,
|
b.MaintenancStartTime,
|
b.MaintenancEendTime,
|
}).ToPageList(pagination.pageIndex, pagination.pageSize, ref totalCount);
|
|
return new WebResponseContent
|
{
|
Status = true,
|
Data = new
|
{
|
TotalCount = totalCount,//总数
|
PageIndex = pagination.pageIndex,//页数
|
PageSize = pagination.pageSize,//一页多少个数据
|
Items = result
|
}
|
};
|
}
|
catch (Exception ex)
|
{
|
|
return new WebResponseContent { Status = false, Message = "错误信息:" + ex.Message };
|
}
|
}
|
|
|
/// <summary>
|
/// 查看今天的检修任务(运行检修隐藏了,这个接口也用不道)
|
/// </summary>
|
/// <param name="id"></param>
|
/// <returns></returns>
|
public WebResponseContent MaintenanceTasksOfTheDay(string account)
|
{
|
try
|
{
|
var today = DateTime.Today; // 今天的零点
|
var tomorrow = today.AddDays(1); // 明天的零点
|
|
var query = _user.Db.Queryable<Sys_User>()
|
.InnerJoin<Dt_Maintenance>((a, b) => a.UserName == b.UserAccount)
|
.Where((a, b) => b.IsPossible == "true" && b.UserAccount == account &&
|
b.MaintenanceDate >= today && b.MaintenanceDate < tomorrow).Select((a, b) => new
|
{
|
b.ID,
|
a.UserTrueName,
|
a.UserName,
|
a.CardNumber,
|
a.Userteam,
|
a.HeadImageUrl,
|
b.IsPossible,
|
b.MaintenanceDate,
|
b.MaintenancStartTime,
|
b.MaintenancEendTime,
|
}).ToList();
|
if (query == null)
|
{
|
return new WebResponseContent { Status = false, Message = "你今天没有检修任务" };
|
}
|
|
|
|
return new WebResponseContent { Status = true, Data = query };
|
}
|
catch (Exception ex)
|
{
|
|
return new WebResponseContent { Status = false, Message = "错误信息:" + ex.Message };
|
}
|
|
}
|
|
/// <summary>
|
/// 看今天的检修任务(运行检修隐藏了,这个接口也用不道)
|
/// </summary>
|
/// <param name="id"></param>
|
/// <returns></returns>
|
//public WebResponseContent MaintenanceTasksOfTheDay(string account)
|
//{
|
// try
|
// {
|
|
// var query = _user.Db.Queryable<Sys_User>()
|
// .InnerJoin<Dt_Maintenance>((a, b) => a.UserName == b.UserAccount)
|
// .Where((a,b) => b.UserAccount == account ).Select((a, b) => new
|
// {
|
// b.ID,
|
// a.UserTrueName,
|
// a.UserName,
|
// a.CardNumber,
|
// a.Userteam,
|
// a.HeadImageUrl,
|
// b.IsPossible,
|
// b.MaintenanceStatus,
|
// b.MaintenanceDate,
|
// b.MaintenancStartTime,
|
// b.MaintenancEendTime,
|
// }).ToList();
|
// if (query == null)
|
// {
|
// return new WebResponseContent { Status = false, Message = "你今天没有检修任务" };
|
// }
|
|
|
|
// return new WebResponseContent { Status = true, Data = query };
|
// }
|
// catch (Exception ex)
|
// {
|
|
// return new WebResponseContent { Status = false, Message = "错误信息:" + ex.Message };
|
// }
|
|
//}
|
|
|
|
/// <summary>
|
/// 开始检修
|
/// </summary>
|
/// <param name="account">账号</param>
|
/// <param name="LocalIP">那台电脑登入的ip地址</param>
|
/// <returns></returns>
|
public WebResponseContent StartMaintenceTask(string account, string LocalIP)
|
{
|
try
|
{
|
var user = BaseDal.QueryData();//检修表
|
var maint = user.Where(x => x.UserAccount == account).FirstOrDefault(); //在检修表中先找到该用户
|
var sysuer = _user.QueryData(x => x.UserName == account).FirstOrDefault();//在用户表中找到用户
|
|
if (maint == null || sysuer == null)
|
{
|
return new WebResponseContent { Status = false, Message = "没有找到该用户" };
|
}
|
// 查找该用户在 Dt_MaintenanceTeam 表中的最新一条记录
|
var lastRecord = _team.QueryData(x => x.Account == account)
|
.OrderByDescending(x => x.CreateDate)
|
.FirstOrDefault();
|
|
if (lastRecord != null && lastRecord.MaintenanceStatus == 1)
|
{
|
return new WebResponseContent { Status = false, Message = "你已经在开始检修了,请不要重复点击" };
|
}
|
|
//先要找到该用户属于那个角色的,这个角色中属于那个什么类型(组长/组员)
|
if (maint.IsLeader != 1)//如果该用户不是班长,那么要等班长先做,他才可以做
|
{
|
bool leaderStarted = user.Any(x => x.IsLeader == 1 && x.MaintenanceStatus == 1); //否有满足条件 找到一条就返回true MaintenanceStatus(0没有开始,1开始)
|
if (!leaderStarted)
|
{
|
return new WebResponseContent { Status = false, Message = "请等待一级人员开始检修" };
|
}
|
|
}
|
|
var ip = LocalIP?.Replace("::ffff:", "") ?? ""; // 如果 LocalIP 是 null,ip 会是 ""
|
//如果满足下面的条件就开始
|
maint.MaintenancStartTime = DateTime.Now;//记录开始时间
|
maint.MaintenanceStatus = 1;//更改状态
|
maint.IPAddress = ip;//更改ip地址
|
BaseDal.UpdateData(maint);
|
var address = _ipaddress.QueryData(x=>x.IPaddress== ip).FirstOrDefault();
|
var addressIp = "";
|
if (address!=null) //找到了
|
{
|
addressIp = address.Addressname;
|
}
|
else
|
{
|
addressIp = ip;
|
}
|
//插入记录表
|
var recording = new Dt_MaintenanceTeam //添加记录
|
{
|
Account = account,
|
MaintenanceStatus=1,
|
OperatorName = sysuer.UserTrueName,
|
TeamName = sysuer.Userteam,
|
DistributionTime = DateTime.Now,
|
IPAddress = addressIp,
|
};
|
_team.AddData(recording);
|
|
return new WebResponseContent { Status = true, Data = maint, Message = "开始检修" };
|
}
|
catch (Exception ex)
|
{
|
return new WebResponseContent { Status = false, Message = "错误信息:" + ex.Message };
|
}
|
}
|
|
/// <summary>
|
/// 结束检修
|
/// </summary>
|
/// <param name="account"></param>
|
/// <returns></returns>
|
public WebResponseContent StopMaintenanceTask(string account,string LocalIP)
|
{
|
try
|
{
|
var user = BaseDal.QueryData(); // 检修表
|
var maint = user.Where(x => x.UserAccount == account).FirstOrDefault();
|
var sysuer = _user.QueryData(x => x.UserName == account).FirstOrDefault(); // 用户表
|
|
if (maint == null || sysuer == null)
|
{
|
return new WebResponseContent { Status = false, Message = "没有找到该用户" };
|
}
|
|
// 查找该用户在 _team 表中的最新一条记录
|
var lastRecord = _team.QueryData(x => x.Account == account)
|
.OrderByDescending(x => x.CreateDate)
|
.FirstOrDefault();
|
|
if (lastRecord == null || lastRecord.MaintenanceStatus != 1)
|
{
|
return new WebResponseContent { Status = false, Message = "你还没有开始检修,请先点击开始检修" };
|
}
|
|
// 检查是否至少有一个组长在场
|
var activeLeaders = user.Where(x => x.IsLeader == 1 && x.MaintenanceStatus == 1).ToList();
|
|
// 如果当前用户是组长
|
if (maint.IsLeader == 1)
|
{
|
// 如果是最后一个在场的组长
|
if (activeLeaders.Count == 1 && activeLeaders[0].UserAccount == account)
|
{
|
// 检查是否还有未完成的组员
|
var unfinishedMembers = user.Where(x => x.IsLeader == 0 && x.MaintenanceStatus == 1).Any();
|
if (unfinishedMembers)
|
{
|
return new WebResponseContent
|
{
|
Status = false,
|
Message = "您是最后一个在场的一级人员,请等待所有二级人员完成检修后再结束"
|
};
|
}
|
}
|
}
|
//if (LocalIP!=maint.IPAddress)
|
//{
|
// return new WebResponseContent { Status = false, Message = "操作设备不同,请回到原本设备再点击结束检修" };
|
//}
|
var ip = LocalIP?.Replace("::ffff:", "") ?? ""; // 如果 LocalIP 是 null,ip 会是 ""
|
var address = _ipaddress.QueryData(x => x.IPaddress == ip).FirstOrDefault();
|
var addressIp = "";
|
if (address != null) //找到了
|
{
|
addressIp = address.Addressname;
|
}
|
else
|
{
|
addressIp = ip;
|
}
|
|
// 更新当前用户的检修状态
|
maint.MaintenancEendTime = DateTime.Now; // 记录结束时间
|
maint.MaintenanceStatus = 0; // 更改状态
|
BaseDal.UpdateData(maint);
|
|
// 插入记录表
|
var recording = new Dt_MaintenanceTeam // 添加记录
|
{
|
Account = account,
|
MaintenanceStatus = 0,
|
OperatorName = sysuer.UserTrueName,
|
TeamName = sysuer.Userteam,
|
IPAddress = addressIp,
|
EndTime = DateTime.Now,
|
};
|
_team.AddData(recording);
|
|
return new WebResponseContent { Status = true, Data = maint, Message = "完成检修" };
|
}
|
catch (Exception ex)
|
{
|
return new WebResponseContent { Status = false, Message = "错误信息:" + ex.Message };
|
}
|
}
|
|
|
/// <summary>
|
/// 展示开始检修人员
|
/// </summary>
|
/// <returns></returns>
|
public WebResponseContent YShowStartTake()
|
{
|
try
|
{
|
var reslut=BaseDal.QueryData(x => x.MaintenanceStatus == 1).ToList();
|
if (reslut.Count<=0)
|
{
|
return new WebResponseContent { Status = false, Message = "还没有人开始检修" };
|
}
|
return new WebResponseContent { Status = true, Data = reslut,Message="检修人员有以下" };
|
}
|
catch (Exception ex)
|
{
|
|
return new WebResponseContent { Status = false, Message = ex.Message };
|
}
|
}
|
|
/// <summary>
|
/// 状态枚举
|
/// </summary>
|
private enum Maint
|
{
|
//待检修=0,
|
待开始=0,
|
检修中=1,
|
已完成=2
|
|
}
|
|
}
|
}
|