计算请假天数,去掉节假日的
前端
function countDays() {
//外出开始日期
var startDate = new Date(DSF.getElementValueByKey("C-OUTMANAGE-0001").replace(/\-/g, "/"));
//外出开始日期
var endDate = new Date(DSF.getElementValueByKey("C-OUTMANAGE-0002").replace(/\-/g, "/"));
//外出开始时段
var startmOra = DSF.getElementValueByKey("201013095627Un0GQcbx49VtOEzIApJ"); //1 上午 2下午
//外出结束时段
var endmOra = DSF.getElementValueByKey("201013095840LYsDrbU0n8n807T2skM"); //1 上午 2下午
if(!startDate||!endDate||!startmOra||!endmOra){
debugger
layuiError("外出开始或结束时间不能为空");
return false;
}
if(startDate>endDate || (startDate==endDate && startmOra>endmOra) ) {
DSF.setElementValueByKey("C-OUTMANAGE-0001", {"value": ""});
debugger
DSF.setElementValueByKey("C-OUTMANAGE-0002", {"value": ""});
DSF.setElementValueByKey("C-OUTMANAGE-0014", {"value": ""});
layuiError("开始日期不能小于结束日期");
return false;
}
//获取去除节假日的请假天数
var startTime = DSF.getElementValueByKey("C-OUTMANAGE-0001");
var endTime = DSF.getElementValueByKey("C-OUTMANAGE-0002");
//请求后台, 填写请假起止时间后自动判断天数,节假日不算请假
DSF.Utils.ajaxRequest("ctrl/yearVacation/offHoliday", {"startTime": startTime, "endTime" : endTime, "startAPM" : startmOra, "endAPM" : endmOra}, function (result) {
if ("success" == result.type) {
DSF.setElementValueByKey("C-OUTMANAGE-0014", {"value": result.data});
}
});
}
后台
/**
* 节假日接口地址, 0上班,1周末,2节假日, 后面加年份20201010,20201011或2020-10-10
*/
public static final String HOLIDAYAPI = "http://tool.bitefu.net/jiari/?d=";
/**
* 获取去掉节假日的请假天数
* @return
*/
@RequestMapping("/offHoliday")
public MessageBean offHoliday (){
IContextDictionary dic = this.getContextDictionary();
//开始时间 例如:2020-10-16
String startTime = dic.getString("startTime");
//结束时间
String endTime = dic.getString("endTime");
//开始时间上下午
int startAPM = dic.getInt("startAPM");
//结束时间上下午
int endAPM = dic.getInt("endAPM");
// 返回的日期集合
StringBuffer days = new StringBuffer();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//总共请假天数
double numDays = 0;
try {
//转化日期
Date start = dateFormat.parse(startTime);
Date end = dateFormat.parse(endTime);
//获取指定时间点(定时)
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end);
// 日期加1(包含结束)
tempEnd.add(Calendar.DATE, +1);
//把开始日期和结束日期之间天天数日期循环出来放在日期集合里
while (tempStart.before(tempEnd)) {
days.append(dateFormat.format(tempStart.getTime()) + ",");
tempStart.add(Calendar.DAY_OF_YEAR, 1);
}
//把日期集合传入 节假日接口地址里检验,日期中有没有对应节假日,返回一个json格式,{"日期(2020-10-01):2",.省略..,"日期(2020-20-08):0"}
String result = HttpClientUtil.doGet(HOLIDAYAPI + days.toString());
//再次转成json格式{"2020-10-01":2,...省略...,{"2020-10-08":0}}
JSONObject json = JSONObject.parseObject(result);
System.out.println(json.toString());
String[] array = days.toString().split(",");
String type="";
//循环数组,如果是上班则在请假总天数加1
for (String arr : array) {
type = json.getString(DateFormatUtil.format(dateFormat.parse(arr), DateFormatUtil.DATE_PATTERN_NO_CHART));
// 0 上班 1周末 2节假日, 网站失效-1, 数据格式改变-2
if ("0".equals(type)) {
numDays ++;
}
}
//开始上午,结束下午,并且算出来请假总天数大于0,类型是上班日,减半天0.5
if (startAPM == endAPM && numDays>0 && "0".equals(type)) {
numDays -= 0.5;
}
//开始时段下午(2)大于结束时段上午(1)
if(startAPM > endAPM){
numDays -= 1;
}
if(numDays % 1.0 == 0){
return SuperKit.success((long)numDays);
}
} catch (Exception e) {
e.printStackTrace();
}
return SuperKit.success(numDays);
}
