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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ningdatech.basic.exception.BizException;
import com.ningdatech.carapi.industry.entity.MortarEnterpriseFilingCertificate;
import com.ningdatech.carapi.industry.entity.MortarEnterpriseFilingCertificateChange;
import com.ningdatech.carapi.industry.mapper.MortarEnterpriseFilingCertificateChangeMapper;
import com.ningdatech.carapi.industry.mapper.MortarEnterpriseFilingCertificateMapper;
import com.ningdatech.carapi.industry.model.req.LabWorkerCertificateAuditReq;
import com.ningdatech.carapi.industry.service.IMortarEnterpriseFilingCertificateService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 砂浆企业备案证 服务实现类
 * </p>
 *
 * @author WendyYang
 * @since 2022-11-08
 */
@Service
@RequiredArgsConstructor
public class MortarEnterpriseFilingCertificateServiceImpl extends ServiceImpl<MortarEnterpriseFilingCertificateMapper, MortarEnterpriseFilingCertificate> implements IMortarEnterpriseFilingCertificateService {

    private final MortarEnterpriseFilingCertificateMapper mortarEnterpriseFilingCertificateMapper;
    private final MortarEnterpriseFilingCertificateChangeMapper mortarEnterpriseFilingCertificateChangeMapper;

    @Override
    public Boolean audit(LabWorkerCertificateAuditReq req, Long userId) {
        MortarEnterpriseFilingCertificateChange mortarEnterpriseFilingCertificateChange = mortarEnterpriseFilingCertificateChangeMapper.selectById(req.getId());
        if (Objects.isNull(mortarEnterpriseFilingCertificateChange)){
            throw new BizException("证书信息不存在!");
        }
        // mortarEnterpriseFilingCertificateChange.setAuditStatus(1);
        mortarEnterpriseFilingCertificateChange.setUpdateOn(LocalDateTime.now());
        mortarEnterpriseFilingCertificateChange.setUpdateBy(userId);
        mortarEnterpriseFilingCertificateChange.setReplacementTime(LocalDate.now());
        mortarEnterpriseFilingCertificateChangeMapper.updateById(mortarEnterpriseFilingCertificateChange);
        MortarEnterpriseFilingCertificate mortarEnterpriseFilingCertificate
                = BeanUtil.copyProperties(mortarEnterpriseFilingCertificateChange, MortarEnterpriseFilingCertificate.class);
        mortarEnterpriseFilingCertificate.setId(null);
        mortarEnterpriseFilingCertificate.setCreateOn(LocalDateTime.now());
        mortarEnterpriseFilingCertificate.setCreateBy(userId);
        mortarEnterpriseFilingCertificateMapper.insert(mortarEnterpriseFilingCertificate);
        return Boolean.TRUE;
    }
}
