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.AuditAndApproveDTO;
import digital.laboratory.platform.reagent.dto.PeriodVerificationImplementationDTO;
import digital.laboratory.platform.reagent.entity.PeriodVerificationImplementation;
import digital.laboratory.platform.reagent.entity.PeriodVerificationPlan;
import digital.laboratory.platform.reagent.mapper.PeriodVerificationImplementationMapper;
import digital.laboratory.platform.reagent.service.PeriodVerificationImplementationService;
import digital.laboratory.platform.reagent.service.PeriodVerificationPlanService;
import digital.laboratory.platform.reagent.vo.PeriodVerificationImplementationVO;
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
public class PeriodVerificationImplementationServiceImpl extends ServiceImpl<PeriodVerificationImplementationMapper, PeriodVerificationImplementation> implements PeriodVerificationImplementationService {
    @Autowired
    private PeriodVerificationPlanService periodVerificationPlanService;

    @Autowired
    private PeriodVerificationImplementationService periodVerificationImplementationService;

    @Override
    public IPage<PeriodVerificationImplementationVO> getPeriodVerificationImplementationVOPage(IPage<PeriodVerificationImplementation> page, QueryWrapper<PeriodVerificationImplementation> qw) {

        IPage<PeriodVerificationImplementationVO> periodVerificationImplementationVOPage = baseMapper.getPeriodVerificationImplementationVOPage(page, qw);

        return periodVerificationImplementationVOPage;
    }

    @Override
    public PeriodVerificationImplementationVO getPeriodVerificationImplementationVO(String periodVerificationImplementationId) {

        PeriodVerificationImplementationVO periodVerificationImplementationVO = baseMapper.getPeriodVerificationImplementationVO(periodVerificationImplementationId);

        return periodVerificationImplementationVO;
    }

    @Override
    public PeriodVerificationImplementation addById(PeriodVerificationImplementationDTO periodVerificationImplementationDTO, DLPUser dlpUser) {

        PeriodVerificationImplementation periodVerificationImplementation = new PeriodVerificationImplementation();

        BeanUtils.copyProperties(periodVerificationImplementationDTO, periodVerificationImplementation);

        periodVerificationImplementation.setPeriodVerificationImplementationId(IdWorker.get32UUID().toUpperCase());

        String result = periodVerificationImplementationDTO.getResult();

        PeriodVerificationPlan byId = periodVerificationPlanService.getById(periodVerificationImplementationDTO.getPeriodVerificationPlanId());

        byId.setVerificationResult(result);

        byId.setImplementationDate(periodVerificationImplementation.getCheckingTime());

        byId.setDateOfNextCheck(byId.getImplementationDate().minusMonths(byId.getPlannedVerificationCycle()));

        if (periodVerificationImplementationService.save(periodVerificationImplementation) && periodVerificationPlanService
                .updateById(byId)) {

            return periodVerificationImplementation;
        } else throw new RuntimeException(String.format("保存失败"));

    }

    @Override
    public PeriodVerificationImplementation editById(PeriodVerificationImplementationDTO periodVerificationImplementationDTO) {

        PeriodVerificationImplementation byId = periodVerificationImplementationService.getById(periodVerificationImplementationDTO.getPeriodVerificationImplementationId());

        BeanUtils.copyProperties(periodVerificationImplementationDTO, byId);

        String result = periodVerificationImplementationDTO.getResult();

        PeriodVerificationPlan periodVerificationPlan = periodVerificationPlanService.getById(periodVerificationImplementationDTO.getPeriodVerificationPlanId());

        periodVerificationPlan.setVerificationResult(result);

        periodVerificationPlan.setImplementationDate(byId.getCheckingTime());

        if (periodVerificationImplementationService.updateById(byId) && periodVerificationPlanService
                .updateById(periodVerificationPlan)) {

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

    }
    @Override
    public PeriodVerificationImplementation commitById(PeriodVerificationImplementationDTO periodVerificationImplementationDTO, DLPUser dlpUser){

        if (periodVerificationImplementationDTO.getPeriodVerificationImplementationId()==null){

            PeriodVerificationImplementation periodVerificationImplementation = periodVerificationImplementationService.addById(periodVerificationImplementationDTO, dlpUser);

            periodVerificationImplementation.setCommitStatus(1);

           if ( periodVerificationImplementationService.updateById(periodVerificationImplementation)){
               return periodVerificationImplementation;

           }else throw new RuntimeException(String.format("提交失败"));
        }else {

            PeriodVerificationImplementation byId = periodVerificationImplementationService.getById(periodVerificationImplementationDTO.getPeriodVerificationImplementationId());

            byId.setCommitStatus(1);

            if ( periodVerificationImplementationService.updateById(byId)){
                return byId;

            }else throw new RuntimeException(String.format("提交失败"));
        }
    }
    @Override
    public PeriodVerificationImplementation auditById(AuditAndApproveDTO auditAndApproveDTO, DLPUser dlpUser){

        PeriodVerificationImplementation byId = periodVerificationImplementationService.getById(auditAndApproveDTO.getUuId());

        byId.setAuditResultOfTechnical(auditAndApproveDTO.getAuditResult());
        byId.setAuditOpinionOfTechnical(auditAndApproveDTO.getAuditOpinion());
        byId.setAuditTimeOfTechnical(LocalDateTime.now());
        byId.setTechnicalDirectorId(dlpUser.getId());

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



}
