package com.bizzan.bitrade.controller;

import com.bizzan.bitrade.constant.SysConstant;
import com.bizzan.bitrade.entity.Country;
import com.bizzan.bitrade.entity.Member;
import com.bizzan.bitrade.entity.transform.AuthMember;
import com.bizzan.bitrade.service.CountryService;
import com.bizzan.bitrade.service.LocaleMessageSourceService;
import com.bizzan.bitrade.service.MemberService;
import com.bizzan.bitrade.util.*;
import com.bizzan.bitrade.vendor.provider.SMSProvider;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import static com.bizzan.bitrade.constant.SysConstant.SESSION_MEMBER;
import static com.bizzan.bitrade.util.MessageResult.error;
import static com.bizzan.bitrade.util.MessageResult.success;

/**
 * @author QQ 2232965526
 * @date 2020年01月08日
 */
@Api(value = "SmsController" , tags = "短信发送接口")
@Slf4j
@RestController
@RequestMapping("/mobile")
public class SmsController {
    @Autowired
    private SMSProvider saiyouSMSProvider;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MemberService memberService;
    @Resource
    private LocaleMessageSourceService localeMessageSourceService;
    @Autowired
    private CountryService countryService;
    @Value("${dev.mode.enabled:false}")
    private boolean devModeEnabled;
    @Value("${dev.mode.vcode:}")
    private String devModeVcode;

    /**
     * 注册验证码发送
     *
     * @return
     */
    @ApiOperation(value="注册验证码发送", notes="注册验证码发送")
    @ApiImplicitParams(value = {
    		@ApiImplicitParam(name = "phone",value = "手机号",required = true,dataType = "string"),
    		@ApiImplicitParam(name = "country",value = "国家 中文",required = true,dataType = "string")
    })
    @PostMapping("/code")
    public MessageResult sendCheckCode(String phone, String country) throws Exception {
        Assert.hasLength(phone, localeMessageSourceService.getMessage("REQUEST_ILLEGAL"));
        Assert.isTrue(!memberService.phoneIsExist(phone), localeMessageSourceService.getMessage("PHONE_ALREADY_EXISTS"));
        if (null == country) {
            return error(localeMessageSourceService.getMessage("REQUEST_ILLEGAL"));
        }
        Country country1 = countryService.findOne(country);
        if (null == country1) {
            return error(localeMessageSourceService.getMessage("REQUEST_ILLEGAL"));
        }
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String key = SysConstant.PHONE_REG_CODE_PREFIX + phone;
        Object code = valueOperations.get(key);
        if (code != null) {
            //判断如果请求间隔小于一分钟则请求失败
            if (!BigDecimalUtils.compare(DateUtil.diffMinute((Date) (valueOperations.get(key + "Time"))), BigDecimal.ONE)) {
                return error(localeMessageSourceService.getMessage("FREQUENTLY_REQUEST"));
            }
        }
        String randomCode = String.valueOf(GeneratorUtil.getRandomNumber(100000, 999999));
        if (devModeEnabled) {
            valueOperations.getOperations().delete(key);
            valueOperations.getOperations().delete(key + "Time");
            valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
            valueOperations.set(key + "Time", new Date(), 10, TimeUnit.MINUTES);
            log.info("[dev模式] 注册短信验证码已写入Redis, phone={}, code={}", phone, randomCode);
            return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
        }
        MessageResult result;

        System.err.println(country1.getAreaCode());

        if ("86".equals(country1.getAreaCode())) {
          /*  Assert.isTrue(ValidateUtil.isMobilePhone(phone.trim()), localeMessageSourceService.getMessage("PHONE_EMPTY_OR_INCORRECT"));*/
            result = saiyouSMSProvider.sendVerifyMessage(phone, randomCode);
        } else {

            result = saiyouSMSProvider.sendInternationalMessage(randomCode, country1.getAreaCode(), phone);
        }
        if (result.getCode() == 0) {
            valueOperations.getOperations().delete(key);
            valueOperations.getOperations().delete(key + "Time");
            // 缓存验证码
            valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
            valueOperations.set(key + "Time", new Date(), 10, TimeUnit.MINUTES);
            return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
        } else {
            return error(localeMessageSourceService.getMessage("SEND_SMS_FAILED"));
        }
    }
    
