package com.bohuikeji.delivery.backstage.service.impl;

import com.alibaba.fastjson.JSON;
import com.bohuikeji.delivery.backstage.entity.TNews;
import com.bohuikeji.delivery.backstage.listener.news.PushNewsListener;
import com.bohuikeji.delivery.backstage.mapper.TNewsMapper;
import com.bohuikeji.delivery.backstage.service.ITNewsService;
import com.bohuikeji.frame.module.admin.base.ServiceImplBase;
import com.bohuikeji.frame.module.admin.component.MqTool;
import com.bohuikeji.frame.module.admin.exception.MethodException;
import com.bohuikeji.frame.module.admin.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;

/**
 * <p>
 * 消息发布记录表 服务实现类
 * </p>
 *
 * @author 10552
 * @since 2019-08-22
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TNewsServiceImpl extends ServiceImplBase<TNewsMapper, TNews> implements ITNewsService {
    final MqTool mqTool;

    /**
     * 发送消息给派送员
     * @param courierId 派送员id
     * @param newsClass 消息类型
     * @param title 消息标题
     * @param msg 消息内容
     * @param args 消息填充数据
     * @return
     */
    @Override
    public TNews pushCourierMsg(Integer courierId, String newsClass, String title, String msg,Object... args) {

        TNews news=initNews(newsClass,title,msg,"t_courier",courierId,args);
        mqTool.sendMsg(JSON.toJSONString(news), PushNewsListener.class);
        return news;
    }

    /**
     * 发送消息给派送员
     *
     * @param courierId 骑手ID
     * @param title     消息标题
     * @param msg       消息内容
     * @param args      内容参数
     * @return
     */
    @Override
    public TNews pushCourierMsg1(Integer courierId, String title, String msg, Object... args) {
        TNews news=initNews(null,title,msg,"t_courier",courierId,args);
        mqTool.sendMsg(JSON.toJSONString(news), PushNewsListener.class);
        return news;
    }

    /**
     * 发送简易消息给骑手
     *
     * @param courierId 骑手ID
     * @param msg       消息内容
     * @param args      内容参数
     * @return
     */
    @Override
    public TNews pushCourierMsg2(Integer courierId, String msg, Object... args) {
        TNews news=initNews(null,null,msg,"t_courier",courierId,args);
        mqTool.sendMsg(JSON.toJSONString(news), PushNewsListener.class);
        return news;
    }

    /**
     * 发送消息给客户
     * @param customerId 客户id
     * @param newsClass 消息类型
     * @param title 消息标题
     * @param msg 消息内容
     * @param args 消息填充数据
     * @return
     */
    @Override
    public TNews pushCustomerMsg(Integer customerId, String newsClass, String title, String msg,Object... args) {
        TNews news=initNews(newsClass,title,msg,"t_customer",customerId,args);
        mqTool.sendMsg(JSON.toJSONString(news), PushNewsListener.class);
        return news;
    }

    /**
     * 发送消息给客户
     *
     * @param customerId
     * @param title
     * @param msg
     * @param args
     * @return
     */
    @Override
    public TNews pushCustomerMsg1(Integer customerId, String title, String msg, Object... args) {
        TNews news=initNews(null,title,msg,"t_customer",customerId,args);
        mqTool.sendMsg(JSON.toJSONString(news), PushNewsListener.class);
        return news;
    }

    /**
     * 发送简易消息给客户
     *
     * @param customerId 客户id
     * @param msg        消息内容
     * @param args       内容参数
     * @return
     */
    @Override
    public TNews pushCustomerMsg2(Integer customerId, String msg, Object... args) {
        TNews news=initNews(null,null,msg,"t_customer",customerId,args);
        mqTool.sendMsg(JSON.toJSONString(news), PushNewsListener.class);
        return news;
    }

    /**
     * 初始化消息对象
     * @param newsClass
     * @param title
     * @param msg
     * @param model
     * @param receiveId
     * @param args
     * @return
     */
    private TNews initNews(String newsClass, String title, String msg,String model,Integer receiveId, Object... args){
        if(StringUtils.isBlank(msg)){
            log.error("消息发送失败，消息对象内容为空。消息体：[{}]，参数：[{}]",msg,args);
            throw new MethodException("发送消息方法调用错误，请检查参数是否匹配！");
        }
        TNews news=new TNews();
        news.setNewsClass(newsClass==null?"系统消息":newsClass);
        news.setNewsTitle(title==null?"新消息提醒":title);
        news.setNewsDesc((args==null||args.length<1)?msg:MessageFormat.format(msg,args));
        news.setNewsType(1);
        news.setReceiveUserId(receiveId);
        news.setPublishType(1);
        news.setPublishModel(model);
        return news;
    }
}
