package com.hs.admin.service.impl;

import com.hs.admin.common.enums.StateType;
import com.hs.admin.mapper.SysRoleMapper;
import com.hs.admin.mapper.SysUserRoleRsMapper;
import com.hs.admin.model.SysRole;
import com.hs.admin.model.SysUserRoleRs;
import com.hs.admin.model.reqmodel.RoleReq;
import com.hs.admin.service.SysRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Resource
    private SysRoleMapper sysRoleMapper;
    @Resource
    private SysUserRoleRsMapper sysUserRoleRsMapper;

    @Override
    public SysRole findByCode(Long roleCode) {
        return sysRoleMapper.selectByCode(roleCode);
    }

    @Override
    public boolean addRole(Long userId, RoleReq.AddRoleReq role) {
        if (role != null) {
            SysRole sysRole = new SysRole();
            sysRole.setCreateDate(new Date());
            sysRole.setCreateUserid(userId);
            sysRole.setState((short) StateType.ON.getValue());
            sysRole.setRoleCode(role.getRoleCode());
            sysRole.setRoleName(role.getRoleName());
            sysRoleMapper.insert(sysRole);
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteRole(RoleReq.DeleteRoleReq role) {
        if (role != null) {
            SysRole sysRole = sysRoleMapper.selectByPrimaryKey(role.getRoleId());
            if (sysRole != null) {
                sysRole.setState((short) StateType.OFF.getValue());
                sysRoleMapper.updateByPrimaryKey(sysRole);
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean updateRole(RoleReq.UpdateRoleReq role) {
        if (role != null) {
            SysRole sysRole = sysRoleMapper.selectByPrimaryKey(role.getRoleId());
            if (sysRole != null) {
                sysRole.setRoleCode(role.getRoleCode());
                sysRole.setRoleName(role.getRoleName());
                sysRoleMapper.updateByPrimaryKey(sysRole);
                return true;
            }
        }
        return false;
    }

    @Override
    public List<SysRole> getAll(RoleReq.GetRoleReq role) {
        List<SysRole> list = sysRoleMapper.selectAll();
        if (role.getRoleName() != null) {
            list = list.stream().filter(
                    o -> (o.getRoleName() != null && o.getRoleName().toLowerCase().contains(role.getRoleName().toLowerCase())))
                    .collect(Collectors.toList());
        }
        return list;
    }

    @Override
    public SysRole info(Integer roleId) {
        SysRole sysRole = sysRoleMapper.selectByPrimaryKey(roleId);
        return sysRole;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveUserRole(Long userId, Long saveUserId, Long roleId) {
        int result = 0;
        if (saveUserId != null && roleId != null) {
            //先删除用户关联数据
            sysUserRoleRsMapper.deleteByUser(saveUserId);
            SysUserRoleRs info = sysUserRoleRsMapper.selectByUser(saveUserId, roleId);
            if (info != null) {
                info.setState((short) StateType.ON.getValue());
                result = sysUserRoleRsMapper.updateByPrimaryKey(info);
            } else {
                info = new SysUserRoleRs();
                info.setCreateDate(new Date());
                info.setCreateUserid(userId);
                info.setState((short) StateType.ON.getValue());
                info.setUserId(saveUserId);
                info.setRoleId(roleId);
                result = sysUserRoleRsMapper.insert(info);
            }
        }
        return result > 0;
    }
}