    //@RequestMapping(value = "/login/code", method = RequestMethod.POST)
    public MessageResult sendLoginCode(@SessionAttribute(SESSION_MEMBER) AuthMember user) throws Exception {
    	Member member = memberService.findOne(user.getId());
        Assert.hasText(member.getMobilePhone(), localeMessageSourceService.getMessage("NOT_BIND_PHONE"));
        MessageResult result;
        log.info("===登录验证码发送===mobile："+member.getMobilePhone());
        String randomCode = String.valueOf(GeneratorUtil.getRandomNumber(100000, 999999));
        if ("86".equals(member.getCountry().getAreaCode())) {
            result = saiyouSMSProvider.sendVerifyMessage(member.getMobilePhone(), randomCode);
        } else {
            result = saiyouSMSProvider.sendInternationalMessage(randomCode, member.getCountry().getAreaCode(), member.getMobilePhone());
        }
      //  result = saiyouSMSProvider.sendVerifyMessage(member.getMobilePhone(), randomCode);
        if (result.getCode() == 0) {
            ValueOperations valueOperations = redisTemplate.opsForValue();
            String key = SysConstant.PHONE_LOGIN_CODE_PREFIX + member.getMobilePhone();
            valueOperations.getOperations().delete(key);
            // 缓存验证码
            valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
            return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
        } else {
            return error(localeMessageSourceService.getMessage("SEND_SMS_FAILED"));
        }
    }

    /**
     * 重置交易密码验证码
     *
     * @param user
     * @return
     */
    @ApiOperation(value="重置交易密码验证码", notes="重置交易密码验证码")
    @RequestMapping(value = "/transaction/code", method = RequestMethod.POST)
    public MessageResult sendResetTransactionCode(@SessionAttribute(SESSION_MEMBER) AuthMember user) throws Exception {
        Member member = memberService.findOne(user.getId());
        Assert.hasText(member.getMobilePhone(), localeMessageSourceService.getMessage("NOT_BIND_PHONE"));
        String randomCode = String.valueOf(GeneratorUtil.getRandomNumber(100000, 999999));
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String key = SysConstant.PHONE_RESET_TRANS_CODE_PREFIX + member.getMobilePhone();
        if (devModeEnabled) {
            valueOperations.getOperations().delete(key);
            valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
            log.info("[dev模式] 重置交易密码短信验证码已写入Redis, phone={}, code={}", member.getMobilePhone(), randomCode);
            return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
        }
        MessageResult result;
        if ("86".equals(member.getCountry().getAreaCode())) {
            /*  Assert.isTrue(ValidateUtil.isMobilePhone(phone.trim()), localeMessageSourceService.getMessage("PHONE_EMPTY_OR_INCORRECT"));*/
            result = saiyouSMSProvider.sendVerifyMessage(member.getMobilePhone(), randomCode);
        } else {

            result = saiyouSMSProvider.sendInternationalMessage(randomCode, member.getCountry().getAreaCode(), member.getMobilePhone());
        }
       // result = smsProvider.sendVerifyMessage(member.getMobilePhone(), randomCode);
        if (result.getCode() == 0) {
            valueOperations.getOperations().delete(key);
            // 缓存验证码
            valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
            return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
        } else {
            return error(localeMessageSourceService.getMessage("SEND_SMS_FAILED"));
        }
    }

    /**
     * 绑定手机号验证码
     *
     * @param phone
     * @return
     * @throws Exception
     */
    @ApiOperation(value="绑定手机号验证码", notes="绑定手机号验证码")
    @ApiImplicitParams(value = {
    		@ApiImplicitParam(name = "phone",value = "手机号",required = true,dataType = "string"),
    		@ApiImplicitParam(name = "country",value = "国家 中文",required = true,dataType = "string")
    })
    @RequestMapping(value = "/bind/code", method = RequestMethod.POST)
    public MessageResult setBindPhoneCode(String country, String phone, @SessionAttribute(SESSION_MEMBER) AuthMember user) throws Exception {
        Country country1 = countryService.findOne(country);
        if (null == country1) {
            return error(localeMessageSourceService.getMessage("REQUEST_ILLEGAL"));
        }
        Member member = memberService.findOne(user.getId());
        Assert.isNull(member.getMobilePhone(), localeMessageSourceService.getMessage("REPEAT_PHONE_REQUEST"));
        String randomCode = String.valueOf(GeneratorUtil.getRandomNumber(100000, 999999));

        // 修改所在国家
        member.setCountry(country1);
        memberService.saveAndFlush(member);

        ValueOperations valueOperations = redisTemplate.opsForValue();
        String key = SysConstant.PHONE_BIND_CODE_PREFIX + phone;
        if (devModeEnabled) {
            valueOperations.getOperations().delete(key);
            valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
            log.info("[dev模式] 绑定手机短信验证码已写入Redis, phone={}, code={}", phone, randomCode);
            return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
        }
        MessageResult result;
        if ("86".equals(country1.getAreaCode())) {
            /*  Assert.isTrue(ValidateUtil.isMobilePhone(phone.trim()), localeMessageSourceService.getMessage("PHONE_EMPTY_OR_INCORRECT"));*/
            result = saiyouSMSProvider.sendVerifyMessage(phone, randomCode);
        } else {

            result = saiyouSMSProvider.sendInternationalMessage(randomCode, country1.getAreaCode(), phone);
        }
        if (result.getCode() == 0) {
            valueOperations.getOperations().delete(key);
            // 缓存验证码
            valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
            return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
        } else {
            return error(localeMessageSourceService.getMessage("SEND_SMS_FAILED"));
        }
    }

