package com.fs.hospital.service.impl;

import java.util.*;

import com.fs.common.constant.UserConstants;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.StringUtils;

import com.fs.company.domain.CompanyRole;
import com.fs.company.domain.CompanyRoleDept;
import com.fs.company.domain.CompanyRoleMenu;
import com.fs.company.mapper.CompanyRoleDeptMapper;
import com.fs.company.mapper.CompanyRoleMenuMapper;
import com.fs.company.mapper.CompanyUserRoleMapper;
import com.fs.hospital.domain.HospitalRoleDept;
import com.fs.hospital.domain.HospitalRoleMenu;
import com.fs.hospital.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.hospital.domain.HospitalRole;
import com.fs.hospital.service.IHospitalRoleService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 角色信息Service业务层处理
 *
 * @author fs
 * @date 2024-02-26
 */
@Service
public class HospitalRoleServiceImpl implements IHospitalRoleService
{
    @Autowired
    private HospitalRoleMapper hospitalRoleMapper;
    @Autowired
    private HospitalMenuMapper hospitalMenuMapper;

    @Autowired
    private HospitalRoleMenuMapper roleMenuMapper;
    @Autowired
    private HospitalRoleDeptMapper roleDeptMapper;
    @Autowired
    private HospitalUserRoleMapper userRoleMapper;
    /**
     * 查询角色信息
     *
     * @param roleId 角色信息主键
     * @return 角色信息
     */
    @Override
    public HospitalRole selectHospitalRoleByRoleId(Long roleId)
    {
        return hospitalRoleMapper.selectHospitalRoleByRoleId(roleId);
    }

    /**
     * 查询角色信息列表
     *
     * @param hospitalRole 角色信息
     * @return 角色信息
     */
    @Override
    public List<HospitalRole> selectHospitalRoleList(HospitalRole hospitalRole)
    {
        return hospitalRoleMapper.selectHospitalRoleList(hospitalRole);
    }

    /**
     * 新增角色信息
     *
     * @param hospitalRole 角色信息
     * @return 结果
     */
    @Override
    public int insertHospitalRole(HospitalRole hospitalRole)
    {
        hospitalRole.setCreateTime(DateUtils.getNowDate());
        return hospitalRoleMapper.insertHospitalRole(hospitalRole);
    }

    /**
     * 修改角色信息
     *
     * @param hospitalRole 角色信息
     * @return 结果
     */
    @Override
    public int updateHospitalRole(HospitalRole hospitalRole)
    {
        hospitalRole.setUpdateTime(DateUtils.getNowDate());
        return hospitalRoleMapper.updateHospitalRole(hospitalRole);
    }

    /**
     * 批量删除角色信息
     *
     * @param roleIds 需要删除的角色信息主键
     * @return 结果
     */
    @Override
    public int deleteHospitalRoleByRoleIds(Long[] roleIds)
    {
        return hospitalRoleMapper.deleteHospitalRoleByRoleIds(roleIds);
    }

    /**
     * 删除角色信息信息
     *
     * @param roleId 角色信息主键
     * @return 结果
     */
    @Override
    public int deleteHospitalRoleByRoleId(Long roleId)
    {
        return hospitalRoleMapper.deleteHospitalRoleByRoleId(roleId);
    }

    @Override
    public Collection<? extends String> selectMenuPermsByUserId(Long userId) {
        List<String> perms = hospitalMenuMapper.selectMenuPermsByUserId(userId);
        Set<String> permsSet = new HashSet<>();
        for (String perm : perms)
        {
            if (StringUtils.isNotEmpty(perm))
            {
                permsSet.addAll(Arrays.asList(perm.trim().split(",")));
            }
        }
        return permsSet;
    }

    @Override
    public Collection<? extends String> selectRolePermissionByUserId(Long userId) {
        List<HospitalRole> perms = hospitalRoleMapper.selectRolePermissionByUserId(userId);
        Set<String> permsSet = new HashSet<>();
        for (HospitalRole perm : perms)
        {
            if (StringUtils.isNotNull(perm))
            {
                permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(",")));
            }
        }
        return permsSet;
    }

    @Override
    public List<Integer> selectRoleListByUserId(Long userId) {
        return hospitalRoleMapper.selectRoleListByUserId(userId);
    }

    @Override
    public String checkRoleNameUnique(HospitalRole role) {
        Long roleId = StringUtils.isNull(role.getRoleId()) ? -1L : role.getRoleId();
        HospitalRole info = hospitalRoleMapper.checkRoleNameUnique(role.getRoleName());
        if (StringUtils.isNotNull(info) && info.getRoleId().longValue() != roleId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public String checkRoleKeyUnique(HospitalRole role) {
        Long roleId = StringUtils.isNull(role.getRoleId()) ? -1L : role.getRoleId();
        HospitalRole info = hospitalRoleMapper.checkRoleKeyUnique(role.getRoleKey());
        if (StringUtils.isNotNull(info) && info.getRoleId().longValue() != roleId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public int insertRole(HospitalRole role) {
        // 新增角色信息
        hospitalRoleMapper.insertHospitalRole(role);
        return insertRoleMenu(role);
    }

    @Override
    @Transactional
    public int updateRole(HospitalRole role) {
        // 修改角色信息
        hospitalRoleMapper.updateHospitalRole(role);
        // 删除角色与菜单关联
        roleMenuMapper.deleteRoleMenuByRoleId(role.getRoleId());
        return insertRoleMenu(role);
    }

    @Override
    public int authDataScope(HospitalRole role) {
        // 修改角色信息
        hospitalRoleMapper.updateHospitalRole(role);
        // 删除角色与部门关联
        roleDeptMapper.deleteHospitalRoleDeptByRoleId(role.getRoleId());
        // 新增角色和部门信息（数据权限）
        return insertRoleDept(role);
    }

    @Override
    public int updateRoleStatus(HospitalRole role) {
        return hospitalRoleMapper.updateHospitalRole(role);
    }

    private int insertRoleDept(HospitalRole role) {
        int rows = 1;
        // 新增角色与部门（数据权限）管理
        List<HospitalRoleDept> list = new ArrayList<HospitalRoleDept>();
        for (Long deptId : role.getDeptIds())
        {
            HospitalRoleDept rd = new HospitalRoleDept();
            rd.setRoleId(role.getRoleId());
            rd.setDeptId(deptId);
            list.add(rd);
        }
        if (list.size() > 0)
        {
            rows = roleDeptMapper.batchRoleDept(list);
        }
        return rows;
    }

    /**
     * 新增角色菜单信息
     *
     * @param role 角色对象
     */
    private int insertRoleMenu(HospitalRole role) {
        int rows = 1;
        // 新增用户与角色管理
        List<HospitalRoleMenu> list = new ArrayList<HospitalRoleMenu>();
        for (Long menuId : role.getMenuIds())
        {
            HospitalRoleMenu rm = new HospitalRoleMenu();
            rm.setRoleId(role.getRoleId());
            rm.setMenuId(menuId);
            list.add(rm);
        }
        if (list.size() > 0)
        {
            rows = roleMenuMapper.batchRoleMenu(list);
        }
        return rows;
    }

}
