package com.lantone.report.facade;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lantone.common.enums.IsDeleteEnum;
import com.lantone.common.exception.Asserts;
import com.lantone.report.dto.GetAppealInfoDTO;
import com.lantone.report.dto.GetAppealReviewDTO;
import com.lantone.report.dto.GetComplaintRecordDTO;
import com.lantone.report.entity.AppealExamineInfo;
import com.lantone.report.entity.AppealInfo;
import com.lantone.report.entity.QcresultDetail;
import com.lantone.report.enums.AppealOperationTypeEnum;
import com.lantone.report.enums.ExampleStatusEnum;
import com.lantone.report.facade.base.AppealExamineInfoFacade;
import com.lantone.report.facade.base.AppealInfoFacade;
import com.lantone.report.facade.base.QcresultDetailFacade2;
import com.lantone.report.vo.GetAppealInfoVO;
import com.lantone.report.vo.GetAppealReviewVO;
import com.lantone.report.vo.GetAuditNumberVO;
import com.lantone.report.vo.GetComplaintDetailMsgVO;
import com.lantone.report.vo.GetComplaintRecordVO;
import com.lantone.report.vo.RejectedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * <p>
 * 申诉审核
 * </p>
 *
 * @author zhanghang
 * @since 2022/2/24
 */

@Component
public class MedAppealExamineInfoManagementFacade {
    @Autowired
    private AppealExamineInfoFacade appealExamineInfoFacade;
    @Autowired
    private AppealInfoFacade appealInfoFacade;
    @Autowired
    MedAppealInfoManagementFacade medAppealInfoManagementFacade;
    @Autowired
    QcresultDetailFacade2 qcresultDetailFacade2;

    /**
     * 申诉记录列表
     *
     * @param getComplaintRecordVO
     * @return
     */
    public Page<GetComplaintRecordDTO> getComplaintRecord(GetComplaintRecordVO getComplaintRecordVO) {
        return appealExamineInfoFacade.getBaseMapper().getComplaintRecord(getComplaintRecordVO);
    }

    /**
     * 申诉审核列表
     *
     * @param getAppealReviewVO
     * @return
     */
    public Page<GetAppealReviewDTO> getAppealReview(GetAppealReviewVO getAppealReviewVO) {
        return appealExamineInfoFacade.getBaseMapper().getAppealReview(getAppealReviewVO);
    }

    /**
     * 驳回
     *
     * @param rejectedVo
     * @return
     */
    public Boolean rejected(RejectedVo rejectedVo) {
        AppealInfo appealInfo = new AppealInfo();
        appealInfo.setWorkFlowNodeId(rejectedVo.getWorkFlowNodeId());
        int appealInfoUpdate = appealInfoFacade.getBaseMapper().update(appealInfo, new UpdateWrapper<AppealInfo>()
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("hospital_id", rejectedVo.getHospitalId())
                .eq("behospital_code", rejectedVo.getBehospitalCode())
                .eq("check_id", rejectedVo.getAppealId())
                .eq("id", rejectedVo.getId())
        );
        AppealExamineInfo appealExamineInfo = new AppealExamineInfo();
        appealExamineInfo.setExampleStatus(ExampleStatusEnum.REJECT_EXAMPLE.getKey());
        appealExamineInfo.setGmtCreate(new Date());
        appealExamineInfo.setRejectReason(rejectedVo.getRejectedReason());
        int appealExamineInfoUpdate = appealExamineInfoFacade.getBaseMapper().update(appealExamineInfo, new UpdateWrapper<AppealExamineInfo>()
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("appeal_info_id", rejectedVo.getId())
                .eq("check_id", rejectedVo.getAppealId())
        );
        if (appealInfoUpdate == 1 && appealExamineInfoUpdate == 1) {
            return true;
        } else {
            Asserts.fail("驳回失败!");
            return false;
        }
    }

    /**
     * 获取待审核数量
     *
     * @param getAuditNumberVO
     * @return
     */
    public Integer getAuditNumber(GetAuditNumberVO getAuditNumberVO) {
        return appealExamineInfoFacade.getBaseMapper().getAuditNumber(getAuditNumberVO);
    }

    /**
     * 审核查看
     *
     * @param getAppealInfoVO
     * @return
     */
    public GetAppealInfoDTO getApprovedView(GetAppealInfoVO getAppealInfoVO) {
        //1.获取病历基本信息和病历内容
        GetAppealInfoDTO getAppealInfoDTO = appealInfoFacade.getBaseMapper().getAppealInfo(getAppealInfoVO);
        //2.获取该条申诉记录详情
        AppealInfo appealInfo = appealInfoFacade.getById(getAppealInfoVO.getId());
        if (appealInfo != null) {
            //如果是新增缺失，获取申诉说明（通过申诉说明区别该病历新增的新增缺失）
            if (AppealOperationTypeEnum.ADD_NO_EXIST.getKey().equals(appealInfo.getAppealOperationType())) {
                getAppealInfoVO.setAppealExplain(appealInfo.getAppealExplain());
            }
            getAppealInfoVO.setModeId(appealInfo.getModeId());
        }
        //3.获取申诉审核记录
        getAppealInfoDTO.setAppealExamineRecordDTOList(appealInfoFacade.getBaseMapper().getAppealExamineRecords(getAppealInfoVO));
        return getAppealInfoDTO;
    }

    /**
     * 获取该申诉记录最新缺陷详情
     *
     * @param getComplaintDetailMsgVO
     * @return
     */
    public QcresultDetail getComplaintDetailMsg(GetComplaintDetailMsgVO getComplaintDetailMsgVO) {
        QcresultDetail qcresultDetail = qcresultDetailFacade2.getOne(new QueryWrapper<QcresultDetail>()
                .eq("behospital_code", getComplaintDetailMsgVO.getBehospitalCode())
                .eq("id", getComplaintDetailMsgVO.getId())
                .eq("cases_entry_id", getComplaintDetailMsgVO.getCasesEntryId())
                .eq("hospital_id", getComplaintDetailMsgVO.getHospitalId())
        );
        return qcresultDetail;
    }

}
