package org.jeecg.modules.roleMaptool.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
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 org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.roleMaptool.entity.RoleMaptool;
import org.jeecg.modules.roleMaptool.service.IRoleMaptoolService;

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.jeecg.modules.resManager.roleRescatalog.entity.RoleRescatalog;
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-30
 * @Version: V1.0
 */
@Api(tags="角色地图工具表")
@RestController
@RequestMapping("/roleMaptool/roleMaptool")
@Slf4j
public class RoleMaptoolController extends JeecgController<RoleMaptool, IRoleMaptoolService> {
	@Autowired
	private IRoleMaptoolService roleMaptoolService;
	
	/**
	 * 分页列表查询
	 *
	 * @param roleMaptool
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "角色地图工具表-分页列表查询")
	@ApiOperation(value="角色地图工具表-分页列表查询", notes="角色地图工具表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<RoleMaptool>> queryPageList(RoleMaptool roleMaptool,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<RoleMaptool> queryWrapper = QueryGenerator.initQueryWrapper(roleMaptool, req.getParameterMap());
		Page<RoleMaptool> page = new Page<RoleMaptool>(pageNo, pageSize);
		IPage<RoleMaptool> pageList = roleMaptoolService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param roleMaptool
	 * @return
	 */
	@AutoLog(value = "角色地图工具表-添加")
	@ApiOperation(value="角色地图工具表-添加", notes="角色地图工具表-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody RoleMaptool roleMaptool) {
		roleMaptoolService.save(roleMaptool);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param roleMaptool
	 * @return
	 */
	@AutoLog(value = "角色地图工具表-编辑")
	@ApiOperation(value="角色地图工具表-编辑", notes="角色地图工具表-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody RoleMaptool roleMaptool) {
		roleMaptoolService.updateById(roleMaptool);
		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) {
		roleMaptoolService.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.roleMaptoolService.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<RoleMaptool> queryById(@RequestParam(name="id",required=true) String id) {
		RoleMaptool roleMaptool = roleMaptoolService.getById(id);
		if(roleMaptool==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(roleMaptool);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param roleMaptool
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, RoleMaptool roleMaptool) {
        return super.exportXls(request, roleMaptool, RoleMaptool.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, RoleMaptool.class);
    }

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

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