package com.zhgd.xmgl.modules.basicdata.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhgd.annotation.OperLog;
import com.zhgd.jeecg.common.api.vo.Result;
import com.zhgd.jeecg.common.system.query.QueryGenerator;
import com.zhgd.jeecg.common.util.PageUtil;
import com.zhgd.xmgl.modules.basicdata.entity.SystemRole;
import com.zhgd.xmgl.modules.basicdata.entity.SystemRoleMenu;
import com.zhgd.xmgl.modules.basicdata.service.ISystemRoleMenuService;
import com.zhgd.xmgl.modules.basicdata.service.ISystemRoleService;
import com.zhgd.xmgl.security.SecurityUser;
import com.zhgd.xmgl.security.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


 /**
 * @Title: Controller
 * @Description: 系统角色
 * @author： pds
 * @date：   2023-02-16
 * @version： V1.0
 */
@RestController
@RequestMapping("/xmgl/systemRole")
@Slf4j
@Api(tags = "系统角色")
public class SystemRoleController {
	@Autowired
	private ISystemRoleService systemRoleService;

	 @Autowired
	 private ISystemRoleMenuService systemRoleMenuService;
	
	/**
	  * 分页列表查询
	 * @return
	 */
	@OperLog(operModul = "角色管理", operType = "分页查询", operDesc = "分页列表查询系统角色信息")
	@ApiOperation(value = " 分页列表查询系统角色信息", notes = "分页列表查询系统角色信息", httpMethod="POST")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "roleName", value = "角色名称", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "state", value = "状态(0:停用；1:启用)", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "beginTime", value = "开始日期", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "endTime", value = "结束日期", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "pageNo", value = "页数", paramType = "query", required = true, defaultValue = "1", dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query", required = true, defaultValue = "10", dataType = "Integer")
	})
	@PostMapping(value = "/page")
	public Result<IPage<SystemRole>> queryPageList(@RequestBody Map<String, Object> map) {
		Result<IPage<SystemRole>> result = new Result<IPage<SystemRole>>();
		SecurityUser user = SecurityUtil.getUser();
		QueryWrapper<SystemRole> queryWrapper = QueryGenerator.initPageQueryWrapper(SystemRole.class, map);
		if (StringUtils.isNotBlank(MapUtils.getString(map, "beginTime"))) {
			queryWrapper.ge("create_time", MapUtils.getString(map, "beginTime"));
		}
		if (StringUtils.isNotBlank(MapUtils.getString(map, "endTime"))) {
			queryWrapper.le("create_time", MapUtils.getString(map, "endTime"));
		}
		queryWrapper.eq("role_type", user.getAccountType());
		queryWrapper.eq("sn", user.getSn());
		queryWrapper.orderByAsc("priority");
		Page<SystemRole> page = PageUtil.getPage(map);
		IPage<SystemRole> pageList = systemRoleService.page(page, queryWrapper);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}

	 /**
	  * 列表查询
	  * @param systemRole
	  * @param req
	  * @return
	  */
	 @OperLog(operModul = "角色管理", operType = "列表查询", operDesc = "列表查询系统角色信息")
	 @ApiOperation(value = " 列表查询系统角色信息", notes = "列表查询系统角色信息", httpMethod="POST")
	 @PostMapping(value = "/list")
	 public Result<List<SystemRole>> queryList(@RequestBody SystemRole systemRole,
													HttpServletRequest req) {
		 Result<List<SystemRole>> result = new Result<List<SystemRole>>();
		 SecurityUser user = SecurityUtil.getUser();
		 systemRole.setRoleType(user.getAccountType());
		 systemRole.setSn(user.getSn());
		 QueryWrapper<SystemRole> queryWrapper = QueryGenerator.initQueryWrapper(systemRole, req.getParameterMap());
		 queryWrapper.orderByAsc("priority");
		 List<SystemRole> list = systemRoleService.list(queryWrapper);
		 result.setSuccess(true);
		 result.setResult(list);
		 return result;
	 }
	
	/**
	  *   添加
	 * @param systemRole
	 * @return
	 */
	@OperLog(operModul = "角色管理", operType = "新增", operDesc = "添加系统角色信息")
	@ApiOperation(value = " 添加系统角色信息", notes = "添加系统角色信息" , httpMethod="POST")
	@PostMapping(value = "/add")
	public Result<SystemRole> add(@RequestBody SystemRole systemRole) {
		Result<SystemRole> result = new Result<SystemRole>();
		SecurityUser user = SecurityUtil.getUser();
		systemRole.setRoleType(user.getAccountType());
		systemRole.setSn(user.getSn());
		systemRole.setCreateTime(new Date());
		systemRoleService.save(systemRole);
		result.success("添加成功！");
		return result;
	}
	
	/**
	  *  编辑
	 * @param systemRole
	 * @return
	 */
	@OperLog(operModul = "角色管理", operType = "修改", operDesc = "编辑系统角色信息")
	@ApiOperation(value = "编辑系统角色信息", notes = "编辑系统角色信息" , httpMethod="POST")
	@PostMapping(value = "/edit")
	public Result<SystemRole> edit(@RequestBody SystemRole systemRole) {
		Result<SystemRole> result = new Result<SystemRole>();
		SystemRole systemRoleEntity = systemRoleService.getById(systemRole.getRoleId());
		if(systemRoleEntity==null) {
			result.error500("未找到对应实体");
		}else {
			SecurityUser user = SecurityUtil.getUser();
			systemRole.setRoleType(user.getAccountType());
			systemRole.setSn(user.getSn());
			boolean ok = systemRoleService.updateById(systemRole);
			if(ok) {
				result.success("修改成功!");
			}
		}
		
		return result;
	}
	
	/**
	  *   通过id删除
	 * @return
	 */
	@OperLog(operModul = "角色管理", operType = "删除", operDesc = "删除系统角色信息")
	@ApiOperation(value = "删除系统角色信息", notes = "删除系统角色信息" , httpMethod="POST")
	@ApiImplicitParam(name = "id", value = "系统角色ID", paramType = "query", required = true, dataType = "Integer")
	@PostMapping(value = "/delete")
	public Result<SystemRole> delete(@RequestBody Map<String,Object> map) {
		Result<SystemRole> result = new Result<SystemRole>();
		SystemRole systemRole = systemRoleService.getById(MapUtils.getLong(map,"id"));
		if(systemRole==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = systemRoleService.removeById(MapUtils.getLong(map,"id"));
			if(ok) {
				result.success("删除成功!");
			}
		}
		
		return result;
	}
	
	/**
	  *  批量删除
	 * @return
	 */
	@OperLog(operModul = "角色管理", operType = "批量删除", operDesc = "批量删除系统角色信息")
	@ApiOperation(value = "批量删除系统角色信息", notes = "批量删除系统角色信息",  httpMethod="POST")
    @ApiImplicitParam(name = "ids", value = "系统角色ID字符串", paramType = "query", required = true, dataType = "String")
	@PostMapping(value = "/deleteBatch")
	public Result<SystemRole> deleteBatch(@RequestBody Map<String,Object> map) {
		 String ids = MapUtils.getString(map,"ids");
		 Result<SystemRole> result = new Result<SystemRole>();
		 if(ids==null || "".equals(ids.trim())) {
			 result.error500("参数不识别！");
		 }else {
			 this.systemRoleService.removeByIds(Arrays.asList(ids.split(",")));
			 result.success("删除成功!");
		 }
		 return result;
	}
	
	/**
	  * 通过id查询
	 * @return
	 */
	@OperLog(operModul = "角色管理", operType = "查询", operDesc = "通过id查询系统角色信息")
	@ApiOperation(value = "通过id查询系统角色信息", notes = "通过id查询系统角色信息" , httpMethod="POST")
    @ApiImplicitParam(name = "id", value = "系统角色ID", paramType = "query", required = true, dataType = "Integer")
	@PostMapping(value = "/queryById")
	public Result<SystemRole> queryById(@RequestBody Map<String,Object> map) {
		Result<SystemRole> result = new Result<SystemRole>();
		SystemRole systemRole = systemRoleService.getById(MapUtils.getString(map,"id"));
		if(systemRole==null) {
			result.error500("未找到对应实体");
		}else {
			result.setResult(systemRole);
			result.setSuccess(true);
		}
		return result;
	}

	 /**
	  *  编辑角色权限
	  * @return
	  */
	 @OperLog(operModul = "角色管理", operType = "修改", operDesc = "编辑角色权限")
	 @ApiOperation(value = "编辑角色权限", notes = "编辑角色权限" , httpMethod="POST")
	 @PostMapping(value = "/editMenu")
	 public Result<SystemRole> menu(@RequestBody List<SystemRoleMenu> systemRoleMenus) {
		 Result<SystemRole> result = new Result<SystemRole>();
		 systemRoleMenuService.remove(Wrappers.<SystemRoleMenu>lambdaQuery()
				 .eq(SystemRoleMenu::getRoleId, systemRoleMenus.get(0).getRoleId()));
		 systemRoleMenuService.saveBatch(systemRoleMenus);
		 return result;
	 }
}
