package com.zkqy.execution.produce.dispersed.service.impl.script.common;

import com.zkqy.common.utils.http.Sending;
import com.zkqy.execution.produce.dispersed.entity.BpmExecuteNode;
import com.zkqy.execution.produce.dispersed.service.IBpmExecuteNodeService;
import com.zkqy.execution.produce.dispersed.service.IScriptService;
import com.zkqy.execution.produce.dispersed.entity.runbpm.IntoProduction;
import com.zkqy.system.entity.CommonEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.PreDestroy;
import java.util.HashMap;
import java.util.Map;

/**
 * 公共的投产节点脚本
 *
 * @author hanzihang
 * @date 2023/11/13
 */
@Service("commonStartNodeProductionScheduling")
@Scope("prototype")
public class CommonProductionSchedulingServiceImpl implements IScriptService {

    @Autowired  // 执行节点记录表
    private IBpmExecuteNodeService iBpmExecuteNodeService;

    @Autowired
    private Sending sending;

    @Override
    public Object preExecutionScript(Object parameter) {
        return true;
    }

    @Override
    public Object executionScript(Object parameter) {
        // 得到当前脚本执行所需数据
        IntoProduction intoProduction = (IntoProduction) parameter;
        // 根据流程别名 把开始节点执行
        BpmExecuteNode bpmExecuteNode = new BpmExecuteNode();
        bpmExecuteNode.setTaskNodeState("1");           // 0未执行、1已执行
        bpmExecuteNode.setTaskNodeType("startEvent");   // 每个流程里只会存在一个开始节点
        bpmExecuteNode.setTaskProcessKey(intoProduction.getTaskProcessKey()); // 当前任务流程的任务编码
        iBpmExecuteNodeService.updateNodeState(bpmExecuteNode);  // 执行修改节点状态

        // 修改用户业务表中的状态
        CommonEntity commonEntity = new CommonEntity();
        Map<String, Object> conMap = new HashMap<>();  // sql条件map
        Map<String, Object> basicMap = new HashMap<>();  // 默认参数map

        String con[] = intoProduction.getGuid().split("=");
        conMap.put(con[0], con[1]);
        commonEntity.setConditionMap(conMap);  // 得到修改表的条件
        basicMap.put("tableName", intoProduction.getTableName());
        commonEntity.setBasicMap(basicMap);  // 得到修改的表名
        commonEntity.setCommMap(intoProduction.getStateMap());  // 需要修改的列
        // 根据其他的条件更新投产表状态
        ResponseEntity responseEntity = sending.sendCommonUpdate(commonEntity);
        System.out.println(responseEntity);
        return null;
    }

    @Override
    public Object afterExecutionScript(Object parameter) {
        return null;
    }

    @Override
    public boolean isVerificationMethod() {
        return true;
    }

    @Override
    @PreDestroy
    public boolean preDestroy() {
        System.out.println("清理成功！");
        return true;
    }
}
