package com.njzscloud.supervisory.sys.role.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.njzscloud.common.core.utils.R;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.supervisory.sys.role.pojo.entity.RoleEntity;
import com.njzscloud.supervisory.sys.role.pojo.param.RoleAddParam;
import com.njzscloud.supervisory.sys.role.pojo.param.RoleModifyParam;
import com.njzscloud.supervisory.sys.role.pojo.param.RoleQueryParam;
import com.njzscloud.supervisory.sys.role.pojo.result.RoleDetailResult;
import com.njzscloud.supervisory.sys.role.service.RoleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色
 */
@Slf4j
@RestController
@RequestMapping("/role")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public R<Long> add(@RequestBody @Validated RoleAddParam roleAddParam) {
        return R.success(roleService.add(roleAddParam));
    }

    /**
     * 修改
     */
    @PostMapping("/modify")
    public R<?> modify(@RequestBody RoleModifyParam roleModifyParam) {
        roleService.modify(roleModifyParam);
        return R.success();
    }

    /**
     * 删除
     */
    @PostMapping("/del")
    public R<?> del(@RequestBody List<Long> ids) {
        Assert.notEmpty(ids, "未指定要删除的数据");
        roleService.del(ids);
        return R.success();
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    public R<RoleDetailResult> detail(@RequestParam Long id) {
        return R.success(roleService.detail(id));
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    public R<List<RoleEntity>> list(@RequestParam(required = false, value = "roleName") String roleName) {
        return R.success(roleService.list(Wrappers.lambdaQuery(RoleEntity.class).like(StrUtil.isNotBlank(roleName), RoleEntity::getRoleName, roleName)));
    }

    /**
     * 分页查询
     */
    @GetMapping("/paging")
    public R<PageResult<RoleDetailResult>> paging(PageParam page, RoleQueryParam roleQueryParam) {
        return R.success(roleService.paging(page.toPage(), roleQueryParam));
    }

}
