/*
* Copyright (c) 京北方信息技术股份有限公司 Corporation 2021 . All rights reserved.
*
*/
package net.northking.cctp.testbug.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.common.http.ResultCodeEnum;
import net.northking.cctp.common.http.ResultWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
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;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 缺陷管理 => Restful 类型 HTTP/HTTPS 接口
 *
 * @author maven-cctp-plugin
 * @since 1.0
 */
@Api(tags = "缺陷无需token的接口")
@RequestMapping("/pub/v1")
@RestController
public class BugPubCtrl
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(BugPubCtrl.class);

  @Autowired
  private RedisTemplate redisTemplate;


  /**
   * 接收短信存入数据库
   *
   * @param param
   * @return
   */
  @ApiOperation(value = "将短信存入数据库")
  @PostMapping(
          value = "/putSMS",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper putSMS(
          @RequestBody Map<String,Object> param)
  {
    String phoneNumber = (String) param.get("phoneNumber");
    String message = (String)param.get("message");
    ResultWrapper wrapper = new ResultWrapper<>();
    if (!StringUtils.isEmpty(phoneNumber)&&!StringUtils.isEmpty(message)) {
      redisTemplate.opsForValue().set(phoneNumber,message);
      redisTemplate.expire(phoneNumber,60*60L, TimeUnit.SECONDS);
    }else {
      return wrapper.fail(ResultCodeEnum.FAIL.getCode(),"手机号或信息为空");
    }

    return wrapper.success();
  }

  /**
   * 根据手机号查询短信
   *
   * @param param 集合
   * @return
   */
  @ApiOperation(value = "根据手机号查询短信")
  @PostMapping(
          value = "/getSMS",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<String> getSMS(
          @RequestBody Map<String,Object> param)
  {
    String phoneNumber = (String) param.get("phoneNumber");
    String message=null;
    ResultWrapper<String> wrapper = new ResultWrapper<>();
    if (!StringUtils.isEmpty(phoneNumber)) {
      message= (String) redisTemplate.opsForValue().get(phoneNumber);
    }else {
      return wrapper.fail(ResultCodeEnum.FAIL.getCode(),"手机号为空");
    }
    return wrapper.success(message);
  }
}
