package com.njcn.gather.user.user.controller;

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.LogUtil;
import com.njcn.gather.user.user.pojo.param.SysRoleParam;
import com.njcn.gather.user.user.pojo.po.SysRole;
import com.njcn.gather.user.user.service.ISysRoleService;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.HttpResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author caozehui
 * @date 2024-11-11
 */
@Slf4j
@Api(tags = "角色管理")
@RestController
@RequestMapping("/sysRole")
@RequiredArgsConstructor
public class SysRoleController extends BaseController {
    private final ISysRoleService sysRoleService;

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

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

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

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

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

}

