package com.xmomen.module.account.web.controller;

import com.xmomen.module.account.service.PermissionService;
import com.xmomen.module.account.service.RoleService;
import com.xmomen.module.account.service.UserService;
import com.xmomen.module.account.web.controller.vo.CreatePermissionVo;
import com.xmomen.module.user.entity.SysPermissions;
import com.xmomen.module.user.entity.SysPermissionsExample;
import com.xmomen.framework.mybatis.dao.MybatisDao;
import com.xmomen.framework.mybatis.page.Page;
import com.xmomen.framework.web.exceptions.ArgumentValidException;
import com.xmomen.module.logger.Log;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;


@RestController
public class PermissionController {

    // 注入UserService
    @Autowired
    UserService userService;

    // 注入PermissionService
    @Autowired
    PermissionService permissionService;

    // 注入RoleService
    @Autowired
    RoleService roleService;

    // 注入MybatisDao
    @Autowired
    MybatisDao mybatisDao;

    // 权限权限
    @RequestMapping(value = "/user/permissions", method = RequestMethod.GET)
    public Map getPermission(){
        // 获取当前用户名
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        // 获取当前用户的角色
        Set<String> roles = userService.findRoles(username);
        // 获取当前用户的权限
        Set<String> permissions = userService.findPermissions(username);
        // 将角色和权限放入Map中
        Map rolesMap = new HashMap();
        rolesMap.put("roles", roles);
        rolesMap.put("permissions", permissions);
        return rolesMap;
    }

    // 权限列表
    @RequestMapping(value = "/permission", method = RequestMethod.GET)
    @Log(actionName = "查看权限列表")
    public Page<SysPermissions> getPermissionList(@RequestParam(value = "limit") Integer limit,
                                        @RequestParam(value = "offset") Integer offset,
                                        @RequestParam(value = "keyword", required = false) String keyword){
        // 创建权限示例
        SysPermissionsExample sysPermissionsExample = new SysPermissionsExample();
        // 添加权限名称模糊查询条件
        sysPermissionsExample.createCriteria()
                .andPermissionLike("%" + StringUtils.trimToEmpty(keyword) + "%");
        // 添加权限描述模糊查询条件
        sysPermissionsExample.or()
                .andDescriptionLike("%" + StringUtils.trimToEmpty(keyword) + "%");
        // 根据示例查询权限列表
        return mybatisDao.selectPageByExample(sysPermissionsExample, limit, offset);
    }

    // 权限资源
    @RequestMapping(value = "/permission/{id}", method = RequestMethod.GET)
    @Log(actionName = "查询单个权限")
    public SysPermissions getPermission(@PathVariable(value = "id") Integer id){
        // 根据id查询权限
        return mybatisDao.selectByPrimaryKey(SysPermissions.class, id);
    }

    // 新增权限
    @RequestMapping(value = "/permission", method = RequestMethod.POST)
    @Log(actionName = "新增权限资源")
    public SysPermissions createPermission(@RequestBody @Valid CreatePermissionVo createPermissionVo, BindingResult bindingResult) throws ArgumentValidException {
        // 验证参数
        if(bindingResult != null && bindingResult.hasErrors()){
            throw new ArgumentValidException(bindingResult);
        }
        // 创建权限对象
        SysPermissions sysPermissions = new SysPermissions();
        // 设置权限描述
        sysPermissions.setDescription(createPermissionVo.getDescription());
        // 设置权限代码
        sysPermissions.setPermission(createPermissionVo.getPermissionCode().toUpperCase());
        // 设置权限是否可用
        sysPermissions.setAvailable(createPermissionVo.getAvailable() != null && createPermissionVo.getAvailable() ? 1 : 0);
        // 调用权限服务创建权限
        return permissionService.createPermission(sysPermissions);
    }

    // 删除权限
    @RequestMapping(value = "/permission/{id}", method = RequestMethod.DELETE)
    @Log(actionName = "删除权限资源")
    public void deletePermission(@PathVariable(value = "id") Long id){
        // 根据id删除权限
        mybatisDao.deleteByPrimaryKey(SysPermissions.class, id);
    }

}
