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

import cn.hutool.core.bean.BeanUtil;
import com.ningdatech.basic.exception.BizException;
import com.ningdatech.carapi.industry.entity.LabAcceptanceCertificate;
import com.ningdatech.carapi.industry.entity.LabAcceptanceCertificateChange;
import com.ningdatech.carapi.industry.mapper.LabAcceptanceCertificateChangeMapper;
import com.ningdatech.carapi.industry.mapper.LabAcceptanceCertificateMapper;
import com.ningdatech.carapi.industry.model.req.LabWorkerCertificateAuditReq;
import com.ningdatech.carapi.industry.service.ILabAcceptanceCertificateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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-08
 */
@Service
@RequiredArgsConstructor
public class LabAcceptanceCertificateServiceImpl extends ServiceImpl<LabAcceptanceCertificateMapper, LabAcceptanceCertificate> implements ILabAcceptanceCertificateService {

    private final LabAcceptanceCertificateMapper labAcceptanceCertificateMapper;
    private final LabAcceptanceCertificateChangeMapper labAcceptanceCertificateChangeMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean audit(LabWorkerCertificateAuditReq req, Long userId) {
        LabAcceptanceCertificateChange labAcceptanceCertificateChange = labAcceptanceCertificateChangeMapper.selectById(req.getId());
        if (Objects.isNull(labAcceptanceCertificateChange)){
            throw new BizException("证书信息不存在!");
        }
        // labAcceptanceCertificateChange.setAuditStatus(1);
        labAcceptanceCertificateChange.setUpdateOn(LocalDateTime.now());
        labAcceptanceCertificateChange.setUpdateBy(userId);
        labAcceptanceCertificateChange.setReplacementTime(LocalDate.now());
        labAcceptanceCertificateChangeMapper.updateById(labAcceptanceCertificateChange);
        LabAcceptanceCertificate labAcceptanceCertificate = BeanUtil.copyProperties(labAcceptanceCertificateChange, LabAcceptanceCertificate.class);
        labAcceptanceCertificate.setId(null);
        labAcceptanceCertificate.setCreateOn(LocalDateTime.now());
        labAcceptanceCertificate.setCreateBy(userId);
        labAcceptanceCertificateMapper.insert(labAcceptanceCertificate);
        return Boolean.TRUE;
    }
}
