org.foxbpm.calendar.mybatis.cmd.GetWorkCalendarEndTimeCmd.java Source code

Java tutorial

Introduction

Here is the source code for org.foxbpm.calendar.mybatis.cmd.GetWorkCalendarEndTimeCmd.java

Source

/**
 * Copyright 1996-2014 FoxBPM ORG.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @author demornain
 */
package org.foxbpm.calendar.mybatis.cmd;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.time.DateUtils;
import org.foxbpm.calendar.mybatis.entity.CalendarPartEntity;
import org.foxbpm.calendar.mybatis.entity.CalendarRuleEntity;
import org.foxbpm.calendar.mybatis.entity.CalendarTypeEntity;
import org.foxbpm.calendar.service.WorkCalendarService;
import org.foxbpm.calendar.utils.DateCalUtils;
import org.foxbpm.engine.ProcessEngineManagement;
import org.foxbpm.engine.exception.FoxBPMIllegalArgumentException;
import org.foxbpm.engine.impl.interceptor.Command;
import org.foxbpm.engine.impl.interceptor.CommandContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GetWorkCalendarEndTimeCmd implements Command<Date> {

    private static Logger log = LoggerFactory.getLogger(GetWorkCalendarEndTimeCmd.class);
    //?
    public static final int WORKSTATUS = 0;
    //??
    public static final int FREESTATUS = 1;
    public static final long HOURTIME = 1000L * 60 * 60;
    private Date begin;
    private double hours;
    private WorkCalendarService workCalendarService;
    private SimpleDateFormat timeFormat;
    private int year = 0;
    private int month = 0;
    private int day = 0;
    private CalendarTypeEntity calendarTypeEntity;
    private boolean isAddDay = false;
    private String ruleId;

    public GetWorkCalendarEndTimeCmd(Date begin, double hours, String ruleId) {
        this.begin = begin;
        this.hours = hours;
        this.workCalendarService = ProcessEngineManagement.getDefaultProcessEngine()
                .getService(WorkCalendarService.class);
        this.timeFormat = new SimpleDateFormat("hh:mm");
        this.ruleId = ruleId;
    }

    public Date execute(CommandContext commandContext) {
        //?
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(begin);
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH) + 1;
        day = calendar.get(Calendar.DATE);

        //
        calendarTypeEntity = getCalendarTypeById(ruleId);

        //??
        initCalendarType(calendarTypeEntity);

        CalendarRuleEntity calendarRuleEntity = null;
        //
        for (int k = 0; k < calendarTypeEntity.getCalendarRuleEntities().size(); k++) {
            CalendarRuleEntity calRuleEntity = calendarTypeEntity.getCalendarRuleEntities().get(k);
            //??
            if (calRuleEntity.getStatus() == FREESTATUS && calRuleEntity.getWorkdate() != null
                    && calRuleEntity.getYear() == year && DateUtils.isSameDay(calRuleEntity.getWorkdate(), begin)) {
                //
                if (calRuleEntity.getCalendarPartEntities().size() == 0) {
                    begin = DateUtils.addDays(begin, 1);
                    calendar.setTime(begin);
                    calendar.set(Calendar.HOUR, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    begin = calendar.getTime();
                    year = calendar.get(Calendar.YEAR);
                    month = calendar.get(Calendar.MONTH) + 1;
                    day = calendar.get(Calendar.DATE);
                    calendar.get(Calendar.HOUR);
                    calendar.get(Calendar.MINUTE);
                    calendar.get(Calendar.SECOND);
                }
                // ? ??
                else {
                    calendarRuleEntity = getCalendarRuleEntityWithHoliday(calRuleEntity);
                }
            }
            //?
            if (calRuleEntity.getWeek() != 0 && calRuleEntity.getYear() == year
                    && calRuleEntity.getWeek() == DateCalUtils.dayForWeek(begin)) {
                calendarRuleEntity = calRuleEntity;
            }
            //?
            if (calendarRuleEntity == null) {
                continue;
            }
            //
            else {
                Calendar endCalendar = Calendar.getInstance();
                Date endDate = CalculateEndTime(calendarRuleEntity);
                endCalendar.setTime(endDate);
                log.debug("" + endCalendar.getTime());
                return endDate;
            }
        }

        log.debug("?");
        return null;
    }

    /**
     * ??
     * @param calRuleEntity
     * @return
     */
    private CalendarRuleEntity getCalendarRuleEntityWithHoliday(CalendarRuleEntity calRuleEntity) {
        List<CalendarPartEntity> rightPartEntities = new ArrayList<CalendarPartEntity>();
        if (calRuleEntity.getYear() == year
                && DateCalUtils.dayForWeek(calRuleEntity.getWorkdate()) == DateCalUtils.dayForWeek(begin)) {
            for (CalendarRuleEntity workRuleEntity : calendarTypeEntity.getCalendarRuleEntities()) {
                //?
                if (workRuleEntity.getWeek() == DateCalUtils.dayForWeek(calRuleEntity.getWorkdate())) {
                    //
                    List<CalendarPartEntity> workPartEntities = workRuleEntity.getCalendarPartEntities();
                    //?
                    List<CalendarPartEntity> freePartEntities = calRuleEntity.getCalendarPartEntities();

                    int size = workPartEntities.size() < freePartEntities.size() ? workPartEntities.size()
                            : freePartEntities.size();

                    for (int i = 0; i < size; i++) {
                        String workStart = workPartEntities.get(i).getStarttime();
                        String workEnd = workPartEntities.get(i).getEndtime();

                        String freeStart = freePartEntities.get(i).getStarttime();
                        String freeEnd = freePartEntities.get(i).getEndtime();

                        long workStartDate = getCalculateTime(workStart, workPartEntities.get(i).getAmorpm());
                        long workEndDate = getCalculateTime(workEnd, workPartEntities.get(i).getAmorpm());

                        long freeStartDate = getCalculateTime(freeStart, workPartEntities.get(i).getAmorpm());
                        long freeEndDate = getCalculateTime(freeEnd, workPartEntities.get(i).getAmorpm());

                        //???
                        if (workPartEntities.get(i).getAmorpm() == workPartEntities.get(i).getAmorpm()) {
                            calculateWorkTimeWithHoliday(workStartDate, workEndDate, freeStartDate, freeEndDate,
                                    workPartEntities.get(i), i, rightPartEntities);
                        }
                    }
                }
            }
        }
        for (CalendarPartEntity calendarPartEntity : rightPartEntities) {
            log.debug("??:" + calendarPartEntity.getStarttime() + "---"
                    + calendarPartEntity.getEndtime());
        }
        calRuleEntity.setCalendarPartEntities(rightPartEntities);
        return calRuleEntity;
    }

    /**
     * ?
     * @param calendarRuleEntity
     * @return
     */
    private Date CalculateEndTime(CalendarRuleEntity calendarRuleEntity) {
        //?
        if (getCalendarRuleByDate(begin) == null) {
            day += 1;
            begin = DateUtils.addDays(begin, 1);
            CalculateEndTime(calendarRuleEntity);
        }

        Date endDate = null;

        for (CalendarRuleEntity caRuleEntity : calendarTypeEntity.getCalendarRuleEntities()) {
            //??
            if (caRuleEntity.getStatus() == FREESTATUS && caRuleEntity.getWorkdate() != null
                    && caRuleEntity.getYear() == year && DateUtils.isSameDay(caRuleEntity.getWorkdate(), begin)) {
                //
                if (caRuleEntity.getCalendarPartEntities().size() == 0) {
                    begin = DateUtils.addDays(begin, 1);
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(begin);
                    calendar.set(Calendar.HOUR, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    begin = calendar.getTime();
                    year = calendar.get(Calendar.YEAR);
                    month = calendar.get(Calendar.MONTH) + 1;
                    day = calendar.get(Calendar.DATE);
                    calendar.get(Calendar.HOUR);
                    calendar.get(Calendar.MINUTE);
                    calendar.get(Calendar.SECOND);
                }
                // ? ??
                else {
                    calendarRuleEntity = getCalendarRuleEntityWithHoliday(caRuleEntity);
                }
            }
        }

        //?
        if (calendarRuleEntity.getStatus() == FREESTATUS
                && calendarRuleEntity.getCalendarPartEntities().size() == 0) {
            day += 1;
            calendarRuleEntity = getCalendarRuleByDate(DateUtils.addDays(begin, 1));
            Calendar calendar = Calendar.getInstance();

            if (calendarRuleEntity.getCalendarPartEntities().size() > 0) {
                calendar.setTimeInMillis(
                        getCalculateTime(calendarRuleEntity.getCalendarPartEntities().get(0).getStarttime(),
                                calendarRuleEntity.getCalendarPartEntities().get(0).getAmorpm()));
                begin = calendar.getTime();
            }
            endDate = CalculateEndTime(calendarRuleEntity);
        }
        for (int j = 0; j < calendarRuleEntity.getCalendarPartEntities().size(); j++) {
            CalendarPartEntity calendarPartEntity = calendarRuleEntity.getCalendarPartEntities().get(j);
            //??? ????  //TODO ?
            if (calendarPartEntity.getAmorpm() == DateCalUtils.dayForAMorPM(begin)) {
                Calendar formatCalendar = Calendar.getInstance();

                //
                long startTime = getCalculateTime(calendarPartEntity.getStarttime(),
                        calendarPartEntity.getAmorpm());
                formatCalendar.setTimeInMillis(startTime);
                log.debug("" + formatCalendar.getTime());

                //?
                long endTime = getCalculateTime(calendarPartEntity.getEndtime(), calendarPartEntity.getAmorpm());
                formatCalendar.setTimeInMillis(endTime);
                log.debug("?" + formatCalendar.getTime());

                //?
                long beginTime = begin.getTime();
                formatCalendar.setTime(begin);
                log.debug("?" + formatCalendar.getTime());

                //??
                long beginEndTime = (long) (hours * HOURTIME + beginTime);
                formatCalendar.setTimeInMillis(beginEndTime);
                log.debug("?" + formatCalendar.getTime());

                log.debug("" + hours + "?");

                endDate = CalculateTime(startTime, endTime, beginTime, beginEndTime, calendarRuleEntity, j);
                if (endDate == null) {
                    log.debug("");
                    break;
                }

                return endDate;
            }
        }
        return endDate;
    }

    /**
     *  
     * @param startTime
     * @param endTime
     * @param beginTime
     * @param beginEndTime
     * @param calendarRuleEntity
     * @param i
     * @return
     */
    private Date CalculateTime(long startTime, long endTime, long beginTime, long beginEndTime,
            CalendarRuleEntity calendarRuleEntity, int i) {
        Calendar endCalendar = Calendar.getInstance();
        //1??
        if ((beginTime < startTime && beginEndTime < startTime)) {
            //
            CalendarPartEntity calendarPartEntity = calendarRuleEntity.getCalendarPartEntities().get(i);
            startTime = getCalculateTime(calendarPartEntity.getStarttime(), calendarPartEntity.getAmorpm());
            endTime = getCalculateTime(calendarPartEntity.getEndtime(), calendarPartEntity.getAmorpm());

            beginTime = startTime;
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(beginTime);
            begin = calendar.getTime();
            beginEndTime = (long) (beginTime + hours * HOURTIME);

            //?
            return CalculateTime(startTime, endTime, beginTime, beginEndTime, calendarRuleEntity, i);
        }
        //??
        else if ((beginTime > endTime && beginEndTime > endTime)) {
            //
            if (i + 1 < calendarRuleEntity.getCalendarPartEntities().size()) {
                CalendarPartEntity calendarPartEntity = calendarRuleEntity.getCalendarPartEntities().get(i + 1);
                startTime = getCalculateTime(calendarPartEntity.getStarttime(), calendarPartEntity.getAmorpm());
                endTime = getCalculateTime(calendarPartEntity.getEndtime(), calendarPartEntity.getAmorpm());

                beginTime = startTime;
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(beginTime);
                begin = calendar.getTime();
                beginEndTime = (long) (beginTime + hours * HOURTIME);

                //?
                return CalculateTime(startTime, endTime, beginTime, beginEndTime, calendarRuleEntity, i + 1);
            }
            //??
            if (isAddDay) {
                CalendarPartEntity calendarPartEntity = calendarRuleEntity.getCalendarPartEntities().get(0);
                startTime = getCalculateTime(calendarPartEntity.getStarttime(), calendarPartEntity.getAmorpm());
                endTime = getCalculateTime(calendarPartEntity.getEndtime(), calendarPartEntity.getAmorpm());
                beginTime = startTime;
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(beginTime);
                begin = calendar.getTime();
                beginEndTime = (long) (beginTime + hours * HOURTIME);

                //?
                isAddDay = false;
                return CalculateTime(startTime, endTime, beginTime, beginEndTime, calendarRuleEntity, i);
            }

            // ??
            else {
                CalendarRuleEntity calendarRuleEntity2 = getCalendarRuleByDate(DateUtils.addDays(begin, 1));
                if (calendarRuleEntity2.getStatus() == FREESTATUS) {
                    day += 1;
                } else {
                    day += 1;
                    isAddDay = true;
                    startTime = getCalculateTime(
                            calendarRuleEntity2.getCalendarPartEntities().get(0).getStarttime(),
                            calendarRuleEntity2.getCalendarPartEntities().get(0).getAmorpm());
                    endTime = getCalculateTime(calendarRuleEntity2.getCalendarPartEntities().get(0).getEndtime(),
                            calendarRuleEntity2.getCalendarPartEntities().get(0).getAmorpm());
                    beginTime = startTime;
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTimeInMillis(beginTime);
                    begin = calendar.getTime();
                    beginEndTime = (long) (beginTime + hours * HOURTIME);
                }
                return CalculateEndTime(calendarRuleEntity2);
            }
        }

        //2? ?
        else if (beginTime < startTime && beginEndTime > startTime && beginEndTime <= endTime) {
            //
            //?
            if ((endTime - startTime) - hours * HOURTIME > 0) {
                endCalendar.setTimeInMillis((long) (startTime + hours * HOURTIME));
                return endCalendar.getTime();
            }
            //???
            else {
                if (i + 1 < calendarRuleEntity.getCalendarPartEntities().size()) {
                    CalendarPartEntity calendarPartEntity = calendarRuleEntity.getCalendarPartEntities().get(i + 1);
                    beginEndTime = (long) (hours * HOURTIME - (endTime - startTime));
                    hours = hours - ((double) (endTime - startTime) / (HOURTIME));
                    startTime = getCalculateTime(calendarPartEntity.getStarttime(), calendarPartEntity.getAmorpm());
                    endTime = getCalculateTime(calendarPartEntity.getEndtime(), calendarPartEntity.getAmorpm());
                    beginTime = startTime;
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTimeInMillis(beginTime);
                    begin = calendar.getTime();
                    beginEndTime += startTime;

                    //?
                    return CalculateTime(startTime, endTime, beginTime, beginEndTime, calendarRuleEntity, i + 1);
                }
            }
        }

        //3?
        else if (startTime <= beginTime && beginEndTime > startTime && beginEndTime <= endTime) {
            //
            endCalendar.setTimeInMillis(beginEndTime);
        }

        //4??
        else if (startTime <= beginTime && beginEndTime > endTime) {
            CalendarPartEntity calendarPartEntity = null;
            if (i + 1 < calendarRuleEntity.getCalendarPartEntities().size()) {
                calendarPartEntity = calendarRuleEntity.getCalendarPartEntities().get(i + 1);
                // ???
                if (startTime == beginTime) {
                    beginEndTime = (long) (hours * HOURTIME - (endTime - startTime));
                    hours = hours - ((double) (endTime - startTime) / (HOURTIME));
                }
                //????
                else {
                    beginEndTime = (long) (hours * HOURTIME - (endTime - beginTime));
                    hours = hours - ((double) (endTime - beginTime) / (HOURTIME));
                }

                startTime = getCalculateTime(calendarPartEntity.getStarttime(), calendarPartEntity.getAmorpm());
                endTime = getCalculateTime(calendarPartEntity.getEndtime(), calendarPartEntity.getAmorpm());
                beginTime = startTime;
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(beginTime);
                begin = calendar.getTime();
                beginEndTime += startTime;

                //?
                return CalculateTime(startTime, endTime, beginTime, beginEndTime, calendarRuleEntity, i + 1);
            }

            //??
            else {
                CalendarRuleEntity calendarRuleEntity2 = getCalendarRuleByDate(DateUtils.addDays(begin, 1));
                if (calendarRuleEntity2.getStatus() == FREESTATUS || isHoliday(begin)) {
                    day += 1;
                    // ???
                    if (startTime == beginTime) {
                        beginEndTime = (long) (hours * HOURTIME - (endTime - startTime));
                        hours = hours - ((double) (endTime - startTime) / (HOURTIME));
                    }
                    //????
                    else {
                        beginEndTime = (long) (hours * HOURTIME - (endTime - beginTime));
                        hours = hours - ((double) (endTime - beginTime) / (HOURTIME));
                    }
                } else {
                    day += 1;
                    // ???
                    if (startTime == beginTime) {
                        beginEndTime = (long) (hours * HOURTIME - (endTime - startTime));
                        hours = hours - ((double) (endTime - startTime) / (HOURTIME));
                    }
                    //????
                    else {
                        beginEndTime = (long) (hours * HOURTIME - (endTime - beginTime));
                        hours = hours - ((double) (endTime - beginTime) / (HOURTIME));
                    }
                    startTime = getCalculateTime(
                            calendarRuleEntity2.getCalendarPartEntities().get(0).getStarttime(),
                            calendarRuleEntity2.getCalendarPartEntities().get(0).getAmorpm());
                    endTime = getCalculateTime(calendarRuleEntity2.getCalendarPartEntities().get(0).getEndtime(),
                            calendarRuleEntity2.getCalendarPartEntities().get(0).getAmorpm());
                    beginTime = startTime;
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTimeInMillis(beginTime);
                    begin = calendar.getTime();
                    beginEndTime += startTime;
                }

                return CalculateEndTime(calendarRuleEntity2);
            }
        }

        //5?
        else if (beginTime < startTime && beginEndTime > endTime) {
            //
            CalendarPartEntity calendarPartEntity = calendarRuleEntity.getCalendarPartEntities().get(i);
            startTime = getCalculateTime(calendarPartEntity.getStarttime(), calendarPartEntity.getAmorpm());
            endTime = getCalculateTime(calendarPartEntity.getEndtime(), calendarPartEntity.getAmorpm());
            beginTime = startTime;
            beginEndTime = (long) (beginTime + (hours * HOURTIME));
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(beginTime);
            begin = calendar.getTime();

            //?
            return CalculateTime(startTime, endTime, beginTime, beginEndTime, calendarRuleEntity, i);
        }

        return endCalendar.getTime();
    }

    /**
     * ???
     * @param begin2
     * @return
     */
    private boolean isHoliday(Date begin2) {
        boolean isHoliday = false;
        for (CalendarRuleEntity calendarRuleEntity : calendarTypeEntity.getCalendarRuleEntities()) {
            if (calendarRuleEntity.getStatus() == FREESTATUS && calendarRuleEntity.getWorkdate() != null
                    && calendarRuleEntity.getYear() == year
                    && DateUtils.isSameDay(calendarRuleEntity.getWorkdate(), begin)
                    && calendarRuleEntity.getCalendarPartEntities().size() == 0) {
                isHoliday = true;
            }
        }
        return isHoliday;
    }

    /**
     * ?
     * @param calendarTypeEntity
     */
    private void initCalendarType(CalendarTypeEntity calendarTypeEntity) {
        //ID?
        List<CalendarRuleEntity> calendarRuleEntities = workCalendarService
                .getCalendarRulesByTypeId(calendarTypeEntity.getId());

        for (CalendarRuleEntity calendarRuleEntity : calendarRuleEntities) {
            //ID?
            List<CalendarPartEntity> calendarPartEntities = workCalendarService
                    .getCalendarPartsByRuleId(calendarRuleEntity.getId());

            //?
            Collections.sort(calendarPartEntities, new Comparator<CalendarPartEntity>() {

                public int compare(CalendarPartEntity o1, CalendarPartEntity o2) {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm");
                    long o1s = 0;
                    long o2s = 0;
                    Calendar calendar = Calendar.getInstance();
                    Date o1d = null;
                    try {
                        o1d = simpleDateFormat.parse(o1.getStarttime());
                    } catch (ParseException e) {
                        throw new FoxBPMIllegalArgumentException(
                                "???HH:mm,?" + o1.getStarttime());
                    }
                    calendar.setTime(o1d);
                    //"120??"
                    if (o1.getStarttime().equals("12:00")) {
                        calendar.set(Calendar.AM_PM, 1);
                    } else if (o1.getStarttime().equals("00:00")) {
                        calendar.set(Calendar.AM_PM, 0);
                    } else {
                        calendar.set(Calendar.AM_PM, o1.getAmorpm());
                    }

                    o1s = calendar.getTimeInMillis();

                    Date o2d = null;
                    try {
                        o2d = simpleDateFormat.parse(o2.getStarttime());
                    } catch (ParseException e) {
                        throw new FoxBPMIllegalArgumentException(
                                "???HH:mm,?" + o2.getStarttime());
                    }
                    calendar.setTime(o2d);
                    //"120??"
                    if (o2.getStarttime().equals("12:00")) {
                        calendar.set(Calendar.AM_PM, 1);
                    } else if (o2.getStarttime().equals("00:00")) {
                        calendar.set(Calendar.AM_PM, 0);
                    } else {
                        calendar.set(Calendar.AM_PM, o2.getAmorpm());
                    }

                    o2s = calendar.getTimeInMillis();
                    calendar.getTime();

                    return o1s < o2s == true ? 0 : 1;
                }
            });

            calendarRuleEntity.setCalendarPartEntities(calendarPartEntities);
        }

        calendarTypeEntity.setCalendarRuleEntities(calendarRuleEntities);
    }

    /**
     * ?userId
     * @param userId2
     * @return
     */
    private CalendarTypeEntity getCalendarTypeById(String userId2) {
        return workCalendarService.getCalendarTypeById(userId2);
    }

    /**
     * ?
     * @param date
     * @param amorpm
     * @return
     * @throws ParseException
     */
    private long getCalculateTime(String date, int amorpm) {
        Date startDate = null;
        try {
            startDate = timeFormat.parse(date);
        } catch (ParseException ex) {
            throw new FoxBPMIllegalArgumentException(
                    "???HH:mm,?" + date);
        }

        Calendar formatCalendar = Calendar.getInstance();
        formatCalendar.setTime(startDate);
        formatCalendar.set(Calendar.YEAR, year);
        formatCalendar.set(Calendar.MONTH, month - 1);
        formatCalendar.set(Calendar.DATE, day);
        //"120??"
        if (date.equals("12:00")) {
            formatCalendar.set(Calendar.AM_PM, 1);
        } else if (date.equals("00:00")) {
            formatCalendar.set(Calendar.AM_PM, 0);
        } else {
            formatCalendar.set(Calendar.AM_PM, amorpm);
        }

        //
        long startTime = formatCalendar.getTimeInMillis();

        return startTime;
    }

    private void calculateWorkTimeWithHoliday(long workStartDate, long workEndDate, long freeStartDate,
            long freeEndDate, CalendarPartEntity workPart, int i, List<CalendarPartEntity> rightPartEntities) {
        Calendar calendar = Calendar.getInstance();

        //1???(?)
        if (freeStartDate <= workStartDate && freeEndDate <= workStartDate) {
            return;
        }
        //2?
        else if (freeStartDate >= workStartDate && freeEndDate <= workEndDate) {
            //
            if (freeStartDate == workStartDate && freeEndDate == workEndDate) {
                return;
            } else {
                CalendarPartEntity calendarPartEntity = new CalendarPartEntity(
                        java.util.UUID.randomUUID().toString());
                calendarPartEntity.setAmorpm(workPart.getAmorpm());
                calendarPartEntity.setRuleid(workPart.getRuleid());
                calendar.setTimeInMillis(workStartDate);
                calendarPartEntity.setStarttime(timeFormat.format(calendar.getTime()));
                calendar.setTimeInMillis(freeStartDate);
                calendarPartEntity.setEndtime(timeFormat.format(calendar.getTime()));
                rightPartEntities.add(calendarPartEntity);

                CalendarPartEntity calendarPartEntitynew = new CalendarPartEntity(
                        java.util.UUID.randomUUID().toString());
                calendarPartEntitynew.setAmorpm(workPart.getAmorpm());
                calendarPartEntitynew.setRuleid(workPart.getRuleid());
                calendar.setTimeInMillis(freeEndDate);
                calendarPartEntitynew.setStarttime(timeFormat.format(calendar.getTime()));
                calendar.setTimeInMillis(workEndDate);
                calendarPartEntitynew.setEndtime(timeFormat.format(calendar.getTime()));
                rightPartEntities.add(calendarPartEntitynew);
            }
        }
        //3?
        else if (freeStartDate < workStartDate && freeEndDate <= workEndDate) {
            CalendarPartEntity calendarPartEntity = new CalendarPartEntity(java.util.UUID.randomUUID().toString());
            calendarPartEntity.setAmorpm(workPart.getAmorpm());
            calendarPartEntity.setRuleid(workPart.getRuleid());
            calendarPartEntity.setStarttime(timeFormat.format(workStartDate));
            calendarPartEntity.setEndtime(timeFormat.format(freeEndDate));
            rightPartEntities.add(calendarPartEntity);

        }
        //4??
        else if (freeStartDate >= workStartDate && freeEndDate > workEndDate) {
            CalendarPartEntity calendarPartEntity = new CalendarPartEntity(java.util.UUID.randomUUID().toString());
            calendarPartEntity.setAmorpm(workPart.getAmorpm());
            calendarPartEntity.setRuleid(workPart.getRuleid());
            calendarPartEntity.setStarttime(timeFormat.format(freeStartDate));
            calendarPartEntity.setEndtime(timeFormat.format(workEndDate));
        }
    }

    /**
     * ?
     * @param date
     * @return
     */
    private CalendarRuleEntity getCalendarRuleByDate(Date date) {
        CalendarRuleEntity calendarRuleEntity = null;
        for (CalendarRuleEntity calendarRuleEntity2 : calendarTypeEntity.getCalendarRuleEntities()) {
            if (calendarRuleEntity2.getWeek() == DateCalUtils.dayForWeek(date)) {
                calendarRuleEntity = calendarRuleEntity2;
            }
            if (calendarRuleEntity2.getWorkdate() != null
                    && DateUtils.isSameDay(calendarRuleEntity2.getWorkdate(), date)
                    && calendarRuleEntity2.getCalendarPartEntities().size() != 0) {
                calendarRuleEntity = calendarRuleEntity2;
            }
        }
        //?
        if (calendarRuleEntity == null) {
            date = DateUtils.addDays(date, 1);
            return getCalendarRuleByDate(date);
        }
        return calendarRuleEntity;
    }

    //   /**
    //    * ?
    //    * @param currentRule
    //    * @return
    //    */
    //   private CalendarRuleEntity getNextCalendarRule(CalendarRuleEntity currentRule) {
    //      CalendarRuleEntity calendarRule = null;
    //      for (CalendarRuleEntity calendarRuleEntity : calendarTypeEntity.getCalendarRuleEntities()) {
    //         //1???
    //         if(calendarRuleEntity.getWeek()==(currentRule.getWeek()+1)%7) {
    //            calendarRule = calendarRuleEntity;
    //         }
    //         //?????
    //         else if(currentRule.getWeek()==0 || currentRule.getStatus()==FREESTATUS || currentRule.getCalendarPartEntities().size()==0) {
    //            
    //         }
    //      }
    //      
    //      return calendarRule;
    //   }
}