package cn.axzo.msg.center.notices.client.controller;

import cn.axzo.msg.center.notices.common.annotation.PushOperation;
import cn.axzo.msg.center.notices.http.api.api.MessageNotifyApi;
import cn.axzo.msg.center.notices.http.api.request.SendBatchMessageRequest;
import cn.axzo.msg.center.notices.http.api.request.SendMobileMessageRequest;
import cn.axzo.msg.center.notices.manager.api.dto.request.MnsRequestDto;
import cn.axzo.msg.center.notices.service.api.MessageService;
import cn.azxo.framework.common.logger.MethodAroundLog;
import cn.azxo.framework.common.model.CommonResponse;
import cn.azxo.framework.common.utils.LogUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 消息发送 Controller
 *
 * @author zhaoyong_sh
 * @see MessageNotifyController
 * @since 2021-05-20 11:33
 */
@RestController
@Slf4j
@RequestMapping("sms/notify")
public class MessageNotifyController implements MessageNotifyApi {

  @Resource
  private MessageService messageService;

  @Resource
  private GenericConversionService genericConversionService;

  @Override
  @MethodAroundLog(source = "业务方", target = "msg-center", value = "发送单条短信")
  @PushOperation("发送短信")
  public CommonResponse sendMobileMessage(@RequestBody @Valid SendMobileMessageRequest request) {
    // 转换对象
    MnsRequestDto requestDto = genericConversionService.convert(request, MnsRequestDto.class);

    // 发送短信
    messageService.sendMessage(requestDto);

    // 响应请求
    return CommonResponse.success();
  }

  @Override
  @MethodAroundLog(source = "业务方", target = "msg-center", value = "发送批量短信", requestExcludeName = {"smsContents"})
  public CommonResponse sendBatchMessage(@RequestBody @Valid SendBatchMessageRequest request) {

      LogUtil.error("请联系管理员，批量发送短信功能已弃用");
      return CommonResponse.fail("请联系管理员，批量发送短信功能已弃用");
  }

}
