package com.njzscloud.supervisory.user.controller;

import cn.hutool.core.lang.Assert;
import com.njzscloud.common.core.utils.R;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.supervisory.user.pojo.*;
import com.njzscloud.supervisory.user.service.SysUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>sys_user</p>
 * <p>用户信息表</p>
 */
@Slf4j
@RestController
@RequestMapping("/sys_user")
@RequiredArgsConstructor
public class SysUserController {

    private final SysUserService sysUserService;

    /**
     * 新增
     *
     * @param addUserParam 数据
     * @return boolean 结果; true-->成功、false-->失败
     */
    @PostMapping("/add")
    public R<Long> add(@RequestBody @Validated AddUserParam addUserParam) {
        return R.success(sysUserService.add(addUserParam));
    }

    /**
     * 修改
     *
     * @param userModifyParam 数据
     */
    @PostMapping("/modify")
    public R<?> modify(@RequestBody UserModifyParam userModifyParam) {
        sysUserService.modify(userModifyParam);
        return R.success();
    }

    /**
     * 删除
     *
     * @param ids Ids
     */
    @PostMapping("/del")
    public R<?> del(@RequestBody List<Long> ids) {
        Assert.notEmpty(ids, "未指定要删除的数据");
        sysUserService.del(ids);
        return R.success();
    }

    /**
     * 详情
     *
     * @param id Id
     * @return UserDetailResult 结果
     */
    @GetMapping("/detail")
    public R<UserDetailResult> detail(@RequestParam Long id) {
        return R.success(sysUserService.detail(id));
    }

    @GetMapping("/my")
    public R<MyResult> my() {
        return R.success(sysUserService.my());
    }

    /**
     * 分页查询
     *
     * @param userQueryParam 筛选条件
     * @param page           分页参数
     * @return PageResult&lt;UserDetailResult&gt; 分页结果
     */
    @GetMapping("/paging")
    public R<PageResult<UserDetailResult>> paging(PageParam page, UserQueryParam userQueryParam) {
        return R.success(sysUserService.paging(page.toPage(), userQueryParam));
    }

    /**
     * 用户注册
     *
     * @param userRegisterParam 参数
     */
    @PostMapping("/register")
    public R<?> register(@RequestBody @Validated UserRegisterParam userRegisterParam) {
        sysUserService.register(userRegisterParam);
        return R.success();
    }

    @PostMapping("/modify_passwd")
    public R<?> modifyPasswd(@RequestBody ModifyPasswdParam modifyPasswdParam) {
        sysUserService.modifyPasswd(modifyPasswdParam);
        return R.success();
    }
}
