package cn.iocoder.yudao.module.hrm.controller.admin.certificate;
|
|
import cn.hutool.core.collection.CollUtil;
|
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
|
import cn.iocoder.yudao.module.hrm.controller.admin.certificate.vo.HrmCertificatePageReqVO;
|
import cn.iocoder.yudao.module.hrm.controller.admin.certificate.vo.HrmCertificateRespVO;
|
import cn.iocoder.yudao.module.hrm.controller.admin.certificate.vo.HrmCertificateSaveReqVO;
|
import cn.iocoder.yudao.module.hrm.dal.dataobject.certificate.HrmCertificateDO;
|
import cn.iocoder.yudao.module.hrm.dal.dataobject.employee.HrmEmployeeDO;
|
import cn.iocoder.yudao.module.hrm.service.certificate.HrmCertificateService;
|
import cn.iocoder.yudao.module.hrm.service.employee.HrmEmployeeService;
|
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
import jakarta.annotation.Resource;
|
import jakarta.validation.Valid;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.validation.annotation.Validated;
|
import org.springframework.web.bind.annotation.*;
|
|
import java.time.LocalDate;
|
import java.util.List;
|
import java.util.Map;
|
|
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
|
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
|
|
@Tag(name = "管理后台 - HRM 证书资质")
|
@RestController
|
@RequestMapping("/hrm/certificate")
|
@Validated
|
public class HrmCertificateController {
|
|
@Resource
|
private HrmCertificateService certificateService;
|
@Resource
|
private HrmEmployeeService employeeService;
|
|
@PostMapping("/create")
|
@Operation(summary = "创建证书资质")
|
@PreAuthorize("@ss.hasPermission('hrm:certificate:create')")
|
public CommonResult<Long> createCertificate(@Valid @RequestBody HrmCertificateSaveReqVO createReqVO) {
|
return success(certificateService.createCertificate(createReqVO));
|
}
|
|
@PutMapping("/update")
|
@Operation(summary = "更新证书资质")
|
@PreAuthorize("@ss.hasPermission('hrm:certificate:update')")
|
public CommonResult<Boolean> updateCertificate(@Valid @RequestBody HrmCertificateSaveReqVO updateReqVO) {
|
certificateService.updateCertificate(updateReqVO);
|
return success(true);
|
}
|
|
@DeleteMapping("/delete")
|
@Operation(summary = "删除证书资质")
|
@Parameter(name = "ids", description = "编号数组", required = true)
|
@PreAuthorize("@ss.hasPermission('hrm:certificate:delete')")
|
public CommonResult<Boolean> deleteCertificate(@RequestParam("ids") List<Long> ids) {
|
certificateService.deleteCertificate(ids);
|
return success(true);
|
}
|
|
@GetMapping("/get")
|
@Operation(summary = "获得证书资质")
|
@Parameter(name = "id", description = "编号", required = true, example = "1024")
|
@PreAuthorize("@ss.hasPermission('hrm:certificate:query')")
|
public CommonResult<HrmCertificateRespVO> getCertificate(@RequestParam("id") Long id) {
|
HrmCertificateDO certificate = certificateService.getCertificate(id);
|
return success(certificate != null ? buildRespVO(certificate) : null);
|
}
|
|
@GetMapping("/page")
|
@Operation(summary = "获得证书资质分页")
|
@PreAuthorize("@ss.hasPermission('hrm:certificate:query')")
|
public CommonResult<PageResult<HrmCertificateRespVO>> getCertificatePage(@Valid HrmCertificatePageReqVO pageReqVO) {
|
PageResult<HrmCertificateDO> pageResult = certificateService.getCertificatePage(pageReqVO);
|
if (CollUtil.isEmpty(pageResult.getList())) {
|
return success(PageResult.empty(pageResult.getTotal()));
|
}
|
Map<Long, HrmEmployeeDO> employeeMap = employeeService.getEmployeeList(
|
convertSet(pageResult.getList(), HrmCertificateDO::getEmployeeId)).stream()
|
.collect(java.util.stream.Collectors.toMap(HrmEmployeeDO::getId, e -> e, (a, b) -> a));
|
return success(BeanUtils.toBean(pageResult, HrmCertificateRespVO.class,
|
vo -> buildRespVOItem(vo, employeeMap.get(vo.getEmployeeId()))));
|
}
|
|
private HrmCertificateRespVO buildRespVO(HrmCertificateDO certificate) {
|
HrmCertificateRespVO vo = BeanUtils.toBean(certificate, HrmCertificateRespVO.class);
|
HrmEmployeeDO employee = employeeService.getEmployee(certificate.getEmployeeId());
|
buildRespVOItem(vo, employee);
|
return vo;
|
}
|
|
private void buildRespVOItem(HrmCertificateRespVO vo, HrmEmployeeDO employee) {
|
if (employee != null) {
|
vo.setEmployeeName(employee.getName());
|
}
|
vo.setCertTypeName(convertCertTypeName(vo.getCertType()));
|
vo.setExpireStatus(calcExpireStatus(vo.getExpireDate()));
|
}
|
|
private String convertCertTypeName(Integer certType) {
|
return switch (certType == null ? 0 : certType) {
|
case 1 -> "特种作业证书";
|
case 2 -> "从业资质";
|
case 3 -> "技能等级证书";
|
case 4 -> "安全培训合格证";
|
default -> "其他";
|
};
|
}
|
|
/**
|
* 到期状态:0=有效,1=即将到期(30天内),2=已过期
|
*/
|
private Integer calcExpireStatus(LocalDate expireDate) {
|
if (expireDate == null) {
|
return 0;
|
}
|
LocalDate today = LocalDate.now();
|
if (expireDate.isBefore(today)) {
|
return 2;
|
}
|
if (expireDate.isBefore(today.plusDays(30))) {
|
return 1;
|
}
|
return 0;
|
}
|
|
}
|