package com.ningdatech.carapi.industry.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ningdatech.basic.exception.BizException;
import com.ningdatech.carapi.industry.entity.LabWorkerCertificate;
import com.ningdatech.carapi.industry.entity.LabWorkerCertificateChange;
import com.ningdatech.carapi.industry.mapper.LabWorkerCertificateChangeMapper;
import com.ningdatech.carapi.industry.mapper.LabWorkerCertificateMapper;
import com.ningdatech.carapi.industry.model.req.LabWorkerCertificateAuditReq;
import com.ningdatech.carapi.industry.service.ILabWorkerCertificateService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <p>
 * 实验室人员从业资格证 服务实现类
 * </p>
 *
 * @author WendyYang
 * @since 2022-11-04
 */
@Service
@RequiredArgsConstructor
public class LabWorkerCertificateServiceImpl extends ServiceImpl<LabWorkerCertificateMapper, LabWorkerCertificate> implements ILabWorkerCertificateService {

    private final LabWorkerCertificateMapper labWorkerCertificateMapper;
    private final LabWorkerCertificateChangeMapper labWorkerCertificateChangeMapper;

    /**
     * 从业资格证|继续教育合格证换证申请审核
     * @param req
     * @param userId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean audit(LabWorkerCertificateAuditReq req, Long userId) {
        LabWorkerCertificateChange labWorkerCertificateChange = labWorkerCertificateChangeMapper.selectById(req.getId());
        if (Objects.isNull(labWorkerCertificateChange)){
            throw new BizException("证书信息不存在!");
        }
        // labWorkerCertificateChange.setAuditStatus(1);
        labWorkerCertificateChange.setUpdateBy(userId);
        labWorkerCertificateChange.setUpdateOn(LocalDateTime.now());
        labWorkerCertificateChange.setReplacementTime(LocalDate.now());
        labWorkerCertificateChangeMapper.updateById(labWorkerCertificateChange);
        LabWorkerCertificate labWorkerCertificate = BeanUtil.copyProperties(labWorkerCertificateChange, LabWorkerCertificate.class);
        labWorkerCertificate.setId(null);
        labWorkerCertificate.setCreateOn(LocalDateTime.now());
        labWorkerCertificate.setCreateBy(userId);
        labWorkerCertificateMapper.insert(labWorkerCertificate);
        return Boolean.TRUE;
    }

    /**
     * 从业资格证取消审核
     * @param req
     * @param userId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean cancelAudit(LabWorkerCertificateAuditReq req, Long userId) {
        LabWorkerCertificateChange labWorkerCertificateChange = labWorkerCertificateChangeMapper.selectById(req.getId());
        if (Objects.isNull(labWorkerCertificateChange)){
            throw new BizException("证书信息不存在!");
        }
        // labWorkerCertificateChange.setAuditStatus(0);
        labWorkerCertificateChange.setUpdateBy(userId);
        labWorkerCertificateChange.setUpdateOn(LocalDateTime.now());
        labWorkerCertificateChangeMapper.updateById(labWorkerCertificateChange);
        labWorkerCertificateMapper.delete(Wrappers.lambdaQuery(LabWorkerCertificate.class)
                .eq(Objects.nonNull(labWorkerCertificateChange.getCertificateNo()),LabWorkerCertificate::getCertificateNo,labWorkerCertificateChange.getCertificateNo()));
        return Boolean.TRUE;
    }
}
