package com.yida.data.system.controller;

import com.yida.data.common.core.annotation.ControllerLog;
import com.yida.data.common.core.common.ModuleName;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.FebsResponse;
import com.yida.data.common.core.entity.QueryRequest;
import com.yida.data.common.core.entity.constant.StringConstant;
import com.yida.data.common.core.entity.system.Role;
import com.yida.data.common.core.entity.system.enums.OperationLogTypeEnum;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.log.annotation.OperationLog;
import com.yida.data.system.service.IRoleService;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
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;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author MrBird
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("role")
public class RoleController {

    private final IRoleService roleService;

    @GetMapping
    public FebsResponse roleList(QueryRequest queryRequest, Role role) {
        Map<String, Object> dataTable = FebsUtil.getDataTable(roleService.findRoles(role, queryRequest));
        return new FebsResponse().data(dataTable);
    }

    @GetMapping("options")
    public FebsResponse roles() {
        List<Role> allRoles = roleService.findAllRoles();
        return new FebsResponse().data(allRoles);
    }

    @GetMapping("check/{roleName}")
    public boolean checkRoleName(@NotBlank(message = "{required}") @PathVariable String roleName) {
        Role result = this.roleService.findByName(roleName);
        return result == null;
    }

    @PostMapping
    @PreAuthorize("hasAuthority('role:add')")
    @OperationLog(module = ModuleName.SYSTEM, methods = "新增角色", type = OperationLogTypeEnum.INSERT)
    public void addRole(@Valid Role role) {
        this.roleService.createRole(role);
    }

    @DeleteMapping("/{roleIds}")
    @PreAuthorize("hasAuthority('role:delete')")
    @OperationLog(module = ModuleName.SYSTEM, methods = "删除角色失败", type = OperationLogTypeEnum.DELETE)
    public void deleteRoles(@NotBlank(message = "{required}") @PathVariable String roleIds) {
        String[] ids = roleIds.split(StringConstant.COMMA);
        this.roleService.deleteRoles(ids);
    }

    @PutMapping
    @PreAuthorize("hasAuthority('role:update')")
    @OperationLog(module = ModuleName.SYSTEM, methods = "修改角色", type = OperationLogTypeEnum.UPDATE)
    public void updateRole(@Valid Role role) {
        this.roleService.updateRole(role);
    }

    @PostMapping("excel")
    @PreAuthorize("hasAuthority('role:export')")
    public void export(QueryRequest queryRequest, Role role, HttpServletResponse response) {
        List<Role> roles = this.roleService.findRoles(role, queryRequest).getRecords();
        //ExcelKit.$Export(Role.class, response).downXlsx(roles, false);
    }

    @ApiOperation("根据角色查询菜单")
    @GetMapping("/listMenuByRole")
    public ResultBean<List> listMenuByRole(@ApiParam("角色id") @RequestParam Long[] roleIds) {
        return ResultBean.buildSuccess(roleService.listMenuByRole(roleIds));
    }

    @ApiOperation("根据角色查询菜单")
    @GetMapping("/listMenuByRoleName")
    public ResultBean<List> listMenuByRoleName(@ApiParam("角色名称") @RequestParam String[] roleNames) {
        return ResultBean.buildSuccess(roleService.listMenuByRoleName(roleNames));
    }

    @ApiOperation("根据角色名查询角色 不存在进行新增")
    @GetMapping("/getRoleByName")
    public ResultBean<Role> getRoleByName(String roleName) {
        return ResultBean.buildSuccess(roleService.getRoleByNameOrCreate(roleName));
    }
}
