package com.gree.mall.manager.schedule;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.gree.mall.manager.constant.Constant;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.logic.sop.JobLogic;
import com.gree.mall.manager.logic.wxCustomer.SingleChatMessageLogic;
import com.gree.mall.manager.plus.service.AdminCompanyWechatService;
import com.gree.mall.manager.plus.service.MarketingMaterialsService;
import com.gree.mall.manager.plus.service.SopPushTaskService;
import com.gree.mall.manager.plus.service.SopWorkerPushService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 推送redis延迟消息定时任务
 */
@ConditionalOnProperty(name = "schedule.enable", havingValue = "true", matchIfMissing = true)
@Slf4j
@Component
public class CarryJobScheduled {

    @Autowired
    private JobLogic jobLogic;

    @Autowired
    private SopPushTaskService sopPushTaskService;

    @Autowired
    private CommonLogic commonLogic;

    @Autowired
    private RedisLockRegistry redisLockRegistry;

    @Autowired
    private SingleChatMessageLogic singleChatMessageLogic;

    @Autowired
    private MarketingMaterialsService marketingMaterialsService;

    @Autowired
    private SopWorkerPushService sopWorkerPushService;


    @Autowired
    private AdminCompanyWechatService adminCompanyWechatService;

    static {
    /*    if (isWindows()) {
            System.loadLibrary("WeWorkFinanceSdk");

        }*/
        if (!isWindows()) {
            java.io.File file1 = new java.io.File("/tmp/file/libWeWorkFinanceSdk_Java.so");
            log.info("加载文件开始");
            if (file1.exists()) {
                System.load("/tmp/file/libWeWorkFinanceSdk_Java.so");
            } else {
                try {

                    InputStream stream = ResourceUtil.getStream("static/libWeWorkFinanceSdk_Java.so");
                    cn.hutool.core.io.file.FileWriter writer = new cn.hutool.core.io.file.FileWriter("/tmp/file/libWeWorkFinanceSdk_Java.so");
                    writer.writeFromStream(stream);
                    log.info("写入文件结束");
                } catch (Exception e) {
                    log.error(e.getMessage());
                }
                System.load("/tmp/file/libWeWorkFinanceSdk_Java.so");
                log.info("加载成功");
            }
        }
    }


    public static boolean isWindows() {
        String osName = System.getProperties().getProperty("os.name");
        return osName.toUpperCase().contains("WIN");
    }

    public void push(Map<String, String> map, String accessToken, Map<String, Object> mapTO) {
        List<String> externalUserid = new ArrayList<>();
        externalUserid.add(map.get("externalUserID"));
        mapTO.put("external_userid", externalUserid);
        mapTO.put("sender", map.get("toUser"));
        mapTO.put("access_token", accessToken);
        JSONObject object = new JSONObject(mapTO);
        String post = HttpRequest.post(Constant.WXUrl.WX_PUSH_MSG_URL + accessToken)
                .body(object.toString())
                .execute().body();
        log.info("=====================" + post);
    }


}
