package com.unis.crk.basic.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.unis.crk.basic.entity.OrgInfo;
import com.unis.crk.basic.entity.RoleButton;
import com.unis.crk.basic.entity.UserInfo;
import com.unis.crk.basic.entity.vo.RoleButtonVo;
import com.unis.crk.basic.entity.vo.UserInfoVo;
import com.unis.crk.basic.service.IRoleButtonService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.unis.crk.utils.ResultUtil;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 角色按钮权限表 前端控制器
 * </p>
 *
 * @author ghh
 * @since 2021-09-26
 */
@Api(value = "角色按钮权限表",tags = {"角色按钮权限表"})
@RestController
@RequestMapping("roleButton")
@CrossOrigin
public class RoleButtonController {

    private final static Logger LOGGER = LoggerFactory.getLogger(RoleButtonController.class);


    @Autowired
    private IRoleButtonService roleButtonService;
   /**
    * 获取角色按钮权限列表
    * @return	List<FuncInfo>	功能列表
    */
   @ApiOperation(value="查询", notes="获取角色按钮权限列表")
   @GetMapping("getRoleButton/{roleId}")
    public List<RoleButton> getRoleButton(@PathVariable("roleId") String roleId) {
        QueryWrapper<RoleButton> objectQueryWrapper = new QueryWrapper<>();

            objectQueryWrapper.eq("role_id",roleId);
       List<RoleButton> list = roleButtonService.list(objectQueryWrapper);

        return list;
    }



    /**
     * 查询当前用户点击菜单按钮权限
     * @param userId 用戶ID
     * @param funcId 功能ID
     *
     * @return
     */
    @ApiOperation(value="查询当前用户点击菜单按钮权限", notes="查询当前用户点击菜单按钮权限")
    @RequestMapping(value="/getByUserId/{userId}/{funcId}", method = RequestMethod.POST)


    public List<String> getByUser(@PathVariable("userId") String userId,@PathVariable("funcId") String funcId) {
            ResultUtil<RoleButton> data = new ResultUtil<>();
            List<String> strings = new ArrayList<>();
            ObjectMapper mapper = new ObjectMapper();
            if(StringUtils.isEmpty(userId) || StringUtils.isEmpty(funcId)) {
                data.failed("所接收userId为null或者funcName为null");
                return null;
            }
        strings= roleButtonService.queryByUser(userId, funcId);
        return strings;
    }

    /**
     * 保存角色按钮权限
     * @return
     */
    @RequestMapping(value="/save", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id", paramType = "query"),
            @ApiImplicitParam(name = "btnIds", value = "按钮编号，多个之间用逗号隔开", paramType = "query")
    })
    @ApiOperation(value="保存角色按钮权限", notes="保存角色按钮权限")
    public Map<String, Object> save(@RequestBody RoleButtonVo roleButtonVo) {
        String[] btnIdList = roleButtonVo.getBtnIds().split(",");
        roleButtonService.saveButton(roleButtonVo.getRoleId(), btnIdList);
        return new HashMap<String, Object>();
    }
    /**
     * 根据ID查询数据
     * @param id
     * @return
     */
    @ApiOperation(value="查询", notes="根据ID查询数据")
    @GetMapping("getOne/{id}")
    public ResultUtil getOne(@PathVariable("id") String id){
        LOGGER.info("入参为:{}",id);
        ResultUtil<RoleButton> data = new ResultUtil<>();
        if (StringUtils.isEmpty(id)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.roleButtonService.getById(id));
        return data;
    }


    /**
     * 查询全部数据
     * @return
     */
    @ApiOperation(value="查询", notes="查询全部数据")
    @GetMapping("getAll")
    public ResultUtil getAll(){
        ResultUtil<List<RoleButton>> data = new ResultUtil<>();
        data.success(this.roleButtonService.list());
        return data;
    }


    /**
     * 新增一条数据
     * @param param
     * @return
     */
    @ApiOperation(value="新增", notes="新增一条数据")
    @PostMapping("add")
    public ResultUtil add(@RequestBody RoleButton param){
        LOGGER.info("入参为:{}", param);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(param)){
            data.failed("所接收对象为null");
            return data;
        }
        data.success(this.roleButtonService.save(param));
        return data;
    }


    /**
     * 修改一条数据
     * @param param
     * @return
     */
    @ApiOperation(value="修改", notes="修改一条数据")
    @PatchMapping("edit")
    public ResultUtil edit(@RequestBody RoleButton param){
        LOGGER.info("入参为:{}", param);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(param)){
            data.failed("所接收对象为null");
            return data;
        }
        data.success(this.roleButtonService.updateById(param));
        return data;
    }


    /**
     * 根据ID删除数据
     * @param id
     * @return
     */
    @ApiOperation(value="删除", notes="根据ID删除数据")
    @DeleteMapping("delete/{id}")
    public ResultUtil delete(@PathVariable("id") String id){
        LOGGER.info("入参为:{}", id);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (StringUtils.isEmpty(id)){
            data.failed("所接收id为null");
            return data;
        }
        data.success(this.roleButtonService.removeById(id));
        return data;
    }


}