    /**
     * 更改登录密码验证码
     *
     * @param user
     * @return
     */
    @ApiOperation(value="更改登录密码验证码", notes="更改登录密码验证码")
    @RequestMapping(value = "/update/password/code", method = RequestMethod.POST)
    public MessageResult updatePasswordCode(@SessionAttribute(SESSION_MEMBER) AuthMember user) throws Exception {
        Member member = memberService.findOne(user.getId());
        Assert.hasText(member.getMobilePhone(), localeMessageSourceService.getMessage("NOT_BIND_PHONE"));
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String key = SysConstant.PHONE_UPDATE_PASSWORD_PREFIX + member.getMobilePhone();
        Object code = valueOperations.get(key);
        if (code != null) {
            //判断如果请求间隔小于一分钟则请求失败
            if (!BigDecimalUtils.compare(DateUtil.diffMinute((Date) (valueOperations.get(key + "Time"))), BigDecimal.ONE)) {
                return error(localeMessageSourceService.getMessage("FREQUENTLY_REQUEST"));
            }
        }
        //TODO
        String randomCode = String.valueOf(GeneratorUtil.getRandomNumber(100000, 999999));
        if (devModeEnabled) {
            valueOperations.getOperations().delete(key);
            valueOperations.getOperations().delete(key + "Time");
            valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
            valueOperations.set(key + "Time", new Date(), 10, TimeUnit.MINUTES);
            log.info("[dev模式] 更改登录密码短信验证码已写入Redis, phone={}, code={}", member.getMobilePhone(), randomCode);
            return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
        }
        MessageResult result;
        if ("86".equals(member.getCountry().getAreaCode())) {
            result = saiyouSMSProvider.sendVerifyMessage(member.getMobilePhone(), randomCode);
        } else {
            result = saiyouSMSProvider.sendInternationalMessage(randomCode, member.getCountry().getAreaCode(), member.getMobilePhone());
        }

        if (result.getCode() == 0) {
            valueOperations.getOperations().delete(key);
            valueOperations.getOperations().delete(key + "Time");
            // 缓存验证码
            valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
            valueOperations.set(key + "Time", new Date(), 10, TimeUnit.MINUTES);
            return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
        } else {
            return error(localeMessageSourceService.getMessage("SEND_SMS_FAILED"));
        }
    }
    
    @ApiOperation(value="谷歌认证验证码", notes="谷歌认证验证码")
    @RequestMapping(value = "/google/auth/code", method = RequestMethod.POST)
    public MessageResult authGoogleCode(@SessionAttribute(SESSION_MEMBER) AuthMember user) throws Exception {
    	Member member = memberService.findOne(user.getId());
        Assert.hasText(member.getMobilePhone(), localeMessageSourceService.getMessage("NOT_BIND_PHONE"));
        log.info("===提币验证码发送===mobile："+member.getMobilePhone());
        String randomCode = String.valueOf(GeneratorUtil.getRandomNumber(100000, 999999));
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String key = SysConstant.PHONE_AUTH_GOOGLE_PREFIX + member.getMobilePhone();
        if (devModeEnabled) {
            String devCode = (devModeVcode != null && !devModeVcode.isEmpty()) ? devModeVcode : randomCode;
            valueOperations.getOperations().delete(key);
            valueOperations.set(key, devCode, 10, TimeUnit.MINUTES);
            log.info("[dev模式] 谷歌认证短信验证码已写入Redis, phone={}, code={}", member.getMobilePhone(), devCode);
            return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
        }
        MessageResult result;
        if ("86".equals(member.getCountry().getAreaCode())) {
            result = saiyouSMSProvider.sendVerifyMessage(member.getMobilePhone(), randomCode);
        } else {
            result = saiyouSMSProvider.sendInternationalMessage(randomCode, member.getCountry().getAreaCode(), member.getMobilePhone());
        }
      //  result = saiyouSMSProvider.sendVerifyMessage(member.getMobilePhone(), randomCode);
        if (result.getCode() == 0) {
            valueOperations.getOperations().delete(key);
            // 缓存验证码
            valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
            return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
        } else {
            return error(localeMessageSourceService.getMessage("SEND_SMS_FAILED"));
        }
    }

