package com.njzscloud.supervisory.sys.user.controller;

import cn.hutool.core.lang.Assert;
import com.njzscloud.common.core.ex.Exceptions;
import com.njzscloud.common.core.utils.R;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.common.security.util.SecurityUtil;
import com.njzscloud.supervisory.sys.user.pojo.entity.UserEntity;
import com.njzscloud.supervisory.sys.user.pojo.param.AddUserParam;
import com.njzscloud.supervisory.sys.user.pojo.param.ModifyPasswdParam;
import com.njzscloud.supervisory.sys.user.pojo.param.UserModifyParam;
import com.njzscloud.supervisory.sys.user.pojo.param.UserQueryParam;
import com.njzscloud.supervisory.sys.user.pojo.result.UserDetailResult;
import com.njzscloud.supervisory.sys.user.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息
 */
@Slf4j
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public R<Long> add(@RequestBody @Validated AddUserParam addUserParam) {
        return R.success(userService.add(addUserParam));
    }

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

    /**
     * 修改
     */
    @PostMapping("/modify_info")
    public R<?> modifyInfo(@RequestBody UserModifyParam userModifyParam) {
        userService.updateById(new UserEntity()
                .setId(SecurityUtil.currentUserId())
                .setAvatar(userModifyParam.getAvatar())
                .setNickname(userModifyParam.getNickname()));
        return R.success();
    }

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

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

    /**
     * 分页查询
     */
    @GetMapping("/paging")
    public R<PageResult<UserDetailResult>> paging(PageParam page, UserQueryParam userQueryParam) {
        return R.success(userService.paging(page.toPage(), userQueryParam));
    }

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

    /**
     * 修改密码
     */
    @PostMapping("/modify_passwd")
    public R<?> modifyPasswd(@RequestBody ModifyPasswdParam modifyPasswdParam) {
        userService.modifyPasswd(modifyPasswdParam);
        return R.success();
    }

    /**
     * 重置密码
     */
    @GetMapping("/reset_passwd")
    public R<?> resetPasswd(@RequestParam("id") Long id) {
        userService.resetPasswd(id);
        return R.success(true, "重置成功，新密码：6个6");
    }

}
