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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.xyzh.api.system.user.UserService;
import org.xyzh.common.annotation.HttpLogin;
import org.xyzh.common.core.domain.LoginDomain;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.core.page.PageParam;
import org.xyzh.common.core.page.PageRequest;
import org.xyzh.common.dto.user.TbSysUser;
import org.xyzh.common.dto.user.TbSysUserDeptRole;
import org.xyzh.common.dto.user.TbSysUserInfo;
import org.xyzh.common.vo.UserDeptRoleVO;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;


/**
 * @description    UserController.java文件描述  用户控制器
 * @filename    UserController.java
 * @author    yslg
 * @copyright xyzh
 * @ since 2025-10-06
 */
@RestController
@RequestMapping("/users")
public class UserController {

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

    @Autowired
    private UserService userService;


    // ---------------- 用户 ----------------
    /**
     * @description    注册用户
     * @param    
     * @author    yslg
     * @since 2025-10-09
     */
    @PostMapping("/register")
    public ResultDomain<TbSysUser> registerUser(@RequestBody TbSysUser user){
        return userService.registerUser(user);
    }

    /**
     * @description    创建用户
     * @param user
     * @author    yslg
     * @since 2025-10-09
     */
    @PostMapping("/user")
    public ResultDomain<TbSysUser> createUser(@RequestBody TbSysUser user) {
        return userService.createUser(user);
    }

    /**
     * @description    更新用户
     * @param user
     * @author    yslg
     * @since 2025-10-09
     */
    @PutMapping("/user")
    public ResultDomain<TbSysUser> updateUser(@RequestBody TbSysUser user) {
        return userService.updateUser(user);
    }

    /**
     * @description    删除用户
     * @param user
     * @author    yslg
     * @since 2025-10-09
     */
    @DeleteMapping("/user")
    public ResultDomain<TbSysUser> deleteUser(@RequestBody TbSysUser user) {
        return userService.deleteUser(user.getID());
    }

    /**
     * @description    获取用户
     * @param filter
     * @author    yslg
     * @since 2025-10-09
     */
    @PostMapping("/list")
    public ResultDomain<TbSysUser> getUserList(@RequestBody TbSysUser filter) {
        return userService.getUserByFilter(filter);
    }

    /**
     * @description    获取用户列表分页
     * @param filter
     * @author    yslg
     * @since 2025-10-09
     */
    @PostMapping("/page")
    public ResultDomain<TbSysUser> getUserPage(@RequestBody PageRequest<TbSysUser> pageRequest) {
        TbSysUser filter = pageRequest.getFilter();
        PageParam pageParam = pageRequest.getPageParam();
        return userService.getUserPage(filter, pageParam);
    }
    
    /**
     * @description    绑定用户部门角色
     * @param filter
     * @author    yslg
     * @since 2025-10-09
     */
    @PostMapping("/bind/deptrole/list")
    public ResultDomain<TbSysUserDeptRole> getBindUserDeptRoleList(@RequestBody TbSysUserDeptRole filter) {
        return userService.getBindUserDeptRoleList(filter);
    }

    /**
     * @description    绑定用户部门角色
     * @param filter
     * @author    yslg
     * @since 2025-10-09
     */
    @PostMapping("/bind/deptrole")
    public ResultDomain<UserDeptRoleVO> bindUserDeptRole(@RequestBody UserDeptRoleVO userDeptRoleVO) {
        return userService.bindUserDeptRole(userDeptRoleVO);
    }

    /**
     * @description    解绑用户部门角色
     * @param filter
     * @author    yslg
     * @since 2025-10-09
     */
    @PostMapping("/unbind/deptrole")
    public ResultDomain<UserDeptRoleVO> unbindUserDeptRole(@RequestBody UserDeptRoleVO userDeptRoleVO) {
        return userService.unbindUserDeptRole(userDeptRoleVO);
    }








    // ---------------- 系统级用户信息管理 ----------------

    /**
     * @description    管理员获取用户信息
     * @param userID 用户ID
     * @author    yslg
     * @ since 2025-10-06
     */
    @GetMapping("/admin/userinfo/{userID}")
    public ResultDomain<TbSysUserInfo> getAdminUserInfo(@PathVariable String userID){
        return userService.getUserInfoById(userID);
    }

    /**
     * @description    管理员更新用户信息
     * @param userInfo
     * @author    yslg
     * @ since 2025-10-06
     */
    @PutMapping("/admin/userinfo")
    public ResultDomain<TbSysUserInfo> updateAdminUserInfo(@RequestBody TbSysUserInfo userInfo) {
        return userService.updateUserInfo(userInfo);
    }

    
}
