package com.njcn.user.controller;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.common.utils.LogUtil;
import com.njcn.user.pojo.param.RoleParam;
import com.njcn.user.pojo.po.Role;
import com.njcn.user.pojo.vo.RoleVO;
import com.njcn.user.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.njcn.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hongawen
 * @since 2021-12-13
 */
@Validated
@Slf4j
@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
@AllArgsConstructor
public class RoleController extends BaseController {

    private final IRoleService roleService;

    /**
     * 分页查询角色信息
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/list")
    @ApiOperation("查询角色信息")
    @ApiImplicitParam(name = "queryParam", value = "查询参数", required = true)
    public HttpResult<Page<RoleVO>> list(@RequestBody @Validated RoleParam.QueryParam queryParam) {
        String methodDescribe = getMethodDescribe("list");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, queryParam);
        Page<RoleVO> result = roleService.listRole(queryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * 新增角色
     *
     * @param roleParam 角色新增
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增角色信息")
    @ApiImplicitParam(name = "roleParam", value = "角色信息数据", required = true)
    public HttpResult<Object> add(@RequestBody @Validated RoleParam roleParam) {
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，角色信息数据为：{}", methodDescribe, roleParam);
        boolean result = roleService.addRole(roleParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    /**
     * 修改角色信息
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改角色信息")
    @ApiImplicitParam(name = "updateParam", value = "角色信息", required = true)
    public HttpResult<Object> update(@RequestBody @Validated RoleParam.RoleUpdateParam updateParam) {
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log, "{}，角色信息数据为：{}", methodDescribe, updateParam);
        boolean result = roleService.updateRole(updateParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    /**
     * 批量删除角色信息
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("删除角色信息")
    @ApiImplicitParam(name = "ids", value = "角色索引", required = true)
    public HttpResult<Object> delete(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("delete");
        LogUtil.njcnDebug(log, "{}，角色信息数据为：{}", methodDescribe, ids);
        boolean result = roleService.deleteRole(ids);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/getRoleListByIds")
    @ApiOperation("根据角色id集合查询角色信息")
    @ApiImplicitParam(name = "ids", value = "角色id集合", required = true)
    public HttpResult<List<Role>> getRoleListByIds(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("getRoleListByIds");
        List<Role> users = roleService.list((new LambdaQueryWrapper<Role>().in(CollUtil.isNotEmpty(ids), Role::getId, ids)));
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, users, methodDescribe);
    }

    /**
     * 根据角色id查询相关联的资源和组件
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/selectFunctionComponent")
    @ApiOperation("根据角色id查询相关联的资源和组件")
    @ApiImplicitParam(name = "ids", value = "角色索引", required = true)
    public HttpResult<Object> selectFunctionComponent(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("selectFunctionComponent");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, ids);
        boolean result = roleService.selectRelevance(ids);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.DELETE_PID_EXIST, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.DELETE_PID_UNEXIST, null, methodDescribe);
        }
    }

    /**
     * 根据权限类型查询相关角色
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/selectRoleDetail")
    @ApiOperation("根据权限类型查询相关角色")
    @ApiImplicitParam(name = "id", value = "权限类型索引", required = true)
    public HttpResult<Object> selectRoleDetail(@RequestParam("id") Integer id) {
        String methodDescribe = getMethodDescribe("selectRoleDetail");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, id);
        List<Role> result = roleService.selectRoleDetail(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * 查询所有角色
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/allRoleList")
    @ApiOperation("查询所有角色")
    public HttpResult<Object> allRoleList() {
        String methodDescribe = getMethodDescribe("allRoleList");
        List<Role> result = roleService.allRoleList();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * 查询所有角色
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/simpleList")
    @ApiOperation("查询所有角色作为下拉框")
    public HttpResult<Object> simpleList() {
        String methodDescribe = getMethodDescribe("simpleList");
        List<Role> result = roleService.simpleList();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }
}