    @ApiOperation(value="提币地址验证码", notes="提币地址验证码")
    @RequestMapping(value = "/add/address/code", method = RequestMethod.POST)
    public MessageResult addAddressCode(@SessionAttribute(SESSION_MEMBER) AuthMember user) throws Exception {
        Member member = memberService.findOne(user.getId());
        Assert.hasText(member.getMobilePhone(), localeMessageSourceService.getMessage("NOT_BIND_PHONE"));
        String randomCode = String.valueOf(GeneratorUtil.getRandomNumber(100000, 999999));
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String key = SysConstant.PHONE_ADD_ADDRESS_PREFIX + member.getMobilePhone();
        if (devModeEnabled) {
            valueOperations.getOperations().delete(key);
            valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
            log.info("[dev模式] 提币地址短信验证码已写入Redis, phone={}, code={}", member.getMobilePhone(), randomCode);
            return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
        }
        MessageResult result;
        if ("86".equals(member.getCountry().getAreaCode())) {
            result = saiyouSMSProvider.sendVerifyMessage(member.getMobilePhone(), randomCode);
        } else {
            result = saiyouSMSProvider.sendInternationalMessage(randomCode, member.getCountry().getAreaCode(), member.getMobilePhone());
        }
        if (result.getCode() == 0) {
            valueOperations.getOperations().delete(key);
            // 缓存验证码
            valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
            return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
        } else {
            return error(localeMessageSourceService.getMessage("SEND_SMS_FAILED"));
        }
    }

    @RequestMapping(value = "/activity/code", method = RequestMethod.POST)
    public MessageResult attendActivityCode(@SessionAttribute(SESSION_MEMBER) AuthMember user) throws Exception {
        Member member = memberService.findOne(user.getId());
        Assert.hasText(member.getMobilePhone(), localeMessageSourceService.getMessage("NOT_BIND_PHONE"));
        String randomCode = String.valueOf(GeneratorUtil.getRandomNumber(100000, 999999));
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String key = SysConstant.PHONE_ATTEND_ACTIVITY_PREFIX + member.getMobilePhone();
        if (devModeEnabled) {
            valueOperations.getOperations().delete(key);
            valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
            log.info("[dev模式] 参与活动短信验证码已写入Redis, phone={}, code={}", member.getMobilePhone(), randomCode);
            return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
        }
        MessageResult result;
        if ("86".equals(member.getCountry().getAreaCode())) {
            result = saiyouSMSProvider.sendVerifyMessage(member.getMobilePhone(), randomCode);
        } else {
            result = saiyouSMSProvider.sendInternationalMessage(randomCode, member.getCountry().getAreaCode(), member.getMobilePhone());
        }
       // result = saiyouSMSProvider.sendVerifyMessage(member.getMobilePhone(), randomCode);
       /* if(result != null){
            //

        }*/
        if (result.getCode() == 0) {
            valueOperations.getOperations().delete(key);
            // 缓存验证码
            valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
            return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
        } else {
            return error(localeMessageSourceService.getMessage("SEND_SMS_FAILED"));
        }
    }

