package com.ningdatech.pmapi.sys.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.log.annotation.WebLog;
import com.ningdatech.pmapi.common.util.BizUtils;
import com.ningdatech.pmapi.sys.manage.RoleManage;
import com.ningdatech.pmapi.sys.model.dto.MenuDataScopeDTO;
import com.ningdatech.pmapi.sys.model.dto.RoleSaveDTO;
import com.ningdatech.pmapi.sys.model.dto.RoleUpdateDTO;
import com.ningdatech.pmapi.sys.model.entity.Role;
import com.ningdatech.pmapi.sys.model.req.RolePageReq;
import com.ningdatech.pmapi.sys.model.vo.RoleVO;
import com.ningdatech.pmapi.sys.service.IRoleService;
import com.ningdatech.pmapi.user.util.LoginUserUtil;
import io.swagger.annotations.Api;
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 javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 前端控制器-角色
 * </p>
 *
 * @author WendyYang
 * @since 2022-09-30
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/api/v1/role")
@Api(value = "Role", tags = "系统管理-角色")
@RequiredArgsConstructor
public class RoleController {

    private final IRoleService roleService;
    private final RoleManage roleManage;


    @ApiOperation(value = "角色列表查询", notes = "角色列表查询")
    @GetMapping("/list")
    public PageVo<RoleVO> query(@Valid @ModelAttribute RolePageReq rolePageReq) {
        return roleManage.queryList(rolePageReq);
    }

    /**
     * 查询角色
     *
     * @param id 主键id
     * @return 查询结果
     */
    @ApiOperation(value = "查询角色", notes = "查询角色")
    @GetMapping("/details")
    public RoleVO getDetails(@Valid @RequestParam Long id) {
        Role role = roleService.getById(id);
        Assert.notNull(role, "角色不存在");
        RoleVO query = BeanUtil.toBean(role, RoleVO.class);
        query.setManageRoleIdList(BizUtils.splitToLong(role.getManageRoleIds()));
        List<MenuDataScopeDTO> menuDataScope = roleManage.getMenuDataScope(id);
        query.setMenuDataScopeList(menuDataScope);
        // 菜单
        roleManage.buildMenu(query);
        return query;
    }

    @ApiOperation(value = "保存新角色", notes = "保存新角色")
    @PostMapping("/save")
    @WebLog("保存新角色")
    public RoleVO handlerSave(@Valid @RequestBody RoleSaveDTO data) {
        roleService.saveRole(data, LoginUserUtil.getUserId());
        return BeanUtil.toBean(data, RoleVO.class);
    }

    @ApiOperation(value = "编辑角色", notes = "编辑角色")
    @PostMapping("/modify")
    @WebLog("编辑角色")
    public RoleVO handlerUpdate(@Valid @RequestBody RoleUpdateDTO data) {
        roleService.updateRole(data, LoginUserUtil.getUserId());
        return BeanUtil.toBean(data, RoleVO.class);
    }

    @ApiOperation(value = "删除角色", notes = "删除角色")
    @PostMapping("/remove")
    @WebLog("删除角色")
    public Boolean handlerDelete(@Valid @RequestBody List<Long> ids) {
        return roleService.removeByIdWithCache(ids);
    }

}
