package com.mortals.xhx.daemon.demo;

import com.alibaba.fastjson.JSON;
import com.mortals.xhx.common.model.RequestTaskReq;
import com.mortals.xhx.common.pdu.api.ApiReqPdu;
import com.mortals.xhx.common.pdu.api.ApiRespPdu;
import com.mortals.xhx.feign.flowable.IApiFlowFeign;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.HashMap;

/**
 * @author karlhoo
 */
@CommonsLog
@RestController
@RequestMapping("test")
public class RequestDispatchController {
    @Autowired
    private IApiFlowFeign apiFlowFeign;


    @PostMapping("task/acceptor")
    public ApiRespPdu taskAcceptor(@RequestBody ApiReqPdu<RequestTaskReq> req) {

        log.info("receive api  request:" + JSON.toJSONString(req));
        String processDiagram = apiFlowFeign.getProcessDiagram(req.getTransmission().getProcessInstanceId());
        log.info("receive api base64  receive:" + processDiagram);

        ApiRespPdu<String> respPdu = new ApiRespPdu<>();

        respPdu.setData(processDiagram);
        return respPdu;
    }

    @PostMapping("task/deploy")
    public ApiRespPdu deploy(@RequestBody ApiReqPdu<RequestTaskReq> req) {

        log.info("receive api  request:" + JSON.toJSONString(req));
        ApiRespPdu<String> deploy = apiFlowFeign.deploy(req.getTransmission().getModelId());
        log.info("receive api base64  receive:" + JSON.toJSONString(deploy));

        ApiRespPdu<String> respPdu = new ApiRespPdu<>();
        respPdu.setData(JSON.toJSONString(deploy));
        return respPdu;
    }

    @PostMapping("task/start")
    public ApiRespPdu start(@RequestBody ApiReqPdu<RequestTaskReq> req) {
        log.info("receive api  request:" + JSON.toJSONString(req));

        ApiRespPdu<String> start = apiFlowFeign.start(req.getTransmission().getDeployId(), req.getTransmission().getUserId(), req.getTransmission().getDataKey());
        log.info("receive api start  receive:" + JSON.toJSONString(start));

        ApiRespPdu<String> respPdu = new ApiRespPdu<>();
        respPdu.setData(JSON.toJSONString(start));
        return respPdu;
    }


    @PostMapping("task/complete")
    public ApiRespPdu taskComplete(@RequestBody ApiReqPdu<RequestTaskReq> req) {
        log.info("receive api  request:" + JSON.toJSONString(req));

        //任务完成，设置审核人，完成当前任务

        HashMap<String, Object> map = new HashMap<>();
        map.put("test","1111");

        apiFlowFeign.taskByAssignee(req.getTransmission().getTaskId(),req.getTransmission().getAssignee(),map);

        ApiRespPdu<String> start = apiFlowFeign.start(req.getTransmission().getDeployId(), req.getTransmission().getUserId(), req.getTransmission().getDataKey());
        log.info("receive api complete  receive:" + JSON.toJSONString(start));

        ApiRespPdu<String> respPdu = new ApiRespPdu<>();
        respPdu.setData(JSON.toJSONString(start));
        return respPdu;
    }

    public static void main(String[] args) {

        RequestTaskReq requestTaskReq = new RequestTaskReq();
        requestTaskReq.setProcessInstanceId("11111");
        requestTaskReq.setDeployId("1");
        requestTaskReq.setUserId("admin");
        requestTaskReq.setDataKey("data");

        ApiReqPdu<RequestTaskReq> requestTaskReqApiReqPdu = new ApiReqPdu<>();
        requestTaskReqApiReqPdu.setTransmission(requestTaskReq);
        requestTaskReqApiReqPdu.setSeq("123456");

        System.out.println(JSON.toJSONString(requestTaskReqApiReqPdu));

    }

}
