private String preAccomplishTime(DB db,String orgid,Double dealTimeTotal,String orgId) throws Exception{
// 开始日期
String sysdate = SysTime.getSysDate();
// 开始时间
String systime = SysTime.getSysTime();
// 预定结束时间
String preendtime = null;
// 取工作时间
String statussql = "select status from "
+ "(select ce.companycode,year || '-' || month || '-' || day || ' ' || trim(amworktime) || ':00' amstarttime,"
+ "year || '-' || month || '-' || day || ' ' || amendtime || ':00' amendtime,"
+ "year || '-' || month || '-' || day || ' ' || pmworktime || ':00' pmstarttime,"
+ "year || '-' || month || '-' || day || ' ' || pmendtime || ':00' pmendtime,status"
+ " from CMN_WORKCALENDAR_TB cr,CMN_WORKTIME_TB ce"
+ " where cr.companycode = ce.companycode and cr.companycode = '" + orgid + "' and cr.deleteflag = '0'"
+ " and ce.deleteflag = '0') where substr(amstarttime,0,10) = '" + sysdate + "'";
System.out.println("statussql:" + statussql);
MapList statusMapList = db.query(statussql);
Date starttime = null;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if("1".equals(statusMapList.getRow(0).get("status"))){
String workday = isworkday(db,systime,orgId);
sysdate = workday.substring(0,10);
starttime = df.parse(sysdate + " 00:00:01");
}
String datetimesql = "select amstarttime,amendtime,pmstarttime,pmendtime from "
+ "(select ce.companycode,year || '-' || month || '-' || day || ' ' || trim(amworktime) || ':00' amstarttime,"
+ "year || '-' || month || '-' || day || ' ' || amendtime || ':00' amendtime,"
+ "year || '-' || month || '-' || day || ' ' || pmworktime || ':00' pmstarttime,"
+ "year || '-' || month || '-' || day || ' ' || pmendtime || ':00' pmendtime"
+ " from CMN_WORKCALENDAR_TB cr,CMN_WORKTIME_TB ce"
+ " where cr.companycode = ce.companycode and cr.companycode = '" + orgid + "' and cr.deleteflag = '0'"
+ " and ce.deleteflag = '0') where substr(amstarttime,0,10) = '" + sysdate + "'";
MapList dayMapList = db.query(datetimesql);
String amstarttime = dayMapList.getRow(0).get("amstarttime");
String amendtime = dayMapList.getRow(0).get("amendtime");
String pmstarttime = dayMapList.getRow(0).get("pmstarttime");
String pmendtime = dayMapList.getRow(0).get("pmendtime");
// 上午开始时间
Date amsdate = df.parse(amstarttime);
// 上午结束时间
Date amedate = df.parse(amendtime);
// 下午开始时间
Date pmsdate = df.parse(pmstarttime);
// 下午结束时间
Date pmedate = df.parse(pmendtime);
// 开始时间
starttime = df.parse(systime);
// 上午工作时间
long stime = (amedate.getTime() - amsdate.getTime())/(1000*60*60);
// 下午工作时间
long etime = (pmedate.getTime() - pmsdate.getTime())/(1000*60*60);
// 中午休息时间
long mtime = (pmsdate.getTime() - amedate.getTime())/(1000*60*60);
// 全天工作时间
long qtime = stime + etime;
// 判断几天
Double daystime = Double.valueOf(new BigDecimal(dealTimeTotal / qtime).setScale(1, BigDecimal.ROUND_DOWN).toString());
// 天数
int days = (int)(daystime/1);
// 小时数
double hours = dealTimeTotal - days*qtime;
// 开始时间早于等于上午上班时间
if(starttime.getTime() <= amsdate.getTime()){
// 小于上午工作时间
if(hours <= stime){
preendtime = getNextDate(amsdate,workday(db,sysdate,orgid,days),hours);
}else{
preendtime = getNextDate(amsdate,workday(db,sysdate,orgid,days),hours + mtime);
}
// 大于上午上班时间,小于等于上午下班时间
}else if(starttime.getTime()-amsdate.getTime() > 0 && starttime.getTime() - amedate.getTime()<= 0){
// 处于当天的上午
if((amedate.getTime() - starttime.getTime())/(1000.00*60*60) >= hours){
if(days == 0){
preendtime = getNextDate(starttime,workday(db,sysdate,orgid,days),hours);
}else{
preendtime = getNextDate(amsdate,workday(db,sysdate,orgid,days),hours);
}
// 处在当天的下午
}else if(((pmedate.getTime() - starttime.getTime())/(1000*60*60)) - 1 >= hours ){
if(days == 0){
preendtime = getNextDate(starttime,workday(db,sysdate,orgid,days),hours + mtime);
}else{
preendtime = getNextDate(amsdate,workday(db,sysdate,orgid,days),hours + mtime);
}
}else{
// 除去今天工作时间,还剩工时
double sstime = hours - 1 - (pmedate.getTime() - starttime.getTime())/(1000.00*60*60);
// 处在下一天上午
if((amedate.getTime() - amsdate.getTime())/(1000.00*60*60) >= sstime){
preendtime = getNextDate(amsdate,workday(db,amstarttime,orgid,days) + 1,sstime);
}else{
// 处在下一天下午
preendtime = getNextDate(amsdate,workday(db,amstarttime,orgid,days) + 1,sstime + mtime);
}
}
// 大于上午下班时间,小于等于下午上班时间
}else if(starttime.getTime() - amedate.getTime() > 0 && starttime.getTime() - pmsdate.getTime() <= 0){
// 处在当天
if((pmedate.getTime() - pmsdate.getTime())/(1000*60*60) > hours){
preendtime = getNextDate(pmsdate,workday(db,sysdate,orgid,days),hours);
}else{
// 除去今天工作时间,还剩工时
double sstime = hours - (pmedate.getTime() - pmsdate.getTime())/(1000.00*60*60);
// 处在下一天上午
if((amedate.getTime() - amsdate.getTime())/(1000.00*60*60) > sstime){
preendtime = getNextDate(amsdate,workday(db,amstarttime,orgid,days) + 1,sstime);
}else{
// 处在下一天下午
preendtime = getNextDate(amsdate,workday(db,amstarttime,orgid,days) + 1,sstime + mtime);
}
}
// 大于下午上班时间,小于等于下午下班时间
}else if(starttime.getTime() - pmsdate.getTime() > 0 && starttime.getTime() - pmedate.getTime() <= 0){
// 处在当天
if((pmedate.getTime() - starttime.getTime())/(1000*60*60) >= dealTimeTotal){
if(days == 0){
preendtime = getNextDate(starttime,workday(db,sysdate,orgid,days),hours);
}else{
preendtime = getNextDate(amsdate,workday(db,sysdate,orgid,days),hours);
}
}else{
// 除去今天工作时间,还剩工时
double sstime = hours - (pmedate.getTime() - starttime.getTime())/(1000.00*60*60);
// 处在下一天上午
if((amedate.getTime() - amsdate.getTime())/(1000.00*60*60) >= sstime){
preendtime = getNextDate(amsdate,workday(db,sysdate,orgid,days) + 1,sstime);
}else{
// 处在下一天下午
preendtime = getNextDate(amsdate,workday(db,sysdate,orgid,days) + 1,sstime + mtime);
}
}
}else {
// 小于等于上午工作时间
if(hours <= stime){
preendtime = getNextDate(amsdate,workday(db,sysdate,orgid,days) + 1,hours);
}else{
preendtime = getNextDate(amsdate,workday(db,sysdate,orgid,days) + 1,hours);
}
}
preendtime = isworkday(db,preendtime,orgId);
return preendtime;
}
// 得到预定完成时间
private String getNextDate(Date startdate,int days,double hours){
java.text.Format formatter=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long afterTime=(long) ((startdate.getTime()/1000)+60*60*24*days + 60*60*hours);
startdate.setTime(afterTime*1000);
String nextdate=formatter.format(startdate);
System.out.println(nextdate);
return nextdate;
}
// 判断是否是工作日
private String isworkday(DB db,String datetime,String orgid) throws Exception{
String workdaysql = "select status from CMN_WORKCALENDAR_TB"
+" where year = substr('" + datetime + "',0,4) and month = trim(substr('" + datetime + "',6,2))"
+" and day = trim(substr( '" + datetime + "',9,2)) and companycode = '" + orgid + "'";
MapList mapList = db.query(workdaysql);
String status = mapList.getRow(0).get("status");
if("0".equals(status)){
return datetime;
}else{
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return isworkday(db,getNextDate(formatter.parse(datetime),1,0),orgid);
}
}
// 判断n天里有多少休息日
private int workday(DB db,String sysdate,String orgid,int days) throws Exception{
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String nextday = getNextDate(formatter.parse(sysdate),days,0);
String finalday = finalday(db,orgid,sysdate,nextday,days);
return (int) ((formatter.parse(finalday).getTime() - formatter.parse(sysdate).getTime())/(1000*60*60*24));
}
// 得出最终的日期
private String finalday(DB db,String orgid,String sysdate,String nextday,int days) throws Exception{
nextday = nextday.substring(0,10);
sysdate = sysdate.substring(0,10);
String workdaysql = "select count(status) workdays from("
+ "select to_date(year || '-' || month || '-' || day,'yyyy-mm-dd') workday,status from CMN_WORKCALENDAR_TB"
+ " where companycode='" + orgid + "')"
+ " where workday >to_date('" + sysdate + "','yyyy-mm-dd') and workday <=to_date('" + nextday + "','yyyy-mm-dd')"
+ " and status='0'";
MapList mapList = db.query(workdaysql);
int workdays = mapList.getRow(0).getInt("workdays", -1000);
if(workdays != days){
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
nextday = getNextDate(formatter.parse(nextday),days - workdays,0);
return finalday(db,orgid,sysdate,nextday,days);
}
return nextday;
}