package org.jeecg.modules.approval.model.vo;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.Constant;
import org.jeecg.common.base.entity.BaseTaskEntity;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.common.utils.StringUtils;
import org.jeecg.modules.apply.service.ApplyProcessService;

import java.util.List;
import java.util.Map;

/**
 * 发起审批
 *
 * @author yao
 * @since 2019/7/2 14:50
 */
@Slf4j
@Data
public class ApprovalVO<T> {

    /**
     * 当前任务实例
     */
    private T t;

    public ApprovalVO(T t) {
        this.t = t;
    }

    public Map getBaseTaskVariables() {

        BaseTaskEntity baseTaskEntity = getBaseTask();
        Map<String, Object> taskMap = Maps.newHashMap();

        taskMap.put(Constant.ACT_BASE_TASK, JSONObject.toJSONString(baseTaskEntity));
        taskMap.put(Constant.ACT_APPLY_USER, baseTaskEntity.getUserId());
        //保存变量
        if(StringUtils.isNotEmpty(baseTaskEntity.getParam())){
            Map<String, Object> param = baseTaskEntity.getParam();
            for (Map.Entry<String, Object> m : param.entrySet()) {
                taskMap.put(m.getKey(),m.getValue());
                System.out.println("key:" + m.getKey() + " value:" + m.getValue());
            }
        }

        //根据表单发起人动态保存部门领导

        return taskMap;
    }

    public BaseTaskEntity getBaseTask() {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.convertValue(this.t, BaseTaskEntity.class);
    }

    public String getBusinessKey() {
        return String.format("%s/%s", getBaseTask().getFormKey(), getBaseTask().getId());
    }

}
