package com.ruoyi.basic.service.impl;
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.ruoyi.basic.dto.CertificationImageUpdateDto;
|
import com.ruoyi.basic.dto.CertificationUpdateDto;
|
import com.ruoyi.basic.mapper.CertificationMapper;
|
import com.ruoyi.basic.pojo.Certification;
|
import com.ruoyi.basic.service.CertificationService;
|
import com.ruoyi.common.exception.base.BaseException;
|
import com.ruoyi.common.utils.StringUtils;
|
import lombok.AllArgsConstructor;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.util.Arrays;
|
import java.util.List;
|
import java.util.stream.Collectors;
|
|
@Service
|
@AllArgsConstructor
|
@Transactional(rollbackFor = Exception.class)
|
public class CertificationServiceImpl extends ServiceImpl<CertificationMapper, Certification> implements CertificationService {
|
|
|
private CertificationMapper certificationMapper;
|
|
|
//查询资质明细列表
|
@Override
|
public IPage<Certification> getCertificationDetail(Page page, Certification certification) {
|
return certificationMapper.getCertificationDetail(page, certification);
|
}
|
|
@Override
|
public Certification getCertificationById(Integer id) {
|
if (id == null) {
|
throw new BaseException("资质ID不能为空");
|
}
|
Certification certification = certificationMapper.selectById(id);
|
if (certification == null) {
|
throw new BaseException("资质记录不存在");
|
}
|
return certification;
|
}
|
|
//添加资质明细列表
|
@Override
|
public int addCertificationDetail(Certification certification) {
|
validateRequiredFields(certification.getName(), certification.getCode(), certification.getOrganization(),
|
certification.getDateOfIssuance(), certification.getExpireTime());
|
return certificationMapper.insert(certification);
|
}
|
|
@Override
|
public int updateCertificationDetail(CertificationUpdateDto certification) {
|
if (certification == null || certification.getId() == null) {
|
throw new BaseException("资质ID不能为空");
|
}
|
Certification existing = getCertificationById(certification.getId());
|
validateRequiredFields(certification.getName(), certification.getCode(), certification.getOrganization(),
|
certification.getDateOfIssuance(), certification.getExpireTime());
|
|
String imageUrl = StringUtils.isNotBlank(certification.getImageUrl())
|
? certification.getImageUrl() : existing.getImageUrl();
|
String fileUrl = StringUtils.isNotBlank(certification.getFileUrl())
|
? certification.getFileUrl() : existing.getFileUrl();
|
int updated = certificationMapper.update(new Certification(), Wrappers.<Certification>lambdaUpdate()
|
.eq(Certification::getId, certification.getId())
|
.set(Certification::getName, certification.getName())
|
.set(Certification::getCode, certification.getCode())
|
.set(Certification::getOrganization, certification.getOrganization())
|
.set(Certification::getExplanation, certification.getExplanation())
|
.set(Certification::getDateOfIssuance, certification.getDateOfIssuance())
|
.set(Certification::getExpireTime, certification.getExpireTime())
|
.set(Certification::getImageUrl, imageUrl)
|
.set(Certification::getFileUrl, fileUrl));
|
if (updated < 1) {
|
throw new BaseException("资质更新失败");
|
}
|
return updated;
|
}
|
|
@Override
|
public int updateCertificationImage(CertificationImageUpdateDto certification) {
|
if (certification == null || certification.getId() == null) {
|
throw new BaseException("资质ID不能为空");
|
}
|
if (StringUtils.isBlank(certification.getImageUrl())) {
|
throw new BaseException("资质图片不能为空");
|
}
|
getCertificationById(certification.getId());
|
int updated = certificationMapper.update(new Certification(), Wrappers.<Certification>lambdaUpdate()
|
.eq(Certification::getId, certification.getId())
|
.set(Certification::getImageUrl, certification.getImageUrl()));
|
if (updated < 1) {
|
throw new BaseException("资质图片更新失败");
|
}
|
return updated;
|
}
|
|
//删除资质明细列表
|
@Override
|
public int delCertificationDetail(String ids) {
|
List<Integer> list = Arrays.stream(ids.split(",")).map(Integer::parseInt).collect(Collectors.toList());
|
return certificationMapper.deleteBatchIds(list);
|
}
|
|
private void validateRequiredFields(String name, String code, String organization,
|
java.time.LocalDateTime dateOfIssuance,
|
java.time.LocalDateTime expireTime) {
|
if (StringUtils.isBlank(name)) {
|
throw new BaseException("资质名称不能为空");
|
}
|
if (StringUtils.isBlank(code)) {
|
throw new BaseException("资质编码不能为空");
|
}
|
if (StringUtils.isBlank(organization)) {
|
throw new BaseException("颁发机构不能为空");
|
}
|
if (dateOfIssuance == null) {
|
throw new BaseException("颁发时间不能为空");
|
}
|
if (expireTime == null) {
|
throw new BaseException("到期时间不能为空");
|
}
|
}
|
|
}
|