package com.ningdatech.carapi.sys.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.carapi.sys.entity.dto.UserPageQuery;
import com.ningdatech.carapi.sys.entity.dto.UserSaveDTO;
import com.ningdatech.carapi.sys.entity.dto.UserUpdateDTO;
import com.ningdatech.carapi.sys.entity.vo.UserInfoVO;
import com.ningdatech.carapi.sys.manage.UserManage;
import com.ningdatech.carapi.user.util.LoginUserUtil;
import com.ningdatech.log.annotation.WebLog;
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 PoffyZhang
 * @since 2022-09-30
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/api/user")
@Api(value = "User", tags = "系统管理-用户管理")
@RequiredArgsConstructor
public class UserController {

    private final UserManage userManage;

    @ApiOperation(value = "用户列表查询", notes = "用户列表查询")
    @GetMapping("/list")
    @WebLog(value = "用户列表查询", modular = "系统管理-用户管理")
    public PageVo<UserInfoVO> query(@Valid @ModelAttribute UserPageQuery userPageQuery) {
        return userManage.queryUserList(userPageQuery);
    }

    /**
     * 查询用户
     *
     * @param id 主键id
     * @return 查询结果
     */
    @ApiOperation(value = "查询用户", notes = "查询用户")
    @GetMapping("/details")
    @WebLog(value = "查询用户", modular = "系统管理-用户管理")
    public UserInfoVO getDetails(@Valid @RequestParam Long id) {
        return userManage.queryDetails(id);
    }


    @ApiOperation(value = "保存新用户", notes = "保存新用户")
    @PostMapping("/save")
    @WebLog(value = "保存新用户", modular = "系统管理-用户管理")
    public UserInfoVO handlerSave(@Valid @RequestBody UserSaveDTO data) {
        userManage.save(data, LoginUserUtil.getUserId());
        return BeanUtil.toBean(data, UserInfoVO.class);
    }

    @ApiOperation(value = "编辑用户", notes = "编辑用户")
    @PostMapping("/modify")
    @WebLog(value = "编辑用户", modular = "系统管理-用户管理")
    public UserInfoVO handlerUpdate(@Valid @RequestBody UserUpdateDTO data) {
        userManage.update(data, LoginUserUtil.getUserId());
        return BeanUtil.toBean(data, UserInfoVO.class);
    }

    @ApiOperation(value = "删除用户", notes = "删除用户")
    @PostMapping("/remove")
    @WebLog(value = "删除用户", modular = "系统管理-用户管理")
    public Boolean handlerDelete(@Valid @RequestBody List<Long> ids) {
        return userManage.remove(ids);
    }

}
