package ink.wgink.service.permission.controller.api;

import ink.wgink.annotation.CheckRequestBodyAnnotation;
import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.exceptions.ParamsException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.permission.PermissionDTO;
import ink.wgink.pojo.dtos.permission.PermissionGroupDTO;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultData;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.service.permission.pojo.vos.PermissionVO;
import ink.wgink.service.permission.pojo.vos.SystemApiVO;
import ink.wgink.service.permission.service.IPermissionService;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: PermissionController
 * @Description: 权限
 * @Author: WenG
 * @Date: 2020-05-27 15:58
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "权限接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/permission")
public class PermissionController extends DefaultBaseController {

    @Autowired
    private IPermissionService permissionService;

    @ApiOperation(value = "新增权限", notes = "新增权限接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("save")
    @CheckRequestBodyAnnotation
    public SuccessResult save(@RequestBody PermissionVO permissionVO) {
        permissionService.save(permissionVO);
        return new SuccessResult();
    }

    @ApiOperation(value = "新增权限（快速）", notes = "新增权限（快速）接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("save-quick/{permissionType}")
    @CheckRequestBodyAnnotation
    public SuccessResult saveQuick(@PathVariable("permissionType") String permissionType, @RequestBody SystemApiVO systemApiVO) {
        boolean permissionTypeFlag = StringUtils.equals(permissionType, IPermissionService.PERMISSION_INSERT) ||
                StringUtils.equals(permissionType, IPermissionService.PERMISSION_DELETE) ||
                StringUtils.equals(permissionType, IPermissionService.PERMISSION_UPDATE) ||
                StringUtils.equals(permissionType, IPermissionService.PERMISSION_QUERY);
        if (!permissionTypeFlag) {
            throw new ParamsException("权限类型错误");
        }
        permissionService.saveQuick(permissionType, systemApiVO);
        return new SuccessResult();
    }

    @ApiOperation(value = "删除权限(id列表)", notes = "删除权限(id列表)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ID列表，用下划线分隔", paramType = "path", example = "1_2_3")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("remove/{ids}")
    public SuccessResult remove(@PathVariable("ids") String ids) {
        permissionService.remove(Arrays.asList(ids.split("\\_")));
        return new SuccessResult();
    }

    @ApiOperation(value = "修改权限", notes = "修改权限接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "permissionId", value = "权限ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("update/{permissionId}")
    @CheckRequestBodyAnnotation
    public SuccessResult update(@PathVariable("permissionId") String permissionId, @RequestBody PermissionVO permissionVO) {
        permissionService.update(permissionId, permissionVO);
        return new SuccessResult();
    }

    @ApiOperation(value = "权限详情(通过ID)", notes = "权限详情(通过ID)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "permissionId", value = "权限ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get/{permissionId}")
    public PermissionDTO get(@PathVariable("permissionId") String permissionId) {
        return permissionService.get(permissionId);
    }

    @ApiOperation(value = "权限列表", notes = "权限列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list")
    public List<PermissionDTO> list() {
        Map<String, Object> params = requestParams();
        return permissionService.list(params);
    }

    @ApiOperation(value = "权限分页列表", notes = "权限分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "int", defaultValue = "20"),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "permissionType", value = "权限类型", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpage")
    public SuccessResultList<List<PermissionDTO>> listPage(ListPage page) {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return permissionService.listPage(page);
    }

    @ApiOperation(value = "权限分组列表", notes = "权限分组列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listgroup")
    public List<PermissionGroupDTO> listGroup() {
        Map<String, Object> params = requestParams();
        return permissionService.listGroup(params);
    }

    @ApiOperation(value = "权限统计", notes = "权限统计接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("count")
    SuccessResultData<Integer> count() {
        Map<String, Object> params = requestParams();
        return permissionService.count(params);
    }

}