package com.xinelu.manage.service.patientblacklist.impl;

import com.xinelu.common.annotation.DataScope;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.bean.BeanUtils;
import com.xinelu.manage.domain.patientblacklist.PatientBlacklist;
import com.xinelu.manage.domain.patientinfo.PatientInfo;
import com.xinelu.manage.dto.patientblacklist.PatientBlacklistDto;
import com.xinelu.manage.dto.patientblacklist.PatientBlacklistSaveDto;
import com.xinelu.manage.dto.patientinfo.PatientInfoDto;
import com.xinelu.manage.mapper.patientblacklist.PatientBlacklistMapper;
import com.xinelu.manage.mapper.patientinfo.PatientInfoMapper;
import com.xinelu.manage.service.patientblacklist.IPatientBlacklistService;
import com.xinelu.manage.vo.patientblacklist.PatientBlacklistVo;
import java.time.LocalDateTime;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 患者-黑明单关系Service业务层处理
 *
 * @author haown
 * @date 2024-02-27
 */
@Service
public class PatientBlacklistServiceImpl implements IPatientBlacklistService {
    @Resource
    private PatientBlacklistMapper patientBlacklistMapper;

    @Resource
    private PatientInfoMapper patientInfoMapper;

    /**
     * 查询患者-黑明单关系
     *
     * @param id 患者-黑明单关系主键
     * @return 患者-黑明单关系
     */
    @Override
    public PatientBlacklistVo selectPatientBlacklistById(Long id) {
        return patientBlacklistMapper.selectPatientBlacklistById(id);
    }

    /**
     * 查询患者-黑明单关系列表
     *
     * @param patientBlacklist 患者-黑明单关系
     * @return 患者-黑明单关系
     */
    @Override
    @DataScope(agencyAlias = "b", deptAlias = "b", userAlias = "p.attending_physician_id")
    public List<PatientBlacklistVo> selectPatientBlacklistList(PatientBlacklistDto patientBlacklist) {
        return patientBlacklistMapper.selectPatientBlacklistList(patientBlacklist);
    }

    /**
     * 新增患者-黑明单关系
     *
     * @param blacklistSaveDto 患者-黑明单关系
     * @return 结果
     */
    @Override
    public int insertPatientBlacklist(PatientBlacklistSaveDto blacklistSaveDto) {
        // 根据患者姓名、身份证号查询患者
        PatientInfoDto patientInfoDto = new PatientInfoDto();
        patientInfoDto.setPatientName(blacklistSaveDto.getPatientName());
        patientInfoDto.setPatientPhone(blacklistSaveDto.getPatientPhone());
        patientInfoDto.setCardNo(blacklistSaveDto.getCardNo());
	    patientInfoDto.setHospitalAgencyId(blacklistSaveDto.getHospitalAgencyId());
        List<PatientInfo> patientList = patientInfoMapper.selectPatientInfoList(patientInfoDto);
        if (CollectionUtils.isEmpty(patientList)) {
            throw new ServiceException("未找到该患者，请确认姓名身份证号手机号是否正确");
        }
        if (patientList.size() > 1) {
            throw new ServiceException("找到多名患者信息，请确认姓名身份证号手机号是否正确");
        }
		// 查询患者是否已加入黑名单
	    PatientBlacklistDto blacklistQuery = new PatientBlacklistDto();
		blacklistQuery.setPatientId(patientList.get(0).getId());
	    List<PatientBlacklistVo> patientBlacklists = patientBlacklistMapper.selectPatientBlacklistList(blacklistQuery);
		if (!CollectionUtils.isEmpty(patientBlacklists)) {
			throw new ServiceException("该患者已加入黑名单，请勿重复加入");
		}
        PatientBlacklist patientBlacklist = new PatientBlacklist();
        BeanUtils.copyBeanProp(patientBlacklist, blacklistSaveDto);
        patientBlacklist.setPatientId(patientList.get(0).getId());
        patientBlacklist.setDelFlag(0);
        patientBlacklist.setCreateTime(LocalDateTime.now());
        return patientBlacklistMapper.insertPatientBlacklist(patientBlacklist);
    }

    /**
     * 修改患者-黑明单关系
     *
     * @param patientBlacklist 患者-黑明单关系
     * @return 结果
     */
    @Override
    public int updatePatientBlacklist(PatientBlacklist patientBlacklist) {
        patientBlacklist.setUpdateTime(LocalDateTime.now());
        return patientBlacklistMapper.updatePatientBlacklist(patientBlacklist);
    }

    /**
     * 批量删除患者-黑明单关系
     *
     * @param ids 需要删除的患者-黑明单关系主键
     * @return 结果
     */
    @Override
    public int deletePatientBlacklistByIds(Long[] ids) {
        return patientBlacklistMapper.deletePatientBlacklistByIds(ids);
    }

    /**
     * 删除患者-黑明单关系信息
     *
     * @param id 患者-黑明单关系主键
     * @return 结果
     */
    @Override
    public int deletePatientBlacklistById(Long id) {
        PatientBlacklist patientBlacklist = new PatientBlacklist();
        patientBlacklist.setId(id);
        patientBlacklist.setDelFlag(1);
        patientBlacklist.setUpdateTime(LocalDateTime.now());
        return patientBlacklistMapper.updatePatientBlacklist(patientBlacklist);
    }
}
