package com.gkhy.exam.system.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.gkhy.exam.common.api.CommonPage;
import com.gkhy.exam.common.api.CommonResult;
import com.gkhy.exam.common.constant.UserConstant;
import com.gkhy.exam.common.utils.PageUtils;
import com.gkhy.exam.common.utils.SecurityUtils;
import com.gkhy.exam.system.domain.AnnualVerificationDevice;
import com.gkhy.exam.system.domain.AnnualVerificationPlan;
import com.gkhy.exam.system.domain.OutsourcedReview;
import com.gkhy.exam.system.domain.OutsourcedReviewSubsidiary;
import com.gkhy.exam.system.mapper.AnnualVerificationDeviceMapper;
import com.gkhy.exam.system.mapper.AnnualVerificationPlanMapper;
import com.gkhy.exam.system.service.AnnualVerificationPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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-08-25 15:01:44
*/
@Service
public class AnnualVerificationPlanServiceImpl extends ServiceImpl implements AnnualVerificationPlanService {
@Autowired
private AnnualVerificationPlanMapper annualVerificationPlanMapper;
@Autowired
private AnnualVerificationDeviceMapper annualVerificationDeviceMapper;
@Override
public CommonPage selectAnnualVerificationPlanList(AnnualVerificationPlan annualVerificationPlan) {
PageUtils.startPage();
List annualVerificationPlans = annualVerificationPlanMapper.selectAnnualVerificationPlanList(annualVerificationPlan);
return CommonPage.restPage(annualVerificationPlans);
}
@Override
@Transactional
public CommonResult saveAnnualVerificationPlan(AnnualVerificationPlan annualVerificationPlan) {
List annualVerificationDevices = annualVerificationPlan.getAnnualVerificationDevices();
if (ObjectUtils.isEmpty(annualVerificationDevices)){
return CommonResult.failed("保存参数不能为空");
}
int i = 0;
if (annualVerificationPlan.getId() == null){
annualVerificationPlan.setCreateBy(SecurityUtils.getUsername());
annualVerificationPlan.setCreateTime(LocalDateTime.now());
i = annualVerificationPlanMapper.insert(annualVerificationPlan);
}else {
annualVerificationPlan.setUpdateTime(LocalDateTime.now());
annualVerificationPlan.setUpdateBy(SecurityUtils.getUsername());
i = annualVerificationPlanMapper.updateById(annualVerificationPlan);
}
if (i > 0){
batchSave(annualVerificationPlan.getId(), annualVerificationDevices, annualVerificationPlan.getDelData());
}
return i > 0 ? CommonResult.success() : CommonResult.failed();
}
private void batchSave(Long id, List annualVerificationDevices, List delData) {
if (ObjectUtils.isNotEmpty(delData)){
int update = annualVerificationDeviceMapper.update(new AnnualVerificationDevice(),
new LambdaUpdateWrapper().set(AnnualVerificationDevice::getDelFlag, UserConstant.DEPT_DISABLE)
.set(AnnualVerificationDevice::getUpdateTime, LocalDateTime.now()).set(AnnualVerificationDevice::getUpdateBy, SecurityUtils.getUsername())
.in(AnnualVerificationDevice::getId, delData)
);
if (update >= 0){
throw new RuntimeException("操作失败");
}
}
annualVerificationDevices.forEach(annualVerificationDevice -> {
if (annualVerificationDevice.getId() == null){
annualVerificationDevice.setAnnualVerificationId(id);
annualVerificationDevice.setCreateTime(LocalDateTime.now());
annualVerificationDevice.setCreateBy(SecurityUtils.getUsername());
annualVerificationDeviceMapper.insert(annualVerificationDevice);
}else {
annualVerificationDevice.setUpdateTime(LocalDateTime.now());
annualVerificationDevice.setUpdateBy(SecurityUtils.getUsername());
annualVerificationDeviceMapper.updateById(annualVerificationDevice);
}
});
}
@Override
public CommonResult getAnnualVerificationPlan(Long id) {
AnnualVerificationPlan annualVerificationPlan = annualVerificationPlanMapper.selectById(id);
if (annualVerificationPlan != null){
List annualVerificationDevices = annualVerificationDeviceMapper.selectList(new LambdaQueryWrapper<>(AnnualVerificationDevice.class).eq(AnnualVerificationDevice::getAnnualVerificationId, id)
.eq(AnnualVerificationDevice::getDelFlag, UserConstant.ENABLE).orderByDesc(AnnualVerificationDevice::getCreateTime));
annualVerificationPlan.setAnnualVerificationDevices(annualVerificationDevices);
}
return CommonResult.success(annualVerificationPlan);
}
@Override
@Transactional
public CommonResult deletedAnnualVerificationPlan(Long id) {
int i = annualVerificationPlanMapper.update(new AnnualVerificationPlan(),
new LambdaUpdateWrapper().eq(AnnualVerificationPlan::getId, id).set(AnnualVerificationPlan::getDelFlag, UserConstant.DEPT_DISABLE)
.set(AnnualVerificationPlan::getUpdateTime, LocalDateTime.now()).set(AnnualVerificationPlan::getUpdateBy, SecurityUtils.getUsername())
);
if (i > 0) {
annualVerificationDeviceMapper.update(new AnnualVerificationDevice(),
new LambdaUpdateWrapper().eq(AnnualVerificationDevice::getAnnualVerificationId, id)
.set(AnnualVerificationDevice::getDelFlag, UserConstant.DEPT_DISABLE)
.set(AnnualVerificationDevice::getUpdateTime, LocalDateTime.now())
.set(AnnualVerificationDevice::getUpdateBy, SecurityUtils.getUsername()));
}
return CommonResult.success();
}
}