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

import ink.wgink.annotation.CheckRequestBodyAnnotation;
import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.exceptions.*;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.ZTreeDTO;
import ink.wgink.pojo.dtos.role.RoleDTO;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.pojo.vos.IdsVO;
import ink.wgink.service.role.pojo.vos.RoleVO;
import ink.wgink.service.role.service.IRoleService;
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: RoleController
 * @Description: 角色
 * @Author: wenc
 * @Date: 2019/1/6 3:03 PM
 * @Version: 1.0
 * @MenuUrl: admin/role/goroles
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "角色")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/role")
public class RoleController extends DefaultBaseController {

    @Autowired
    private IRoleService roleService;

    @ApiOperation(value = "角色新增", notes = "角色新增接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("save")
    @CheckRequestBodyAnnotation
    public SuccessResult save(@RequestBody RoleVO roleVO) {
        roleService.save(roleVO);
        return new SuccessResult();
    }

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

    @ApiOperation(value = "角色修改", notes = "角色修改接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("update/{roleId}")
    @CheckRequestBodyAnnotation
    public SuccessResult update(@PathVariable("roleId") String roleId, @RequestBody RoleVO roleVO) {
        roleService.update(roleId, roleVO);
        return new SuccessResult();
    }

    @ApiOperation(value = "角色列表", notes = "角色列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleParentId", value = "角色上级ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listall/{roleParentId}")
    public List<RoleDTO> listall(@PathVariable("roleParentId") String roleParentId) {
        Map<String, Object> params = getParams();
        params.put("roleParentId", roleParentId);
        return roleService.listAll(params);
    }

    @ApiOperation(value = "角色详情", notes = "角色详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get/{roleId}")
    public RoleDTO getRole(@PathVariable("roleId") String roleId) {
        return roleService.get(roleId);
    }

    @ApiOperation(value = "角色zTree列表", notes = "角色zTree列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "上级ID", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listztree")
    public List<ZTreeDTO> listZTree() throws SearchException {
        Map<String, Object> params = requestParams();
        String roleParentId = ISystemConstant.TREE_ROOT_ID;
        if (!StringUtils.isBlank(params.get(ISystemConstant.PARAMS_ID) == null ? null : params.get(ISystemConstant.PARAMS_ID).toString())) {
            roleParentId = params.get(ISystemConstant.PARAMS_ID).toString();
        }
        params.put("roleParentId", roleParentId);
        return roleService.listZTree(params);
    }

    @ApiOperation(value = "角色分页列表", notes = "角色分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "上级ID", paramType = "query", dataType = "String"),
            @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")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpage")
    public SuccessResultList<List<RoleDTO>> listPage(ListPage page) throws SearchException {
        Map<String, Object> params = requestParams();
        String roleParentId = "0";
        if (!StringUtils.isBlank(params.get(ISystemConstant.PARAMS_PARENT_ID) == null ? null : params.get(ISystemConstant.PARAMS_PARENT_ID).toString())) {
            roleParentId = params.get(ISystemConstant.PARAMS_PARENT_ID).toString();
        }
        params.put("roleParentId", roleParentId);
        page.setParams(params);
        return roleService.listPage(page);
    }

    @ApiOperation(value = "角色列表", notes = "角色列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("list/role-ids")
    public List<RoleDTO> listByRoleIds(@RequestBody IdsVO idsVO) {
        if (idsVO.getIds().isEmpty()) {
            throw new ParamsException("id列表不能为空");
        }
        return roleService.listByRoleIds(idsVO.getIds());
    }

}
