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.ButtonInfo;
import com.unis.crk.basic.entity.RoleButton;
import com.unis.crk.basic.entity.UserInfo;
import com.unis.crk.basic.entity.UserRole;
import com.unis.crk.basic.mapper.RoleButtonMapper;
import com.unis.crk.basic.mapper.UserRoleMapper;
import com.unis.crk.basic.service.IRoleButtonService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 角色按钮权限表 服务实现类
 * </p>
 *
 * @author ghh
 * @since 2021-09-26
 */
@Service
public class RoleButtonServiceImpl extends ServiceImpl<RoleButtonMapper, RoleButton> implements IRoleButtonService {
    @Resource
    RoleButtonMapper roleButtonMapper;
    @Resource
    UserRoleMapper userRoleMapper;
    @Override
    public List<String> queryByUser(String userId,String funcId) {
        List<String> strings =   new ArrayList<String>();
        QueryWrapper<UserRole> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("user_id",userId);
        List<UserRole> userRoles = userRoleMapper.selectList(objectQueryWrapper);
        if(userRoles !=null && userRoles.size()>0){
            for (int i=0;i<userRoles.size();i++){

                 strings = roleButtonMapper.selectButtonByUserId(userRoles.get(i).getRoleId(), funcId);
                 if(strings.size()>0){
                     break;
                 }

            }
        }

        return strings;
    }

    @Override
    public Boolean saveButton(String roleId, String[] btnIdList) {
        //删除之前角色权限
        Boolean flag=true;
        UpdateWrapper<RoleButton> roleButtonUpdateWrapper = new UpdateWrapper<>();
        roleButtonUpdateWrapper.eq("role_id",roleId);
        roleButtonMapper.delete(roleButtonUpdateWrapper);
        // 批量新增
        if (btnIdList.length > 0) {
            List<RoleButton> list = new ArrayList<>();
            for (String btnId : btnIdList) {
                RoleButton roleButton = new RoleButton();
                roleButton.setRoleId(roleId);
                roleButton.setBtnId(btnId);
                int insert = roleButtonMapper.insert(roleButton);
                if(insert<0){
                    flag=false;
                }

            }

        }
        return flag;
    }

    @Override
    public void saveForList(String roleId, List<ButtonInfo> btnList) {
        //先删除原有权限
        UpdateWrapper<RoleButton> buttonInfoUpdateWrapper = new UpdateWrapper<>();
        buttonInfoUpdateWrapper.eq("role_id",roleId);
        roleButtonMapper.delete(buttonInfoUpdateWrapper);
        // 批量新增
        if (btnList.size() > 0) {
            List<RoleButton> list = new ArrayList<>();
            for (ButtonInfo btn : btnList) {
                RoleButton roleButton = new RoleButton();
                roleButton.setRoleId(roleId);
                roleButton.setBtnId(btn.getBtnId());
                roleButtonMapper.insert(roleButton);
            }
        }
    }
}
