package com.cm.websocket.controller.apis.message;

import com.cm.common.annotation.CheckRequestBodyAnnotation;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.result.ErrorResult;
import com.cm.common.result.SuccessResult;
import com.cm.websocket.pojo.vos.message.NoticeVO;
import com.cm.websocket.service.IMessageService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: MessageController
 * @Description: 消息
 * @Author: wanggeng
 * @Date: 2021/1/14 3:25 下午
 * @Version: 1.0
 */
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "消息接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/message")
public class MessageController {

    @Autowired
    private IMessageService messageService;

    @ApiOperation(value = "发送消息（通过APPID）", notes = "发送消息（通过APPID）接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "APPID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("sendbyappid/{appId}")
    public SuccessResult sendByAppId(@PathVariable("appId") String appId) throws Exception {
        return messageService.sendByAppId(appId);
    }

    @ApiOperation(value = "发送跳转目标消息（通过APPID和目标）", notes = "发送跳转目标消息（通过APPID和目标）接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "APPID", paramType = "path"),
            @ApiImplicitParam(name = "target", value = "目标，页面或者是页面的标识", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("sendbyappidtarget/{appId}/{target}")
    public SuccessResult sendByAppIdTarget(@PathVariable("appId") String appId, @PathVariable("target") String target) {
        return messageService.sendByAppIdTarget(appId, target);
    }

    @ApiOperation(value = "通知消息", notes = "通知消息接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("noticewithtarget")
    @CheckRequestBodyAnnotation
    public SuccessResult saveNoticeWithTarget(@RequestBody NoticeVO noticeVO) throws Exception {
        return messageService.saveNoticeWithTarget(noticeVO, false);
    }

    @ApiOperation(value = "通知消息", notes = "通知消息接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("noticewithtarget/save")
    @CheckRequestBodyAnnotation
    public SuccessResult saveNoticeWithTargetAndSave(@RequestBody NoticeVO noticeVO) throws Exception {
        return messageService.saveNoticeWithTarget(noticeVO, true);
    }

    @ApiOperation(value = "删除通知", notes = "删除通知接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "serviceIds", value = "业务ID", paramType = "path"),
    })
    @DeleteMapping("delete/{serviceIds}")
    public SuccessResult delete(@PathVariable("serviceIds") String serviceIds) {
        messageService.delete(Arrays.asList(serviceIds.split("\\_")));
        return new SuccessResult();
    }

}
