package  org.ts.ddcs.Worker;

import com.alibaba.fastjson.JSONObject;
import org.ts.ddcs.common.constant.GBConstants;
import org.ts.ddcs.kafka.KafkaClient;
import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.main.DataProcess;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Callable;


/**
 * @author dylan
 */
public class WarningInfoNotifyWorker implements Callable<Integer> {
    private String agreement;
    private String rtuCode;
    private String datagram;
    private String warningKind;
    private String warningInfo;
    private Date checkTime;

    public WarningInfoNotifyWorker(Date checkTime, String agreement, String rtuCode, String warningKind, String datagram, String warningInfo) {
        this.checkTime = checkTime;
        this.agreement = agreement;
        this.rtuCode = rtuCode;
        this.datagram = datagram;
        this.warningKind = warningKind;
        this.warningInfo = warningInfo;
    }

    @Override
    public Integer call() throws Exception {
        String logId = LogHelper.createLogId("WarningInfoNotifyWorker");
        try {
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            JSONObject body = new JSONObject();
            body.put(GBConstants.BASE_KEY_WARNING_CHECK_TIME, timeFormat.format(this.checkTime));
            if (null != this.agreement) {
                body.put(GBConstants.BASE_KEY_AGREEMENT, this.agreement);
            }
            if (null != this.rtuCode) {
                body.put(GBConstants.BASE_KEY_RTU, this.rtuCode);
            }
            body.put(GBConstants.BASE_KEY_DATAGRAM, this.datagram);
            body.put(GBConstants.BASE_KEY_WARNING_KIND, this.warningKind);
            if (null != this.warningInfo) {
                body.put(GBConstants.BASE_KEY_WARNING_INFO, this.warningInfo);
            }
            body.put(GBConstants.BASE_KEY_WARNING_STATUS, GBConstants.WARNING_STATUS_HAPPEN);
            String queue = DataProcess.queueConfig().getWarningMessage();
            KafkaClient.messageSubmit(queue, body.toJSONString());
            LogHelper.info(logId + " warning notify " + queue + " " + this.rtuCode + " " + this.datagram + " " + this.warningKind + " " + this.warningInfo);
        } catch (Exception e) {
            LogHelper.error(e);
        }
        return 0;
    }
}
