package com.pj.project.re_role_menu;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.pj.project.app_menu.AppMenu;
import com.pj.project.re_role_menu.dto.AssignPermissionsDto;
import com.pj.project4sp.SP;
import com.pj.utils.sg.AjaxJson;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * Controller: re_role_menu -- app用户菜单中间表
 * @author qzy
 */
@RestController
@RequestMapping("/ReRoleMenu/")
public class PcRoleMenuController {

	/** 底层 Service 对象 */
	@Autowired
	ReRoleMenuService reRoleMenuService;

	/** 增 */
	@RequestMapping("add")
	@SaCheckPermission(ReRoleMenu.PERMISSION_CODE_ADD)
	public AjaxJson add(ReRoleMenu r){
		reRoleMenuService.add(r);
		r = reRoleMenuService.getById(SP.publicMapper.getPrimarykey());
		return AjaxJson.getSuccessData(r);
	}

	/** 删 */
	@RequestMapping("delete")
	@SaCheckPermission(ReRoleMenu.PERMISSION_CODE_DEL)
	public AjaxJson delete(Long id){
		 reRoleMenuService.delete(id);
		return AjaxJson.getSuccess();
	}

	/** 删 - 根据id列表 */
	@RequestMapping("deleteByIds")
	@SaCheckPermission(ReRoleMenu.PERMISSION_CODE_DEL)
	public AjaxJson deleteByIds(){
		List<Long> ids = SoMap.getRequestSoMap().getListByComma("ids", long.class);
		int line = SP.publicMapper.deleteByIds(ReRoleMenu.TABLE_NAME, ids);
		return AjaxJson.getByLine(line);
	}

	/** 改 */
	@RequestMapping("update")
	@SaCheckPermission(ReRoleMenu.PERMISSION_CODE_EDIT)
	public AjaxJson update(ReRoleMenu r){
		reRoleMenuService.update(r);
		return AjaxJson.getSuccess();
	}

	/** 查 - 根据id */
	@RequestMapping("getById")
		@SaCheckPermission(ReRoleMenu.PERMISSION_CODE)
	public AjaxJson getById(Long id){
		ReRoleMenu r = reRoleMenuService.getById(id);
		return AjaxJson.getSuccessData(r);
	}

	/**
	 * 查集合 - 根据条件（参数为空时代表忽略指定条件）
	 * @param appRoleId 职务ID,非必填
	 * @return
	 */
	@RequestMapping("getList")
		@SaCheckPermission(AppMenu.PERMISSION_CODE)
	public AjaxJson getList(@RequestParam(value = "appRoleId",required = false) Integer appRoleId) {
		SoMap so = SoMap.getRequestSoMap();
		List<ReRoleMenu> list = reRoleMenuService.getList(so.startPage(),appRoleId);
		return AjaxJson.getPageData(so.getDataCount(), list);
	}

	/**
	 * pc端:可以根据不同app角色进行菜单权限分配
	 * @param assignPermissionsDto 职务ID和该职务下属的权限的集合
	 * @return
	 */
	@RequestMapping("assignPermissions")
		@SaCheckPermission(AppMenu.PERMISSION_CODE)
	public AjaxJson assignPermissions(@Validated AssignPermissionsDto assignPermissionsDto) {
		boolean permissions = reRoleMenuService.assignPermissions(assignPermissionsDto);
		if(permissions)return AjaxJson.getSuccess("权限分配成功!");
		return AjaxJson.getError("权限分配失败!");
	}









}
