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.Constants;
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.*;
import digital.laboratory.platform.reagent.entity.*;
import digital.laboratory.platform.reagent.mapper.DecentralizedRequestMapper;
import digital.laboratory.platform.reagent.service.*;
import digital.laboratory.platform.reagent.vo.DecentralizedRequestVO;
import org.apache.ibatis.annotations.Param;
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-03-10
 * @describe (分散采购申请) 服务实现类
 */
@Service
@SuppressWarnings("all")
public class DecentralizedRequestServiceImpl extends ServiceImpl<DecentralizedRequestMapper, DecentralizedRequest> implements DecentralizedRequestService {

    @Autowired
    private DecentralizedRequestService decentralizedRequestService;
    @Autowired
    private DecentralizeDetailsService decentralizeDetailsService;
    @Autowired
    private CheckContentService checkContentService;
    @Autowired
    private ComplianceCheckService complianceCheckService;
    @Autowired
    private ReagentConsumablesService reagentConsumablesService;

    @Override//分散采购申请分页
    public IPage<DecentralizedRequestVO> getDecentralizedRequestVOPage
            (@Param("page") IPage<DecentralizedRequest> page, @Param(Constants.WRAPPER) QueryWrapper<DecentralizedRequest> qw) {

        IPage<DecentralizedRequestVO> decentralizedRequestVOPage = baseMapper.getDecentralizedRequestVOPage(page, qw);

        return decentralizedRequestVOPage;
    }


    @Override//分散采购申请列表
    public List<DecentralizedRequestVO> getDecentralizedRequestVOList(@Param(Constants.WRAPPER) QueryWrapper<DecentralizedRequest> qw) {

        List<DecentralizedRequestVO> decentralizedRequestVOList = baseMapper.getDecentralizedRequestVOList(qw);

        return decentralizedRequestVOList;
    }

    @Override//通过ID查询分散采购申请
    public DecentralizedRequestVO getRequestById(String decentralizedRequestId) {

        DecentralizedRequestVO decentralizedRequestVO = baseMapper.getDecentralizedRequest(decentralizedRequestId);

        List<DecentralizeDetails> decentralizeDetailsList = decentralizeDetailsService.getDecentralizeDetailsList(decentralizedRequestId);

        decentralizedRequestVO.setDecentralizeDetailsVOList(decentralizeDetailsList);

        return decentralizedRequestVO;
    }

    @Override//创建分散采购申请
    public DecentralizedRequest addRequest(List<DecentralizedRequestDTO> decentralizedRequestDTOList, DLPUser dlpUser) {

        DecentralizedRequest decentralizedRequest = new DecentralizedRequest();

        decentralizedRequest.setDecentralizedRequestId(IdWorker.get32UUID().toUpperCase());

        decentralizedRequest.setApplicantId(dlpUser.getId());

        decentralizedRequest.setDateOfApplication(LocalDateTime.now());

        decentralizedRequest.setStatus(0);

        List<DecentralizeDetails> list = new ArrayList<>();
        //接收分散采购DTO，将其赋值给分散采购明细
        for (DecentralizedRequestDTO decentralizedRequestDTO : decentralizedRequestDTOList) {
            //新添加试剂耗材

            DecentralizeDetails decentralizedDetails = new DecentralizeDetails();

            BeanUtils.copyProperties(decentralizedRequestDTO, decentralizedDetails);

            decentralizedDetails.setDecentralizeDetailsId(IdWorker.get32UUID().toUpperCase());

            decentralizedDetails.setDecentralizedRequestId(decentralizedRequest.getDecentralizedRequestId());

            list.add(decentralizedDetails);
        }
        if (decentralizedRequestService.save(decentralizedRequest) & decentralizeDetailsService.saveBatch(list)) {
            return decentralizedRequest;
        } else return null;
    }

    @Override
    public DecentralizeDetails addDetails(DecentralizedRequestDTO decentralizedRequestDTO){

        DecentralizeDetails decentralizeDetails = new DecentralizeDetails();

        BeanUtils.copyProperties(decentralizedRequestDTO,decentralizeDetails);

        if (decentralizeDetailsService.save(decentralizeDetails)){
            return decentralizeDetails;
        }else throw new RuntimeException(String.format("保存失败"));
    }

    @Override//修改分散采购申请明细
    public DecentralizeDetails editRequest(DecentralizedRequestDTO decentralizedRequestDTO) {

        DecentralizeDetails decentralizeDetails = decentralizeDetailsService.getById(decentralizedRequestDTO.getDecentralizeDetailsId());

        BeanUtils.copyProperties(decentralizedRequestDTO, decentralizeDetails);

        if (decentralizeDetailsService.updateById(decentralizeDetails)) {
            return decentralizeDetails;
        } else return null;

    }

    @Override//删除分散采购申请
    public Boolean delRequestById(String decentralizedRequestId) {

        LambdaQueryWrapper<DecentralizeDetails> decentralizeDetailsLambdaQueryWrapper = new LambdaQueryWrapper<>();

        decentralizeDetailsLambdaQueryWrapper.eq(DecentralizeDetails::getDecentralizedRequestId, decentralizedRequestId);

        List<DecentralizeDetails> list = decentralizeDetailsService.list(decentralizeDetailsLambdaQueryWrapper);

        return decentralizedRequestService.removeById(reagentConsumablesService.getById(decentralizedRequestId))
                && decentralizeDetailsService.removeBatchByIds(list);

    }

