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.QcAbnormalDTO;
import com.diagbot.dto.QcAbnormalModeDTO;
import com.diagbot.dto.QcModeAllDTO;
import com.diagbot.entity.ModelHospital;
import com.diagbot.entity.QcAbnormal;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.service.impl.QcAbnormalServiceImpl;
import com.diagbot.util.DateUtil;
import com.diagbot.util.SysUserUtils;
import com.diagbot.vo.QcAbnormalDelVO;
import com.diagbot.vo.QcAbnormalModeVO;
import com.diagbot.vo.QcAbnormalSaveNameVO;
import com.diagbot.vo.QcAbnormalSaveVO;
import com.diagbot.vo.QcAbnormalVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Description:
 * @author: gaodm
 * @time: 2020/5/15 9:26
 */
@Component
public class QcAbnormalFacade extends QcAbnormalServiceImpl {

    @Autowired
    private FilterFacade filterFacade;
    @Autowired
    QcModeFacade qcModeFacade;
    @Autowired
    ModelHospitalFacade modelHospitalFacade;
    /**
     * 查询异常数据监控信息
     *
     * @param qcAbnormalVO 异常数据监控信息查询参数
     * @return 异常数据监控信息
     */
    public IPage<QcAbnormalDTO> getQcAnnormalFac(QcAbnormalVO qcAbnormalVO) {
        String startDate = filterFacade.getStartDateStr(qcAbnormalVO.getStatisticsType(), null);
        String endDate = filterFacade.getEndDateStr(qcAbnormalVO.getStatisticsType(), null);
        qcAbnormalVO.setStartDate(startDate);
        qcAbnormalVO.setEndDate(endDate);
        qcAbnormalVO.setHospitalId(Long.valueOf(SysUserUtils.getCurrentHospitalID()));
        return this.getQcAnnormal(qcAbnormalVO);
    }

    /**
     * 更新异常数据监控信息
     *
     * @param qcAbnormalSaveVO 异常数据监控信息更新参数
     * @return 是否成功
     */
    public Boolean saveQcAnnormal(QcAbnormalSaveVO qcAbnormalSaveVO) {
        return this.update(new UpdateWrapper<QcAbnormal>()
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("hospital_id", SysUserUtils.getCurrentHospitalID())
                .eq("id", qcAbnormalSaveVO.getId())
                .set("status", qcAbnormalSaveVO.getStatus())
                .set("remark", qcAbnormalSaveVO.getRemark())
                .set("gmt_modified", DateUtil.now())
                .set("modifier", SysUserUtils.getCurrentPrincipleID())
        );
    }

    /**
     * 删除异常数据监控信息
     *
     * @param qcAbnormalDelVO 异常数据监控信息更新参数
     * @return 是否成功
     */
    public Boolean delQcAnnormal(QcAbnormalDelVO qcAbnormalDelVO) {
        return this.update(new UpdateWrapper<QcAbnormal>()
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("hospital_id", SysUserUtils.getCurrentHospitalID())
                .eq("id", qcAbnormalDelVO.getId())
                .set("is_deleted", IsDeleteEnum.Y.getKey())
                .set("gmt_modified", DateUtil.now())
                .set("modifier", SysUserUtils.getCurrentPrincipleID())
        );
    }
    /**
     * 根据异常异常描述和异常类型 更新所有异常异常描一样的数据。
     *
     * @param qcAbnormalSaveNameVO 异常数据监控信息更新参数
     * @return 是否成功
     */
    public Boolean saveQcAnnormalNameAll(QcAbnormalSaveNameVO qcAbnormalSaveNameVO) {

        boolean res = false;
            res = this.update(new UpdateWrapper<QcAbnormal>()
                    .eq("is_deleted", IsDeleteEnum.N.getKey())
                    .eq("hospital_id", SysUserUtils.getCurrentHospitalID())
                    .eq("type", 1)//异常类型(1:数据模块 2：质控类型 3：XML结构)
                    .eq("description", qcAbnormalSaveNameVO.getDescription())
                    .set("status", qcAbnormalSaveNameVO.getStatus())
                    .set("gmt_modified", DateUtil.now())
                    .set("modifier", SysUserUtils.getCurrentPrincipleID())
            );
            //最后更新备注
            res = this.update(new UpdateWrapper<QcAbnormal>()
                    .eq("id", qcAbnormalSaveNameVO.getId())
                    .eq("is_deleted", IsDeleteEnum.N.getKey())
                    .set("remark", qcAbnormalSaveNameVO.getRemark())
            );
        return res;
    }


    public QcAbnormalModeDTO getQcAnnormalMode(QcAbnormalModeVO qcAbnormalModeVO) {
        QcAbnormalModeDTO data = new QcAbnormalModeDTO();

        QueryWrapper<ModelHospital> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("hospital_id", SysUserUtils.getCurrentHospitalID())
                .eq("hospital_model_name", qcAbnormalModeVO.getDescription());
        ModelHospital modeHospital = modelHospitalFacade.getOne(queryWrapper, false);
        List<QcModeAllDTO> dataMode = qcModeFacade.getModeAll();
        data.setQcModeDatas(dataMode);
        data.setDescription(qcAbnormalModeVO.getDescription());
        if(modeHospital==null){
            data.setModeId(0L);
            data.setModeMame("");
        }else {
            data.setModeId(modeHospital.getStandModelId());
            data.setModeMame(modeHospital.getStandModelName());
        }
        return data;
    }
}