    @ApiOperation(value="提币验证码", notes="提币验证码")
    @RequestMapping(value = "/withdraw/code", method = RequestMethod.POST)
    public MessageResult withdrawCode(@SessionAttribute(SESSION_MEMBER) AuthMember user) throws Exception {
        Member member = memberService.findOne(user.getId());
        Assert.hasText(member.getMobilePhone(), localeMessageSourceService.getMessage("NOT_BIND_PHONE"));
        log.info("===提币验证码发送===mobile："+member.getMobilePhone());
        String randomCode = String.valueOf(GeneratorUtil.getRandomNumber(100000, 999999));
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String key = SysConstant.PHONE_WITHDRAW_MONEY_CODE_PREFIX + member.getMobilePhone();
        if (devModeEnabled) {
            valueOperations.getOperations().delete(key);
            valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
            log.info("[dev模式] 提币短信验证码已写入Redis, phone={}, code={}", member.getMobilePhone(), randomCode);
            return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
        }
        MessageResult result;
        if ("86".equals(member.getCountry().getAreaCode())) {
            result = saiyouSMSProvider.sendVerifyMessage(member.getMobilePhone(), randomCode);
        } else {
            result = saiyouSMSProvider.sendInternationalMessage(randomCode, member.getCountry().getAreaCode(), member.getMobilePhone());
        }
      //  result = saiyouSMSProvider.sendVerifyMessage(member.getMobilePhone(), randomCode);
        if (result.getCode() == 0) {
            valueOperations.getOperations().delete(key);
            // 缓存验证码
            valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
            return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
        } else {
            return error(localeMessageSourceService.getMessage("SEND_SMS_FAILED"));
        }
    }

    //@RequestMapping(value = "/ctc/code", method = RequestMethod.POST)
    public MessageResult ctcCode(@SessionAttribute(SESSION_MEMBER) AuthMember user) throws Exception {
        Member member = memberService.findOne(user.getId());
        Assert.hasText(member.getMobilePhone(), localeMessageSourceService.getMessage("NOT_BIND_PHONE"));
        MessageResult result;
        log.info("===C2C验证码发送===mobile："+member.getMobilePhone());
        String randomCode = String.valueOf(GeneratorUtil.getRandomNumber(100000, 999999));
        if ("86".equals(member.getCountry().getAreaCode())) {
            result = saiyouSMSProvider.sendVerifyMessage(member.getMobilePhone(), randomCode);
        } else {
            result = saiyouSMSProvider.sendInternationalMessage(randomCode, member.getCountry().getAreaCode(), member.getMobilePhone());
        }


        //result = saiyouSMSProvider.sendVerifyMessage(member.getMobilePhone(), randomCode);
        if (result.getCode() == 0) {
            ValueOperations valueOperations = redisTemplate.opsForValue();
            String key = SysConstant.PHONE_CTC_TRADE_CODE_PREFIX + member.getMobilePhone();
            valueOperations.getOperations().delete(key);
            // 缓存验证码
            valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
            return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
        } else {
            return error(localeMessageSourceService.getMessage("SEND_SMS_FAILED"));
        }
    }

    /**
     * 忘记密码验证码
     */
    @ApiOperation(value="忘记密码验证码", notes="忘记密码验证码")
    @RequestMapping(value = "/reset/code", method = RequestMethod.POST)
    public MessageResult resetPasswordCode(String account, String areaCode) throws Exception {
        Member member = memberService.findByPhone(account.trim());
        if (null == member) {
            return error(localeMessageSourceService.getMessage("MEMBER_NOT_EXISTS"));
        }
        //if(!member.getCountry().getAreaCode().equals(areaCode))
        //	return error(localeMessageSourceService.getMessage("MEMBER_NOT_EXISTS"));
        String randomCode = String.valueOf(GeneratorUtil.getRandomNumber(100000, 999999));
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String key = SysConstant.RESET_PASSWORD_CODE_PREFIX + member.getMobilePhone();
        if (devModeEnabled) {
            valueOperations.getOperations().delete(key);
            valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
            log.info("[dev模式] 忘记密码短信验证码已写入Redis, phone={}, code={}", member.getMobilePhone(), randomCode);
            return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
        }
        MessageResult result;
        if ("86".equals(member.getCountry().getAreaCode())) {
            result = saiyouSMSProvider.sendVerifyMessage(member.getMobilePhone(), randomCode);
        } else {
            result = saiyouSMSProvider.sendInternationalMessage(randomCode, member.getCountry().getAreaCode(), member.getMobilePhone());
        }
       // result = saiyouSMSProvider.sendVerifyMessage(member.getMobilePhone(), randomCode);
        if (result.getCode() == 0) {
            valueOperations.getOperations().delete(key);
            // 缓存验证码
            valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
            return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
        } else {
            return error(localeMessageSourceService.getMessage("SEND_SMS_FAILED"));
        }
    }

