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

import com.red.circle.console.app.dto.clienobject.admin.ComboUserCO;
import com.red.circle.console.app.dto.clienobject.admin.UserCO;
import com.red.circle.console.app.dto.cmd.admin.UserCmd;
import com.red.circle.console.app.dto.cmd.admin.UserIdCmd;
import com.red.circle.console.app.dto.cmd.admin.UserPageQryCmd;
import com.red.circle.console.app.service.admin.UserAccountService;
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 org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
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 = "/users", produces = MediaType.APPLICATION_JSON_VALUE)
public class UserRestController extends BaseController {

  private final UserAccountService userAccountService;

  /**
   * 查询所有用户.
   */
  @GetMapping
  public PageResult<UserCO> page(UserPageQryCmd cmd) {
    return userAccountService.pageUser(cmd);
  }

  /**
   * 查询单个用户.
   */
  @GetMapping("/get")
  public UserCO get(UserIdCmd cmd) {
    return userAccountService.getUser(cmd);
  }

  /**
   * 重置用户密码.
   */
  @PutMapping("/reset/password")
  public void resetPwd(@Validated UserIdCmd cmd) {
    userAccountService.resetPassword(cmd);
  }

  /**
   * 设置用户状态.
   */
  @OpsOperationReqLog(value = "设置用户状态")
  @PutMapping("/update/status")
  public void updateStatus(@RequestBody @Validated(UserCmd.Status.class) UserCmd cmd) {
    userAccountService.updateUser(cmd);
  }

  /**
   * 创建用户.
   */
  @PostMapping
  @OpsOperationReqLog(value = "创建用户")
  public void create(@RequestBody @Validated(UserCmd.Create.class) UserCmd cmd) {
    userAccountService.addUser(cmd);
  }

  /**
   * 修改用户.
   */
  @OpsOperationReqLog(value = "修改用户")
  @PutMapping("/update")
  public void update(@RequestBody @Validated(UserCmd.Update.class) UserCmd cmd) {
    userAccountService.updateUser(cmd);
  }

  /**
   * 获得成员列表.
   */
  @GetMapping("/list/combo")
  public List<ComboUserCO> listUser() {
    return userAccountService.listUser();
  }

}

