package com.mortals.xhx.common.pdu.flow;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.List;
import java.util.Map;


@Getter
@Setter
@ApiModel("工作流任务相关-返回参数")
public class FlowTaskVars implements Serializable {

    @ApiModelProperty("任务Id")
    private String taskId;


    @ApiModelProperty("节点")
    private String targetKey;

    private String name;


    @ApiModelProperty("审批人")
    private String assignee;

    @ApiModelProperty("候选人")
    private List<String> candidateUsers;

    @ApiModelProperty("候选人")
    private String candidateUsersName;

    @ApiModelProperty("候选人名")
    private String candidateUsersIds;

    @ApiModelProperty("审批组")
    private List<String> candidateGroups;

    @ApiModelProperty("审批组名")
    private String candidateGroupsName;

    @ApiModelProperty("审批组ids")
    private String candidateGroupsids;


    @ApiModelProperty("el表达式")
    private String el;

    @ApiModelProperty("是否多实例并行，0否1是")
    private Integer multiple;

    @ApiModelProperty("审核人列表")
    private List<String> assigneeList;

    public static void main(String[] args) {

        String temp="[{\"el\":\"${assignee1}\",\"multiple\":0,\"name\":\"行政审批\",\"targetKey\":\"Activity_04yxg8r\",\"assignee\":\"finegirl\",\"assigneeName\":\"张三\"},{\"el\":\"${assignee2}\",\"multiple\":0,\"name\":\"经理审批\",\"targetKey\":\"Activity_0m7qwz2\",\"assignee\":\"zhaoxiaofei\",\"assigneeName\":\"赵啸非\"},{\"el\":\"assigneeList\",\"multiple\":1,\"name\":\"领导会签\",\"targetKey\":\"Activity_1uv5dhe\",\"assigneeList\":[\"zhang1\",\"zhangbao\"],\"assigneeName\":\"张1,张宝,\"}]\n";

        List<FlowTaskVars> list = JSON.parseObject(temp, new TypeReference<List<FlowTaskVars>>() {
        });

        //构建提交参数
        JSONObject jsonObject = new JSONObject();

        for(FlowTaskVars var:list){
            //判断是否并联
            if(var.getMultiple()==0){
                //key
                String el = var.getEl();
                String key = el.substring(2, el.length() - 1);
                //值
                String assignee = var.getAssignee();
                jsonObject.put(key,assignee);
            }else{
               //key
                String key = var.getEl();
                JSONArray jsonArray = new JSONArray();
                jsonArray.addAll(var.getAssigneeList());
                jsonObject.put(key,jsonArray);
            }
        }


        System.out.println(JSON.toJSONString(jsonObject));
    }


}