    @Override//提交分散采购申请
    public DecentralizedRequest commitRequest(List<DecentralizedRequestDTO> decentralizedRequestDTOList, DLPUser dlpUser) {

        DecentralizedRequest decentralizedRequest = decentralizedRequestService.addRequest(decentralizedRequestDTOList, dlpUser);

        decentralizedRequest.setStatus(1);

        if (decentralizedRequestService.updateById(decentralizedRequest)) {
            return decentralizedRequest;
        } else return null;
    }

    @Override//一级审核
    public DecentralizedRequest primaryAuditRequest(DLPUser dlpUser, AuditAndApproveDTO auditAndApproveDto) {

        DecentralizedRequest byId = decentralizedRequestService.getById(auditAndApproveDto.getUuId());

        byId.setPrimaryAuditorId(dlpUser.getId());

        byId.setAuditResultOfPrimary(auditAndApproveDto.getAuditResult());

        byId.setAuditOpinionOfPrimary(auditAndApproveDto.getAuditOpinion());

        byId.setAuditTimeOfPrimary(LocalDateTime.now());

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


    }

    @Override//二级审核
    public DecentralizedRequest secondaryAuditRequest(DLPUser dlpUser, AuditAndApproveDTO auditAndApproveDto) {

        DecentralizedRequest byId = decentralizedRequestService.getById(auditAndApproveDto.getUuId());

        byId.setSecondaryAuditorId(dlpUser.getId());

        byId.setAuditResultOfSecondary(auditAndApproveDto.getAuditResult());

        byId.setAuditOpinionOfSecondary(auditAndApproveDto.getAuditOpinion());

        byId.setAuditTimeOfSecondary(LocalDateTime.now());

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

    @Transactional
    @Override//三级审核
    public DecentralizedRequest threeLevelAuditRequest(DLPUser dlpUser, AuditDecentralizedRequestDTO auditDecentralizedRequestDTO) {

        DecentralizedRequest byId = decentralizedRequestService.getById(auditDecentralizedRequestDTO.getUuId());

        byId.setThreeLevelAuditId(dlpUser.getId());

        byId.setAuditResultOfThreeLevel(auditDecentralizedRequestDTO.getAuditResult());

        byId.setAuditOpinionOfThreeLevel(auditDecentralizedRequestDTO.getAuditOpinion());

        byId.setAuditTimeOfThreeLevel(LocalDateTime.now());

        byId.setComplianceTesting(auditDecentralizedRequestDTO.getHaveCheck());

        byId.setStatus(2);

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

    @Override//审批
    @Transactional
    public DecentralizedRequest approveRequest(DLPUser dlpUser, AuditAndApproveDTO auditAndApproveDto) {

        DecentralizedRequest byId = decentralizedRequestService.getById(auditAndApproveDto.getUuId());

        byId.setApproverId(dlpUser.getId());

        byId.setResultOfApproval(auditAndApproveDto.getApproveResult());

        byId.setOpinionOfApproval(auditAndApproveDto.getApproveOpinion());

        byId.setApprovalOfTime(LocalDateTime.now());

        if (byId.getResultOfApproval()==true){
            //如果未购入过该物品，则将该物品信息更新至试剂耗材类
            LambdaQueryWrapper<DecentralizeDetails> decentralizeDetailsLambdaQueryWrapper = new LambdaQueryWrapper<>();

            decentralizeDetailsLambdaQueryWrapper.eq(DecentralizeDetails::getDecentralizedRequestId, byId.getDecentralizedRequestId());

            List<DecentralizeDetails> list = decentralizeDetailsService.list(decentralizeDetailsLambdaQueryWrapper);

            LambdaQueryWrapper<ReagentConsumables> reagentConsumablesLambdaQueryWrapper = new LambdaQueryWrapper<>();

            for (DecentralizeDetails decentralizeDetails : list) {

               if (decentralizeDetails.getReagentConsumableId()==null){

                   ReagentConsumables reagentConsumables = reagentConsumablesService.addReagentConsumables(decentralizeDetails);

                   decentralizeDetails.setReagentConsumableId(reagentConsumables.getReagentConsumableId());

                   decentralizeDetailsService.updateById(decentralizeDetails);
               }

            }
        }
        //是否需要进行符合性检查
//        if (byId.getComplianceTesting()==true){
//
//            LambdaQueryWrapper<DecentralizeDetails> decentralizeDetailsLambdaQueryWrapper = new LambdaQueryWrapper<>();
//
//            decentralizeDetailsLambdaQueryWrapper.eq(DecentralizeDetails::getDecentralizedRequestId,byId.getDecentralizedRequestId());
//
//            List<DecentralizeDetails> list = decentralizeDetailsService.list(decentralizeDetailsLambdaQueryWrapper);
//
//            for (DecentralizeDetails decentralizeDetails : list) {
//
//                ComplianceCheckDTO complianceCheckDTO = new ComplianceCheckDTO();
//
//                complianceCheckDTO.setBrand(decentralizeDetails.getBrand());
//
//                complianceCheckDTO.setReagentConsumableId(decentralizeDetails.getReagentConsumableId());
//
//                complianceCheckDTO.setSpecificationAndModel(complianceCheckDTO.getSpecificationAndModel());
//
//                complianceCheckService.addCheck(complianceCheckDTO,dlpUser);
//            }
//        }

        byId.setStatus(3);
        if (decentralizedRequestService.updateById(byId)) {
            return byId;
        } else return null;

    }
}




