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.OperationInfo;
import com.lantone.daqe.service.impl.OperationInfoServiceImpl;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>
 * 表名：hos_operation_info 业务类
 * </p>
 */
@Component
public class OperationInfoFacade extends OperationInfoServiceImpl {

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

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

}