package xiaoqu.home.open.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hutool.core.util.RandomUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import xiaoqu.home.open.constant.Constants;
import xiaoqu.home.open.constant.Errors;
import xiaoqu.home.open.model.db2.SmsSendRecord;
import xiaoqu.home.open.publicBean.BaseOutput;
import xiaoqu.home.open.service.RedisService4;
import xiaoqu.home.open.service.db2.SmsSendRecordService;
import xiaoqu.home.open.utils.ResultResponseBeanUtil;
import xiaoqu.home.open.utils.SmsUtil;

/**********************
 * 短信发送接口
 *
 * @author wh
 */
@Controller
@Api(value = "短信发送接口", description = "短信发送接口")
@RequestMapping(value = "/sms")
public class SendSmsController {

	@Autowired
	private RedisService4 redisService4;
	
	@Value("${spring.profiles.active}")
	private String springProfilesActive;
	@Autowired
	private SmsSendRecordService smsSendRecordService;

	/**************
	 * 短信验证码接口
	 *
	 * @param mobile
	 * @return login
	 */
	@ApiOperation(value = "短信验证码接口")
	@RequestMapping(value = "/sendCode", method = RequestMethod.GET)
	@ResponseBody
	public BaseOutput<Void> smsSend(HttpServletRequest request, @RequestParam(name = "mobile") String mobile) {
		if (mobile != null && mobile.length() != 11) {
			return ResultResponseBeanUtil.fail(Errors.PARAM_ERROR, "请输入有效的手机号");
		}
		if (redisService4.isMember(Constants.whitelistMobile, mobile)) {
			redisService4.set(Constants.verificationCode + mobile, "123456", 120L);
			return ResultResponseBeanUtil.success();
		}
		if (springProfilesActive.contains("test") || springProfilesActive.equals("dev")) {
			redisService4.set(Constants.verificationCode + mobile, "123456", 120L);
			return ResultResponseBeanUtil.success();
		}
		// 限制一分钟内只输入一次
		if (redisService4.get("mobileLimit:" + mobile) != null) {
			return ResultResponseBeanUtil.fail(Errors.SYSTEM_ERROR, "一分钟内只能发送一次短信，请稍后重试");
		}
		// 加入手机号限制
		redisService4.set("mobileLimit:" + mobile, 1 + "", 60L);
		String random = RandomUtil.randomNumbers(6);
		Long smsId = System.currentTimeMillis();
		boolean flag = SmsUtil.sendSms(random, mobile, smsId);
		if (flag) {
			SmsSendRecord smsSendRecord = new SmsSendRecord();
			smsSendRecord.setBussType("验证码");
			smsSendRecord.setReportState(99);
			smsSendRecord.setSmsContent("验证码=" + random);
			smsSendRecord.setSmsid(smsId + "");
			smsSendRecord.setUserMobile(mobile);
			smsSendRecord.setSendState(0);
			smsSendRecordService.save(smsSendRecord);
			// 验证码有效期放redis里
			// 120秒有效
			redisService4.set(Constants.verificationCode + mobile, random, 120L);
			return ResultResponseBeanUtil.success();
		}
		SmsSendRecord smsSendRecord = new SmsSendRecord();
		smsSendRecord.setBussType("验证码");
		smsSendRecord.setReportState(99);
		smsSendRecord.setSmsContent("验证码=" + random);
		smsSendRecord.setSmsid(smsId + "");
		smsSendRecord.setUserMobile(mobile);
		smsSendRecord.setSendState(1);
		smsSendRecordService.save(smsSendRecord);
		return ResultResponseBeanUtil.fail(Errors.VER_CODE_FAIL, "短信发送失败，请重试");
	}

}
