package com.tuoheng.admin.controller;

import com.tuoheng.admin.entity.User;
import com.tuoheng.admin.request.user.QueryUserPageListRequest;
import com.tuoheng.admin.request.user.UserInitRequest;
import com.tuoheng.admin.request.user.UserResetPwdRequest;
import com.tuoheng.admin.service.user.IUserService;
import com.tuoheng.common.core.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户前端控制器
 *
 * @author wanjing
 * @team tuoheng
 * @date 2022-12-08
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 获取用户分页列表
     *
     * @param request 查询条件
     * @return
     */
    @GetMapping("/page/list")
    public JsonResult getPageList(QueryUserPageListRequest request) {
        return userService.getPageList(request);
    }

    /**
     * 获取用户详情
     *
     * @return
     */
    @GetMapping("/info")
    public JsonResult getUserInfo() {
        return userService.getUserInfo();
    }

    /**
     * 添加用户
     *
     * @param entity 实体对象
     * @return
     */
    @PostMapping("/add")
    public JsonResult add(@RequestBody User entity) {
        return userService.add(entity);
    }

    /**
     * 编辑用户
     *
     * @param entity 实体对象
     * @return
     */
    @PutMapping("/edit")
    public JsonResult edit(@RequestBody User entity) {
        return userService.edit(entity);
    }

    /**
     * 删除用户
     *
     * @param idList 用户ID
     * @return
     */
    @DeleteMapping("/delete/{idList}")
    public JsonResult deleteByIdList(@PathVariable("idList") List<String> idList) {
        return userService.deleteByIdList(idList);
    }

    /**
     * 设置状态
     *
     * @param id
     * @param status
     *
     * @return
     */
    @PutMapping("/edit/status/{id}/{status}")
    public JsonResult status(@PathVariable("id") String id, @PathVariable("status") Integer status) {
        return userService.updeateStatus(id, status);
    }

    /**
     * 重置密码
     *
     * @param id
     * @return
     */
    @PutMapping("/resetPwd/{id}")
    public JsonResult resetPwd(@PathVariable("id") String id) {
        return userService.resetPassword(id);
    }

    /**
     * 根据部门id查询用户列表
     */
    @GetMapping("/list/dept/{deptId}")
    public JsonResult getListByDeptId(@PathVariable("deptId") String deptId) {
        log.info("进入根据部门id查询用户列表接口, deptId:{}", deptId);
        return userService.getListByDeptId(deptId);
    }

    /**
     * 初始化系统用户
     *
     * @param request
     * @return
     */
    @PostMapping("/init")
    public JsonResult init(@RequestBody UserInitRequest request) {
        return userService.init(request);
    }

}
