package com.lantone.daqe.facade.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantone.common.exception.Asserts;
import com.lantone.common.util.ListUtil;
import com.lantone.common.util.StringUtil;
import com.lantone.daqe.entity.DiseaseInfo;
import com.lantone.daqe.service.impl.DiseaseInfoServiceImpl;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>
 * 表名：hos_disease_info 业务类
 * </p>
 */
@Component
public class DiseaseInfoFacade extends DiseaseInfoServiceImpl {

    /**
     * 判断即将新增/修改的诊断是否与已存在的其他诊断存在冲突
     *
     * @param diseaseInfo 即将新增/修改的诊断，有id是修改，无id是新增。
     * @return true-存在冲突，false-无冲突
     */
    public boolean isExist(DiseaseInfo diseaseInfo) {
        if (diseaseInfo.getHospitalId() == null
                || StringUtil.isBlank(diseaseInfo.getCode())
                || StringUtil.isBlank(diseaseInfo.getName())) {
            Asserts.fail("判断即将新增/修改的诊断是否与已存在的其他诊断存在冲突时，hospitalId、code、name不能为空！");
        }
        QueryWrapper<DiseaseInfo> diseaseInfoQueryWrapper = new QueryWrapper<>();
        diseaseInfoQueryWrapper.eq("hospital_id", diseaseInfo.getHospitalId());
        diseaseInfoQueryWrapper.eq("code", diseaseInfo.getCode());
        diseaseInfoQueryWrapper.eq("name", diseaseInfo.getName());
        List<DiseaseInfo> diseaseInfoList = list(diseaseInfoQueryWrapper);
        if (diseaseInfo.getId() == null) {
            return ListUtil.isNotEmpty(diseaseInfoList);
        } else {
            return diseaseInfoList.size() > 1
                    || (diseaseInfoList.size() == 1
                    && diseaseInfoList.get(0).getId().longValue() != diseaseInfo.getId().longValue());
        }
    }

    /**
     * 即将新增/修改的诊断与已存在的其他诊断存在冲突时，抛出异常
     *
     * @param diseaseInfo 即将新增/修改的诊断，有id是修改，无id是新增。
     */
    public void assertIsExist(DiseaseInfo diseaseInfo) {
        if (isExist(diseaseInfo)) {
            Asserts.fail("与已存在的其他诊断存在冲突！");
        }
    }

}