package com.sunxung.factoring.component.util;

import com.sunxung.factoring.component.exception.BusinessException;
import com.sunxung.factoring.entity.BaseEntity;
import com.sunxung.factoring.entity.project.Project;
import com.sunxung.factoring.entity.sys.User;
import com.sunxung.factoring.entity.sys.workingDayManage.WorkingDayManage;
import com.sunxung.factoring.service.sys.workingDayManage.WorkingDayManageService;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;

/**
 * @Description:工作流引擎生成businessKey和解析其业务数据工具类
 * @Author:wanxiangZhou
 * @E-mail:zhouyu@sunxung.cn
 * @Date:2018年6月25日 下午4:17:47
 */
public class FlowableUtil {

    /**
     * 分隔符
     **/
    public static final String splitChar = "_";

    /**
     * 生成Flowable工作流引擎businessKey值<br>
     * 约定说明：针对一条源(主)业务对应多个业务流程，如合同付款信息可对应多个放款申请流程;<br>
     * 这种情况，返回值格式为"源业务类别clazz_源业务ID_流程业务ID_搜索条件"第三个必须为流程业务ID因为FlowableUtil.getBusinessDateById()方法需要据此获取业务对象。
     *
     * @param entity        业务实体
     * @param key           流程管理key
     * @param queryBusiness 业务查询条件数据
     * @return
     */
    public static String generateBusinessKey(BaseEntity entity, String key, String queryBusiness) {
        if (null != entity) {
            if (StringUtil.isNotEmpty(queryBusiness)) {
                return key + splitChar + entity.getId() + splitChar + queryBusiness;
            }
            return key + splitChar + entity.getId();
        }
        return null;
    }

