package com.cm.manager.sms.controller;

import com.cm.common.annotation.CheckRequestBodyAnnotation;
import com.cm.common.base.AbstractController;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.exception.ParamsException;
import com.cm.common.exception.RemoveException;
import com.cm.common.exception.SearchException;
import com.cm.common.pojo.ListPage;
import com.cm.common.result.ErrorResult;
import com.cm.common.result.SuccessResult;
import com.cm.common.result.SuccessResultList;
import com.cm.common.utils.RegexUtil;
import com.cm.manager.sms.pojo.dtos.sms.SmsDTO;
import com.cm.manager.sms.pojo.vos.sms.SmsVO;
import com.cm.manager.sms.service.ISmsService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: VerificationCodeController
 * @Description: 验证码
 * @Author: WangGeng
 * @Date: 2020/3/4 7:13 下午
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "验证码接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/sms")
@CrossOrigin(origins = "*", maxAge = 3600)
public class SmsController extends AbstractController {

    @Autowired
    private ISmsService smsService;

    @ApiOperation(value = "新增短信", notes = "新增短信接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("savesms")
    @CheckRequestBodyAnnotation
    public SuccessResult saveSms(@RequestBody SmsVO smsVO) throws Exception {
        return smsService.saveSms(smsVO);
    }

    @ApiOperation(value = "删除短信(id列表)", notes = "删除短信(id列表)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ID列表，用下划线分隔", paramType = "path", example = "1_2_3")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("removesms/{ids}")
    public SuccessResult removeSms(@PathVariable("ids") String ids) throws RemoveException {
        return smsService.removeSms(ids);
    }

    @ApiOperation(value = "修改短信", notes = "修改短信接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "smsId", value = "短信ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("updatesms/{smsId}")
    @CheckRequestBodyAnnotation
    public SuccessResult updateSms(@PathVariable("smsId") String smsId, @RequestBody SmsVO smsVO) throws Exception {
        return smsService.updateSms(smsId, smsVO);
    }

    @ApiOperation(value = "短信详情(通过ID)", notes = "短信详情(通过ID)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "smsId", value = "短信ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("getsmsbyid/{smsId}")
    public SmsDTO getSmsById(@PathVariable("smsId") String smsId) throws SearchException {
        return smsService.getSmsById(smsId);
    }

    @ApiOperation(value = "短信列表", notes = "短信列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listsms")
    public List<SmsDTO> listSms() throws SearchException {
        Map<String, Object> params = requestParams();
        return smsService.listSms(params);
    }

    @ApiOperation(value = "短信分页列表", notes = "短信分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "int", defaultValue = "20"),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpagesms")
    public SuccessResultList<List<SmsDTO>> listPageSms(ListPage page) throws SearchException {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return smsService.listPageSms(page);
    }

    @ApiOperation(value = "获取短信验证码", notes = "获取短信验证码接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("getverificationcode/{phone}")
    public SuccessResult getVerificationCode(@PathVariable("phone") String phone) throws Exception {
        if (!RegexUtil.isPhone(phone)) {
            throw new ParamsException("手机号格式错误");
        }
        return smsService.saveVerificationCode(phone);
    }

}
