package digital.laboratory.platform.reagent.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import digital.laboratory.platform.common.mybatis.security.service.DLPUser;
import digital.laboratory.platform.reagent.dto.AuditAndApproveDTO;
import digital.laboratory.platform.reagent.dto.EvaluationFormDTO;
import digital.laboratory.platform.reagent.entity.EvaluationForm;
import digital.laboratory.platform.reagent.mapper.EvaluationFormMapper;
import digital.laboratory.platform.reagent.service.EvaluationFormService;
import digital.laboratory.platform.reagent.service.ProvideServicesOrSuppliesService;
import digital.laboratory.platform.reagent.service.PurchaseListDetailsService;
import digital.laboratory.platform.reagent.service.SupplierInformationService;
import digital.laboratory.platform.reagent.vo.EvaluationFormVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * (服务商/供应商评价表)服务实现类
 *
 * @author Zhang Xiaolong created at  2023-03-10
 * @describe (服务商 / 供应商评价表) 服务实现类
 */
@SuppressWarnings("all")
@Service
@RequiredArgsConstructor
public class EvaluationFormServiceImpl extends ServiceImpl<EvaluationFormMapper, EvaluationForm> implements EvaluationFormService {


    private EvaluationFormService evaluationFormService;

    private ProvideServicesOrSuppliesService provideServicesOrSuppliesService;


    private SupplierInformationService supplierInformationService;


    private PurchaseListDetailsService purchaseListDetailsService;

    @Override//通过ID查询评价表
    public EvaluationForm getFormById(String evaluationFormId) {

        EvaluationFormVO evaluationForm = baseMapper.getEvaluationForm(evaluationFormId);

        evaluationForm.setProvideServicesOrSuppliesVOList(provideServicesOrSuppliesService.getProvideServicesOrSuppliesVOList(evaluationFormId));

        return evaluationForm;
    }

    @Override//增加评价表
    public EvaluationForm addFormById(String supplierInformationId) {

        EvaluationForm evaluationForm = new EvaluationForm();

        evaluationForm.setEvaluationFormId(IdWorker.get32UUID().toUpperCase());

        evaluationForm.setSupplierInformationId(supplierInformationId);

        evaluationFormService.save(evaluationForm);

        return evaluationForm;
    }

    @Override
    public EvaluationForm editFormById(EvaluationFormDTO evaluationFormDTO) {

        EvaluationForm byId = evaluationFormService.getById(evaluationFormDTO.getEvaluationFormId());

        BeanUtils.copyProperties(evaluationFormDTO, byId);

        if (evaluationFormService.updateById(byId)) {
            return byId;
        } else throw new RuntimeException(String.format("保存失败"));
    }

    @Override//二级审核
    public EvaluationForm auditFormOfSecondary(AuditAndApproveDTO auditAndApproveDTO, DLPUser dlpUser) {

        EvaluationForm byId = evaluationFormService.getById(auditAndApproveDTO.getUuId());

        byId.setSecondaryUserId(dlpUser.getId());

        byId.setCommentsDateFromSecondary(LocalDate.now());

        byId.setCommentsFromSecondary(auditAndApproveDTO.getAuditOpinion());

        byId.setCommentsResultFromSecondary(auditAndApproveDTO.getAuditResult());

        if (evaluationFormService.updateById(byId)) {
            return byId;
        } else {
            return null;
        }
    }

    @Override//三级审核

    public EvaluationForm auditFormOfThreeLevel(AuditAndApproveDTO auditAndApproveDTO, DLPUser dlpUser) {

        EvaluationForm byId = evaluationFormService.getById(auditAndApproveDTO.getUuId());

        byId.setSecondaryUserId(dlpUser.getId());

        byId.setCommentsDateFromThreeLevel(LocalDate.now());

        byId.setCommentsFromThreeLevel(auditAndApproveDTO.getAuditOpinion());

        byId.setCommentsResultFromThreeLevel(auditAndApproveDTO.getAuditResult());

        byId.setStatus(2);

        if (evaluationFormService.updateById(byId)) {
            return byId;
        } else {
            return null;
        }
    }
}









