package com.cm.websocket.controller.resource.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.NoticeDeleteServiceVO;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

    @Autowired
    private IMessageService messageService;

    @ApiOperation(value = "通知消息（不保存记录）", notes = "通知消息（不保存记录）接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access_token", value = "access_token", paramType = "query")
    })
    @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 = "通知消息（保存记录）接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access_token", value = "access_token", paramType = "query")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("noticewithtarget/save")
    @CheckRequestBodyAnnotation
    public synchronized SuccessResult saveNoticeWithTargetAndSave(@RequestBody NoticeVO noticeVO) throws Exception {
        return messageService.saveNoticeWithTarget(noticeVO, true);
    }

    @ApiOperation(value = "删除通知", notes = "删除通知接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "serviceIds", value = "业务ID", paramType = "path"),
    })
    @PostMapping("delete")
    @CheckRequestBodyAnnotation
    public SuccessResult delete(@RequestBody NoticeDeleteServiceVO noticeDeleteServiceVO) {
        messageService.delete(noticeDeleteServiceVO.getServiceIds());
        return new SuccessResult();
    }

}
