package com.zbf.system.listener;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zbf.common.annotation.Log;
import com.zbf.common.core.domain.entity.SysUser;
import com.zbf.system.domain.OutBound;
import com.zbf.system.domain.TCkOrderdetail;
import com.zbf.system.domain.TCkOrders;
import com.zbf.system.domain.TMiStock;
import com.zbf.system.mapper.OutBoundMapper;
import com.zbf.system.mapper.TCkOrderdetailMapper;
import com.zbf.system.mapper.TMiStockMapper;
import com.zbf.system.service.ISysUserService;
import com.zbf.system.service.ITCkOrdersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.math.BigDecimal;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.*;

/**
 * 自定义任务监听器
 */
@Slf4j
@Component
public class MyTaskListener implements TaskListener {
    @Autowired
    private ITCkOrdersService tCkOrdersService;

//    @Resource
//    private OutBoundMapper outBoundMapper;
    @Autowired
    private RuntimeService runtimeService;
    private static MyTaskListener myListener;

    // 解决监听器中 Bean 获取不到问题
    @PostConstruct
    public void init() {
        myListener = this;
        myListener.runtimeService = this.runtimeService;
        myListener.tCkOrdersService = this.tCkOrdersService;
//        myListener.outBoundMapper = this.outBoundMapper;
    }

    public void notify(DelegateTask delegateTask) {
        System.out.println("============自定义任务监听器 start============");
        String taskDefinitionKey = delegateTask.getTaskDefinitionKey();
        String eventName = delegateTask.getEventName();
        System.out.println("事件名称：" + eventName);
        System.out.println("监听器：" + taskDefinitionKey);
        System.out.println("============自定义任务监听器 end============");
        if (EVENTNAME_COMPLETE.equals(delegateTask.getEventName())) {
            System.out.println("处理成功");
            Map<String, Object> variables = myListener.runtimeService.getVariables(delegateTask.getExecutionId());
            System.out.println(variables);
            String deliveryId = (String) delegateTask.getVariable("deliveryId");
            TCkOrders tCkOrders1 = myListener.tCkOrdersService.selectTCkOrdersByDeliveryId(deliveryId);
            TCkOrders tCkOrders = new TCkOrders();
            tCkOrders.setId(tCkOrders1.getId());
//            String userName = delegateTask.getAssignee();
////            SysUser user = userService.selectUserByUserName(userName);
//
//            tCkOrders.setAuditMan(delegateTask.getAssignee());
//            tCkOrders.setAuditDate(new Date());
            tCkOrders.setIsAudit("1");
            myListener.tCkOrdersService.updateTCkOrders(tCkOrders);
            System.out.println("============推送机器人开始 begin============");
            myListener.tCkOrdersService.sendIwms(tCkOrders);
            System.out.println("============推送机器人结束 end============");
        }
//        else if(EVENTNAME_CREATE.equals(delegateTask.getEventName())){
//            System.out.println("创建任务成功");
//            String taskId = delegateTask.getId();
//            String id =(String) delegateTask.getVariable("outBoundId");
//            System.out.println("获取到任务ID:"+taskId);
//            //更新outbound表和tckOrder表
//            OutBound outBound =new OutBound();
//            outBound.setId(Long.parseLong(id));
//            outBound.setTaskId(taskId);
//            myListener.outBoundMapper.updateOutBound(outBound);
//            System.out.println("更新完成");
//        }
    }


}
