package com.zt.plat.module.qms.business.bus.controller.admin;

import java.util.ArrayList;
import java.util.List;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson2.JSON;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;

import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.tenant.core.context.TenantContextHolder;
import com.zt.plat.module.qms.business.bus.liteflow.param.SampleFlowInfo;
import com.zt.plat.module.qms.business.bus.liteflow.param.SampleFlowParam;
import com.zt.plat.module.qms.business.bus.liteflow.slot.SampleFlowContext;
import com.zt.plat.module.qms.business.bus.service.SampleFlowService;
import com.zt.plat.module.qms.core.sampleflow.SampleFlowDefinition;
import com.zt.plat.module.qms.core.sampleflow.SampleFlowNode;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;

import static com.zt.plat.framework.common.pojo.CommonResult.success;
import static com.zt.plat.framework.common.pojo.CommonResult.error;

@Tag(name = "管理后台 - 样品流转")
@RestController
@RequestMapping("/qms/bus/sample/flow")
@Validated
public class SampleFlowController {
	
	@Resource
	private SampleFlowService sampleFlowService;

    @Resource
    private FlowExecutor flowExecutor;
    
    @PostMapping("/sampleFlow")
    public CommonResult<?> sampleFlow(@Validated @RequestBody SampleFlowParam sampleFlowParam) {
    	LiteflowResponse response = sampleFlowService.sampleFlow(sampleFlowParam);
    	return success(response.getContextBean(SampleFlowContext.class));
    }    

    @GetMapping("/test")
    public CommonResult<?> test() {
    	
    	SampleFlowDefinition sampleFlowDefinition = new SampleFlowDefinition();
    	sampleFlowDefinition.setFlowKey("flw_sub_sample_process");
    	sampleFlowDefinition.setFlowName("子样样品流转流程");
    	List<SampleFlowNode> flowNodeList = new ArrayList<>();
    	
    	SampleFlowNode zxr = new SampleFlowNode();
    	zxr.setNodeName("中心收样");
    	zxr.setNodeKey("flw_zx_receive");
    	zxr.setSort(3100);

    	SampleFlowNode zxs = new SampleFlowNode();
    	zxs.setNodeName("中心送样");
    	zxs.setNodeKey("flw_zx_send");
    	zxs.setSort(3200);

    	SampleFlowNode bzr = new SampleFlowNode();
    	bzr.setNodeName("班组收样");
    	bzr.setNodeKey("flw_bz_receive");
    	bzr.setSort(3300);

    	SampleFlowNode ypbm = new SampleFlowNode();
    	ypbm.setNodeName("样品编密");
    	ypbm.setNodeKey("flw_ypbm");
    	ypbm.setSort(3400);

    	SampleFlowNode fxr = new SampleFlowNode();
    	fxr.setNodeName("分析收样");
    	fxr.setNodeKey("flw_fx_receive");
    	fxr.setSort(3500);

    	SampleFlowNode fxs = new SampleFlowNode();
    	fxs.setNodeName("分析送样");
    	fxs.setNodeKey("flw_fx_send");
    	fxs.setSort(3600);

    	SampleFlowNode bzs = new SampleFlowNode();
    	bzs.setNodeName("班组送样");
    	bzs.setNodeKey("flw_bz_send");
    	bzs.setSort(3700);

    	SampleFlowNode ypgk = new SampleFlowNode();
    	ypgk.setNodeName("样品归库");
    	ypgk.setNodeKey("flw_ypgk");
    	ypgk.setSort(3800);
    	ypgk.setNextFlowNodeList(null);
    	flowNodeList.add(ypgk);
    	

    	zxr.setNextFlowNodeList(new ArrayList<SampleFlowNode>() {
    		{
    			add(BeanUtils.toBean(zxs,SampleFlowNode.class).setIsDefault(true));
    		}
    	});
    	flowNodeList.add(zxr);
    	

    	zxs.setNextFlowNodeList(new ArrayList<>() {{
    		add(BeanUtils.toBean(bzr,SampleFlowNode.class).setIsDefault(true));
    	}});
    	flowNodeList.add(zxs);

    	bzr.setNextFlowNodeList(new ArrayList<>() {{
    		add(BeanUtils.toBean(ypbm,SampleFlowNode.class).setCondition("样品需要编密并且没编密过"));
    		add(BeanUtils.toBean(fxr,SampleFlowNode.class).setCondition("样品无需编密或样品已编密过"));
    	}});
    	flowNodeList.add(bzr);
    	

    	ypbm.setNextFlowNodeList(new ArrayList<>() {{
    		add(BeanUtils.toBean(fxr,SampleFlowNode.class).setIsDefault(true));
    	}});
    	flowNodeList.add(ypbm);
    	

    	fxr.setNextFlowNodeList(new ArrayList<>() {{
    		add(BeanUtils.toBean(fxs,SampleFlowNode.class).setIsDefault(true));
    	}});
    	flowNodeList.add(fxr);
    	

    	fxs.setNextFlowNodeList(new ArrayList<>() {{
    		add(BeanUtils.toBean(bzs,SampleFlowNode.class).setIsDefault(true).setCondition("当前部门任务已全部分析完"));
    	}});
    	flowNodeList.add(fxs);
    	

    	bzs.setNextFlowNodeList(new ArrayList<>() {{
    		add(BeanUtils.toBean(fxr,SampleFlowNode.class).setCondition("样品当前分析班组结果超差，需要发起复检"));
    		add(BeanUtils.toBean(bzr,SampleFlowNode.class).setCondition("当前部门数据已全部判断上报"));
    		add(BeanUtils.toBean(ypgk,SampleFlowNode.class).setCondition("当前样品所有分析任务已全部上报完"));
    	}});
    	flowNodeList.add(bzs);
    	
    	sampleFlowDefinition.setFlowNodeList(flowNodeList);
    	
    	String jsonSampleFlowDefinition = JSON.toJSONString(sampleFlowDefinition);
    	
    	System.out.println(jsonSampleFlowDefinition);
    	
    	Long tenantId = TenantContextHolder.getRequiredTenantId();
    	
    	List<SampleFlowInfo> sampleFlowInfoList = new ArrayList<>();
    	sampleFlowInfoList.add(new SampleFlowInfo().setId(1967550576457330690L).setIsWeighing(0));
    	
    	SampleFlowParam sampleFlowParam = new SampleFlowParam();
    	sampleFlowParam.setSampleSourceType(2);
    	sampleFlowParam.setCurrentSampleFlowKey("P3000");
    	sampleFlowParam.setSampleFlowInfoList(sampleFlowInfoList);
    	
    	
    	
    	LiteflowResponse response = flowExecutor.execute2Resp("sampleFlowChain" + tenantId, sampleFlowParam, SampleFlowContext.class);
    	if (response.isSuccess()) {
			return success(response.getContextBean(SampleFlowContext.class));
		} else {
			return error(500, response.getMessage());
		}
    	//return success(FlowBus.getNodeMap());
	}

}
