CertDictServiceImpl.java 5.11 KB
package com.subsidy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.subsidy.dto.cert.AddCertDTO;
import com.subsidy.dto.cert.GetAllCertsDTO;
import com.subsidy.dto.cert.GetOneCertDTO;
import com.subsidy.mapper.*;
import com.subsidy.model.*;
import com.subsidy.service.CertDictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.subsidy.service.CertTypeDictService;
import com.subsidy.util.ConstantUtils;
import com.subsidy.vo.cert.GetAllCertsVO;
import com.subsidy.vo.cert.GetAllCertsWithoutTypeVO;
import com.subsidy.vo.cert.GetOneCertVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 证书字典表 服务实现类
 * </p>
 *
 * @author DengMin
 * @since 2022-05-11
 */
@Service
public class CertDictServiceImpl extends ServiceImpl<CertDictMapper, CertDictDO> implements CertDictService {


    @Autowired
    private CertTypeMappingMapper certTypeMappingMapper;

    @Autowired
    private CertTypeDictMapper certTypeDictMapper;

    @Autowired
    private CertRequirementMapper certRequirementMapper;

    @Autowired
    private CertMemberMappingMapper certMemberMappingMapper;

    @Transactional(rollbackFor = Exception.class)
    public String addCert(AddCertDTO addCertDTO) {

        CertDictDO certDictDO = new CertDictDO();
        BeanUtils.copyProperties(addCertDTO, certDictDO);
        this.baseMapper.insert(certDictDO);


        List<Long> list = addCertDTO.getTypeIds();
        for (Long lg : list) {
            CertTypeMappingDO certTypeMappingDO = new CertTypeMappingDO();
            certTypeMappingDO.setCertId(certDictDO.getId());
            certTypeMappingDO.setTypeId(lg);
            certTypeMappingMapper.insert(certTypeMappingDO);
        }

        return ConstantUtils.ADD_SUCCESS;
    }

    @Transactional(rollbackFor = Exception.class)
    public String deleteCert(CertDictDO certDictDO) {

        //删除证书
        this.baseMapper.deleteById(certDictDO.getId());

        //删除证书类目映射关系
        certTypeMappingMapper.delete(new QueryWrapper<CertTypeMappingDO>()
        .lambda()
        .eq(CertTypeMappingDO::getCertId,certDictDO.getId()));

        //删除证书相关条件
        certRequirementMapper.delete(new QueryWrapper<CertRequirementDO>()
        .lambda()
        .eq(CertRequirementDO::getCertId,certDictDO.getId()));

        return ConstantUtils.DELETE_SUCCESS;
    }

    public String updateCert(AddCertDTO addCertDTO) {

        CertDictDO certDictDO = new CertDictDO();
        BeanUtils.copyProperties(addCertDTO, certDictDO);
        this.baseMapper.updateById(certDictDO);

        if (addCertDTO.getTypeIds().size() > 0) {
            certTypeMappingMapper.delete(new QueryWrapper<CertTypeMappingDO>()
                    .lambda()
                    .eq(CertTypeMappingDO::getCertId, addCertDTO.getId()));

            List<Long> list = addCertDTO.getTypeIds();
            for (Long lg : list) {
                CertTypeMappingDO certTypeMappingDO = new CertTypeMappingDO();
                certTypeMappingDO.setCertId(certDictDO.getId());
                certTypeMappingDO.setTypeId(lg);
                certTypeMappingMapper.insert(certTypeMappingDO);
            }
        }
        return ConstantUtils.SET_SUCCESS;
    }

    public GetOneCertVO getOneCert(GetOneCertDTO getOneCertDTO) {

        GetOneCertVO getOneCertVO = new GetOneCertVO();
        CertDictDO certDictDO1 = this.baseMapper.selectById(getOneCertDTO.getId());
        BeanUtils.copyProperties(certDictDO1, getOneCertVO);

        //查看证书有哪些类型
        List<CertTypeDictDO> certTypeDictDOS = certTypeDictMapper.getCertTypes(certDictDO1.getId());
        getOneCertVO.setCertTypeDictDOS(certTypeDictDOS);

        Integer count = certMemberMappingMapper.memberCertStatus(getOneCertDTO.getUserId(),certDictDO1.getId());

        if (count > 0){
            getOneCertVO.setStatus(true);
        }else {
            getOneCertVO.setStatus(false);
        }

        return getOneCertVO;
    }

    public IPage<GetAllCertsVO> getAllCerts(GetAllCertsDTO getAllCertsDTO) {

        Page pager = new Page(getAllCertsDTO.getPageNum(), getAllCertsDTO.getPageSize());
        IPage<GetAllCertsVO> getAllCertsVOIPage = this.baseMapper.getAllCerts(pager,getAllCertsDTO.getTypeId(),getAllCertsDTO.getCertName());

        List<GetAllCertsVO> getAllCertsVOS = getAllCertsVOIPage.getRecords();
        for (GetAllCertsVO gav : getAllCertsVOS){
            List<CertTypeDictDO> certTypeDictDOS = certTypeDictMapper.getCertTypes(gav.getId());
            gav.setCertTypeDictDOS(certTypeDictDOS);
        }
        return getAllCertsVOIPage;
    }

    public List<GetAllCertsWithoutTypeVO> getAllCertsWithoutType(){
        return this.baseMapper.getAllCertsWithoutType();
    }

}