package com.gkhy.exam.system.service.impl; import cn.hutool.core.util.ObjectUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.gkhy.exam.common.api.CommonPage; import com.gkhy.exam.common.api.CommonResult; import com.gkhy.exam.common.utils.PageUtils; import com.gkhy.exam.common.utils.SecurityUtils; import com.gkhy.exam.system.domain.InternalAuditCheck; import com.gkhy.exam.system.domain.InternalAuditCheckCatalogue; import com.gkhy.exam.system.mapper.*; import com.gkhy.exam.system.service.InternalAuditCheckService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** *

* 内审检查 服务实现类 *

* * @author hh * @since 2025-07-10 15:11:50 */ @Service public class InternalAuditCheckServiceImpl extends ServiceImpl implements InternalAuditCheckService { @Autowired private InternalAuditCheckMapper internalAuditCheckMapper; @Autowired private InternalAuditCheckCatalogueMapper checkCatalogueMapper; @Autowired private InternalAuditCheckContentMapper checkContentMapper; @Autowired private SysUserMapper sysUserMapper; @Autowired private SysDeptMapper sysDeptMapper; @Override public CommonPage selectInternalAuditCheckList(Integer companyId,String year) { PageUtils.startPage(); List internalAuditChecks = internalAuditCheckMapper.selectInternalAuditCheckList(companyId, year); if (!CollectionUtils.isEmpty(internalAuditChecks)) { batchLoadCheckDetails(internalAuditChecks); } return CommonPage.restPage(internalAuditChecks); } private void batchLoadCheckDetails(List checks) { //收集所有检查ID List checkIds = checks.stream() .map(InternalAuditCheck::getId) .collect(Collectors.toList()); //批量查询所有目录 List allCatalogues = checkCatalogueMapper.selectByCheckIds(checkIds); if (CollectionUtils.isEmpty(allCatalogues)) { return; } //收集所有目录ID List catalogueIds = allCatalogues.stream() .map(InternalAuditCheckCatalogue::getId) .collect(Collectors.toList()); //按检查ID分组目录 Map> catalogueMap = allCatalogues.stream() .collect(Collectors.groupingBy(InternalAuditCheckCatalogue::getCheckId)); //组装数据 for (InternalAuditCheck check : checks) { List catalogues = catalogueMap.get(check.getId()); if (!CollectionUtils.isEmpty(catalogues)) { check.setCheckCatalogues(catalogues); } } } @Override @Transactional public CommonResult insertInternalAuditCheck(InternalAuditCheck internalAuditCheck) { Map stringObjectHashMap = new HashMap<>(); stringObjectHashMap.put("dept_id",internalAuditCheck.getDeptId()); stringObjectHashMap.put("year",internalAuditCheck.getYear()); stringObjectHashMap.put("company_id",internalAuditCheck.getCompanyId()); stringObjectHashMap.put("del_flag",0); List internalAuditChecks = internalAuditCheckMapper.selectByMap(stringObjectHashMap); if (!CollectionUtils.isEmpty(internalAuditChecks)){ return CommonResult.failed("当前部门存在,请勿重复添加"); } internalAuditCheck.setCreateBy(SecurityUtils.getUsername()); internalAuditCheck.setCreateTime(LocalDateTime.now()); int insert = internalAuditCheckMapper.insert(internalAuditCheck); if (insert <= 0) { return CommonResult.failed("新增内审检查失败"); } //处理目录和内容的插入 List checkCatalogues = internalAuditCheck.getCheckCatalogues(); if (CollectionUtils.isEmpty(checkCatalogues)) { return CommonResult.success(); } //批量处理目录和内容 for (InternalAuditCheckCatalogue catalogue : checkCatalogues) { catalogue.setCheckId(internalAuditCheck.getId()); } // 批量插入目录 int catalogueInsertCount = checkCatalogueMapper.insertBatch(checkCatalogues); if (catalogueInsertCount != checkCatalogues.size()) { throw new RuntimeException("插入目录记录数量不匹配"); } return CommonResult.success(); } @Override public CommonResult updateInternalAuditCheck(InternalAuditCheck internalAuditCheck) { internalAuditCheck.setUpdateBy(SecurityUtils.getUsername()); internalAuditCheck.setUpdateTime(LocalDateTime.now()); int update = internalAuditCheckMapper.updateById(internalAuditCheck); if (update>0){ List checkCatalogues = internalAuditCheck.getCheckCatalogues(); if (CollectionUtils.isEmpty(checkCatalogues)) { return CommonResult.success(); } for (InternalAuditCheckCatalogue catalogue : checkCatalogues) { catalogue.setCheckId(internalAuditCheck.getId()); } // 批量插入目录 checkCatalogueMapper.updateCheckCatalogues(checkCatalogues); return CommonResult.success(); } return CommonResult.failed(); } @Override @Transactional public CommonResult deletedInternalAuditCheck(Integer id) { InternalAuditCheck internalAuditCheck = new InternalAuditCheck(); internalAuditCheck.setId(id); internalAuditCheck.setUpdateBy(SecurityUtils.getUsername()); internalAuditCheck.setUpdateTime(LocalDateTime.now()); internalAuditCheck.setDelFlag(2); int update = internalAuditCheckMapper.updateById(internalAuditCheck); return CommonResult.success(); } @Override public CommonResult updateByYearAndDeptId(InternalAuditCheck internalAuditCheck) { internalAuditCheck.setUpdateBy(SecurityUtils.getUsername()); internalAuditCheck.setUpdateTime(LocalDateTime.now()); Integer update = internalAuditCheckMapper.updateByYearAndDeptId(internalAuditCheck); if (update>0){ checkCatalogueMapper.updatebyCheckId(internalAuditCheck.getId()); List checkCatalogues = internalAuditCheck.getCheckCatalogues(); for (InternalAuditCheckCatalogue catalogue : checkCatalogues) { catalogue.setCheckId(internalAuditCheck.getId()); } // 批量插入目录 int catalogueInsertCount = checkCatalogueMapper.saveBatch(checkCatalogues); if (catalogueInsertCount != checkCatalogues.size()) { throw new RuntimeException("插入目录记录数量不匹配"); } } return CommonResult.success(); } @Override public List selectByMap(Map stringObjectHashMap) { return internalAuditCheckMapper.selectByMap(stringObjectHashMap); } @Override @Transactional public CommonResult copyInternalAuditCheck(Integer companyId, String sourceYear, String targetYear) { if (companyId == null) { companyId = SecurityUtils.getCompanyId().intValue(); } if (sourceYear == null || sourceYear.trim().isEmpty()) { return CommonResult.failed("源年份不能为空"); } if (targetYear == null || targetYear.trim().isEmpty()) { return CommonResult.failed("目标年份不能为空"); } LambdaUpdateWrapper deleteWrapper = new LambdaUpdateWrapper<>(); deleteWrapper.eq(InternalAuditCheck::getCompanyId, companyId) .eq(InternalAuditCheck::getYear, targetYear) .eq(InternalAuditCheck::getDelFlag, 0) .set(InternalAuditCheck::getDelFlag, 2) .set(InternalAuditCheck::getUpdateBy, SecurityUtils.getUsername()) .set(InternalAuditCheck::getUpdateTime, LocalDateTime.now()); internalAuditCheckMapper.update(new InternalAuditCheck(), deleteWrapper); LambdaQueryWrapper queryWrapperSource = new LambdaQueryWrapper<>(); queryWrapperSource.eq(InternalAuditCheck::getCompanyId, companyId) .eq(InternalAuditCheck::getYear, sourceYear) .eq(InternalAuditCheck::getDelFlag, 0); List sourceChecks = internalAuditCheckMapper.selectList(queryWrapperSource); if (ObjectUtil.isEmpty(sourceChecks)) { return CommonResult.success("源年份无相关数据"); } batchLoadCheckDetails(sourceChecks); for (InternalAuditCheck sourceCheck : sourceChecks) { InternalAuditCheck newCheck = new InternalAuditCheck(); newCheck.setCompanyId(sourceCheck.getCompanyId()); newCheck.setYear(targetYear); newCheck.setDeptId(sourceCheck.getDeptId()); newCheck.setPersonId(sourceCheck.getPersonId()); newCheck.setCheckTime(sourceCheck.getCheckTime()); newCheck.setStartTime(sourceCheck.getStartTime()); newCheck.setEndTime(sourceCheck.getEndTime()); newCheck.setDelFlag(0); newCheck.setCreateBy(SecurityUtils.getUsername()); newCheck.setCreateTime(LocalDateTime.now()); newCheck.setUpdateBy(SecurityUtils.getUsername()); newCheck.setUpdateTime(LocalDateTime.now()); int insert = internalAuditCheckMapper.insert(newCheck); if (insert <= 0) { throw new RuntimeException("复制内审检查主表数据失败"); } List sourceCatalogues = sourceCheck.getCheckCatalogues(); if (ObjectUtil.isNotEmpty(sourceCatalogues)) { List newCatalogues = new ArrayList<>(); for (InternalAuditCheckCatalogue sourceCatalogue : sourceCatalogues) { InternalAuditCheckCatalogue newCatalogue = new InternalAuditCheckCatalogue(); newCatalogue.setCheckId(newCheck.getId()); newCatalogue.setCatalogueId(sourceCatalogue.getCatalogueId()); newCatalogue.setPointKey(sourceCatalogue.getPointKey()); newCatalogue.setResult(sourceCatalogue.getResult()); newCatalogue.setFind(sourceCatalogue.getFind()); newCatalogues.add(newCatalogue); } checkCatalogueMapper.insertBatch(newCatalogues); } } return CommonResult.success("复制成功"); } // @Override // public CommonResult internalAuditCheckInfo(Integer id) { // InternalAuditCheck internalAuditCheck = internalAuditCheckMapper.selectById(id); // // InternalAuditCheckVo internalAuditCheckVo = new InternalAuditCheckVo(); // BeanUtils.copyProperties(internalAuditCheck,internalAuditCheckVo); // internalAuditCheckVo.setDeptName(sysDeptMapper.selectDeptById(internalAuditCheck.getDeptId()).getDeptName()); // internalAuditCheckVo.setAuditName(sysUserMapper.selectById(internalAuditCheck.getAuditId()).getName()); // LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); // queryWrapper.eq(InternalAuditCheckPerson::getAuditId, id); // queryWrapper.eq(InternalAuditCheckPerson::getDelFlag, 0); // List internalAuditCheckPeople = internalAuditCheckPersonMapper.selectList(queryWrapper); // internalAuditCheckVo.setInternalAuditCheckPeople(internalAuditCheckPeople); // // return CommonResult.success(internalAuditCheckVo); // } }