package org.dromara.system.controller.common;

import cn.dev33.satoken.secure.BCrypt;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.SysUserBo;
import org.dromara.system.service.ISysUserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static org.dromara.system.constent.Constants.ONE;

/**
 * @Author: tjf
 * @Date: 2024/10/23 9:43
 * @Describe:
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/register")
public class RegisterCommonController extends BaseController {
    private final ISysUserService userService;

    /**
     * 新增用户
     */
    @PostMapping("/common")
    public R<Void> add(@Validated @RequestBody SysUserBo user) {
        if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user)) {
            return R.fail("新增用户'" + user.getUserName() + "'失败，手机号码已存在");
        }
       else if (ONE.equals(userService.checkStrongPwd(user))) {
            return R.fail("密码必须包含数字、大小写字母、特殊符号且大于8位");
        }
        user.setPassword(BCrypt.hashpw(user.getPassword()));
        user.setUserType("register");
        return toAjax(userService.insertUserCommon(user));
    }
}