    /**
     * 更改手机验证码
     */
    @ApiOperation(value="更改手机验证码", notes="更改手机验证码")
    @RequestMapping(value = "/change/code", method = RequestMethod.POST)
    public MessageResult resetPhoneCode(@SessionAttribute(SESSION_MEMBER) AuthMember user) throws Exception {
        Member member = memberService.findOne(user.getId());
        Assert.hasText(member.getMobilePhone(), localeMessageSourceService.getMessage("NOT_BIND_PHONE"));
        String randomCode = String.valueOf(GeneratorUtil.getRandomNumber(100000, 999999));
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String key = SysConstant.PHONE_CHANGE_CODE_PREFIX + member.getMobilePhone();
        if (devModeEnabled) {
            valueOperations.getOperations().delete(key);
            valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
            log.info("[dev模式] 更改手机短信验证码已写入Redis, phone={}, code={}", member.getMobilePhone(), randomCode);
            return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
        }
        MessageResult result;
        if ("86".equals(member.getCountry().getAreaCode())) {
            result = saiyouSMSProvider.sendVerifyMessage(member.getMobilePhone(), randomCode);
        } else {
            result = saiyouSMSProvider.sendInternationalMessage(randomCode, member.getCountry().getAreaCode(), member.getMobilePhone());
        }

        //result = saiyouSMSProvider.sendVerifyMessage(member.getMobilePhone(), randomCode);
        if (result.getCode() == 0) {
            valueOperations.getOperations().delete(key);
            // 缓存验证码
            valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
            return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
        } else {
            return error(localeMessageSourceService.getMessage("SEND_SMS_FAILED"));
        }
    }

    /**
     * 绑定API发送验证码
     * @param user
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "api/code", method = RequestMethod.POST)
    public MessageResult bindApiSendCode(@SessionAttribute(SESSION_MEMBER) AuthMember user) throws Exception {
        Member member = memberService.findOne(user.getId());
        Assert.hasText(member.getMobilePhone(), localeMessageSourceService.getMessage("NOT_BIND_PHONE"));
        log.info("===API密钥验证码发送===mobile："+member.getMobilePhone());
        String randomCode = String.valueOf(GeneratorUtil.getRandomNumber(100000, 999999));
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String key = SysConstant.API_BIND_CODE_PREFIX + member.getMobilePhone();
        if (devModeEnabled) {
            valueOperations.getOperations().delete(key);
            valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
            log.info("[dev模式] API绑定短信验证码已写入Redis, phone={}, code={}", member.getMobilePhone(), randomCode);
            return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
        }
        MessageResult result;
        if ("86".equals(member.getCountry().getAreaCode())) {
            result = saiyouSMSProvider.sendVerifyMessage(member.getMobilePhone(), randomCode);
        } else {
            result = saiyouSMSProvider.sendInternationalMessage(randomCode, member.getCountry().getAreaCode(), member.getMobilePhone());
        }
        if (result.getCode() == 0) {
            valueOperations.getOperations().delete(key);
            // 缓存验证码
            valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
            return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
        } else {
            return error(localeMessageSourceService.getMessage("SEND_SMS_FAILED"));
        }
    }

    @ApiOperation(value="代理商验证码", notes="代理商验证码")
    @RequestMapping(value = "/agent/code", method = RequestMethod.POST)
    public MessageResult sendAgentCode(@SessionAttribute(SESSION_MEMBER) AuthMember user) throws Exception {
        Member member = memberService.findOne(user.getId());
        Assert.hasText(member.getMobilePhone(), localeMessageSourceService.getMessage("NOT_BIND_PHONE"));
        String randomCode = String.valueOf(GeneratorUtil.getRandomNumber(100000, 999999));
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String key = SysConstant.PHONE_AGENT_CODE_PREFIX + member.getMobilePhone();
        if (devModeEnabled) {
            valueOperations.getOperations().delete(key);
            valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
            log.info("[dev模式] 代理商短信验证码已写入Redis, phone={}, code={}", member.getMobilePhone(), randomCode);
            return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
        }
        MessageResult result;
        if ("86".equals(member.getCountry().getAreaCode())) {
            /*  Assert.isTrue(ValidateUtil.isMobilePhone(phone.trim()), localeMessageSourceService.getMessage("PHONE_EMPTY_OR_INCORRECT"));*/
            result = saiyouSMSProvider.sendVerifyMessage(member.getMobilePhone(), randomCode);
        } else {

            result = saiyouSMSProvider.sendInternationalMessage(randomCode, member.getCountry().getAreaCode(), member.getMobilePhone());
        }
       // result = smsProvider.sendVerifyMessage(member.getMobilePhone(), randomCode);
        if (result.getCode() == 0) {
            valueOperations.getOperations().delete(key);
            // 缓存验证码
            valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
            return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
        } else {
            return error(localeMessageSourceService.getMessage("SEND_SMS_FAILED"));
        }
    }
}
