package com.sifang.modules.site.registerInfo.web;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.platform.service.AddressService;
import com.sifang.modules.platform.utils.AddressUtils;
import com.sifang.modules.site.registerInfo.service.RegisterService;
import com.sifang.modules.sys.web.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping(value = "${adminPath}/register")
public class RegisteredController extends BaseController {

    private static Logger logger = Logger.getLogger(RegisteredController.class);

    @Autowired
    private RegisterService registerService;
    @Autowired
    AddressService addressService;

    /**
     * 跳转注册页面
     *
     * @return
     */
    @RequestMapping(value = "jumpRegistered")
    public String jumpRegistered(HttpServletRequest request, Model model) {
        List<Record> province = addressService.getProvinces();//获取所有的省
        if (province.size() > 0) {
            model.addAttribute("province", province);
        }
        return "modules/site/registerInfo/register";
    }

    /**
     * 点击获取验证码按钮---获取验证码
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "sendMsg")
    @ResponseBody
    public String sendMsg(HttpServletRequest request) {
        String mobile = request.getParameter("mobile");//手机号
        String code = randomCode();//获取生成的随机数
        request.getSession().setAttribute("confirmCode", code);
        String checkMobile = "";
        if (StringUtils.isNotBlank(mobile) && StringUtils.isNotBlank(code)) {
            checkMobile = registerService.checkMobile(mobile, code);//发送短信
        }
        return checkMobile;
    }

    /**
     * 获取4位随机验证码
     *
     * @return
     */
    public String randomCode() {
        Random rand = new Random();
        String code = "";
        for (int i = 0; i < 4; i++) {
            code = code + String.valueOf(rand.nextInt(10));
        }
        return code;
    }



}
