package cn.axzo.msg.center.inside.notices.service.impl;


import cn.axzo.framework.jackson.utility.JSON;
import cn.axzo.msg.center.api.request.MsgBody4Guest;
import cn.axzo.msg.center.api.response.Message;
import cn.axzo.msg.center.inside.notices.service.IYouMengMessageService;
import cn.axzo.msg.center.inside.notices.service.umeng.UpushService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;

/**
 *  友盟发送app系统消息
 * @date 2021/5/19
 */
@Service
@Slf4j
public class IYouMengMessageServiceImpl implements IYouMengMessageService {

  @Autowired
  private UpushService upushService;

  @Override
  public void sendPushMessage(MsgBody4Guest msgFromClient) {
    Message msg = new Message();
    msg.setSendTime(new Date());
    msg.setToId(msgFromClient.getT());
    msg.setHint(msgFromClient.getM());
    String extra = msgFromClient.getM2();
    JSONObject extraData = JSONObject.parseObject(extra);
    msg.setRouterType(extraData.getInteger("rt"));
    msg.setRouter(extraData.getString("ar"));
    msg.setParams(extraData.getString("p"));
    msg.setTitle(extraData.getString("t"));
    msg.setAudio(extraData.getString("audio"));
    msg.setMsgId(extraData.getLong("msgId"));
    msg.setType(extraData.getInteger("type"));
    HashMap<String, Object> sendObjects = new HashMap<>();
    sendObjects.put("msg", msg);
    sendObjects.put("msgFromClient", msgFromClient);
    sendObjects.put("extraData", extraData);
    boolean success = false;
    try {
      upushService.sendAndroidCustomizedcast(msg, msgFromClient, extraData);
      msg.setRouter(extraData.getString("ir"));
      upushService.sendIOSCustomizedcast(msg, msgFromClient, extraData);
      success = true;
    } catch (Exception e) {
      log.error("推送消息发送失败:{}", e.getMessage());
    } finally {
      log.info("send youmeng push, success={}, sendObjects={}", success, JSON.toJSONString(sendObjects));
    }

  }

}
