package com.ruoyi.web.controller.index;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
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 com.ruoyi.common.utils.SecurityUtils.checkStrongPwd;

/**
 * @Author: tjf
 * @Date: 2023/2/8 13:46
 * @Describe:
 */
@RestController
@RequestMapping("/index")
public class IndexController extends BaseController {
    @Autowired
    private ISysUserService userService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 新增用户
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysUser user)
    {
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user)))
        {
            return error("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        else if (StringUtils.isNotEmpty(user.getPhonenumber())
                && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user)))
        {
            return error("新增用户'" + user.getUserName() + "'失败，手机号码已存在");
        }
        else if ("1".equals(checkStrongPwd(user.getPassword()))) {
            return AjaxResult.error("密码必须包含数字、大小写字母、特殊符号且大于8位");
        }
        user.setCreateBy("APP");
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        return toAjax(userService.insertUserApp(user));
    }
    /**
     * 重置密码app
     */
    @PostMapping("/resetPwdAPP")
    public AjaxResult resetPwdAPP(@RequestBody SysUser user)
    {
        String verifyKey = CacheConstants.SMS_CODE_KEY + user.getPhonenumber();

        String codeApp = redisCache.getCacheObject(verifyKey);
        if (StringUtils.isBlank(codeApp)){
            return AjaxResult.error("短信验证码已过期");
        }
        String code = user.getCode();
        if (!codeApp.equals(code)){
            return AjaxResult.error("验证码不正确");
        }
        if ("1".equals(checkStrongPwd(user.getPassword()))) {
            return AjaxResult.error("密码必须包含数字、大小写字母、特殊符号且大于8位");
        }
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        return toAjax(userService.resetPwd(user));
    }
    /**
     * 修改用户手机号
     */
    @PostMapping("/updatePhone")
    public AjaxResult updatePhone(@RequestBody SysUser user)
    {

        if (StringUtils.isNotEmpty(user.getPhonenumber())
                && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user)))
        {
            return error("修改用户'" + user.getUserName() + "'失败，手机号码已存在");
        }
        user.setUpdateBy(getUsername());
        return toAjax(userService.updateUser(user));
    }
}
