package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diagbot.dto.QcCasesAllDTO;
import com.diagbot.dto.QcCasesDTO;
import com.diagbot.entity.QcCases;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;
import com.diagbot.service.impl.QcCasesServiceImpl;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.DateUtil;
import com.diagbot.util.ListUtil;
import com.diagbot.util.SysUserUtils;
import com.diagbot.vo.QcCasesEntryVO;
import com.diagbot.vo.QcCasesQueryVO;
import com.diagbot.vo.QcCasesSaveListVO;
import com.diagbot.vo.QcCasesSaveVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 模块数据标准装饰类
 * @author: gaodm
 * @time: 2020/4/12 14:07
 */
@Component
public class QcCasesFacade extends QcCasesServiceImpl {

    @Autowired
    private QcCasesHospitalFacade qcCasesHospitalFacade;

    /**
     * 获取医院模块数据信息
     *
     * @return 医院模块数据信息
     */
    public List<QcCasesDTO> getQcCasesFac() {
        QcCasesQueryVO queryVO = new QcCasesQueryVO();
        queryVO.setHospitalId(Long.valueOf(SysUserUtils.getCurrentHospitalID()));
        return this.getQcCases(queryVO);
    }

    /**
     * 批量更新模块数据
     *
     * @param qcCasesSaveVOList 更新的模块数据
     * @return 是否成功
     */
    public Boolean saveQcCases(QcCasesSaveListVO qcCasesSaveVOList) {
        if (null != qcCasesSaveVOList
                && ListUtil.isNotEmpty(qcCasesSaveVOList.getQcCasesSaveVOList())) {
            Date now = DateUtil.now();
            for (QcCasesSaveVO qcCasesSaveVO : qcCasesSaveVOList.getQcCasesSaveVOList()) {
                qcCasesSaveVO.setHospitalId(Long.valueOf(SysUserUtils.getCurrentHospitalID()));
                qcCasesSaveVO.setModifier(SysUserUtils.getCurrentPrincipleID());
                qcCasesSaveVO.setGmtModified(now);
            }
        } else {
            throw new CommonException(CommonErrorCode.PARAM_IS_NULL);
        }
        qcCasesHospitalFacade.updateBatch(qcCasesSaveVOList.getQcCasesSaveVOList());
        return true;

    }

    /**
     * 获取基础模块数据信息
     *
     * @return 基础模块数据信息列表
     */
    public List<QcCasesAllDTO> getQcCasesAlls(QcCasesEntryVO qcCasesEntryVO) {
        QueryWrapper<QcCases> qc = new QueryWrapper<>();
        qc.eq("is_deleted", IsDeleteEnum.N.getKey());
        if(StringUtils.isNotBlank(qcCasesEntryVO.getCasesName())){
            qc.like("name", qcCasesEntryVO.getCasesName());
        }
        List<QcCases> data = list(qc);
        List<QcCasesAllDTO> dataNew = new ArrayList<QcCasesAllDTO>();
        dataNew = BeanUtil.listCopyTo(data, QcCasesAllDTO.class);
        return dataNew;
    }

    public QcCasesDTO getQcCasesById(String id, String currentHospitalID) {
        return this.getBaseMapper().getQcCasesById(id,currentHospitalID);
    }
}
