package com.liquidnet.service.executor.main.handler;

import com.liquidnet.service.feign.sweet.task.FeignSweetWechatClient;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * sweet
 */
@Component
public class SweetWechatTaskHandler {
    private static final Logger log = LoggerFactory.getLogger(SweetWechatTaskHandler.class);

    @Autowired
    private FeignSweetWechatClient feignSweetWechatClient;

    @XxlJob(value = "sev-sweet:sendMsg")
    public ReturnT<String> sendWechatMsgHandler() {
        try {
            Object data = feignSweetWechatClient.send().getData();
            log.info("sendWechatMsgHandler：结果：{}", data);
            return new ReturnT<>(ReturnT.SUCCESS_CODE, String.valueOf(data));
        } catch (Exception e) {
            log.error("exception of handler:{}", e.getMessage(), e);
            return new ReturnT<>(ReturnT.FAIL_CODE, e.getLocalizedMessage());
        }
    }

    @XxlJob(value = "sev-sweet:sendSubMsg")
    public ReturnT<String> sendSubMsgHandler() {
        try {
            String jobParam = XxlJobHelper.getJobParam();//执行参数
            log.info("jobParam = " + jobParam);
            String[] paramArray = jobParam.split(":");

            ReturnT<String> success = new ReturnT<>();
            success.setCode(ReturnT.SUCCESS_CODE);

            for (String typeAndTargetId : paramArray) {
                String[] typeAndTargetIdArray = typeAndTargetId.split(",");
                Integer type = Integer.parseInt(typeAndTargetIdArray[0]);
                String targetId = typeAndTargetIdArray[1];
                Object data = feignSweetWechatClient.sendSubMsg(type, targetId).getData();
                log.info("sendSubMsgHandler：结果：{}", data);
                success.setMsg(String.valueOf(data));
            }

            return success;
        } catch (Exception e) {
            log.error("exception of handler:{}", e.getMessage(), e);
            return new ReturnT<>(ReturnT.FAIL_CODE, e.getLocalizedMessage());
        }
    }

}
