package com.gkhy.system.service.impl;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.common.constant.Constants;
import com.gkhy.common.core.domain.entity.SysDept;
import com.gkhy.common.exception.ServiceException;
import com.gkhy.common.utils.SecurityUtils;
import com.gkhy.system.domain.DailySafetyInspection;
import com.gkhy.system.domain.DailySafetyInspectionDept;
import com.gkhy.system.domain.DailySafetyInspectionUser;
import com.gkhy.system.mapper.DailySafetyInspectionDeptMapper;
import com.gkhy.system.mapper.DailySafetyInspectionMapper;
import com.gkhy.system.mapper.DailySafetyInspectionUserMapper;
import com.gkhy.system.mapper.SysDeptMapper;
import com.gkhy.system.service.DailySafetyInspectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;
/**
*
* 日常安全检查主表 服务实现类
*
*
* @author hh
* @since 2025-09-08 10:36:52
*/
@Service
public class DailySafetyInspectionServiceImpl extends ServiceImpl implements DailySafetyInspectionService {
@Autowired
private DailySafetyInspectionMapper dailySafetyInspectionMapper;
@Autowired
private DailySafetyInspectionUserMapper dailySafetyInspectionUserMapper;
@Autowired
private DailySafetyInspectionDeptMapper dailySafetyInspectionDeptMapper;
@Autowired
private SysDeptMapper sysDeptMapper;
@Override
public List selectDailySafetyInspectionList(DailySafetyInspection inspection) {
boolean admin = SecurityUtils.isAdmin(SecurityUtils.getUserId());
if (!admin){
boolean sysAdmin = SecurityUtils.hasRole(Constants.SYS_ADMIN);
boolean placeSafetyOfficer = SecurityUtils.hasRole(Constants.PLACE_SAFETY_OFFICER);
if (!(sysAdmin || placeSafetyOfficer)) {
inspection.setCreateById(SecurityUtils.getUserId());
}
}
return dailySafetyInspectionMapper.getDailySafetyInspectionList(inspection);
}
@Override
@Transactional(rollbackFor = Exception.class)
public int saveDailySafetyInspection(DailySafetyInspection inspection) {
// List dailySafetyInspectionUsers = inspection.getDailySafetyInspectionUsers();
// if (ObjectUtil.isEmpty(dailySafetyInspectionUsers)) {
// throw new ServiceException("参检人员不能为空");
// }
// Long researchGroup = inspection.getResearchGroup();
// SysDept sysDept = sysDeptMapper.selectDeptById(researchGroup);
// if (sysDept == null) {
// throw new ServiceException("部门不存在");
// }
// if (!"1".equals(sysDept.getSafety())){
// throw new ServiceException("所选部门未开启安全检查");
// }
int i = 0;
if (inspection.getId() == null) {
inspection.setCreateTime(LocalDateTime.now());
inspection.setCreateBy(SecurityUtils.getUsername());
inspection.setCreateById(SecurityUtils.getUserId());
i = dailySafetyInspectionMapper.insert(inspection);
} else {
inspection.setUpdateTime(LocalDateTime.now());
inspection.setUpdateBy(SecurityUtils.getUsername());
inspection.setUpdateById(SecurityUtils.getUserId());
i = dailySafetyInspectionMapper.updateById(inspection);
}
if (i > 0){
saveDailySafetyInspectionDept(inspection.getDailySafetyInspectionDepts(),inspection.getId());
}
// if (i > 0) {
// saveDailySafetyInspectionUser(dailySafetyInspectionUsers, inspection.getId());
// }
return i;
}
private void saveDailySafetyInspectionUser(List dailySafetyInspectionUsers, Long id) {
DailySafetyInspectionUser dailySafetyInspectionUser = new DailySafetyInspectionUser();
dailySafetyInspectionUser.setDailySafetyInspectionId(id);
dailySafetyInspectionUserMapper.deleteById(dailySafetyInspectionUser);
dailySafetyInspectionUsers.forEach(user -> {
user.setDailySafetyInspectionId(id);
dailySafetyInspectionUserMapper.insert(user);
});
}
private void saveDailySafetyInspectionDept(List dailySafetyInspectionDepts, Long id) {
DailySafetyInspectionDept dept = new DailySafetyInspectionDept();
dept.setDailySafetyInspectionId(id);
dailySafetyInspectionDeptMapper.deleteById(dept);
dailySafetyInspectionDepts.forEach(dept1 -> {
dept1.setDailySafetyInspectionId(id);
dailySafetyInspectionDeptMapper.insert(dept1);
});
}
@Override
public int deleteDailySafetyInspection(Long id) {
return dailySafetyInspectionMapper.update(new DailySafetyInspection(),
new LambdaUpdateWrapper().eq(DailySafetyInspection::getId, id)
.set(DailySafetyInspection::getDelFlag, Constants.FAIL).set(DailySafetyInspection::getUpdateTime, LocalDateTime.now())
.set(DailySafetyInspection::getUpdateBy, SecurityUtils.getUsername()).set(DailySafetyInspection::getUpdateById, SecurityUtils.getUserId()));
}
@Override
public int getCheckCount() {
Long deptId = SecurityUtils.getDeptId();
SysDept sysDept = sysDeptMapper.selectDeptById(deptId);
if (sysDept == null){
return 1;
}else {
if (!"1".equals(sysDept.getSafety())){
return 1;
}
}
return dailySafetyInspectionMapper.getCheckCount(deptId);
}
private void checkPer(){
boolean admin = SecurityUtils.isAdmin(SecurityUtils.getUserId());
if (!admin){
boolean sysAdmin = SecurityUtils.hasRole(Constants.SYS_ADMIN);
if (!sysAdmin){
throw new RuntimeException("没有权限");
}
boolean placeSafetyOfficer = SecurityUtils.hasRole(Constants.PLACE_SAFETY_OFFICER);
if (!placeSafetyOfficer){
throw new RuntimeException("没有权限");
}
}
}
}