var TLineSchedule = {
|
lineId: null,
|
companyId: null,
|
currentYear: null,
|
currentMonth: null,
|
selectedDay: null,
|
monthStatsMap: {}
|
};
|
|
TLineSchedule.init = function () {
|
this.lineId = $("#lineId").val();
|
this.companyId = $("#companyId").val();
|
var now = new Date();
|
this.currentYear = now.getFullYear();
|
this.currentMonth = now.getMonth() + 1;
|
|
var autoPlan = $("#autoPlanValue").val();
|
$("#autoPlanSwitch").prop("checked", autoPlan == "1");
|
this.loadMonthData();
|
};
|
|
TLineSchedule.getMonthStr = function () {
|
return this.currentYear + "-" + (this.currentMonth < 10 ? "0" + this.currentMonth : this.currentMonth);
|
};
|
|
TLineSchedule.prevMonth = function () {
|
if (this.currentMonth == 1) {
|
this.currentMonth = 12;
|
this.currentYear -= 1;
|
} else {
|
this.currentMonth -= 1;
|
}
|
this.selectedDay = null;
|
$("#shiftPanel").hide();
|
this.loadMonthData();
|
};
|
|
TLineSchedule.nextMonth = function () {
|
if (this.currentMonth == 12) {
|
this.currentMonth = 1;
|
this.currentYear += 1;
|
} else {
|
this.currentMonth += 1;
|
}
|
this.selectedDay = null;
|
$("#shiftPanel").hide();
|
this.loadMonthData();
|
};
|
|
TLineSchedule.loadMonthData = function () {
|
var _this = this;
|
var ajax = new $ax(Feng.ctxPath + "/tLine/schedule/month", function (res) {
|
if (!res || res.status != 200) {
|
Feng.error((res && res.msg) || "加载月份排班失败");
|
return;
|
}
|
_this.monthStatsMap = {};
|
var days = res.data && res.data.days ? res.data.days : [];
|
for (var i = 0; i < days.length; i++) {
|
_this.monthStatsMap[days[i].day] = days[i];
|
}
|
_this.renderCalendar();
|
}, function () {
|
Feng.error("加载月份排班失败");
|
});
|
ajax.set("lineId", this.lineId);
|
ajax.set("companyId", this.companyId);
|
ajax.set("month", this.getMonthStr());
|
ajax.start();
|
};
|
|
TLineSchedule.renderCalendar = function () {
|
$("#calendarMonthText").text(this.currentYear + "年 " + this.currentMonth + "月");
|
var firstDay = new Date(this.currentYear, this.currentMonth - 1, 1);
|
var firstWeekDay = (firstDay.getDay() + 6) % 7;
|
var maxDay = new Date(this.currentYear, this.currentMonth, 0).getDate();
|
var prevMaxDay = new Date(this.currentYear, this.currentMonth - 1, 0).getDate();
|
|
var html = "";
|
var dayNum = 1;
|
var nextNum = 1;
|
for (var row = 0; row < 6; row++) {
|
html += "<tr>";
|
for (var col = 0; col < 7; col++) {
|
var index = row * 7 + col;
|
if (index < firstWeekDay) {
|
var prevDay = prevMaxDay - firstWeekDay + index + 1;
|
html += '<td class="calendar-day other"><div class="num">' + prevDay + '</div></td>';
|
} else if (dayNum > maxDay) {
|
html += '<td class="calendar-day other"><div class="num">' + nextNum + '</div></td>';
|
nextNum++;
|
} else {
|
var dayStr = this.currentYear + "-" + this.pad2(this.currentMonth) + "-" + this.pad2(dayNum);
|
var stat = this.monthStatsMap[dayStr];
|
var tagHtml = "";
|
if (stat) {
|
if (stat.status == 1) {
|
tagHtml = '<div class="tag ok">' + stat.assignNum + ' 已排班</div>';
|
} else if (stat.status == 2) {
|
tagHtml = '<div class="tag warn">' + stat.assignNum + ' 未排满</div>';
|
} else {
|
tagHtml = '<div class="tag none">无班次</div>';
|
}
|
}
|
var activeCls = this.selectedDay == dayStr ? " active" : "";
|
html += '<td class="calendar-day' + activeCls + '" data-day="' + dayStr + '" onclick="TLineSchedule.clickDay(this)">' +
|
'<div class="num">' + dayNum + '</div>' + tagHtml + '</td>';
|
dayNum++;
|
}
|
}
|
html += "</tr>";
|
}
|
$("#calendarBody").html(html);
|
};
|
|
TLineSchedule.clickDay = function (el) {
|
var day = $(el).attr("data-day");
|
if (!day) {
|
return;
|
}
|
this.selectedDay = day;
|
$("#calendarBody .calendar-day").removeClass("active");
|
$(el).addClass("active");
|
this.loadDayDetail(day);
|
};
|
|
TLineSchedule.loadDayDetail = function (day) {
|
var _this = this;
|
var ajax = new $ax(Feng.ctxPath + "/tLine/schedule/dayDetail", function (res) {
|
if (!res || res.status != 200) {
|
Feng.error((res && res.msg) || "加载排班详情失败");
|
return;
|
}
|
$("#shiftPanel").show();
|
$("#selectedDayText").text(day + " 排班信息");
|
_this.renderShiftTable(res.data || []);
|
}, function () {
|
Feng.error("加载排班详情失败");
|
});
|
ajax.set("lineId", this.lineId);
|
ajax.set("companyId", this.companyId);
|
ajax.set("day", day);
|
ajax.start();
|
};
|
|
TLineSchedule.renderShiftTable = function (shiftList) {
|
var html = "";
|
for (var i = 0; i < shiftList.length; i++) {
|
var shift = shiftList[i];
|
var drivers = shift.drivers || [];
|
var driverHtml = "";
|
for (var j = 0; j < drivers.length; j++) {
|
var d = drivers[j];
|
var cls = d.assignType == 2 ? "driver-tag manual" : "driver-tag";
|
var del = d.assignType == 2 ? '<span class="del" onclick="TLineSchedule.deleteSchedule(' + d.id + ')">x</span>' : "";
|
driverHtml += '<span class="' + cls + '">' + this.escapeHtml((d.driverName || "") + "-" + (d.driverPhone || "")) + del + '</span>';
|
}
|
if (driverHtml === "") {
|
driverHtml = "-";
|
}
|
var canAdd = shift.canAdd == 1;
|
var addBtn = canAdd ? ('<a href="javascript:;" onclick="TLineSchedule.openAddDialog(' + shift.lineShiftId + ',' + shift.remainNum + ')">添加</a>') : '<span style="color:#999;">已满</span>';
|
html += '<tr>' +
|
'<td>' + this.escapeHtml((shift.startTime || "") + " - " + (shift.endTime || "")) + '</td>' +
|
'<td>' + (shift.carNum || 0) + '</td>' +
|
'<td>' + driverHtml + '</td>' +
|
'<td>' + addBtn + '</td>' +
|
'</tr>';
|
}
|
$("#shiftTableBody").html(html);
|
};
|
|
TLineSchedule.openAddDialog = function (lineShiftId, remainNum) {
|
var _this = this;
|
var ajax = new $ax(Feng.ctxPath + "/tLine/schedule/drivers", function (res) {
|
if (!res || res.status != 200) {
|
Feng.error((res && res.msg) || "获取可排司机失败");
|
return;
|
}
|
var list = res.data || [];
|
if (list.length <= 0) {
|
Feng.info("暂无可添加司机");
|
return;
|
}
|
var itemsHtml = "";
|
for (var i = 0; i < list.length; i++) {
|
itemsHtml += '<label style="display:block;margin-bottom:6px;">' +
|
'<input type="checkbox" name="scheduleDriverCheck" value="' + list[i].driverId + '"> ' +
|
_this.escapeHtml((list[i].driverName || "") + "-" + (list[i].driverPhone || "")) +
|
'</label>';
|
}
|
var html = '<div style="padding:15px 20px;max-height:360px;overflow:auto;">' +
|
'<div style="margin-bottom:8px;">可添加人数:' + remainNum + '</div>' + itemsHtml + '</div>';
|
layer.open({
|
type: 1,
|
title: '选择司机',
|
area: ['420px', '520px'],
|
btn: ['确定', '取消'],
|
content: html,
|
yes: function (index) {
|
var ids = [];
|
$("input[name='scheduleDriverCheck']:checked").each(function () {
|
ids.push(parseInt($(this).val(), 10));
|
});
|
if (ids.length <= 0) {
|
Feng.info("请至少选择一名司机");
|
return;
|
}
|
if (ids.length > remainNum) {
|
Feng.info("最多只能添加" + remainNum + "名司机");
|
return;
|
}
|
_this.addSchedule(lineShiftId, ids, function () {
|
layer.close(index);
|
});
|
}
|
});
|
}, function () {
|
Feng.error("获取可排司机失败");
|
});
|
ajax.set("lineId", this.lineId);
|
ajax.set("companyId", this.companyId);
|
ajax.set("lineShiftId", lineShiftId);
|
ajax.set("day", this.selectedDay);
|
ajax.start();
|
};
|
|
TLineSchedule.addSchedule = function (lineShiftId, driverIds, callback) {
|
var _this = this;
|
var ajax = new $ax(Feng.ctxPath + "/tLine/schedule/add", function (res) {
|
if (!res || res.status != 200) {
|
Feng.error((res && res.msg) || "添加排班失败");
|
return;
|
}
|
Feng.success("添加成功");
|
if (callback) {
|
callback();
|
}
|
_this.loadDayDetail(_this.selectedDay);
|
_this.loadMonthData();
|
}, function () {
|
Feng.error("添加排班失败");
|
});
|
ajax.set("lineId", this.lineId);
|
ajax.set("companyId", this.companyId);
|
ajax.set("lineShiftId", lineShiftId);
|
ajax.set("day", this.selectedDay);
|
ajax.set("driverIds", JSON.stringify(driverIds));
|
ajax.start();
|
};
|
|
TLineSchedule.deleteSchedule = function (scheduleId) {
|
var _this = this;
|
layer.confirm("确认删除该手动排班记录吗?", {icon: 3, title: "提示"}, function (index) {
|
layer.close(index);
|
var ajax = new $ax(Feng.ctxPath + "/tLine/schedule/delete", function (res) {
|
if (!res || res.status != 200) {
|
Feng.error((res && res.msg) || "删除失败");
|
return;
|
}
|
Feng.success("删除成功");
|
_this.loadDayDetail(_this.selectedDay);
|
_this.loadMonthData();
|
}, function () {
|
Feng.error("删除失败");
|
});
|
ajax.set("scheduleId", scheduleId);
|
ajax.set("lineId", _this.lineId);
|
ajax.set("companyId", _this.companyId);
|
ajax.start();
|
});
|
};
|
|
TLineSchedule.copySchedule = function () {
|
var sourceStart = $("#copyStartDate").val();
|
var sourceEnd = $("#copyEndDate").val();
|
var targetStart = $("#targetStartDate").val();
|
if (!sourceStart || !sourceEnd || !targetStart) {
|
Feng.info("请完整填写复制日期与粘贴起始日期");
|
return;
|
}
|
var _this = this;
|
var ajax = new $ax(Feng.ctxPath + "/tLine/schedule/copy", function (res) {
|
if (!res || res.status != 200) {
|
Feng.error((res && res.msg) || "复制失败");
|
return;
|
}
|
var data = res.data || {};
|
Feng.success("复制完成,新增" + (data.addNum || 0) + "条,跳过" + (data.skipNum || 0) + "条");
|
_this.loadMonthData();
|
if (_this.selectedDay) {
|
_this.loadDayDetail(_this.selectedDay);
|
}
|
}, function () {
|
Feng.error("复制失败");
|
});
|
ajax.set("lineId", this.lineId);
|
ajax.set("companyId", this.companyId);
|
ajax.set("sourceStart", sourceStart);
|
ajax.set("sourceEnd", sourceEnd);
|
ajax.set("targetStart", targetStart);
|
ajax.start();
|
};
|
|
TLineSchedule.onAutoPlanChange = function (el) {
|
var checked = $(el).prop("checked");
|
var autoPlan = checked ? 1 : 2;
|
var _this = this;
|
var ajax = new $ax(Feng.ctxPath + "/tLine/schedule/autoPlan", function (res) {
|
if (!res || res.status != 200) {
|
Feng.error((res && res.msg) || "保存失败");
|
$(el).prop("checked", !checked);
|
return;
|
}
|
$("#autoPlanValue").val(autoPlan);
|
Feng.success("保存成功");
|
}, function () {
|
$(el).prop("checked", !checked);
|
Feng.error("保存失败");
|
});
|
ajax.set("lineId", this.lineId);
|
ajax.set("companyId", this.companyId);
|
ajax.set("autoPlan", autoPlan);
|
ajax.start();
|
};
|
|
TLineSchedule.pad2 = function (num) {
|
return num < 10 ? "0" + num : "" + num;
|
};
|
|
TLineSchedule.escapeHtml = function (str) {
|
return (str || "").replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
};
|
|
$(function () {
|
TLineSchedule.init();
|
});
|