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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.PeriodVerificationPlanDTO;
import digital.laboratory.platform.reagent.entity.CheckSchedule;
import digital.laboratory.platform.reagent.entity.PeriodVerificationPlan;
import digital.laboratory.platform.reagent.mapper.CheckScheduleMapper;
import digital.laboratory.platform.reagent.service.CheckScheduleService;
import digital.laboratory.platform.reagent.service.PeriodVerificationPlanService;
import digital.laboratory.platform.reagent.vo.CheckScheduleVO;
import digital.laboratory.platform.reagent.vo.PeriodVerificationPlanVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 服务实现类
 *
 * @author Zhang Xiaolong created at  2023-04-10
 * @describe 服务实现类
 */
@Service
public class CheckScheduleServiceImpl extends ServiceImpl<CheckScheduleMapper, CheckSchedule> implements CheckScheduleService {

    @Autowired
    private CheckScheduleService checkScheduleService;
    @Autowired
    private PeriodVerificationPlanService periodVerificationPlanService;

    @Override
    @Transactional
    public CheckSchedule addPlan(List<PeriodVerificationPlanDTO> periodVerificationPlanDTOS, DLPUser dlpUser) {

        CheckSchedule checkSchedule = new CheckSchedule();

        checkSchedule.setCheckScheduleId(IdWorker.get32UUID().toUpperCase());
        checkSchedule.setManagerId(dlpUser.getId());

        List<PeriodVerificationPlan> periodVerificationPlans = new ArrayList<>();

        for (PeriodVerificationPlanDTO periodVerificationPlanDTO : periodVerificationPlanDTOS) {

            PeriodVerificationPlan periodVerificationPlan = new PeriodVerificationPlan();

            BeanUtils.copyProperties(periodVerificationPlanDTO, periodVerificationPlan);

            periodVerificationPlan.setCheckScheduleId(checkSchedule.getCheckScheduleId());

            periodVerificationPlans.add(periodVerificationPlan);
        }

        if (checkScheduleService.save(checkSchedule) && periodVerificationPlanService.saveBatch(periodVerificationPlans)) {

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


    @Override
    @Transactional
    public CheckSchedule editPlan(List<PeriodVerificationPlanDTO> periodVerificationPlanDTOS) {

        CheckSchedule byId = checkScheduleService.getById(periodVerificationPlanDTOS.get(0).getCheckScheduleId());

        LambdaQueryWrapper<PeriodVerificationPlan> periodVerificationPlanLambdaQueryWrapper = new LambdaQueryWrapper<>();

        periodVerificationPlanLambdaQueryWrapper.eq(PeriodVerificationPlan::getCheckScheduleId, byId.getCheckScheduleId());

        List<PeriodVerificationPlan> list = periodVerificationPlanService.list(periodVerificationPlanLambdaQueryWrapper);

        periodVerificationPlanService.removeBatchByIds(list);

        List<PeriodVerificationPlan> periodVerificationPlans = new ArrayList<>();

        for (PeriodVerificationPlanDTO periodVerificationPlanDTO : periodVerificationPlanDTOS) {

            PeriodVerificationPlan periodVerificationPlan = new PeriodVerificationPlan();

            BeanUtils.copyProperties(periodVerificationPlanDTO, periodVerificationPlan);

            periodVerificationPlan.setCheckScheduleId(byId.getCheckScheduleId());

            periodVerificationPlans.add(periodVerificationPlan);
        }

        if (checkScheduleService.updateById(byId) && periodVerificationPlanService.saveBatch(periodVerificationPlans)) {

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

    @Override
    @Transactional
    public CheckSchedule commitPlan(List<PeriodVerificationPlanDTO> periodVerificationPlanDTOS, DLPUser dlpUser) {

        CheckSchedule byId = checkScheduleService.getById(periodVerificationPlanDTOS.get(0).getCheckScheduleId());

        if (byId == null) {

            CheckSchedule checkSchedule = checkScheduleService.addPlan(periodVerificationPlanDTOS, dlpUser);

            checkSchedule.setStatus(1);
            checkScheduleService.updateById(checkSchedule);

            return checkSchedule;
        } else {
            byId.setStatus(1);

            checkScheduleService.updateById(byId);}

        return byId;
    }

    @Override
    public CheckSchedule auditPlan(AuditAndApproveDTO auditAndApproveDTO,DLPUser dlpUser){

        CheckSchedule byId = checkScheduleService.getById(auditAndApproveDTO.getUuId());

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

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

    @Override
    public IPage<CheckScheduleVO> getCheckScheduleVOPage(IPage<CheckSchedule> page, QueryWrapper<CheckSchedule> qw) {

        IPage<CheckScheduleVO> checkScheduleVOPage = baseMapper.getCheckScheduleVOPage(page, qw);

        return checkScheduleVOPage;
    }

    @Override
    public CheckScheduleVO getCheckScheduleVO(String checkScheduleId) {

        CheckScheduleVO checkScheduleVO = baseMapper.getCheckScheduleVO(checkScheduleId);

        List<PeriodVerificationPlanVO> periodVerificationPlanVOList = periodVerificationPlanService.getPeriodVerificationPlanVOList(checkScheduleVO.getCheckScheduleId());

        checkScheduleVO.setPeriodVerificationPlanVOS(periodVerificationPlanVOList);

        return checkScheduleVO;
    }


}
