package com.ruoyi.web.work.service.impl;


import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.web.work.domain.*;
import com.ruoyi.web.work.domain.dto.PatientCardDto;
import com.ruoyi.web.work.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.work.service.IVisitService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @author lsw
 * @date 2024-07-23
 */
@Service
public class VisitServiceImpl extends ServiceImpl<VisitMapper, Visit> implements IVisitService {
    @Autowired
    private VisitMapper visitMapper;

    @Autowired
    private SysUserMapper userMapper;


    @Override
    public List<Visit> selectList(Visit visit) {
        return visitMapper.queryList(visit);
    }

    @Override
    @DataScope(deptAlias = "v",userAlias = "u")
    public List<PatientCardDto> getPatientCardByNameOrDepartmentOrPhysician(PatientCardDto patientCardDto) {
        return visitMapper.getPatientCardByNameOrDepartmentOrPhysician(patientCardDto);

    }


    @Override
    public List<Visit> getVisitByPatId(String patId,String startTime,String endTime) {
        return visitMapper.selectVisitsByPatId(patId,startTime,endTime);
    }

    /**
     * 根据deptId获取所有部门的患者
     * @param deptId
     * @return
     */
    @Override
    public List<Visit> getPatientByDeptId(Long deptId,String userName) {
        SysUser sysUser = userMapper.queryUserByUserName(userName);
        if (sysUser != null){
            String dirDocSign = sysUser.getDirDocSign();
            if ("1".equals(dirDocSign)){
                //获取所有部门下的所有患者
                return visitMapper.selectVisitsByDeptId(deptId);
            }else {
                //获取当前用户部门下的患者信息
                return visitMapper.selectVisitsByIdAndUserName(deptId,userName);
            }
        }
        throw new ServiceException("当前用户不存在");
    }
}
