【开发】特种设备(三):自动更新下次检验时间后,同步更新定时推送记录
目前,特种设备的推送规则是,每隔30天,7天,当天进行推送,一共三次
只要下次检验时间自动更新,就应该同步触发更新定时推送记录的操作。
推送规则:1:A部门全员推送。2:特种设备归属部门下的特定角色推送
因此自动更新定时推送,和更新下次检验时间的触发机制是相同的。
该步骤的触发有四个机制:
- 新增特种设备
- 更新特种设备
- 特种设备延期验收。
- 新增特种设备验收
这里只写第一个机制的代码,其他机制的触发大同小异。
触发机制
- 新增特种设备
@Autowired
private SpecEquipmentMapper specEquipmentMapper;
@Autowired
private SpecPushMapper specPushMapper;
@Autowired
private SysDeptMapper sysDeptMapper;
@Autowired
private SysUserRoleMapper userRoleMapper;
@Autowired
private SysUserMapper sysUserMapper;
@Override
@Transactional
public int insertSpecEquipment(SpecEquipment specEquipment){
// 获取下次检验时间
specEquipment = checkNextInspectionDate(specEquipment);
specEquipment.setCreateTime(DateUtils.getNowDate());
specEquipment.setCreateBy(SecurityUtils.getRealName());
int rows = specEquipmentMapper.insertSpecEquipment(specEquipment);
// 新增推送日志(30,7,当天共三条记录)
updateAndInsertSpecPush(specEquipment);
return rows;
}
触发自动更新定时推送
/**
* 新增日志推送
* 推送规则:提前30天,7天,当天各推送一次,共三次
* @param specEquipment 特种设备对象
*/
private void updateAndInsertSpecPush(SpecEquipment specEquipment) {
//新增前删除旧记录,避免重复推送
specPushMapper.deleteSpecPushBySpecId(specEquipment.getId());
SysDept sysDept = sysDeptMapper.selectDeptById(specEquipment.getDeptId());
Date nextInspectionDate = specEquipment.getNextInspectionDate();
Date zeroTime = DateUtils.getZeroTime();
Date pushTime = null;
List<SpecPush> specPushList = new ArrayList();
for (int i = 0; i < 3; i++) {
SpecPush sp = new SpecPush();
if(i == 0){
// 更新提前30天推送的时间
pushTime = DateUtils.plusDay(nextInspectionDate,-30);
// pushTime推送时间早于或等于当天凌晨,说明推送过期,该记录作废
if(earlierThan(pushTime,zeroTime)) continue;
sp.setForward(30L);
}else if(i==1){
// 更新提前7天推送的时间
pushTime = DateUtils.plusDay(nextInspectionDate,-7);
if(earlierThan(pushTime,zeroTime)) continue;
sp.setForward(7L);
}else if(i==2){
// 更新提当天推送的时间
pushTime = nextInspectionDate;
if(earlierThan(pushTime,zeroTime)) continue;
sp.setForward(0L);
}
sp.setPushTime(pushTime);
sp.setSpecId(specEquipment.getId());
sp.setSpecName(specEquipment.getSpecName());
sp.setDeptId(specEquipment.getDeptId());
sp.setDeptName(sysDept.getDeptName());
// 环安部所用用户都要推送,本部门下角色为特种设备推送人的用户需要推送
sp = setReceiveManFromRole(sp);
sp.setCreateBy("system");
sp.setCreateTime(DateUtils.getNowDate());
specPushList.add(sp);
}
if(specPushList.size()>0){
specPushMapper.insertSpecPushBatch(specPushList);
}
}
获取推送人员的方法(A部门全部推送,本部门特定角色推送)
/**
* 环安部下所用用户都要推送,本部门下角色为特种设备推送人的用户需要推送
*/
private SpecPush setReceiveManFromRole(SpecPush sp) {
Long deptId = sp.getDeptId();
// 获取环安部所有用户
String HuanAnUser = sysUserMapper.selectUserNameString();
// 获取本部门下角色为推送人的用户
String RoleUser = userRoleMapper.selectSpecUserNameString(deptId);
StringBuilder sb = new StringBuilder(HuanAnUser);
if(StringUtils.isNotEmpty(RoleUser)){
sb.append(",").append(RoleUser);
}
sp.setReceiveMan(sb.toString());
return sp;
}
判断推送时间是否为有效时间(如果早于当前时间,则无效,不推送)
/**
* 如果firstTime早于或等于secondTime 返回true,否则返回false
*/
public boolean earlierThan(Date firstTime, Date secondTime){
if(firstTime.compareTo(secondTime) == -1 || firstTime.compareTo(secondTime) == 0){
return true;
}
return false;
}
SQL语句
批量插入定时推送
<insert id="insertSpecPushBatch">
insert into sys_user_post(spec_id, spec_name, dept_name, push_time, forward, receive_man, create_by, create_time,) values
<foreach item="item" index="index" collection="list" separator=",">
(#{item.specId},#{item.specName},#{item.deptName},#{item.pushTime},#{item.forward},#{item.receiveMan},#{item.createBy},#{item.createTime})
</foreach>
</insert>
根据权限标识查询对应的用户
<select id="selectUserNameString" resultType="java.lang.String">
select GROUP_CONCAT(c.user_name) as nameList from sys_user_role a
left join sys_role b on a.role_id = b.role_id
left join sys_user c on a.user_id = c.user_id
where b.role_key = 'envirsafety'
</select>
根据本部下下特定权限标识的用户
<select id="selectSpecUserNameString" parameterType="Long" resultType="java.lang.String">
select GROUP_CONCAT(b.user_name) as nameList
from sys_user_role a LEFT JOIN sys_user b on a.user_id = b.user_id
left join sys_role c on a.role_id = c.role_id
where b.status = '0'
and c.status = '0'
and b.dept_id = #{deptId}
and c.role_key = 'specpush'
</select>
查询A部门下的所有人员(这个部门目前还不存在,所以用A代替)
<select id="selectUserNameString" resultType="java.lang.String">
select GROUP_CONCAT(a.user_name) as nameList
from sys_user a LEFT JOIN sys_dept b on a.dept_id = b.dept_id
where a.status = '0' and b.status = '0' and b.dept_name like '%A部%'
</select>
查询特定角色下的所有用户
<select id="selectSpecUserNameString" parameterType="Long" resultType="java.lang.String">
select GROUP_CONCAT(b.user_name) as nameList
from sys_user_role a LEFT JOIN sys_user b on a.user_id = b.user_id
left join sys_role c on a.role_id = c.role_id
where b.status = '0'
and c.status = '0'
and b.dept_id = #{deptId}
and c.role_name like '%特种设备%'
</select>
这个查询用户的操作,是返回所有的用户,以,
进行分割,因此在前台,我是用String进行接收。
和A部门的所有用户同样以,
拼接,存入数据库。
后期扫描日志的时候,会遍历推送人,挨个进行推送。
后面我担心两个角色下的人员会有重复,所以额外写了个去重的方法:
/**
* String数组去重
*/
public static String cleanString(String [] arrStr) {
Stream<String> stream = Arrays.stream(arrStr);
List<String> list = stream.distinct().collect(Collectors.toList());
return StringUtils.join(list.toArray(), ',');
}