package com.zd.exam.service.impl;

import java.util.List;

import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.exception.ServiceException;
import com.zd.common.core.utils.DateUtils;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.datascope.annotation.DataScope;
import com.zd.common.security.service.TokenService;
import com.zd.exam.domain.ElExamCertRelation;
import com.zd.exam.domain.ElUserCert;
import com.zd.exam.domain.LabCertificate;
import com.zd.exam.mapper.LabCertificateMapper;
import com.zd.exam.service.IElExamCertRelationService;
import com.zd.exam.service.IElUserCertService;
import com.zd.exam.service.ILabCertificateService;
import com.zd.system.api.domain.SysUser;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 证书管理Service业务层处理
 *
 * @author zd
 * @date 2021-08-31
 */
@Service
public class LabCertificateServiceImpl implements ILabCertificateService {
    @Autowired
    private LabCertificateMapper labCertificateMapper;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IElUserCertService userCertService;
    @Autowired
    private IElExamCertRelationService elExamCertRelationService;

    /**
     * 查询证书管理
     *
     * @param id 证书管理主键
     * @return 证书管理
     */
    @Override
    public LabCertificate selectLabCertificateById(Long id) {
        return labCertificateMapper.selectLabCertificateById(id);
    }

    /**
     * 查询证书管理列表
     *
     * @param labCertificate 证书管理
     * @return 证书管理
     */
    @Override
    @DataScope(deptAlias = "c" , userAlias = "c" , permi = PerPrefix.EXAM_CERTIFICATE)
    public List<LabCertificate> selectLabCertificateList(LabCertificate labCertificate) {
        return labCertificateMapper.selectLabCertificateList(labCertificate);
    }

    /**
     * 新增证书管理
     *
     * @param labCertificate 证书管理
     * @return 结果
     */
    @Override
    public int insertLabCertificate(LabCertificate labCertificate) {
        SysUser user = tokenService.getLoginUser().getSysUser();
        labCertificate.setDeptId(user.getDeptId());
        labCertificate.setDeptName(user.getDeptName());
        labCertificate.setUserId(user.getUserId());
        labCertificate.setCreateBy(user.getNickName());
        labCertificate.setCreateTime(DateUtils.getNowDate());
        labCertificate.setCertValidityUnit("月");
        return labCertificateMapper.insertLabCertificate(labCertificate);
    }

    /**
     * 修改证书管理
     *
     * @param labCertificate 证书管理
     * @return 结果
     */
    @Override
    public int updateLabCertificate(LabCertificate labCertificate) {
        labCertificate.setUpdateBy(SecurityUtils.getUsername());
        labCertificate.setUpdateTime(DateUtils.getNowDate());
        return labCertificateMapper.updateLabCertificate(labCertificate);
    }

    /**
     * 批量删除证书管理
     *
     * @param ids 需要删除的证书管理主键
     * @return 结果
     */
    @Override
    public int deleteLabCertificateByIds(Long[] ids) {
        ElUserCert elUserCert = new ElUserCert();
        ElExamCertRelation elExamCertRelation = new ElExamCertRelation();
        List<ElExamCertRelation> examCertRelations;
        List<ElUserCert> userCerts;
        for (Long id : ids) {
            elExamCertRelation.setCertId(id);
            examCertRelations = elExamCertRelationService.selectElExamCertRelationList(elExamCertRelation);
            if(CollectionUtils.isNotEmpty(examCertRelations)){
                throw new ServiceException("证书已绑定考试，不能删除!");
            }

            elUserCert.setCertId(id);
            userCerts = userCertService.selectElUserCertList(elUserCert);
            if(CollectionUtils.isNotEmpty(userCerts)){
                throw new ServiceException("用户已经获得的证书，不能删除!");
            }
        }
        return labCertificateMapper.deleteLabCertificateByIds(ids);
    }

    /**
     * 删除证书管理信息
     *
     * @param id 证书管理主键
     * @return 结果
     */
    @Override
    public int deleteLabCertificateById(Long id) {
        return labCertificateMapper.deleteLabCertificateById(id);
    }
}
