package com.unis.crk.basic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.unis.crk.basic.entity.FuncInfo;
import com.unis.crk.basic.entity.RoleFunc;
import com.unis.crk.basic.mapper.RoleFuncMapper;
import com.unis.crk.basic.mapper.UserRoleMapper;
import com.unis.crk.basic.service.IRoleFuncService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 角色权限表 服务实现类
 * </p>
 *
 * @author ghh
 * @since 2021-09-26
 */
@Service
public class RoleFuncServiceImpl extends ServiceImpl<RoleFuncMapper, RoleFunc> implements IRoleFuncService {
    @Resource
    private RoleFuncMapper roleFuncMapper;

    @Resource
    private UserRoleMapper userRoleMapper;

    @Override
    public Boolean saveRoleAndFunc(String roleId, String[] funcIdList) {
        // 先删除此角色所有数据
        Boolean flag=true;
        UpdateWrapper<RoleFunc> objectUpdateWrapper = new UpdateWrapper<>();
        objectUpdateWrapper.eq("role_id",roleId);
        roleFuncMapper.delete(objectUpdateWrapper);
        // 新增
        for (String funcId : funcIdList) {
            RoleFunc roleFunc = new RoleFunc();
            roleFunc.setRoleId(roleId);
            roleFunc.setFuncId(funcId);
            roleFunc.setUpdatetime(new Date());
            int insert = roleFuncMapper.insert(roleFunc);
            if(insert<0){
                flag=false;
            }
        }
        return flag;
    }

    @Override
    public void saveForList(String roleId, List<FuncInfo> funcList) {
        // 先删除此角色所有数据
        UpdateWrapper<RoleFunc> objectUpdateWrapper = new UpdateWrapper<>();
        objectUpdateWrapper.eq("role_id",roleId);
        roleFuncMapper.delete(objectUpdateWrapper);
        // 新增
        for (FuncInfo func : funcList) {
            RoleFunc roleFunc = new RoleFunc();
            roleFunc.setRoleId(roleId);
            roleFunc.setFuncId(func.getFuncId());
            roleFuncMapper.insert(roleFunc);
        }
    }
}
