package com.red.circle.console.adapter.admin;

import com.alibaba.fastjson.JSON;
import com.red.circle.console.app.dto.clienobject.admin.RoleCO;
import com.red.circle.console.app.dto.cmd.admin.PageRoleQryCmd;
import com.red.circle.console.app.dto.cmd.admin.RoleCmd;
import com.red.circle.console.app.dto.cmd.admin.RoleIdCmd;
import com.red.circle.console.app.dto.cmd.admin.RoleMenuUpdateCmd;
import com.red.circle.console.app.service.admin.AdminRoleService;
import com.red.circle.console.infra.annotations.OpsOperationReqLog;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.framework.web.controller.BaseController;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
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.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 角色 服务.
 *
 * @author pengliang
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/roles", produces = MediaType.APPLICATION_JSON_VALUE)
public class RoleRestController extends BaseController {

  private static final Logger log = LoggerFactory.getLogger(RoleRestController.class);

  private final AdminRoleService adminRoleService;

  /**
   * 查询所有角色(分页).
   */
  @GetMapping
  public PageResult<RoleCO> page(PageRoleQryCmd cmd) {
    log.info("RoleRestController page:{}", JSON.toJSONString(cmd));
    return adminRoleService.pageRole(cmd);
  }

  /**
   * 查询所有角色.
   */
  @GetMapping("/list")
  public List<RoleCO> list() {
    return adminRoleService.list();
  }

  /**
   * 查询单个角色.
   */
  @GetMapping("/get")
  public RoleCO get(RoleIdCmd cmd) {
    return adminRoleService.get(cmd);
  }

  /**
   * 添加角色.
   */
  @PostMapping
  @OpsOperationReqLog(value = "添加角色")
  public void create(
      @RequestBody @Validated(RoleCmd.Create.class) RoleCmd cmd) {
    log.info("RoleRestController create:{}", JSON.toJSONString(cmd));
    adminRoleService.add(cmd);
  }

  /**
   * 修改角色.
   */
  @PutMapping("/update")
  @OpsOperationReqLog(value = "修改角色")
  public void update(@RequestBody @Validated(RoleCmd.Update.class) RoleCmd cmd) {
    log.info("RoleRestController update:{}", JSON.toJSONString(cmd));
    adminRoleService.update(cmd);
  }

  /**
   * 删除角色.
   */
  @DeleteMapping("/remove")
  @OpsOperationReqLog(value = "删除角色")
  public void delete(RoleIdCmd cmd) {
    adminRoleService.remove(cmd);
  }

  /**
   * 修改角色菜单.
   */
  @PutMapping("/update/menus")
  @OpsOperationReqLog(value = "修改角色菜单")

  public void menus(@RequestBody @Validated RoleMenuUpdateCmd cmd) {
    adminRoleService.updateRoleMenus(cmd);
  }

}

