package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.diagbot.dto.QcCasesDTO;
import com.diagbot.dto.QcCasesEntryHospitalDTO;
import com.diagbot.dto.SysOperationLogDTO;
import com.diagbot.entity.BasDeptInfo;
import com.diagbot.entity.QcType;
import com.diagbot.dto.SysUserRoleDTO;
import com.diagbot.entity.SysRole;
import com.diagbot.entity.SysUser;
import com.diagbot.entity.SysUserDept;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.service.impl.SysOperationLogServiceImpl;
import com.diagbot.util.SysUserUtils;
import com.diagbot.vo.SysOperationLogVO;
import com.diagbot.vo.SysUserBaseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @Description:操作日志处理类
 * @author: songxl
 * @time: 2022/5/10 9:49
 */
@Component
public class OperationLogFacade extends SysOperationLogServiceImpl {
    @Autowired
    QcCasesFacade qcCasesFacade;
    @Autowired
    QcCasesEntryHospitalFacade qcCasesEntryHospitalFacade;
    @Autowired
    QcTypeFacade qcTypeFacade;
    @Autowired
    SysUserFacade sysUserFacade;
    @Autowired
    SysUserDeptFacade sysUserDeptFacade;
    @Autowired
    BasDeptInfoFacade basDeptInfoFacade;

    @Autowired
    SysRoleFacade sysRoleFacade;

    public IPage<SysOperationLogDTO> getOperationLog(SysOperationLogVO sysOperationLogVO) {
        sysOperationLogVO.setHospitalId(SysUserUtils.getCurrentHospitalID());
        return this.getBaseMapper().getOperationLog(sysOperationLogVO);
    }

    public QcCasesDTO getQcCasesById(String id) {
        return qcCasesFacade.getQcCasesById(id, SysUserUtils.getCurrentHospitalID());
    }

    public SysUser getSysUser(Long id) {
        return sysUserFacade.getBaseMapper().selectOne(new QueryWrapper<SysUser>()
                .eq("id",id)
                .eq("is_deleted", IsDeleteEnum.N.getKey()));
    }

    public SysRole getRoleMenu(Long roleId) {
        return sysRoleFacade.getBaseMapper().selectOne(new QueryWrapper<SysRole>()
                .eq("id",roleId)
                .eq("is_deleted", IsDeleteEnum.N.getKey()));
    }

    public SysUserRoleDTO editUserRoles(Long userId) {
        SysUserBaseVO sysUserBaseVO = new SysUserBaseVO();
        sysUserBaseVO.setUserId(userId);
        return sysUserFacade.getUserRoles(sysUserBaseVO);
    }

    public QcCasesEntryHospitalDTO getQcCasesEntryById(Long id) {
        return qcCasesEntryHospitalFacade.getQcCasesEntryById(id);
    }

    public QcType getQCTypeById(Object id) {
        return qcTypeFacade.getOne(new QueryWrapper<QcType>().eq("id",id));
    }

    public List<SysRole> getSysUserRoles(List<Long> roleIds) {
        return sysRoleFacade.list(new QueryWrapper<SysRole>().in("id",roleIds).eq("is_deleted", IsDeleteEnum.N.getKey()));
    }
    /**
     * 获取用户科室
     *
     * @param userId
     * @Return java.util.List<java.lang.String>
     */
    public List<String> getUserDept(Long userId) {

        return sysUserDeptFacade.list(new QueryWrapper<SysUserDept>()
                .eq("user_id",userId)
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("hospital_id",SysUserUtils.getCurrentHospitalID())
        ).stream().map(SysUserDept::getDeptId).collect(Collectors.toList());
    }
    /**
     * 通过科室id获取科室
     *
     * @param deptIds
     * @Return java.util.List<com.diagbot.entity.BasDeptInfo>
     */
    public List<BasDeptInfo> getSysDepts(List<String> deptIds) {

        return basDeptInfoFacade.list(new QueryWrapper<BasDeptInfo>()
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("hospital_id",SysUserUtils.getCurrentHospitalID())
                .in("dept_id",deptIds));
    }
}
