package cn.ezhizao.project.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;

import cn.ezhizao.project.system.domain.SysRoleInIt;
import cn.ezhizao.project.system.domain.SysRoleMenu;
import cn.ezhizao.project.system.domain.SysRoleMenuInit;
import cn.ezhizao.project.system.mapper.SysRoleInItMapper;
import cn.ezhizao.project.system.mapper.SysRoleMenuInitMapper;
import cn.ezhizao.project.system.service.ISysRoleInItService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;


/**
 * roleInitService业务层处理
 *
 * @author ruoyi
 * @date 2024-01-10
 */
@Service
public class SysRoleInItServiceImpl extends ServiceImpl<SysRoleInItMapper, SysRoleInIt> implements ISysRoleInItService
{
    @Resource
    private SysRoleInItMapper sysRoleInitMapper;

    @Resource
    private SysRoleMenuInitMapper  roleMenuMapper;

    /**
     * 查询roleInit列表
     *
     * @param sysRoleInit roleInit
     * @return roleInit
     */
    @Override
    public List<SysRoleInIt> getList(SysRoleInIt sysRoleInit)
    {
        return sysRoleInitMapper.getList(sysRoleInit);
    }

    /**
     * 物理删除
     * @param sysRoleInit
     * @return 删除结果
     */
    @Override
    public int physicalDelete(SysRoleInIt sysRoleInit){ return sysRoleInitMapper.physicalDelete(sysRoleInit); }

    @Override
    public SysRoleInIt getById(Long id) {
        return sysRoleInitMapper.getById(id);
    }

    @Override
    public int insertRoleInIt(SysRoleInIt sysRoleInit) {
        sysRoleInitMapper.insertRoleInIt(sysRoleInit);
        return insertRoleMenuInIt(sysRoleInit);
    }

    @Override
    public int updateRoleInIt(SysRoleInIt sysRoleInit) {

        //修改初始化角色表
        sysRoleInitMapper.updateRoleInIt(sysRoleInit);
        //删除初始化角色表关联菜单
        roleMenuMapper.deleteRoleMenuByRoleId(sysRoleInit.getId());
        return insertRoleMenuInIt(sysRoleInit);
    }

    @Override
    public int deletedRoleInIt(List<Long> ids) {

        roleMenuMapper.deleteRoleMenu(ids);
        return sysRoleInitMapper.deleteRoleInItByIds(ids);
    }

    public int insertRoleMenuInIt(SysRoleInIt role){
        int rows = 1;
        List<SysRoleMenuInit> list = new ArrayList<SysRoleMenuInit>();
        for (Long menuId : role.getMenuIds())
        {
            SysRoleMenuInit rm = new SysRoleMenuInit();
            rm.setInitRoleId(role.getId());
            rm.setMenuId(menuId);
            list.add(rm);
        }
        if (list.size() > 0)
        {
            rows = roleMenuMapper.batchRoleMenu(list);
        }
        return rows;
    }

}
