package com.kiisoo.ic.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kiisoo.ic.common.utils.PinYinUtil;
import com.kiisoo.ic.constants.Constants;
import com.kiisoo.ic.system.bean.PrivilageRoleAccountBean;
import com.kiisoo.ic.system.entity.PrivilageRoleDO;
import com.kiisoo.ic.system.entity.PrivilageUserRoleDO;
import com.kiisoo.ic.system.enums.RoleEnum;
import com.kiisoo.ic.system.mapper.PrivilageRoleDOMapper;
import com.kiisoo.ic.system.mapper.PrivilageRolePermissionDOMapper;
import com.kiisoo.ic.system.mapper.PrivilageUserRoleDOMapper;
import com.kiisoo.ic.system.service.PrivilageRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 角色表  服务实现类
 * @author jinchaofan
 * @since 2020-02-19
 */
@Service
public class PrivilageRoleServiceImpl extends ServiceImpl<PrivilageRoleDOMapper, PrivilageRoleDO> implements PrivilageRoleService {


    /**
     * 角色 mapper
     */
    private final PrivilageRoleDOMapper privilageRoleDOMapper;

    /**
     * 用户角色 mapper
     */
    private final PrivilageUserRoleDOMapper privilageUserRoleDOMapper;

    /**
     * 角色权限 mapper
     */
    private final PrivilageRolePermissionDOMapper privilageRolePermissionDOMapper;

    @Autowired
    public PrivilageRoleServiceImpl(PrivilageRoleDOMapper privilageRoleDOMapper, PrivilageUserRoleDOMapper privilageUserRoleDOMapper, PrivilageRolePermissionDOMapper privilageRolePermissionDOMapper) {
        this.privilageRoleDOMapper = privilageRoleDOMapper;
        this.privilageUserRoleDOMapper = privilageUserRoleDOMapper;
        this.privilageRolePermissionDOMapper = privilageRolePermissionDOMapper;
    }

    @Override
    public boolean addRole(String roleName) {
        Map<String, Object> roleSelectMap = new HashMap<>();
        roleSelectMap.put("name", roleName);
        List<PrivilageRoleDO> privilageRoleDOS = privilageRoleDOMapper.selectByMap(roleSelectMap);
        //角色已存在-名字重复
        if(privilageRoleDOS != null && privilageRoleDOS.size() > 0) return false;
        PrivilageRoleDO privilageRoleDO = new PrivilageRoleDO();
        privilageRoleDO.setName(roleName);
        privilageRoleDO.setCode(PinYinUtil.toPinYin(roleName));
        privilageRoleDO.setOperation(roleName);
        privilageRoleDO.setCreateTime(new Date());
        privilageRoleDOMapper.insert(privilageRoleDO);
        return true;
    }

    @Override
    public boolean removeRole(Long roleId) {
        Map<String, Object> userRoleSelectMap = new HashMap<>();
        userRoleSelectMap.put("role_id", roleId);
        //是否存在绑定了用户的情况
        List<PrivilageUserRoleDO> privilageUserRoleDOS = privilageUserRoleDOMapper.selectByMap(userRoleSelectMap);
        //存在业务绑定不能删除
        if(privilageUserRoleDOS != null && privilageUserRoleDOS.size() > 0 ) return false;
        //删除角色权限
        Map<String, Object> rolePermissionDeleteMap = new HashMap<>();
        rolePermissionDeleteMap.put("role_id", roleId);
        privilageRolePermissionDOMapper.deleteByMap(rolePermissionDeleteMap);
        //删除角色
        privilageRoleDOMapper.deleteById(roleId);
        return true;
    }

    @Override
    public List<PrivilageRoleDO> listRoleInfo() {
        return privilageRoleDOMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public boolean modifyRole(Long roleId,String roleName){
        PrivilageRoleDO roleDO = new PrivilageRoleDO();
        roleDO.setId(roleId);
        roleDO.setName(roleName);
        roleDO.setUpdateTime(new Date());
        roleDO.setUpdateBy(Constants.SYS_OPERATION);
        privilageRoleDOMapper.updateById(roleDO);
        return true;
    }

    @Override
    public List<PrivilageRoleAccountBean> listOfRoleAndAccountCount(){
        List<PrivilageRoleDO> privilageRoleDOS = listRoleInfo();
        List<PrivilageRoleAccountBean> list = new ArrayList<>();
        privilageRoleDOS.forEach(privilageRoleDO -> {
            PrivilageRoleAccountBean accountBean = new PrivilageRoleAccountBean();
            accountBean.setCode(privilageRoleDO.getCode());
            accountBean.setName(privilageRoleDO.getName());
            accountBean.setId(privilageRoleDO.getId());
            QueryWrapper<PrivilageUserRoleDO> wrapper = new QueryWrapper<>();
            wrapper.eq("role_id",privilageRoleDO.getId());
            Integer count = privilageUserRoleDOMapper.selectCount(wrapper);
            accountBean.setNumber(count);
            if(RoleEnum.ROLE_CODE_NULL.equals(RoleEnum.getRoleByCode(privilageRoleDO.getCode()))){
                //此角色可以修改
                accountBean.setType(2);
            }else {
                //此角色不可以修改
                accountBean.setType(1);
            }
            list.add(accountBean);
        });
        return list;
    }


}
