package org.xyzh.system.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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;
import org.springframework.security.access.prepost.PreAuthorize;
import org.xyzh.api.system.service.SysUserService;
import org.xyzh.api.system.vo.SysUserVO;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.core.page.PageRequest;
import org.xyzh.common.dto.sys.TbSysUserDTO;
import org.xyzh.common.dto.sys.TbSysUserInfoDTO;

@RestController
@RequestMapping("/system/user")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private SysUserService sysUserService;

    // ================= 用户相关接口 =================

    @PostMapping
    @PreAuthorize("hasAuthority('system:user:create')")
    public ResultDomain<TbSysUserDTO> createUser(@RequestBody SysUserVO userVO) {
        return sysUserService.insertUser(userVO);
    }

    @PutMapping
    @PreAuthorize("hasAuthority('system:user:edit')")
    public ResultDomain<TbSysUserDTO> updateUser(@RequestBody SysUserVO userVO) {
        return sysUserService.updateUser(userVO);
    }

    @DeleteMapping
    @PreAuthorize("hasAuthority('system:user:delete')")
    public ResultDomain<Boolean> deleteUser(@RequestBody TbSysUserDTO userDTO) {
        return sysUserService.deleteUser(userDTO);
    }

    @PostMapping("/page")
    @PreAuthorize("hasAuthority('system:user:view')")
    public ResultDomain<SysUserVO> getUserPage(@RequestBody PageRequest<SysUserVO> pageRequest) {
        return sysUserService.getUserPage(pageRequest);
    }

    @PostMapping("/list")
    @PreAuthorize("hasAuthority('system:user:view')")
    public ResultDomain<SysUserVO> getUserList(@RequestBody SysUserVO filter) {
        return sysUserService.getUserList(filter);
    }

    // ================= 用户信息相关接口 ==================

    @PutMapping("/info")
    @PreAuthorize("hasAuthority('system:user:edit')")
    public ResultDomain<TbSysUserInfoDTO> updateUserInfo(@RequestBody TbSysUserInfoDTO userInfoDTO) {
        return sysUserService.updateUserInfo(userInfoDTO);
    }

    @GetMapping("/info/{userId}")
    @PreAuthorize("hasAuthority('system:user:view')")
    public ResultDomain<SysUserVO> getUserInfo(@PathVariable("userId") String userId) {
        return sysUserService.getUserInfo(userId);
    }
    
    
}
