package com.tofly.xrtygis.custom.controller.gs;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.xrtygis.custom.entity.gs.NoteSearch;
import com.tofly.xrtygis.custom.entity.gs.note.SendNoteEn;
import com.tofly.xrtygis.custom.entity.message.SendMessage;
import com.tofly.xrtygis.custom.service.SendNoteService;
import com.tofly.xrtygis.custom.service.messge.SendMessageService;
import com.tofly.xrtygis.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author FlashWJL
 * @date 2021/4/6 11:33
 */
@RestController
@AllArgsConstructor
@RequestMapping("/send")
@Api(tags = "热线--短信接口")
public class SendNoteController {

    private final SendMessageService sendMessageService;

    @ApiOperation(value = "短信查看")
    @GetMapping("/getNote")
    public ResultRespone getNote(Page page , NoteSearch noteSearch) {
        return ResultRespone.success(sendMessageService.getMessagePage(page , noteSearch));
    }


    @ApiOperation(value = "短信数量查看")
    @GetMapping("/getNoteNum")
    public ResultRespone getNoteNum() {
        return ResultRespone.success(sendMessageService.getNoteNum());
    }

//
//    /**
//     * 发送短信接口
//     * @return
//     */
//    @ApiOperation(value = "对指定的多个用户发送短信")
//    @PostMapping("/note")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "phone", value = "电话号码，多个用逗号隔开", required = true, dataType = "Sring"),
//            @ApiImplicitParam(name = "content", value = "短信内蓉", required = true, dataType = "Sring")
//    })
//    public ResultRespone sendNote(@RequestBody SendNoteEn note) {
//        String phone = note.getPhone();
//        String content = note.getContent();
//        return ResultRespone.success(sendNoteService.sendNote(phone, content));
//    }


    /**
     * 发送模板短信
     * @return
     */
    @ApiOperation(value = "发送模板短信")
    @PostMapping("/tempNote")
    public ResultRespone sendNoteTemplate(@RequestBody NoteSearch noteSearch) {
        String clientId = noteSearch.getClientId();
        Long tempId = noteSearch.getId();
        return ResultRespone.success(sendMessageService.sendTemplateMessage(clientId, tempId));
    }

//    @ApiOperation(value = "重发短信")
//    @PostMapping("/aginSend")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "短信ID", required = true, dataType = "Sring")
//    })
//    @ToFlyAppLog(title = "重发短信")
//    public ResultRespone aginSendNote(@RequestBody NoteSearch noteSearch) {
//        return ResultRespone.success(sendNoteService.aginSendNote(noteSearch));
//    }

    @ApiOperation(value = "对指定的多个用户发送短信")
    @PostMapping("/sendMessage")
    public ResultRespone sendMessage(@RequestBody SendNoteEn sendNoteEn) {
        try {
            sendNoteEn.getSendMessageList().forEach(sendMessageService::sendMessage);
        }catch (BusinessException b){
            b.printStackTrace();
            return ResultRespone.failed(b.getMsgDes());
        }catch (Exception e){
            e.printStackTrace();
            return ResultRespone.failed(e.getMessage());
        }
       return ResultRespone.success();
    }

}
