package org.jeecg.modules.resManager.roleRescatalog.controller;

import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.resManager.roleRescatalog.entity.RoleRescatalog;
import org.jeecg.modules.resManager.roleRescatalog.service.IRoleRescatalogService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 角色资源表
 * @Author: jeecg-boot
 * @Date:   2022-07-20
 * @Version: V1.0
 */
@Api(tags="角色资源表")
@RestController
@RequestMapping("/resManager.roleRescatalog/roleRescatalog")
@Slf4j
public class RoleRescatalogController extends JeecgController<RoleRescatalog, IRoleRescatalogService> {
	@Autowired
	private IRoleRescatalogService roleRescatalogService;
	
	/**
	 * 分页列表查询
	 *
	 * @param roleRescatalog
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "角色资源表-分页列表查询")
	@ApiOperation(value="角色资源表-分页列表查询", notes="角色资源表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<RoleRescatalog>> queryPageList(RoleRescatalog roleRescatalog,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<RoleRescatalog> queryWrapper = QueryGenerator.initQueryWrapper(roleRescatalog, req.getParameterMap());
		Page<RoleRescatalog> page = new Page<RoleRescatalog>(pageNo, pageSize);
		IPage<RoleRescatalog> pageList = roleRescatalogService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param roleRescatalog
	 * @return
	 */
	@AutoLog(value = "角色资源表-添加")
	@ApiOperation(value="角色资源表-添加", notes="角色资源表-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody RoleRescatalog roleRescatalog) {
		roleRescatalogService.save(roleRescatalog);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param roleRescatalog
	 * @return
	 */
	@AutoLog(value = "角色资源表-编辑")
	@ApiOperation(value="角色资源表-编辑", notes="角色资源表-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody RoleRescatalog roleRescatalog) {
		roleRescatalogService.updateById(roleRescatalog);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "角色资源表-通过id删除")
	@ApiOperation(value="角色资源表-通过id删除", notes="角色资源表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		roleRescatalogService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "角色资源表-批量删除")
	@ApiOperation(value="角色资源表-批量删除", notes="角色资源表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.roleRescatalogService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "角色资源表-通过id查询")
	@ApiOperation(value="角色资源表-通过id查询", notes="角色资源表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<RoleRescatalog> queryById(@RequestParam(name="id",required=true) String id) {
		RoleRescatalog roleRescatalog = roleRescatalogService.getById(id);
		if(roleRescatalog==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(roleRescatalog);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param roleRescatalog
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, RoleRescatalog roleRescatalog) {
        return super.exportXls(request, roleRescatalog, RoleRescatalog.class, "角色资源表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, RoleRescatalog.class);
    }

	 /**
	  * 查询角色授权
	  *
	  * @return
	  */
	 @RequestMapping(value = "/queryRoleRescatalog", method = RequestMethod.GET)
	 public Result<List<String>> queryRoleRescatalog(@RequestParam(name = "roleId", required = true) String roleId) {
		 Result<List<String>> result = new Result<>();
		 try {
			 List<RoleRescatalog> list = roleRescatalogService.list(new QueryWrapper<RoleRescatalog>().lambda().eq(RoleRescatalog::getRoleId, roleId));
			 result.setResult(list.stream().map(RoleRescatalog -> String.valueOf(RoleRescatalog.getRescatalogId())).collect(Collectors.toList()));
			 result.setSuccess(true);
		 } catch (Exception e) {
			 log.error(e.getMessage(), e);
		 }
		 return result;
	 }

	 /**
	  * 保存角色授权
	  *
	  * @return
	  */
	 @RequestMapping(value = "/saveRoleRescatalog", method = RequestMethod.POST)
	 public Result<String> saveRoleRescatalog(@RequestBody JSONObject json) {
		 long start = System.currentTimeMillis();
		 Result<String> result = new Result<>();
		 try {
			 String roleId = json.getString("roleId");
			 String resCatalogIds = json.getString("resCatalogIds");
			 String lastRescatalogIds = json.getString("lastRescatalogIds");
			 roleRescatalogService.saveRolePermission(roleId, resCatalogIds, lastRescatalogIds);
			 result.success("保存成功！");
			 log.info("======角色授权成功=====耗时:" + (System.currentTimeMillis() - start) + "毫秒");
		 } catch (Exception e) {
			 result.error500("授权失败！");
			 log.error(e.getMessage(), e);
		 }
		 return result;
	 }
}
