package com.cm.common.dingding.controller.resource;

import com.cm.common.base.AbstractController;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.dingding.service.IDingDingMessageService;
import com.cm.common.exception.ParamsException;
import com.cm.common.result.ErrorResult;
import com.cm.common.result.SuccessResult;
import com.cm.common.utils.RegexUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: DingDingMessageController
 * @Description: 钉钉消息
 * @Author: WangGeng
 * @Date: 2020/9/24 13:40
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_RESOURCE_PREFIX + "钉钉消息接口")
@RestController
@RequestMapping(ISystemConstant.RESOURCE_PREFIX + "/dingdingmessage")
public class DingDingMessageResourceController extends AbstractController {

    @Autowired
    private IDingDingMessageService dingDingMessageService;

    @ApiOperation(value = "发送消息", notes = "发送消息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access_token", value = "access_token", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "phone", paramType = "query"),
            @ApiImplicitParam(name = "content", value = "content", paramType = "query"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("send")
    public SuccessResult send(@RequestParam("phone") String phone, @RequestParam("content") String content) {
        if (!RegexUtil.isPhone(phone)) {
            throw new ParamsException("手机格式错误");
        }
        return dingDingMessageService.send(phone, content);
    }

    @ApiOperation(value = "通过手机列表发送消息", notes = "通过手机列表发送消息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access_token", value = "access_token", paramType = "query"),
            @ApiImplicitParam(name = "phones", value = "phone", paramType = "query"),
            @ApiImplicitParam(name = "content", value = "content", paramType = "query"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("sendbyphones")
    public SuccessResult sendByPhones(@RequestParam("phones") String phones, @RequestParam("content") String content) {
        return dingDingMessageService.sendByPhones(Arrays.asList(phones.split("_")), content);
    }

}
