package com.unis.detectionnum.server.message.controller;

import com.unis.common.utils.ResponseWrapper;
import com.unis.detectionnum.server.message.common.CommonConstant;
import com.unis.detectionnum.server.message.service.SMSIdentifyCodeService;
import com.unis.detectionnum.server.message.service.SubmitDataService;
import com.unis.detectionnum.server.message.utils.CacheUtil;
import com.unis.detectionnum.server.message.entity.MessageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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;

/**
 * 短信功能 控制器
 *<dependency>
 *     <groupId>dom4j</groupId>
 *     <artifactId>dom4j</artifactId>
 *     <version>1.6.1</version>
 * </dependency>
 * @author YangZR
 * @since 2020-03-10
 */
@RestController
@RequestMapping("/message")
@Slf4j
@SuppressWarnings("all")
@Api(value = "短信功能", tags = "短信功能")
public class SubmitDataConroller{
	
    @Autowired
    private SubmitDataService submitDataService;

    @Autowired
	private SMSIdentifyCodeService smsService;

     /**
   	  * 发送短信验证码
      *
      * @author YangZR
      * @since 2020-03-10
   	 */
      @ApiOperation(value = "发送短信验证码", notes = "参数：手机号，创建人")
      @PostMapping(value = "/sendCodeMessage")
      public ResponseWrapper sendCodeMessage(String mobile, String creater) {
          try {
              Object timeMillisObj = CacheUtil.get(CommonConstant.MOBILE_REDIS_KEY_PRE + mobile);
              if(null != timeMillisObj) {
                  long timeMillis1 = (Long) timeMillisObj;
                  long timeMillis2 = System.currentTimeMillis();
                  Long s = (timeMillis2 - timeMillis1)/1000;
                  if(s < 60) {
                      return ResponseWrapper.error("60秒内不能重复发送");
                  }
              }
              boolean flag = smsService.sendMessage(mobile,creater);
              return ResponseWrapper.success(200, "发送消息成功",flag);
          } catch (Exception e) {
              log.error("发送消息，发生异常："+e.getMessage());
              return ResponseWrapper.error("短信发送失败");
          }
      }


    /**
     * 判断验证码是否正确
     *
     * @author YangZR
     * @since 2020-03-10
     */
    @ApiOperation(value = "判断验证码是否正确", notes = "参数：手机号，验证码")
    @PostMapping(value = "/checkCodeMessage")
    public ResponseWrapper checkCodeMessage(String mobile, String code) {
        try {
        	boolean flag = smsService.checkIsCorrectCode(mobile, code);
        	String info = "";
        	if(flag) {
                info = "短信验证码正确";
            } else {
                info = "短信验证码错误";
            }
     		return ResponseWrapper.success(200, info,flag);
        } catch (Exception e) {
            log.error("短信验证码验证失败："+e.getMessage());
            return ResponseWrapper.error("短信验证码验证失败");
        }
    }


    /**
     * 发送短信
     *
     * @author YangZR
     * @since 2020-03-10
     */
    @ApiOperation(value = "发送短信", notes = "参数：手机号，创建人, 内容")
    @PostMapping(value = "/sendContensMessage")
    public ResponseWrapper sendContensMessage(@RequestBody MessageVo messageVo) {
        try {
            boolean flag = smsService.sendMessageAndContens(messageVo.getMobile(), messageVo.getCreater(), messageVo.getContens());
            return ResponseWrapper.success(200, "发送消息成功",flag);
        } catch (Exception e) {
            log.error("发送消息，发生异常："+e.getMessage());
            return ResponseWrapper.error("发送消息失败");
        }
    }


}
