package com.njzscloud.dispose.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.dispose.sys.user.pojo.param.*;
import com.njzscloud.dispose.sys.user.pojo.result.UserDetailResult;
import com.njzscloud.dispose.sys.user.service.UserService;
import com.njzscloud.dispose.wechat.pojo.param.WechatParam;
import com.njzscloud.dispose.wechat.service.WechatService;
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;
    private final WechatService wechatService;

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

    /**
     * 修改
     */
    @PostMapping("/modify")
    public R<?> modify(@RequestBody @Validated UserModifyParam userModifyParam) {
        userService.modify(userModifyParam);
        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("id") Long id) {
        return R.success(userService.detail(id));
    }

    /**
     * 禁用
     */
    @GetMapping("/disable")
    public R<?> detail(@RequestParam("id") Long id, @RequestParam("disable") Boolean disable) {
        userService.disable(id, disable);
        return R.success();
    }

    /**
     * 绑定角色
     */
    @PostMapping("/bind_role")
    public R<?> bindRole(@RequestBody BindRoleParam bindRoleParam) {
        userService.bindRole(bindRoleParam);
        return R.success();
    }

    /**
     * 授权客户端
     */
    @GetMapping("/bind_client")
    public R<?> bindClient(@RequestParam("id") Long id, @RequestParam("clientCode") Integer clientCode) {
        userService.bindClient(id, clientCode);
        return R.success();
    }

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

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

    /**
     * 修改密码
     */
    @PostMapping("/modify_passwd")
    public R<?> modifyPasswd(@RequestBody @Validated 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");
    }

    /**
     * 获取微信openId
     */
    @PostMapping("/getOpenId")
    public R<?> getOpenId(@RequestBody WechatParam param) {
        return R.success(wechatService.getOpenId(param));
    }

    /**
     * 微信授权手机号
     */
    @PostMapping("/getMiniPhone")
    public R<?> getMiniPhone(@RequestBody WechatParam param) {
        return R.success(wechatService.getMiniPhone(param));
    }
}
