771 lines
46 KiB
Java
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.

package com.zhgd.xmgl.task;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhgd.xmgl.constant.Cts;
import com.zhgd.xmgl.modules.basicdata.entity.Notice;
import com.zhgd.xmgl.modules.basicdata.entity.SystemUser;
import com.zhgd.xmgl.modules.basicdata.mapper.SystemLogoConfigMapper;
import com.zhgd.xmgl.modules.basicdata.mapper.SystemUserMapper;
import com.zhgd.xmgl.modules.basicdata.service.INoticeService;
import com.zhgd.xmgl.modules.basicdata.service.ISystemUserService;
import com.zhgd.xmgl.modules.dangerous.entity.HiddenDangerInspectRecord;
import com.zhgd.xmgl.modules.dangerous.service.IHiddenDangerInspectRecordService;
import com.zhgd.xmgl.modules.project.entity.Project;
import com.zhgd.xmgl.modules.project.entity.ProjectExternalSystemService;
import com.zhgd.xmgl.modules.project.mapper.ProjectExternalSystemServiceMapper;
import com.zhgd.xmgl.modules.project.mapper.ProjectMapper;
import com.zhgd.xmgl.modules.project.service.IProjectUfaceConfigService;
import com.zhgd.xmgl.modules.worker.entity.WorkerAttendance;
import com.zhgd.xmgl.modules.worker.entity.WorkerBlacklist;
import com.zhgd.xmgl.modules.worker.entity.WorkerInfo;
import com.zhgd.xmgl.modules.worker.entity.WorkerMonthAttendanceStatistics;
import com.zhgd.xmgl.modules.worker.mapper.WorkerBlacklistMapper;
import com.zhgd.xmgl.modules.worker.mapper.WorkerCertificateMapper;
import com.zhgd.xmgl.modules.worker.mapper.WorkerInfoMapper;
import com.zhgd.xmgl.modules.worker.service.IWorkerAttendancePresenceService;
import com.zhgd.xmgl.modules.worker.service.IWorkerAttendanceService;
import com.zhgd.xmgl.modules.worker.service.IWorkerCertificateService;
import com.zhgd.xmgl.modules.worker.service.IWorkerMonthAttendanceStatisticsService;
import com.zhgd.xmgl.modules.worker.service.impl.WorkerInfoServiceImpl;
import com.zhgd.xmgl.modules.xz.entity.XzCertificateExpireAlarmRecord;
import com.zhgd.xmgl.modules.xz.entity.XzWorkerSafeWatchAlarm;
import com.zhgd.xmgl.modules.xz.entity.XzWorkerSafeWatchConfig;
import com.zhgd.xmgl.modules.xz.entity.XzWorkerSafeWatchManager;
import com.zhgd.xmgl.modules.xz.security.entity.XzSecurityInspectTaskItemRecord;
import com.zhgd.xmgl.modules.xz.security.entity.XzSecurityInspectTaskRecord;
import com.zhgd.xmgl.modules.xz.security.entity.XzSecurityQualityInspectionRecord;
import com.zhgd.xmgl.modules.xz.security.enums.XzSecurityQualityInspectionRecordStatusEnum;
import com.zhgd.xmgl.modules.xz.security.mapper.XzSecurityInspectTaskItemRecordMapper;
import com.zhgd.xmgl.modules.xz.security.mapper.XzSecurityInspectTaskRecordMapper;
import com.zhgd.xmgl.modules.xz.security.mapper.XzSecurityQualityInspectionRecordMapper;
import com.zhgd.xmgl.modules.xz.security.service.IXzSecurityQualityInspectionRecordService;
import com.zhgd.xmgl.modules.xz.service.IXzWorkerSafeWatchAlarmService;
import com.zhgd.xmgl.modules.xz.service.IXzWorkerSafeWatchConfigService;
import com.zhgd.xmgl.modules.xz.service.IXzWorkerSafeWatchManagerService;
import com.zhgd.xmgl.modules.xz.service.impl.XzCertificateExpireAlarmRecordServiceImpl;
import com.zhgd.xmgl.util.ElecardUtil;
import com.zhgd.xmgl.util.MapBuilder;
import com.zhgd.xmgl.util.NumberUtils;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.core.SchedulerLock;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @program: wisdomSite
* @description: 劳务人员定时任务
* @author: Mr.Peng
* @create: 2021-09-10 17:31
**/
@Slf4j
@Component
@RestController
@RequestMapping("xmgl/task")
public class WorkerTask {
@Autowired
WorkerInfoServiceImpl workerInfoService;
@Autowired
ISystemUserService systemUserService;
@Autowired
private ProjectExternalSystemServiceMapper projectExternalSystemServiceMapper;
@Autowired
private WorkerInfoMapper workerInfoMapper;
@Autowired
private ProjectMapper projectMapper;
@Autowired
private SystemUserMapper systemUserMapper;
@Autowired
private SystemLogoConfigMapper systemLogoConfigMapper;
@Autowired
private IWorkerAttendancePresenceService workerAttendancePresenceService;
@Autowired
private INoticeService noticeService;
@Autowired
private IProjectUfaceConfigService projectUfaceConfigService;
@Autowired
private IWorkerCertificateService workerCertificateService;
@Autowired
private WorkerCertificateMapper workerCertificateMapper;
@Autowired
private WorkerBlacklistMapper workerBlacklistMapper;
@Autowired
private XzCertificateExpireAlarmRecordServiceImpl xzCertificateExpireAlarmRecordService;
@Autowired
private IXzWorkerSafeWatchConfigService xzWorkerSafeWatchConfigService;
@Autowired
private IWorkerMonthAttendanceStatisticsService workerMonthAttendanceStatisticsService;
@Autowired
private XzSecurityQualityInspectionRecordMapper xzSecurityQualityInspectionRecordMapper;
@Autowired
private IWorkerAttendanceService workerAttendanceService;
@Autowired
private IXzWorkerSafeWatchAlarmService xzWorkerSafeWatchAlarmService;
@Autowired
private IXzWorkerSafeWatchManagerService xzWorkerSafeWatchManagerService;
@Autowired
private XzSecurityInspectTaskRecordMapper xzSecurityInspectTaskRecordMapper;
@Autowired
private XzSecurityInspectTaskItemRecordMapper xzSecurityInspectTaskItemRecordMapper;
@Autowired
private IHiddenDangerInspectRecordService hiddenDangerInspectRecordService;
@Autowired
private IXzSecurityQualityInspectionRecordService qualityInspectionRecordService;
/**
* 定时修改用户码状态
*/
@SchedulerLock(name = "updateWorkerCode", lockAtMostFor = 1000 * 60 * 60, lockAtLeastFor = 1000 * 60 * 5)
@Scheduled(cron = "0 0 1 * * ?")
public void updateWorkerCode() {
log.info("------------------------------健康码同步开始-----------------------------------------");
try {
List<ProjectExternalSystemService> list = projectExternalSystemServiceMapper.getChangtongCodeSystemList();
Date now = new Date();
if (list != null && list.size() > 0) {
for (ProjectExternalSystemService projectExternalSystemService : list) {
getWorkerCodeState(projectExternalSystemService, now);
}
}
} catch (Exception e) {
log.error("error", e);
}
log.info("------------------------------健康码同步结束-----------------------------------------");
}
public void getWorkerCodeState(ProjectExternalSystemService projectExternalSystemService, Date now) {
try {
List<WorkerInfo> list = workerInfoMapper.selectAllWorkerInfoList(projectExternalSystemService.getProjectSn());
if (list != null && list.size() > 0) {
String token = ElecardUtil.getToken(projectExternalSystemService.getSystemUrl(), projectExternalSystemService.getAppId(), projectExternalSystemService.getAppSecert(),
projectExternalSystemService.getPublicKey(), projectExternalSystemService.getPrivateKey());
if (StringUtils.isNotEmpty(token)) {
int redNum = 0;
int orangeNum = 0;
StringBuilder redWorkerName = new StringBuilder();
StringBuilder orangeWorkerName = new StringBuilder();
for (WorkerInfo workerInfo : list) {
log.info("---------------" + workerInfo.getWorkerName());
String enterStatus = ElecardUtil.getCardInfo(projectExternalSystemService.getSystemUrl(), projectExternalSystemService.getAppId(), projectExternalSystemService.getAppSecert(),
projectExternalSystemService.getPublicKey(), projectExternalSystemService.getPrivateKey(), token, workerInfo.getIdCard());
//0 :绿色, 1:黄色 ,2:红色
//红黄码需设备取消授权不允许通行
if (StringUtils.isNotEmpty(enterStatus)) {
if ("2".equals(enterStatus)) {
redNum++;
if (redWorkerName.length() > 0) {
redWorkerName.append("");
}
redWorkerName.append(workerInfo.getWorkerName());
//码状态0无码1红2黄3绿
workerInfo.setCodeState(1);
workerInfo.setUfaceDevId("0");
projectUfaceConfigService.deleteWorkerInfo(workerInfo);
} else if ("1".equals(enterStatus)) {
orangeNum++;
if (orangeWorkerName.length() > 0) {
orangeWorkerName.append("");
}
orangeWorkerName.append(workerInfo.getWorkerName());
workerInfo.setCodeState(2);
workerInfo.setUfaceDevId("0");
projectUfaceConfigService.deleteWorkerInfo(workerInfo);
} else {
workerInfo.setCodeState(3);
}
workerInfoMapper.updateById(workerInfo);
}
}
if (redNum > 0 || orangeNum > 0) {
//String title="今日昌通码核验黄码"+orangeNum+"人,红码"+redNum+"人";
StringBuilder title = new StringBuilder();
title.append("今日昌通码核验黄码").append(orangeNum).append("人,");
if (orangeWorkerName.length() > 0) {
title.append("分别为:").append(orangeWorkerName.toString()).append("");
}
title.append("红码").append(redNum).append("");
if (redWorkerName.length() > 0) {
title.append("分别为:").append(redWorkerName.toString()).append("");
}
StringBuilder title2 = new StringBuilder();
if (orangeWorkerName.length() > 0) {
title2.append("黄码人员:").append(orangeWorkerName.toString()).append("");
}
if (redWorkerName.length() > 0) {
title2.append("红码人员:").append(redWorkerName.toString()).append("");
}
title2.append("已销权");
noticeService.sendProjectNotice(projectExternalSystemService.getProjectSn(), "红黄码核验", title.toString(), "17");
noticeService.sendProjectNotice(projectExternalSystemService.getProjectSn(), "红黄码销权", title2.toString(), "17");
}
}
}
} catch (Exception e) {
log.error("error", e);
}
}
/**
* 劳务人员,截止日期到期的话(包括人员的合同信息、资格证书、保险信息),就自动填充原因,就是证件到期到黑名单中
*/
@SchedulerLock(name = "addWorkerBlacklist", lockAtMostFor = 1000 * 60 * 60, lockAtLeastFor = 1000 * 60 * 5)
@Scheduled(cron = "0 0 1 * * ?")
@RequestMapping("/addWorkerBlacklist")
public void addWorkerBlacklist() {
log.info("任务开始,劳务人员,截止日期到期的话(包括人员的合同信息、资格证书、保险信息),到黑名单");
//证书过期
List<WorkerInfo> list = workerCertificateMapper.selectExpiredCertificateNotInBlackWorkerList();
addWorkerBlacklist(list, "证件已到期", 1);
//合同过期
List<WorkerInfo> list1 = workerCertificateMapper.selectExpiredContractNotInBlackWorkerList();
addWorkerBlacklist(list1, "合同已到期", 2);
//保险过期
List<WorkerInfo> list2 = workerCertificateMapper.selectExpiredInsuranceNotInBlackWorkerList();
addWorkerBlacklist(list2, "保险已到期", 3);
}
private void addWorkerBlacklist(List<WorkerInfo> list, String addReason, int type) {
for (WorkerInfo workerInfo : list) {
WorkerBlacklist black = new WorkerBlacklist();
black.setProjectSn(workerInfo.getProjectSn());
black.setWorkerId(workerInfo.getId());
black.setWorkerName(workerInfo.getWorkerName());
black.setIdCard(workerInfo.getIdCard());
black.setAddReason(addReason);
black.setReason(addReason);
black.setAddTime(DateUtil.now());
black.setCreateTime(new Date());
black.setType(type);
workerBlacklistMapper.insert(black);
}
}
/**
* 对人员的资质证书到期进行预警,预警后发送消息提示项目管理员账号
* 系统设置:开启后将会在对企业下的各个项目开启人员证书到期预警提示,提前预警时间可以自行设置,监测内容包括人员的合同信息、资格证书、保险信息。
*/
@SchedulerLock(name = "alarmWorkerBlack", lockAtMostFor = 1000 * 60 * 60, lockAtLeastFor = 1000 * 60 * 5)
@Scheduled(cron = "0 0 1 * * ?")
@RequestMapping("/alarmWorkerBlack")
public void alarmWorkerBlack() {
log.info("任务开始,对人员的合同信息、资格证书、保险信息到期进行预警,预警后发送消息提示项目管理员账号");
//资格证书预警
List<WorkerInfo> list1 = workerCertificateMapper.getAlarmWorkerCertificateWithHeadquarter();
List<WorkerInfo> list2 = workerCertificateMapper.getAlarmWorkerCertificateNotHeadquarter();
addNotice(CollUtil.addAll(list1, list2), "人员的资质证书即将到期", "20");
//合同信息预警
List<WorkerInfo> list3 = workerCertificateMapper.getAlarmWorkerContractWithHeadquarter();
List<WorkerInfo> list4 = workerCertificateMapper.getAlarmWorkerContractNotHeadquarter();
addNotice(CollUtil.addAll(list3, list4), "人员的合同信息即将到期", "21");
//保险信息预警
List<WorkerInfo> list5 = workerCertificateMapper.getAlarmWorkerInsuranceWithHeadquarter();
List<WorkerInfo> list6 = workerCertificateMapper.getAlarmWorkerInsuranceNotHeadquarter();
addNotice(CollUtil.addAll(list5, list6), "人员的保险信息即将到期", "22");
}
/**
* 对人员未履职情况进行监测,及时反馈预警
*/
@Scheduled(cron = "0 0/1 * * * ?")
@RequestMapping("/workerSafeWatchAlarm")
public void workerSafeWatchAlarm() {
log.info("开始执行对人员未履职情况进行监测");
String[] typeName = {"人员日常考勤", "每个项目自检任务", "一个月内缺勤超过", "一个月内迟到超过"};
List<Project> projects = projectMapper.selectList(Wrappers.<Project>lambdaQuery().eq(Project::getStatus, 2));
for (Project project : projects) {
if (project.getEnableWorkerSafeWatch() != null && project.getEnableWorkerSafeWatch() == 1) {
List<SystemUser> projectLevelUsers = systemUserService.getSystemUsersBySn(new MapBuilder<String, Object>().put(Cts.SN, project.getProjectSn()).put(Cts.QUERY_TYPE, Cts.PROJECT_LEVEL).build());
List<XzWorkerSafeWatchConfig> list = xzWorkerSafeWatchConfigService.list(Wrappers.<XzWorkerSafeWatchConfig>lambdaQuery()
.eq(XzWorkerSafeWatchConfig::getProjectSn, project.getProjectSn()));
//安全履职预警增加规则
this.workerSafeWatchAlarmForHidden(list, project, projectLevelUsers);
if (StringUtils.isNotBlank(project.getWorkerSafeWatchTime()) &&
DateUtil.format(new Date(), "HH:mm").equals(
DateUtil.format(DateUtil.parseTime(project.getWorkerSafeWatchTime()), "HH:mm"))) {
List<XzWorkerSafeWatchAlarm> alarmList = new ArrayList<>();
List<Notice> noticeList = new ArrayList<>();
for (XzWorkerSafeWatchConfig xzWorkerSafeWatchConfig : list) {
String type = xzWorkerSafeWatchConfig.getType();
int dayNum = xzWorkerSafeWatchConfig.getDayNum();
Set<String> personSn = new HashSet<>();
//计算日常考勤规则
if ("1".equals(type)) {
dayNum = dayNum + 1;
DateTime dateTime = DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), -dayNum));
Set<String> personSet = workerAttendanceService.list(Wrappers.<WorkerAttendance>lambdaQuery().between(WorkerAttendance::getCreateTime
, dateTime, new Date())).stream().map(w -> w.getPersonSn()).collect(Collectors.toSet());
Set<String> workerSet = workerInfoMapper.selectList(Wrappers.<WorkerInfo>lambdaQuery().eq(WorkerInfo::getInserviceType, 1)).stream().map(w -> w.getPersonSn()).collect(Collectors.toSet());
workerSet.removeAll(personSet);
personSn.addAll(workerSet);
}
if ("2".equals(type)) {
dayNum = dayNum + 1;
List<XzSecurityInspectTaskRecord> xzSecurityInspectTaskRecords = xzSecurityInspectTaskRecordMapper.selectList(Wrappers.<XzSecurityInspectTaskRecord>lambdaQuery()
.eq(XzSecurityInspectTaskRecord::getSn, project.getProjectSn())
.le(XzSecurityInspectTaskRecord::getEndTime, DateUtil.offsetDay(new Date(), -dayNum)));
if (xzSecurityInspectTaskRecords.size() > 0) {
List<XzSecurityInspectTaskItemRecord> itemList = xzSecurityInspectTaskItemRecordMapper.selectList(Wrappers.<XzSecurityInspectTaskItemRecord>lambdaQuery()
.in(XzSecurityInspectTaskItemRecord::getTaskId, xzSecurityInspectTaskRecords.stream().map(s -> s.getId()).collect(Collectors.toList())));
if (itemList != null && itemList.size() > 0) {
for (XzSecurityInspectTaskItemRecord data : itemList) {
XzSecurityInspectTaskRecord xzSecurityInspectTaskRecord = xzSecurityInspectTaskRecords.stream().filter(s -> s.getId().toString().equals(data.getTaskId().toString())).collect(Collectors.toList()).get(0);
SystemUser systemUser = systemUserMapper.selectById(xzSecurityInspectTaskRecord.getInspectUser());
WorkerInfo workerInfo = workerInfoMapper.selectById(systemUser.getWorkerId());
if (workerInfo != null) {
int count = 0;
if (xzSecurityInspectTaskRecord.getType() == 1) {
count = hiddenDangerInspectRecordService.count(Wrappers.<HiddenDangerInspectRecord>lambdaQuery()
.eq(HiddenDangerInspectRecord::getInspectHiddenDangerItemRecordId, data.getId()));
} else {
count = qualityInspectionRecordService.count(Wrappers.<XzSecurityQualityInspectionRecord>lambdaQuery()
.eq(XzSecurityQualityInspectionRecord::getItemId, data.getId()));
}
if (count == 0) {
personSn.add(workerInfo.getPersonSn());
}
}
}
}
}
}
if ("3".equals(type)) {
List<WorkerMonthAttendanceStatistics> list1 = workerMonthAttendanceStatisticsService.list(Wrappers.<WorkerMonthAttendanceStatistics>
lambdaQuery().eq(WorkerMonthAttendanceStatistics::getProjectSn, project.getProjectSn())
.eq(WorkerMonthAttendanceStatistics::getQueryTime, DateUtil.format(new Date(), "yyyy-MM")));
Integer number = 0;
for (WorkerMonthAttendanceStatistics workerMonthAttendanceStatistics : list1) {
JSONObject obj = JSONObject.parseObject(JSON.toJSONString(workerMonthAttendanceStatistics));
Iterator<String> iterator = obj.keySet().iterator();
while (iterator.hasNext()) {
String value = obj.getString(iterator.next());
if ("0".equals(value)) {
number++;
}
if (number > dayNum) {
personSn.add(workerMonthAttendanceStatistics.getPersonSn());
break;
}
}
}
}
if ("4".equals(type)) {
List<WorkerMonthAttendanceStatistics> list1 = workerMonthAttendanceStatisticsService.list(Wrappers.<WorkerMonthAttendanceStatistics>
lambdaQuery().eq(WorkerMonthAttendanceStatistics::getProjectSn, project.getProjectSn())
.eq(WorkerMonthAttendanceStatistics::getQueryTime, DateUtil.format(new Date(), "yyyy-MM")));
Integer number = 0;
for (WorkerMonthAttendanceStatistics workerMonthAttendanceStatistics : list1) {
JSONObject obj = JSONObject.parseObject(JSON.toJSONString(workerMonthAttendanceStatistics));
Iterator<String> iterator = obj.keySet().iterator();
while (iterator.hasNext()) {
String value = obj.getString(iterator.next());
if ("2".equals(value)) {
number++;
}
if (number > dayNum) {
personSn.add(workerMonthAttendanceStatistics.getPersonSn());
break;
}
}
}
}
if ("5".equals(type)) {
List<XzSecurityQualityInspectionRecord> xzSecurityQualityInspectionRecords = xzSecurityQualityInspectionRecordMapper.selectList(Wrappers.<XzSecurityQualityInspectionRecord>lambdaQuery()
.eq(XzSecurityQualityInspectionRecord::getRecordType, 1)
.orderByDesc(XzSecurityQualityInspectionRecord::getCreateTime));
Set<String> ids = new HashSet<>();
for (XzSecurityQualityInspectionRecord xzSecurityQualityInspectionRecord : xzSecurityQualityInspectionRecords) {
String id = xzSecurityQualityInspectionRecord.getEnterpriseSn() + "-" + xzSecurityQualityInspectionRecord.getDangerItemId();
ids.add(id);
}
for (String id : ids) {
String[] split = id.split("-");
List<XzSecurityQualityInspectionRecord> collect = xzSecurityQualityInspectionRecords.stream().filter(x -> x.getEnterpriseSn().equals(split[0]) && x.getDangerItemId().toString().equals(split[1])).collect(Collectors.toList());
if (collect.size() >= dayNum) {
XzSecurityQualityInspectionRecord xzSecurityQualityInspectionRecord = collect.get(0);
SystemUser systemUser = systemUserMapper.selectById(xzSecurityQualityInspectionRecord.getChangeId());
Map<String, Object> requestParam = new HashMap<>(16);
requestParam.put("id", systemUser.getWorkerId());
requestParam.put("inserviceType", 1);
List<WorkerInfo> workerInfoList = workerInfoMapper.selectWorkerInfoList(new Page<>(-1, -1), requestParam);
if (workerInfoList.size() > 0) {
WorkerInfo workerInfo = workerInfoList.get(0);
XzWorkerSafeWatchAlarm xzWorkerSafeWatchAlarm = new XzWorkerSafeWatchAlarm();
xzWorkerSafeWatchAlarm.setWorkerId(workerInfo.getId());
xzWorkerSafeWatchAlarm.setWorkerName(workerInfo.getWorkerName());
String deptName = workerInfo.getDepartmentName() == null ? "" : workerInfo.getDepartmentName();
String teamName = workerInfo.getTeamName() == null ? "" : workerInfo.getTeamName();
xzWorkerSafeWatchAlarm.setDeptName(deptName + teamName);
xzWorkerSafeWatchAlarm.setEnterpriseName(workerInfo.getEnterpriseName());
xzWorkerSafeWatchAlarm.setAlarmTime(new Date());
xzWorkerSafeWatchAlarm.setType(type.toString());
xzWorkerSafeWatchAlarm.setProjectSn(xzSecurityQualityInspectionRecord.getProjectSn());
xzWorkerSafeWatchAlarm.setDayNum(dayNum);
xzWorkerSafeWatchAlarm.setEnterpriseId(workerInfo.getEnterpriseId());
alarmList.add(xzWorkerSafeWatchAlarm);
List<String> userIds = Arrays.asList(xzWorkerSafeWatchManagerService.list(Wrappers.<XzWorkerSafeWatchManager>lambdaQuery()
.eq(XzWorkerSafeWatchManager::getWatchConfigId, xzWorkerSafeWatchConfig.getId()))
.stream().map(l -> l.getUserId()).collect(Collectors.joining(",")).split(","));
for (String userId : userIds) {
if (StringUtils.isNotBlank(userId)) {
Notice notice = new Notice();
notice.setType("35");
notice.setMsg(StrUtil.format("事件:【{}】已连续被检查到【{}】{}次,请提醒责任人【{}】及时整改并加强对此风险的管控措施",
workerInfo.getEnterpriseName(), xzSecurityQualityInspectionRecord.getDangerItemContent(),
collect.size(), workerInfo.getWorkerName()));
notice.setTitle("人员安全履职预警提醒");
notice.setIsRead(0);
notice.setAccountId(Long.valueOf(userId));
notice.setSendTime(DateUtil.formatDateTime(new Date()));
noticeList.add(notice);
}
}
}
}
}
}
if (personSn.size() > 0) {
Map<String, Object> requestParam = new HashMap<>(16);
requestParam.put("personSns", personSn);
requestParam.put("inserviceType", 1);
List<WorkerInfo> workerInfoList = workerInfoMapper.selectWorkerInfoList(new Page<>(-1, -1), requestParam);
for (WorkerInfo workerInfo : workerInfoList) {
if (workerInfo.getEnterpriseId() == null) {
continue;
}
XzWorkerSafeWatchAlarm xzWorkerSafeWatchAlarm = new XzWorkerSafeWatchAlarm();
xzWorkerSafeWatchAlarm.setWorkerId(workerInfo.getId());
xzWorkerSafeWatchAlarm.setWorkerName(workerInfo.getWorkerName());
String deptName = workerInfo.getDepartmentName() == null ? "" : workerInfo.getDepartmentName();
String teamName = workerInfo.getTeamName() == null ? "" : workerInfo.getTeamName();
xzWorkerSafeWatchAlarm.setDeptName(deptName + teamName);
xzWorkerSafeWatchAlarm.setEnterpriseName(workerInfo.getEnterpriseName());
xzWorkerSafeWatchAlarm.setAlarmTime(new Date());
xzWorkerSafeWatchAlarm.setType(type);
xzWorkerSafeWatchAlarm.setProjectSn(project.getProjectSn());
xzWorkerSafeWatchAlarm.setDayNum(dayNum);
xzWorkerSafeWatchAlarm.setEnterpriseId(workerInfo.getEnterpriseId());
alarmList.add(xzWorkerSafeWatchAlarm);
Long id = xzWorkerSafeWatchConfig.getId();
List<String> userIds = Arrays.asList(xzWorkerSafeWatchManagerService.list(Wrappers.<XzWorkerSafeWatchManager>lambdaQuery()
.eq(XzWorkerSafeWatchManager::getWatchConfigId, id))
.stream().map(l -> l.getUserId()).collect(Collectors.joining(",")).split(","));
userIds = userIds.stream().filter(s -> StringUtils.isNotBlank(s)).collect(Collectors.toList());
String msg = StrUtil.format("事件:{},人员名称:{},身份证号:{}",
typeName[Integer.parseInt(type) - 1] + dayNum + "天未履职", workerInfo.getWorkerName(), workerInfo.getIdCard());
for (String userId : userIds) {
Notice notice = new Notice();
notice.setType("35");
notice.setMsg(msg);
notice.setTitle("人员安全履职预警提醒");
notice.setIsRead(0);
notice.setAccountId(Long.valueOf(userId));
notice.setSendTime(DateUtil.formatDateTime(new Date()));
noticeList.add(notice);
}
if (CollUtil.isNotEmpty(userIds)) {
String frameUserNames = systemUserService.getFrameUserNames(StrUtil.join(",", userIds))+ msg;
for (SystemUser user : projectLevelUsers) {
Notice notice = new Notice();
notice.setType("35");
notice.setMsg(frameUserNames);
notice.setTitle("人员安全履职预警提醒");
notice.setIsRead(0);
notice.setAccountId(user.getUserId());
notice.setSendTime(DateUtil.formatDateTime(new Date()));
noticeList.add(notice);
}
}
}
}
}
xzWorkerSafeWatchAlarmService.saveBatch(alarmList);
noticeService.saveBatch(noticeList);
}
}
}
}
private void workerSafeWatchAlarmForHidden(List<XzWorkerSafeWatchConfig> list, Project project, List<SystemUser> projectLevelUsers) {
//(定制)安全履职预警增加规则,一、二、三、四级安全隐患预警规则,分级对未整改、未复查、未核验超过多少时间进行循环通知。
List<XzWorkerSafeWatchAlarm> alarmList = new ArrayList<>();
List<Notice> noticeList = new ArrayList<>();
Map<String, XzWorkerSafeWatchConfig> typeToConfigMap = list.stream().collect(Collectors.toMap(XzWorkerSafeWatchConfig::getType, Function.identity()));
List<XzSecurityQualityInspectionRecord> records = xzSecurityQualityInspectionRecordMapper.getWorkerSafeWatchTimeOutList(project.getProjectSn());
if (CollUtil.isEmpty(records)) {
return;
}
Map<Long, HashSet<String>> configIdToNoticeUserIdsMap = xzWorkerSafeWatchManagerService.list().stream().collect(Collectors.toMap(XzWorkerSafeWatchManager::getWatchConfigId, e -> {
return new HashSet<>(StrUtil.split(e.getUserId(), ","));
}, (e, e2) -> {
e.addAll(e2);
return e;
}));
for (XzSecurityQualityInspectionRecord record : records) {
Integer level = record.getLevel();
Integer status = record.getStatus();
Integer minute = getMinute(level, status, typeToConfigMap);
if (minute == null) {
continue;
}
XzWorkerSafeWatchConfig config = getXzWorkerSafeWatchConfig(level, status, typeToConfigMap);
if (config == null) {
continue;
}
Long alarmUserId = getAlarmWorkerId(record);
Integer safeWatchAlarmExceedMinute = record.getSafeWatchAlarmExceedMinute();
if (safeWatchAlarmExceedMinute >= minute) {
Integer mod = NumberUtils.mod(safeWatchAlarmExceedMinute, minute);
if (mod == 0) {
HashSet<String> noticeUserIds = configIdToNoticeUserIdsMap.get(config.getId());
String now = DateUtil.now();
if (CollUtil.isNotEmpty(noticeUserIds)) {
String msg = StrUtil.format("有一条{}{}安全隐患预警,设置的预警时间是{}分钟,现在已超时{}分钟", getLevelName(level), getStatusName(status), minute, safeWatchAlarmExceedMinute);
for (String userId : noticeUserIds) {
//报警
Notice notice = new Notice();
notice.setType("35");
notice.setMsg(msg);
notice.setAccountId(Long.valueOf(userId));
notice.setTitle("安全隐患预警");
notice.setSendTime(now);
noticeList.add(notice);
}
String frameUserNames = systemUserService.getFrameUserNames(StrUtil.join(",", noticeUserIds))+ msg;
for (SystemUser user : projectLevelUsers) {
Notice notice = new Notice();
notice.setType("35");
notice.setMsg(frameUserNames);
notice.setTitle("安全隐患预警");
notice.setAccountId(user.getUserId());
notice.setSendTime(now);
noticeList.add(notice);
}
}
XzWorkerSafeWatchAlarm alarm = new XzWorkerSafeWatchAlarm();
alarm.setType(getSafeType(level, status));
alarm.setDayNum(safeWatchAlarmExceedMinute);
alarm.setAlarmTime(DateUtil.parse(now));
alarm.setUserId(alarmUserId);
alarm.setProjectSn(project.getProjectSn());
alarmList.add(alarm);
}
}
}
Set<Long> userIdSet = alarmList.stream().map(XzWorkerSafeWatchAlarm::getUserId).collect(Collectors.toSet());
if (CollUtil.isNotEmpty(userIdSet)) {
List<SystemUser> users = systemUserService.list(new LambdaQueryWrapper<SystemUser>().in(SystemUser::getUserId, userIdSet));
Map<Long, List<SystemUser>> userIdMap = users.stream().collect(Collectors.groupingBy(SystemUser::getUserId));
Set<Long> workerIds = users.stream().map(SystemUser::getWorkerId).collect(Collectors.toSet());
List<WorkerInfo> workerInfoList = new ArrayList<>(workerInfoService.list(new LambdaQueryWrapper<WorkerInfo>().in(WorkerInfo::getId, workerIds)));
Map<Long, WorkerInfo> workerIdMap = workerInfoList.stream().collect(Collectors.toMap(WorkerInfo::getId, workerInfo -> workerInfo));
for (XzWorkerSafeWatchAlarm alarm : alarmList) {
List<SystemUser> userList = userIdMap.get(alarm.getUserId());
if (CollUtil.isNotEmpty(userList)) {
for (SystemUser su : userList) {
WorkerInfo workerInfo = workerIdMap.get(su.getWorkerId());
if (workerInfo != null) {
alarm.setWorkerId(workerInfo.getId());
alarm.setWorkerName(workerInfo.getWorkerName());
alarm.setEnterpriseId(workerInfo.getEnterpriseId());
alarm.setEnterpriseName(workerInfo.getEnterpriseName());
alarm.setDeptName(Objects.equals(workerInfo.getPersonType(), 1) ? workerInfo.getTeamName() : workerInfo.getDepartmentName());
}
}
}
}
}
if (CollUtil.isNotEmpty(alarmList)) {
xzWorkerSafeWatchAlarmService.saveBatch(alarmList);
}
if (CollUtil.isNotEmpty(noticeList)) {
noticeService.saveBatch(noticeList);
}
}
private Long getAlarmWorkerId(XzSecurityQualityInspectionRecord record) {
if (Objects.equals(record.getStatus(), XzSecurityQualityInspectionRecordStatusEnum.NOT_RECTIFIED.getValue())) {
return record.getChangeId();
} else if (Objects.equals(record.getStatus(), XzSecurityQualityInspectionRecordStatusEnum.NOT_REVIEWED.getValue())) {
return record.getReviewId();
} else {
return record.getVerifyManId();
}
}
private String getStatusName(Integer status) {
if (status == null) {
return "";
}
switch (status) {
case 2:
return "未整改";
case 3:
return "未复查";
case 4:
return "未核验";
default:
return "";
}
}
private String getLevelName(Integer level) {
if (level == null) {
return "";
}
switch (level) {
case 1:
return "一级";
case 2:
return "二级";
case 3:
return "三级";
case 4:
return "四级";
default:
return "";
}
}
private Integer getMinute(Integer level, Integer status, Map<String, XzWorkerSafeWatchConfig> typeMap) {
if (level == null || status == null) {
return Integer.MAX_VALUE;
}
//6一级未整改、7一级未复查、8一级未核验、9二级未整改、10二级未复查、11二级未核验、12三级未整改、13三级未复查、14三级未核验、15四级未整改、16四级未复查、17四级未核验
//1一级2二级3三级4四级
//2待整改3待复查4待核验5合格
XzWorkerSafeWatchConfig config = getXzWorkerSafeWatchConfig(level, status, typeMap);
if (config == null) {
return null;
}
return config.getDayNum();
}
@Nullable
private XzWorkerSafeWatchConfig getXzWorkerSafeWatchConfig(Integer level, Integer status, Map<String, XzWorkerSafeWatchConfig> typeMap) {
XzWorkerSafeWatchConfig config = null;
config = typeMap.get(getSafeType(level, status));
return config;
}
private String getSafeType(Integer level, Integer status) {
if (level == 1 && status == 2) {
return "6";
} else if (level == 1 && status == 3) {
return "7";
} else if (level == 1 && status == 4) {
return "8";
} else if (level == 2 && status == 2) {
return "9";
} else if (level == 2 && status == 3) {
return "10";
} else if (level == 2 && status == 4) {
return "11";
} else if (level == 3 && status == 2) {
return "12";
} else if (level == 3 && status == 3) {
return "13";
} else if (level == 3 && status == 4) {
return "14";
} else if (level == 4 && status == 2) {
return "15";
} else if (level == 4 && status == 3) {
return "16";
} else if (level == 4 && status == 4) {
return "17";
}
return "";
}
/**
* 添加通知
*
* @param list
* @param title 标题
* @param type
*/
private void addNotice(Collection<WorkerInfo> list, String title, String type) {
Map<String, List<WorkerInfo>> projectMap = list.stream().collect(Collectors.groupingBy(WorkerInfo::getProjectSn));
if (CollUtil.isNotEmpty(projectMap)) {
for (Map.Entry<String, List<WorkerInfo>> entry : projectMap.entrySet()) {
List<SystemUser> systemUserList = systemUserMapper.selectList(new LambdaQueryWrapper<SystemUser>()
.eq(SystemUser::getSn, entry.getKey()));
List<WorkerInfo> workerInfoList = entry.getValue();
if (CollUtil.isNotEmpty(workerInfoList)) {
for (WorkerInfo workerInfo : workerInfoList) {
String certificateTypeName = workerInfo.getCertificateTypeName();
XzCertificateExpireAlarmRecord xzCertificateExpireAlarmRecord = new XzCertificateExpireAlarmRecord();
xzCertificateExpireAlarmRecord.setPersonTypeName(getPersonTypeName(workerInfo.getPersonType()));
xzCertificateExpireAlarmRecord.setEnterpriseName(workerInfo.getEnterpriseName());
xzCertificateExpireAlarmRecord.setTeamDepartmentName(StringUtils.isNotBlank(workerInfo.getDepartmentName()) ? workerInfo.getDepartmentName() : workerInfo.getTeamName());
xzCertificateExpireAlarmRecord.setWorkerName(workerInfo.getWorkerName());
xzCertificateExpireAlarmRecord.setCertificateTypeName(certificateTypeName);
xzCertificateExpireAlarmRecord.setCertificateEndDate(DateUtil.parseDate(workerInfo.getEffectTime()));
xzCertificateExpireAlarmRecord.setAlarmPromptTime(new Date());
xzCertificateExpireAlarmRecord.setProjectSn(workerInfo.getProjectSn());
xzCertificateExpireAlarmRecordService.add(xzCertificateExpireAlarmRecord);
if (CollUtil.isNotEmpty(systemUserList)) {
for (SystemUser systemUser : systemUserList) {
noticeService.addUserNotice(systemUser.getUserId(), title, workerInfo.getWorkerName() + "" + certificateTypeName + "即将到期,请尽快更新!", type);
}
}
}
}
}
}
}
private String getPersonTypeName(Integer personType) {
if (personType == 1) {
return "劳务人员";
} else if (personType == 2) {
return "管理人员";
} else if (personType == 3) {
return "临时人员";
}
return null;
}
/**
* 劳务在场零点清空功能
*/
@Scheduled(cron = "0 10 0 * * ?")
@SchedulerLock(name = "updateWorkerAttendanceZero", lockAtMostFor = 1000 * 60 * 30, lockAtLeastFor = 1000 * 60 * 3)
@RequestMapping("updateWorkerAttendanceZero")
public void updateWorkerAttendanceZero() {
List<Project> projects = projectMapper.selectList(new LambdaQueryWrapper<Project>()
.eq(Project::getEnableWorkerAttendanceZero, 1));
for (Project project : projects) {
String projectSn = project.getProjectSn();
workerAttendancePresenceService.addNowAllWorkerAttendancePresence(projectSn);
}
}
}