package com.hzya.frame.plugin.pushMessage.plugin;

import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.base.PluginBaseEntity;
import com.hzya.frame.plugin.pushMessage.dao.IPushMessageDao;
import com.hzya.frame.plugin.pushMessage.entity.PushMessageEntity;
import com.hzya.frame.sysnew.comparison.masterData.service.IMasterDataService;
import com.hzya.frame.sysnew.pushMessage.dao.ISysPushMessageDao;
import com.hzya.frame.sysnew.pushMessage.entity.SysPushMessageEntity;
import com.hzya.frame.sysnew.pushMessage.service.ISysPushMessageService;
import com.hzya.frame.sysnew.sendMessageLog.service.ISysSendMessageLogService;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("PushMessagePluginInitializer")
public class PushMessagePluginInitializer extends PluginBaseEntity {
    Logger logger = LoggerFactory.getLogger(PushMessagePluginInitializer.class);

    @Autowired
    private ISysPushMessageDao sysPushMessageDao;

    @Resource
    public ISysSendMessageLogService sysSendMessageLogService;

    @Override
    public void initialize() {
        logger.info(getPluginLabel() + "執行初始化方法initialize()");
    }

    @Override
    public void destroy() {
        logger.info(getPluginLabel() + "執行銷毀方法destroy()");
    }

    @Override
    public String getPluginId() {
        return "PushMessagePlugin";
    }

    @Override
    public String getPluginName() {
        return "PushMessagePlugin插件";
    }

    @Override
    public String getPluginLabel() {
        return "PushMessagePlugin";
    }

    @Override
    public String getPluginType() {
        return "1";
    }
    @Override
    public JsonResultEntity executeBusiness(JSONObject requestJson) {
        try {
            logger.info("======开始执行定时消息推送========");
            //目前只查询一周内的异常日志进行消息推送
            List<SysPushMessageEntity> list = sysPushMessageDao.getAll();

            for(SysPushMessageEntity entity : list){
                sysSendMessageLogService.sendMessage(entity);
            }
            return BaseResult.getSuccessMessageEntity("定时推送消息成功");
        }catch (Exception e){
            logger.info("======执行定时消息推送失败:{}========",e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
}
