package com.pj.project4sp.role;

import cn.dev33.satoken.stp.StpUtil;
import com.pj.current.satoken.AuthConst;
import com.pj.current.satoken.StpUserUtil;
import com.pj.utils.sg.AjaxJson;
import com.pj.utils.so.SoMap;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Controller: 系统角色表
 * @author kong
 */
@RestController
@RequestMapping("/role/")
public class SpRoleController {

	/** 底层Mapper依赖 */
	@Resource
    SpRoleMapper spRoleMapper;

	/** 增 */
	@RequestMapping("add")
	@Transactional(rollbackFor = Exception.class)
    AjaxJson add(SpRole s, HttpServletRequest request){
		StpUtil.checkPermission(AuthConst.AUTH);
		s.setCustomerId(StpUserUtil.getCustomerId());
		spRoleMapper.add(s);
		return AjaxJson.getSuccess();
	}

	/** 删 */
	@RequestMapping("delete")
    AjaxJson delete(long id, HttpServletRequest request){
		StpUtil.checkPermission(AuthConst.AUTH);
		StpUtil.checkPermission(AuthConst.ROLE_LIST);
		int line = spRoleMapper.delete(id);
		return AjaxJson.getByLine(line);
	}


	/** 改 */ 
	@RequestMapping("update")
    AjaxJson update(SpRole s){
		StpUtil.checkPermission(AuthConst.AUTH);
		StpUtil.checkPermission(AuthConst.ROLE_LIST);
		SpRoleUtil.checkRoleThrow(s);
		int line = spRoleMapper.update(s);
		return AjaxJson.getByLine(line);
	}

	/** 查 */ 
	@RequestMapping("getById")
    AjaxJson getById(long id){
		StpUtil.checkPermission(AuthConst.AUTH);
		SpRole s = spRoleMapper.getById(id);
		return AjaxJson.getSuccessData(s);
	}

	/** 查 - 集合  */
	@RequestMapping("getList")
    AjaxJson getList(){
		StpUtil.checkPermission(AuthConst.AUTH);
		SoMap so = SoMap.getRequestSoMap();
		so.put("customerId", StpUserUtil.getCustomerId());
		List<SpRole> list = spRoleMapper.getList(so);
		return AjaxJson.getSuccessData(list);
	}
	/** 查 - 集合  */
	@RequestMapping("getCustomerRoleList")
    AjaxJson getCustomerRoleList(){
		SoMap so = SoMap.getRequestSoMap();
		List<SpRole> list = spRoleMapper.getList(so);
		return AjaxJson.getSuccessData(list);
	}




	
	

}
