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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.AcceptanceRecordFormDTO;
import digital.laboratory.platform.reagent.dto.AuditAndApproveDTO;
import digital.laboratory.platform.reagent.entity.AcceptanceRecordForm;
import digital.laboratory.platform.reagent.mapper.AcceptanceRecordFormMapper;
import digital.laboratory.platform.reagent.service.AcceptanceRecordFormService;
import digital.laboratory.platform.reagent.vo.AcceptanceRecordFormVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * (验收记录表)服务实现类
 *
 * @author Zhang Xiaolong created at  2023-03-10
 * @describe (验收记录表) 服务实现类
 */
@Service
@SuppressWarnings("all")
public class AcceptanceRecordFormServiceImpl extends ServiceImpl<AcceptanceRecordFormMapper, AcceptanceRecordForm> implements AcceptanceRecordFormService {

    @Autowired
    private AcceptanceRecordFormService acceptanceRecordFormService;

    @Override
    public AcceptanceRecordForm editFrom(AcceptanceRecordFormDTO acceptanceRecordFormDTO) {

        AcceptanceRecordForm byId = acceptanceRecordFormService.getById(acceptanceRecordFormDTO.getAcceptanceRecordFormId());

        BeanUtils.copyProperties(acceptanceRecordFormDTO, byId);

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

    @Override
    public AcceptanceRecordForm addForm(String reagentConsumableId, String supplierId) {

        AcceptanceRecordForm acceptanceRecordForm = new AcceptanceRecordForm();

        acceptanceRecordForm.setAcceptanceRecordFormId(IdWorker.get32UUID().toUpperCase());

        acceptanceRecordForm.setSupplierId(supplierId);

        acceptanceRecordForm.setReagentConsumableId(reagentConsumableId);

        acceptanceRecordForm.setStatus(0);

        if ((acceptanceRecordFormService.save(acceptanceRecordForm))) {

            return acceptanceRecordForm;

        } else throw new RuntimeException(String.format("创建验收任务失败"));
    }

    @Override
    public AcceptanceRecordForm commitForm(AcceptanceRecordFormDTO acceptanceRecordFormDTO) {

        AcceptanceRecordForm acceptanceRecordForm = acceptanceRecordFormService.editFrom(acceptanceRecordFormDTO);

        acceptanceRecordForm.setStatus(1);

        if (acceptanceRecordFormService.updateById(acceptanceRecordForm)) {
            return acceptanceRecordForm;
        } else throw new RuntimeException(String.format("提交失败"));

    }

    @Override
    public IPage<AcceptanceRecordFormVO> getAcceptanceRecordFormVOPage(IPage<AcceptanceRecordForm> page, QueryWrapper<AcceptanceRecordForm> qw) {

        IPage<AcceptanceRecordFormVO> acceptanceRecordFormVOPage = baseMapper.getAcceptanceRecordFormVOPage(page, qw);

        return acceptanceRecordFormVOPage;

    }

    @Override
    public AcceptanceRecordFormVO getAcceptanceRecordFormVO(String acceptanceRecordFormId) {

        AcceptanceRecordFormVO acceptanceRecordFormVO = baseMapper.getAcceptanceRecordFormVO(acceptanceRecordFormId);

        return acceptanceRecordFormVO;
    }

    @Override
    public AcceptanceRecordForm primaryAudit(AuditAndApproveDTO auditAndApproveDTO, DLPUser dlpUser) {

        AcceptanceRecordForm byId = acceptanceRecordFormService.getById(auditAndApproveDTO.getUuId());

        byId.setPrimaryAuditorId(dlpUser.getId());
        byId.setAuditOpinionOfPrimary(auditAndApproveDTO.getAuditOpinion());
        byId.setAuditTimeOfPrimary(LocalDateTime.now());
        byId.setAuditResultOfPrimary(auditAndApproveDTO.getAuditResult());

        if (acceptanceRecordFormService.updateById(byId)) {
            return byId;
        } else throw new RuntimeException(String.format("审核失败"));
    }
    @Override
    public AcceptanceRecordForm secondaryAudit(AuditAndApproveDTO auditAndApproveDTO, DLPUser dlpUser) {

        AcceptanceRecordForm byId = acceptanceRecordFormService.getById(auditAndApproveDTO.getUuId());

        byId.setSecondaryAuditorId(dlpUser.getId());
        byId.setAuditOpinionOfSecondary(auditAndApproveDTO.getAuditOpinion());
        byId.setAuditTimeOfSecondary(LocalDateTime.now());
        byId.setAuditResultOfSecondary(auditAndApproveDTO.getAuditResult());

        if (acceptanceRecordFormService.updateById(byId)) {
            return byId;
        } else throw new RuntimeException(String.format("审核失败"));
    }
    @Override
    public AcceptanceRecordForm threeLevelAudit(AuditAndApproveDTO auditAndApproveDTO, DLPUser dlpUser) {

        AcceptanceRecordForm byId = acceptanceRecordFormService.getById(auditAndApproveDTO.getUuId());

        byId.setThreeLevelAuditorId(dlpUser.getId());
        byId.setAuditOpinionOfThreeLevel(auditAndApproveDTO.getAuditOpinion());
        byId.setAuditTimeOfThreeLevel(LocalDateTime.now());
        byId.setAuditResultOfThreeLevel(auditAndApproveDTO.getAuditResult());
        byId.setStatus(2);

        if (acceptanceRecordFormService.updateById(byId)) {
            return byId;
        } else throw new RuntimeException(String.format("审核失败"));
    }


}
