package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.diagbot.dto.QcCasesAllDTO;
import com.diagbot.dto.QcCasesEntryFindDTO;
import com.diagbot.dto.QcCasesEntryHospitalDTO;
import com.diagbot.entity.QcCasesEntry;
import com.diagbot.entity.QcCasesEntryHospital;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;
import com.diagbot.service.impl.QcCasesEntryHospitalServiceImpl;
import com.diagbot.util.DateUtil;
import com.diagbot.util.SysUserUtils;
import com.diagbot.vo.QcCasesEntryFindVO;
import com.diagbot.vo.QcCasesEntryHospitalVO;
import com.diagbot.vo.QcCasesEntryUpdataVO;
import com.diagbot.vo.QcCasesEntryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author wangfeng
 * @Description:
 * @time: 2024-04-13 16:23
 */
@Component
public class QcCasesEntryHospitalFacade extends QcCasesEntryHospitalServiceImpl {
    @Autowired
    private QcCasesFacade qcCasesFacade;
    @Autowired
    private QcCasesEntryFacade qcCasesEntryFacade;

    /**
     *
     * @param qcCasesEntryHospitalVO
     * @return
     */
    public IPage<List<QcCasesEntryHospitalDTO>> QcCasesEntryHospitals(QcCasesEntryHospitalVO qcCasesEntryHospitalVO) {
        IPage<List<QcCasesEntryHospitalDTO>> data = this.getQcCasesEntryAlls(qcCasesEntryHospitalVO);
        return data;
    }

    /**
     *
     * @param qcCasesEntryUpdataVO
     * @return
     */
    public boolean updataQcCasesEntrys(QcCasesEntryUpdataVO qcCasesEntryUpdataVO) {
        //1.先判断数据是否存在有效
        QueryWrapper<QcCasesEntryHospital> qc = new QueryWrapper<>();
        qc.eq("id", qcCasesEntryUpdataVO.getId())
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("hospital_id", Long.valueOf(SysUserUtils.getCurrentHospitalID()));
        int sum = count(qc);
        if (sum == 0) {
            throw new CommonException(CommonErrorCode.NOT_EXISTS, "该条目不存在");
        }
        QcCasesEntryHospital entryHospital = this.lambdaQuery()
                .eq(QcCasesEntryHospital::getId, qcCasesEntryUpdataVO.getId())
                .one();
        if (entryHospital != null) {
            QcCasesEntry one = qcCasesEntryFacade.lambdaQuery()
                    .eq(QcCasesEntry::getId, entryHospital.getCasesEntryId())
                    .one();
            if (one != null) {
                one.setName(qcCasesEntryUpdataVO.getName());
                one.setSyRuleType(qcCasesEntryUpdataVO.getSyRuleType());
                one.setRemark(qcCasesEntryUpdataVO.getRemark());
                one.setCasesName(qcCasesEntryUpdataVO.getCasesName());
                one.setRemindType(qcCasesEntryUpdataVO.getRemindType());
                one.setRemindLevel(qcCasesEntryUpdataVO.getRemindLevel());
                qcCasesEntryFacade.saveOrUpdate(one);
            }
        }

        UpdateWrapper<QcCasesEntryHospital> qcCasesEntry = new UpdateWrapper<>();
        qcCasesEntry.eq("id", qcCasesEntryUpdataVO.getId())
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("hospital_id", Long.valueOf(SysUserUtils.getCurrentHospitalID()))
                .set("modifier", SysUserUtils.getCurrentPrincipleID())
                .set("gmt_modified", DateUtil.now());
        qcCasesEntry.set("1".equals(qcCasesEntryUpdataVO.getIsPlacefile()) && qcCasesEntryUpdataVO.getScore() != null,
                "score", qcCasesEntryUpdataVO.getScore());
        qcCasesEntry.set(qcCasesEntryUpdataVO.getMsg() != null,
                "msg", qcCasesEntryUpdataVO.getMsg());
        qcCasesEntry.set("1".equals(qcCasesEntryUpdataVO.getIsPlacefile()) && qcCasesEntryUpdataVO.getIsUsed() != null,
                "is_used", qcCasesEntryUpdataVO.getIsUsed());
        qcCasesEntry.set(qcCasesEntryUpdataVO.getIsReject() != null,
                "is_reject", qcCasesEntryUpdataVO.getIsReject());
        qcCasesEntry.set("0".equals(qcCasesEntryUpdataVO.getIsPlacefile()) && qcCasesEntryUpdataVO.getScore() != null,
                "score_run", qcCasesEntryUpdataVO.getScore());
        qcCasesEntry.set("0".equals(qcCasesEntryUpdataVO.getIsPlacefile()) && qcCasesEntryUpdataVO.getIsUsed() != null,
                "is_used_run", qcCasesEntryUpdataVO.getIsUsed());
        return update(new QcCasesEntryHospital(), qcCasesEntry);
    }

    /**
     *
     * @return
     */
    public List<QcCasesAllDTO> getQcCasesAlls(QcCasesEntryVO qcCasesEntryVO) {
        return  qcCasesFacade.getQcCasesAlls(qcCasesEntryVO);
    }

    /**
     *
     * @param qcCasesEntryFindVO
     * @return
     */
    public List<QcCasesEntryFindDTO> findQcCasesEntryAlls(QcCasesEntryFindVO qcCasesEntryFindVO) {
        List<QcCasesEntryFindDTO> data = this.findQcCasesEntryAll(qcCasesEntryFindVO);
        return data;
    }
}