    /**
     * 根据Flowable工作流引擎业务KEY，获取具体业务对象
     *
     * @param businessKey 工作流业务KEY
     * @return 业务实体对象
     */
    public static Object getBusinessDateById(String businessKey) {
        if (StringUtil.isNotEmpty(businessKey)) {
            String[] result = businessKey.split(splitChar);
            Object sevice;
            if (null != result && result.length >= 2) {
                String className = result[0];
                String id = result[1];
//                if (className.equals(ConstantUtil.PAYMENT_INFO) && result.length >= 3) {
//                    id = result[2];
//                    className = ConstantUtil.PAYMENT_APPLY_BASIC;// 后续需要放置map映射
//                } else {
//                    id = result[1];
//                }
                sevice = SpringUtil
                        .getBeanByName(className.substring(0, 1).toLowerCase() + className.substring(1) + "Service");
                try {
                    Method method = sevice.getClass().getMethod("get", Long.class);
                    return method.invoke(sevice, new Object[]{
                            Long.parseLong(id)
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new BusinessException(CodeUtil.FAIL, "获取流程业务数据出错啦！" + e.getMessage());
                }
            }
        }
        return null;
    }

    /**
     * 获取表单业务信息
     *
     * @param serviceName    业务service名称
     * @param formBusinessId 表单业务ID
     * @return 表单业务对象
     */
    public static Object getFormBusinessDateById(String serviceName, Long formBusinessId) {
        if (null != formBusinessId && StringUtil.isNotEmpty(serviceName)) {
            if (serviceName.equals(ConstantUtil.FINANCIAL_CUSTOMER_COMMIT)) {
                serviceName = ConstantUtil.FINANCIAL_CUSTOMER_PROCESS.substring(0, 1).toLowerCase()
                        + ConstantUtil.FINANCIAL_CUSTOMER_PROCESS.substring(1);
            }else if (serviceName.equals(ConstantUtil.FINANCIAL_CUSTOMER_APPROVAL_PROCESS)){
                serviceName = ConstantUtil.FINANCIAL_CUSTOMER_APPROVAL.substring(0, 1).toLowerCase()
                        + ConstantUtil.FINANCIAL_CUSTOMER_APPROVAL.substring(1);
            }else if(serviceName.equals(ConstantUtil.ACCEPTANCE_SHEET_ADJUST_SERVICE)){
                //单价调整申请
                    serviceName =  ConstantUtil.ACCEPTANCE_SHEET_ADJUST.substring(0, 1).toLowerCase()
                    + ConstantUtil.ACCEPTANCE_SHEET_ADJUST.substring(1);
            }else if(serviceName.equals(ConstantUtil.ACCEPTANCE_SHEET_ADJUST_SERVICE_CHECK)){
                //单价调整审核
                serviceName =  ConstantUtil.ACCEPTANCE_SHEET_ADJUST_CHECK.substring(0, 1).toLowerCase()
                    + ConstantUtil.ACCEPTANCE_SHEET_ADJUST_CHECK.substring(1);
            }else if(serviceName.equals("sale_invoice_approval1")){
                //单价调整审核
                serviceName = "sale_invoice_approval";
            }
            Object sevice = SpringUtil.getBeanByName(serviceName + "Service");
            try {
                Method method = sevice.getClass().getMethod("getView", Long.class);
                return method.invoke(sevice, new Object[]{
                        formBusinessId
                });
            } catch (Exception e) {
                e.printStackTrace();
                throw new BusinessException(CodeUtil.FAIL, "获取表单业务数据出错啦！" + e.getMessage());
            }
        }
        return null;
    }

    /**
     * 删除暂存的表单业务数据
     *
     * @param serviceName 业务service名称
     * @param assignee    处理人信息
     * @param businessKey 流程实例业务ID
     * @return true表示删除成功，否则失败
     */
    public static Object deleteFormBusinessDataByVisiable(String serviceName, User assignee, Long businessKey) {
        if (null != businessKey && null != assignee && StringUtil.isNotEmpty(serviceName)) {

            Object sevice = SpringUtil.getBeanByName(serviceName + "Service");
            try {
                Method method = sevice.getClass().getMethod("deleteByVisiable", User.class, Long.class);
                return method.invoke(sevice, new Object[]{
                        assignee, businessKey
                });
            } catch (Exception e) {
                throw new BusinessException(CodeUtil.FAIL, "删除暂存表单业务数据时出错啦！" + e.getMessage());
            }
        }
        return null;
    }

    /**
     * 更新业务数据状态
     *
     * @param entity 开启流程实例时的业务实体
     * @return
     */
    public static Object updateStatusById(Project entity) {
        if (null != entity && null != entity.getStatusDictionary()) {
            String className = entity.getClass().getSimpleName();
            Object sevice = SpringUtil
                    .getBeanByName(className.substring(0, 1).toLowerCase() + className.substring(1) + "Service");
            try {
                Method method = sevice.getClass().getMethod("updateStatus", Project.class);
                return method.invoke(sevice, new Object[]{
                        entity
                });
            } catch (Exception e) {
                throw new BusinessException(CodeUtil.FAIL, "更新业务数据状态时出错啦！" + e.getMessage());
            }
        }
        return null;
    }

    /**
     * 工作日时限：获取业务办理倒计时详情（最小单位：0.5天）
     *
     * @param createTime 任务开始时间
     * @param timeLimit  任务时限
     * @param workingDay 工作日(true)、自然日(false)标识
     * @return 负数代表逾期天数；正数代表倒计时，未逾期
     */
    public static Float getBusinessDeadlineByWorkingDay(Date createTime, Float timeLimit, boolean workingDay) {
        if (null != createTime && null != timeLimit && timeLimit > 0) {
            Date now = new Date();
            Date endTime = null;// 逾期截止日期
            Float deadline = 0f;
            // 判断任务开始时间是上午还是下午，设置临界时间点
            // 注意：优先判断createTime是否工作日，比如createTime是非工作日，workingDay为true即工作日，那么createTime临界时间点为createTime 23:59:59
            WorkingDayManage today = getWorkingDayManageService().getByDateTime(createTime);
            if (null != today && (workingDay && !today.isWorkingDay()) || (!workingDay && today.isWorkingDay())) {
                endTime = DateUtil.setTimeToMax(createTime);
            } else {
                if (ConstantUtil.AM_CNAME.equals(DateUtil.getCnameOfAMPM(createTime))) {
                    endTime = DateUtil.setTimeToPM(createTime);// 从当前日期11:59:59开始计时
                } else {
                    endTime = DateUtil.setTimeToMax(createTime);// 从当前日期23:59:59开始计时
                }
            }
            if (null != timeLimit && timeLimit > 0) {
                Integer day = null;// 整天
                Integer half = null;// 半天
                String[] liStrs = timeLimit.toString().split("\\.");
                if (null != liStrs && liStrs.length > 1) {
                    day = Integer.parseInt(liStrs[0]);
                    half = Integer.parseInt(liStrs[1]);
                    if (null != day && day > 0) {
                        for (int i = 0; i < day; i++) {
                            endTime = DateUtil.addDay(endTime, 1);
                            WorkingDayManage wdm = getWorkingDayManageService().getByDateTime(endTime);
                            if (null != wdm) {
                                if ((workingDay && !wdm.isWorkingDay()) || (!workingDay && wdm.isWorkingDay())) {// 要求：同为工作日或自然日为单位进行倒计时计算
                                    --i;
                                }
                            }
                        }
                    }
                    if (null != half && half > 0) {
                        endTime = DateUtil.addHour(endTime, 12);// 半天以12小时为标准
                        boolean flag = true;
                        int none = 100;// 防止死循环一
                        while (flag && none > 0) {
                            WorkingDayManage wdm = getWorkingDayManageService().getByDateTime(endTime);
                            if (null != wdm) {
                                if ((workingDay && !wdm.isWorkingDay()) || (!workingDay && wdm.isWorkingDay())) {
                                    endTime = DateUtil.addDay(endTime, 1);
                                    none--;
                                } else {
                                    flag = false;
                                    break;
                                }
                            } else {// 防止死循环二
                                flag = false;
                                break;
                            }
                        }
                    }
                    if (now.after(endTime)) {// 当前日期位于逾期截止日期之后，说明逾期中
                        // 直接过滤掉当天的情况，把endTime日期直接设置成第二天开始，便于在工作日信息比对
                        if (ConstantUtil.PM_CNAME.equals(DateUtil.getCnameOfAMPM(endTime))) {// 下午的临界点为23:59:59，加1h变成第二天
                            endTime = DateUtil.addHour(endTime, 1);
                        } else {// 上午临界点11:59:59，加13h变成第二天
                            endTime = DateUtil.addHour(endTime, 13);
                            deadline = 0.5f;
                        }
                        List<WorkingDayManage> workingDays = getWorkingDayManageService().findByStartEndDate(endTime,
                                now);
                        if (null != workingDays && workingDays.size() > 0) {
                            for (WorkingDayManage wdm : workingDays) {
                                if ((workingDay && !wdm.isWorkingDay()) || (!workingDay && wdm.isWorkingDay())) {
                                    continue;
                                } else {
                                    if (DateUtil.sameDate(wdm.getDateTime(), now)
                                            && ConstantUtil.AM_CNAME.equals(DateUtil.getCnameOfAMPM(now))) {// 这是最后一天，判断上午下午，其结果是0.5天还是1天
                                        deadline = deadline + 0.5f;
                                    } else {
                                        deadline = deadline + 1f;
                                    }
                                }
                            }
                        }
                        if (null != deadline && deadline > 0) {
                            return -deadline;
                        }
                    } else {// 未逾期，计算倒计时
                        // 如果当前时间跟任务开始日期为同一天，则倒计时即为节点时限
                        if (DateUtil.sameDate(now, createTime)) {
                            return timeLimit;
                        }
                        List<WorkingDayManage> workingDays = getWorkingDayManageService().findByStartEndDate(now,
                                endTime);
                        if (null != workingDays && workingDays.size() > 0) {
                            WorkingDayManage firstDay = workingDays.get(0);
                            WorkingDayManage lastDay = workingDays.get(workingDays.size() - 1);
                            boolean firstFlag = false;
                            for (WorkingDayManage wdm : workingDays) {
                                if ((workingDay && !wdm.isWorkingDay()) || (!workingDay && wdm.isWorkingDay())) {
                                    if (firstDay.getId() == wdm.getId()) {// 第一天直接过滤掉，不符合
                                        firstFlag = true;
                                    }
                                    continue;
                                } else {
                                    if (!firstFlag) {
                                        if (ConstantUtil.AM_CNAME.equals(DateUtil.getCnameOfAMPM(now))) {// 开始第一天（同一天情况已return过滤了），与逾期逻辑相反
                                            deadline = deadline + 1f;
                                        } else {
                                            deadline = deadline + 0.5f;
                                        }
                                        firstFlag = true;// 重置值
                                    } else {
                                        if (null != lastDay && lastDay.getId() == wdm.getId()) {// 最后一天
                                            if (ConstantUtil.AM_CNAME.equals(DateUtil.getCnameOfAMPM(endTime))) {
                                                deadline = deadline + 0.5f;
                                            } else {
                                                deadline = deadline + 1f;
                                            }
                                        } else {// 中间天数直接+1
                                            deadline = deadline + 1f;
                                        }
                                    }
                                }
                            }
                        }
                        if (null != deadline && deadline > 0) {
                            return deadline;
                        }
                    }
                }
            }
        }
        return null;
    }

    private static WorkingDayManageService workingDayManageService;

    private static WorkingDayManageService getWorkingDayManageService() {
        if (workingDayManageService == null) {
            workingDayManageService = SpringUtil.getBeanByType(WorkingDayManageService.class);
        }
        return workingDayManageService;
    }

    /**
     * 自然日时限：获取业务办理倒计时详情（最小单位：0.5天）
     *
     * @param createTime 任务开始时间
     * @param timeLimit  任务时限
     * @param workingDay 工作日(true)、自然日(false)标识
     * @return 负数代表逾期天数；正数代表倒计时，未逾期
     */
    public static Float getBusinessDeadlineByNatureDay(Date createTime, Float timeLimit, boolean workingDay) {
        if (null != createTime && null != timeLimit && timeLimit > 0) {
            Date now = new Date();
            Date endTime = null;// 逾期截止日期
            String overdue = null;// 倒计时
            String createTimeAMPM = null;// 标识createTime是上午还是下午
            // 判断任务开始时间是上午还是下午，设置临界时间点
            if (ConstantUtil.AM_CNAME.equals(DateUtil.getCnameOfAMPM(createTime))) {
                createTimeAMPM = ConstantUtil.AM_CNAME;
                endTime = DateUtil.setTimeToPM(createTime);// 从当前日期11:59:59开始计时
            } else {
                createTimeAMPM = ConstantUtil.PM_CNAME;
                endTime = DateUtil.setTimeToMax(createTime);// 从当前日期23:59:59开始计时
            }
            if (null != timeLimit && timeLimit > 0) {
                Integer day = null;// 整天
                Integer half = null;// 半天
                String[] liStrs = timeLimit.toString().split("\\.");
                if (null != liStrs && liStrs.length > 1) {
                    day = Integer.parseInt(liStrs[0]);
                    half = Integer.parseInt(liStrs[1]);
                    if (null != day && day > 0) {
                        endTime = DateUtil.addDay(endTime, day);
                    }
                    if (null != half && half > 0) {
                        endTime = DateUtil.addHour(endTime, 12);// 半天以12小时为标准
                    }
                    if (now.after(endTime)) {// 当前日期位于逾期截止日期之后，说明逾期了
                        overdue = DateUtil.autoOverdue(endTime, now);
                        if (StringUtil.isNotEmpty(overdue)) {
                            return -Float.parseFloat(overdue);
                        }
                    } else {// 未逾期，计算倒计时
                        // 如果当前时间跟任务开始日期为同一天，且判断上午还是下午，则同理设置当前日期对应的上午或下午的临界时间点
                        if (DateUtil.sameDate(now, createTime)) {
                            if (ConstantUtil.AM_CNAME.equals(createTimeAMPM)
                                    && ConstantUtil.AM_CNAME.equals(DateUtil.getCnameOfAMPM(now))) {
                                now = DateUtil.setTimeToPM(now);
                            } else if (ConstantUtil.PM_CNAME.equals(createTimeAMPM)
                                    && ConstantUtil.PM_CNAME.equals(DateUtil.getCnameOfAMPM(now))) {
                                now = DateUtil.setTimeToMax(now);
                            }
                        }
                        overdue = DateUtil.autoOverdue(now, endTime);
                        if (StringUtil.isNotEmpty(overdue)) {
                            return Float.parseFloat(overdue);
                        }
                    }
                }
            }
        }
        return null;
    }

    /**
     * 自然日时限（逾期）：获取业务办理逾期详情（最小单位：0.5天）
     *
     * @param startTime 任务开始时间
     * @param timeLimit 任务时限
     * @return 逾期
     */
    public static Float getBusinessOverdueByNatureDay(Date createTime, Float timeLimit) {
        if (null != createTime && null != timeLimit && timeLimit > 0) {
            Date now = new Date();
            Date endTime = null;// 逾期截止日期
            String overdue = null;// 倒计时
            // 判断任务开始时间是上午还是下午，设置临界时间点
            if (ConstantUtil.AM_CNAME.equals(DateUtil.getCnameOfAMPM(createTime))) {
                endTime = DateUtil.setTimeToPM(createTime);// 从当前日期11:59:59开始计时
            } else {
                endTime = DateUtil.setTimeToMax(createTime);// 从当前日期23:59:59开始计时
            }
            if (null != timeLimit && timeLimit > 0) {
                Integer day = null;// 整天
                Integer half = null;// 半天
                String[] liStrs = timeLimit.toString().split("\\.");
                if (null != liStrs && liStrs.length > 1) {
                    day = Integer.parseInt(liStrs[0]);
                    half = Integer.parseInt(liStrs[1]);
                    if (null != day && day > 0) {
                        endTime = DateUtil.addDay(endTime, day);
                    }
                    if (null != half && half > 0) {
                        endTime = DateUtil.addHour(endTime, 12);// 半天以12小时为标准
                    }
                    if (now.after(endTime)) {// 当前日期位于逾期截止日期之后，说明逾期了
                        overdue = DateUtil.autoOverdue(endTime, now);
                        if (StringUtil.isNotEmpty(overdue)) {
                            return -Float.parseFloat(overdue);
                        }
                    }
                }
            }
        }
        return null;
    }

    public static Float getBusinessDeadlineTest(Date createTime, Float timeLimit, boolean workingDay, Date now) {
        if (null != createTime && null != timeLimit && timeLimit > 0 && null != now) {
            Date endTime = null;// 逾期截止日期
            Float deadline = 0f;
            // 判断任务开始时间是上午还是下午，设置临界时间点
            if (ConstantUtil.AM_CNAME.equals(DateUtil.getCnameOfAMPM(createTime))) {
                endTime = DateUtil.setTimeToPM(createTime);// 从当前日期11:59:59开始计时
            } else {
                endTime = DateUtil.setTimeToMax(createTime);// 从当前日期23:59:59开始计时
            }
            if (null != timeLimit && timeLimit > 0) {
                Integer day = null;// 整天
                Integer half = null;// 半天
                String[] liStrs = timeLimit.toString().split("\\.");
                if (null != liStrs && liStrs.length > 1) {
                    day = Integer.parseInt(liStrs[0]);
                    half = Integer.parseInt(liStrs[1]);
                    if (null != day && day > 0) {
                        for (int i = 0; i < day; i++) {
                            endTime = DateUtil.addDay(endTime, 1);
                            WorkingDayManage wdm = getWorkingDayManageService().getByDateTime(endTime);
                            if (null != wdm) {
                                if ((workingDay && !wdm.isWorkingDay()) || (!workingDay && wdm.isWorkingDay())) {// 要求：同为工作日或自然日为单位进行倒计时计算
                                    --i;
                                }
                            }
                        }
                    }
                    if (null != half && half > 0) {
                        endTime = DateUtil.addHour(endTime, 12);// 半天以12小时为标准
                        boolean flag = true;
                        while (flag) {
                            WorkingDayManage wdm = getWorkingDayManageService().getByDateTime(endTime);
                            if (null != wdm) {
                                if ((workingDay && !wdm.isWorkingDay()) || (!workingDay && wdm.isWorkingDay())) {
                                    endTime = DateUtil.addDay(endTime, 1);
                                } else {
                                    flag = false;
                                    break;
                                }
                            }
                        }
                    }
                    if (now.after(endTime)) {// 当前日期位于逾期截止日期之后，说明逾期中
                        // 直接过滤掉当天的情况，把endTime日期直接设置成第二天开始，便于在工作日信息比对
                        if (ConstantUtil.PM_CNAME.equals(DateUtil.getCnameOfAMPM(endTime))) {// 下午的临界点为23:59:59，加1h变成第二天
                            endTime = DateUtil.addHour(endTime, 1);
                        } else {// 上午临界点11:59:59，加13h变成第二天
                            endTime = DateUtil.addHour(endTime, 13);
                            deadline = 0.5f;
                        }
                        List<WorkingDayManage> workingDays = getWorkingDayManageService().findByStartEndDate(endTime,
                                now);
                        if (null != workingDays && workingDays.size() > 0) {
                            for (WorkingDayManage wdm : workingDays) {
                                if ((workingDay && !wdm.isWorkingDay()) || (!workingDay && wdm.isWorkingDay())) {
                                    continue;
                                } else {
                                    if (DateUtil.sameDate(wdm.getDateTime(), now)
                                            && ConstantUtil.AM_CNAME.equals(DateUtil.getCnameOfAMPM(now))) {// 这是最后一天，判断上午下午，其结果是0.5天还是1天
                                        deadline = deadline + 0.5f;
                                    } else {
                                        deadline = deadline + 1f;
                                    }
                                }
                            }
                        }
                        if (null != deadline && deadline > 0) {
                            return -deadline;
                        }
                    } else {// 未逾期，计算倒计时
                        // 如果当前时间跟任务开始日期为同一天，则倒计时即为节点时限
                        if (DateUtil.sameDate(now, createTime)) {
                            return timeLimit;
                        }
                        List<WorkingDayManage> workingDays = getWorkingDayManageService().findByStartEndDate(now,
                                endTime);
                        if (null != workingDays && workingDays.size() > 0) {
                            WorkingDayManage firstDay = workingDays.get(0);
                            WorkingDayManage lastDay = workingDays.get(workingDays.size() - 1);
                            boolean firstFlag = false;
                            for (WorkingDayManage wdm : workingDays) {
                                if ((workingDay && !wdm.isWorkingDay()) || (!workingDay && wdm.isWorkingDay())) {
                                    if (firstDay.getId() == wdm.getId()) {// 第一天直接过滤掉，不符合
                                        firstFlag = true;
                                    }
                                    continue;
                                } else {
                                    if (!firstFlag) {
                                        if (ConstantUtil.AM_CNAME.equals(DateUtil.getCnameOfAMPM(now))) {// 开始第一天（同一天情况已return过滤了），与逾期逻辑相反
                                            deadline = deadline + 1f;
                                        } else {
                                            deadline = deadline + 0.5f;
                                        }
                                        firstFlag = true;// 重置值
                                    } else {
                                        if (null != lastDay && lastDay.getId() == wdm.getId()) {// 最后一天
                                            if (ConstantUtil.AM_CNAME.equals(DateUtil.getCnameOfAMPM(endTime))) {
                                                deadline = deadline + 0.5f;
                                            } else {
                                                deadline = deadline + 1f;
                                            }
                                        } else {// 中间天数直接+1
                                            deadline = deadline + 1f;
                                        }
                                    }
                                }
                            }
                        }
                        if (null != deadline && deadline > 0) {
                            return deadline;
                        }
                    }
                }
            }
        }
        return null;
    }

    public static Float getBusinessDeadlineByNatureDayTest(Date createTime, Float timeLimit, boolean workingDay,
                                                           Date now) {
        if (null != createTime && null != timeLimit && timeLimit > 0) {
            // Date now = new Date();
            Date endTime = null;// 逾期截止日期
            String overdue = null;// 倒计时
            // 判断任务开始时间是上午还是下午，设置临界时间点
            if (ConstantUtil.AM_CNAME.equals(DateUtil.getCnameOfAMPM(createTime))) {
                endTime = DateUtil.setTimeToPM(createTime);// 从当前日期11:59:59开始计时
            } else {
                endTime = DateUtil.setTimeToMax(createTime);// 从当前日期23:59:59开始计时
            }
            if (null != timeLimit && timeLimit > 0) {
                Integer day = null;// 整天
                Integer half = null;// 半天
                String[] liStrs = timeLimit.toString().split("\\.");
                if (null != liStrs && liStrs.length > 1) {
                    day = Integer.parseInt(liStrs[0]);
                    half = Integer.parseInt(liStrs[1]);
                    if (null != day && day > 0) {
                        endTime = DateUtil.addDay(endTime, day);
                    }
                    if (null != half && half > 0) {
                        endTime = DateUtil.addHour(endTime, 12);// 半天以12小时为标准
                    }
                    if (now.after(endTime)) {// 当前日期位于逾期截止日期之后，说明逾期了
                        overdue = DateUtil.autoOverdue(endTime, now);
                        if (StringUtil.isNotEmpty(overdue)) {
                            return -Float.parseFloat(overdue);
                        }
                    } else {// 未逾期，计算倒计时
                        // 如果当前时间跟任务开始日期为同一天，则同理设置当前日期临界时间点
                        if (DateUtil.sameDate(now, createTime)) {
                            if (ConstantUtil.AM_CNAME.equals(DateUtil.getCnameOfAMPM(now))) {
                                now = DateUtil.setTimeToPM(now);
                            } else {
                                now = DateUtil.setTimeToMax(now);
                            }
                        }
                        overdue = DateUtil.autoOverdue(now, endTime);
                        if (StringUtil.isNotEmpty(overdue)) {
                            return Float.parseFloat(overdue);
                        }
                    }
                }
            }
        }
        return null;
    }

    public static Object getNewFormBusinessDateById(String serviceName, Long businessKey) {
        if (null != businessKey && StringUtil.isNotEmpty(serviceName)) {
            if (serviceName.equals(ConstantUtil.FINANCIAL_CUSTOMER_COMMIT)) {
                serviceName = ConstantUtil.FINANCIAL_CUSTOMER_PROCESS.substring(0, 1).toLowerCase()
                        + ConstantUtil.FINANCIAL_CUSTOMER_PROCESS.substring(1);
            }else if (serviceName.equals(ConstantUtil.FINANCIAL_CUSTOMER_APPROVAL_PROCESS)){
                serviceName = ConstantUtil.FINANCIAL_CUSTOMER_APPROVAL.substring(0, 1).toLowerCase()
                        + ConstantUtil.FINANCIAL_CUSTOMER_APPROVAL.substring(1);
            }
            Object sevice = SpringUtil.getBeanByName(serviceName + "Service");
            try {
                Method method = sevice.getClass().getMethod("getViewNew", Long.class);
                return method.invoke(sevice, new Object[]{
                        businessKey
                });
            } catch (Exception e) {
                e.printStackTrace();
                throw new BusinessException(CodeUtil.FAIL, "获取表单业务数据出错啦！" + e.getMessage());
            }
        }
        return null;
    }
}
