package org.jeecg.modules.alUser.controller;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.alUser.service.UserService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService;
import org.jeecg.modules.postBean.ReqUserRole;
import org.jeecg.modules.postBean.UserLoginModel;
import org.jeecg.modules.utils.SmsClient;
import org.jeecg.modules.utils.ValidateTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author scott
 * @since 2018-12-17
 */
@RestController
@RequestMapping("/alUser")
@Api(tags = "用户登陆注册")
@Slf4j
public class UserLoginController {
    @Autowired
    private IHanHaiMemberService iHanHaiMemberService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private UserService userService;


    @ApiOperation(value = "用户登陆")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result<JSONObject> login(@RequestBody UserLoginModel userLoginModel) {
        if (ValidateTool.isNull(userLoginModel)) {
            return Result.error("参数错误");
        }
        if (ValidateTool.isNull(userLoginModel.getUsername())) {
            return Result.error("请填写用户名");
        }
        if (ValidateTool.isNull(userLoginModel.getPassword())) {
            return Result.error("请填写密码");
        }
        return userService.login(userLoginModel);
    }


    @ApiOperation(value = "用户注册")
    @RequestMapping(value = "/regUesr", method = RequestMethod.POST)
    public Result<JSONObject> regUesr(@RequestBody UserLoginModel userLoginModel) {
        if (ValidateTool.isNull(userLoginModel)) {
            return Result.error("参数错误");
        }
        if (ValidateTool.isNull(userLoginModel.getUsername())) {
            return Result.error("请填写用户名");
        }
        if (ValidateTool.isNull(userLoginModel.getPassword())) {
            return Result.error("请填写密码");
        }
        return userService.reg(userLoginModel);
    }

    @ApiOperation(value = "选择身份")
    @RequestMapping(value = "/role", method = RequestMethod.POST)
    public Result<JSONObject> role(@RequestBody ReqUserRole reqUserRole) {
        if (ValidateTool.isNull(reqUserRole)) {
            return Result.error("参数错误");
        }
        if (ValidateTool.isNull(reqUserRole.getUserName())) {
            return Result.error("请填写用户名");
        }
        if (ValidateTool.isNull(reqUserRole.getPassword())) {
            return Result.error("请填写密码");
        }
        return userService.role(reqUserRole);
    }

    /**
     * 发送短信接口
     *
     * @param
     * @return
     */


    @ApiOperation(value = "发送短信接口")
    @PostMapping(value = "/sendSms")
    public Result<String> sendSms(@RequestBody UserLoginModel userLoginModel) {
        Result<String> result = new Result<String>();
        String mobile = userLoginModel.getUsername();
        log.info(mobile);
        if (oConvertUtils.isEmpty(mobile)) {
            result.setMessage("手机号不允许为空！");
            result.setSuccess(false);
            return result;
        }
        Object object = redisUtil.get(CommonConstant.PHONE_CODE + mobile);
        if (object != null) {
            result.setMessage("验证码3分钟内，仍然有效！");
            result.setSuccess(false);
            return result;
        }
        //随机数
        String captcha = RandomUtil.randomNumbers(6);
        LambdaQueryWrapper<HanHaiMember> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HanHaiMember::getAccount, mobile);
        HanHaiMember one = iHanHaiMemberService.getOne(queryWrapper);
        if (ValidateTool.isNull(one)) {
            result.error500("请先注册账号");
            return result;
        }

        String send = SmsClient.send(mobile, captcha);
        log.info("短信发送结果{}", send);
        //验证码三分钟过期
        redisUtil.set(CommonConstant.PHONE_CODE + mobile, captcha, 180);
        //todo 刷短信接口限制
        return Result.ok(captcha);
    }


}
