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

import com.zkqy.common.config.bpm.BpmProperties;
import com.zkqy.common.core.domain.AjaxResult;
import com.zkqy.execution.produce.dispersed.service.IScriptService;
import com.zkqy.system.entity.CommonEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * 测试排产任务节点
 */
@Service("eaf4bf2b-3142-4cb0-affb-5d427796fa61")
@Scope("prototype")
public class ProductionWarehousing implements IScriptService {


    @Autowired
    private BpmProperties bpmProperties;

    RestTemplate restTemplate = new RestTemplate();


    @Override
    public Object preExecutionScript(Object parameter) {
        //接收参数,查看参数是否存在
//        Map<String, Object> map = (Map<String, Object>) parameter;
//        //判断表名是否存在
//        if (!map.containsKey("basicMap")) return false;
//        //判断字段是否存在
//        if (!map.containsKey("commMap")) return false;
//        //判断修改条件是否存在
//        if (!map.containsKey("conditionMap")) return false;
//        //判断查询条件是否存在
//        if (!map.containsKey("queryConditions")) return false;
//        if (map.get("commMap") == null || map.get("conditionMap") == null || map.get("queryConditions") == null) {
//            return false;
//        }
        return true;
    }

    @Override
    public Object executionScript(Object parameter) {
        //接收参数
        Map<String, Object> map = (Map<String, Object>) parameter;

        //查询
        CommonEntity queryMap = new CommonEntity();
        queryMap.setBasicMap((Map<String, Object>) map.get("basicMap"));
        queryMap.setConditionMap((Map<String, Object>) map.get("queryConditions"));
        ResponseEntity<AjaxResult> response = null;
        try {
            response = restTemplate.exchange(
                    bpmProperties.formCommonGetInfoIp, HttpMethod.GET, new HttpEntity<>(queryMap), AjaxResult.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (response.getStatusCodeValue() != 200) {
            return false;
        }

        AjaxResult body = response.getBody();
        //得到详情数据
        Map<String, Object> data = (Map<String, Object>) body.get("data");
        //库里planWeight
        Long planWeight = Long.valueOf(data.get("planWeight").toString());
        Map<String, Object> basicMap = (Map<String, Object>) map.get("basicMap");
        //累加的planWeight
        Long planWeight1 = Long.valueOf(basicMap.get("planWeight").toString());
        //计算planWeight
        Long weight = planWeight + planWeight1;
        String planState = basicMap.get("planState").toString();

        //修改map
        CommonEntity editMap = new CommonEntity();
        queryMap.setBasicMap((Map<String, Object>) map.get("basicMap"));
        Map<String, Object> commMap = new HashMap<>();
        commMap.put("plan_weight", weight);
        commMap.put("plan_state", planState);
        queryMap.setCommMap(commMap);
        queryMap.setConditionMap((Map<String, Object>) map.get("conditionMap"));
        AjaxResult ajaxResult = restTemplate.postForObject(bpmProperties.formCommonUpdateIp, editMap, AjaxResult.class);
        if (Long.valueOf(ajaxResult.get("code").toString()) != 200L) return false;
        return true;
    }

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

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

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