longz
2026-06-04 da780b14b8db78043d2c0f27d3bfd73111e6d550
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
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, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
};
 
$(function () {
    TLineSchedule.init();
});