Files
PropertyDeployment/resources/Web/MicroCommunityWeb/html/components/property/addCommunitySpacePerson/addCommunitySpacePerson.js
2025-12-09 20:22:03 +08:00

410 lines
18 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

(function (vc) {
vc.extends({
propTypes: {
callBackListener: vc.propTypes.string, //父组件名称
callBackFunction: vc.propTypes.string //父组件监听方法
},
data: {
addCommunitySpacePersonInfo: {
cspId: '',
spaceId: '',
personName: '',
personTel: '',
appointmentDate: '',
appointmentTime: '',
receivableAmount: '',
receivedAmount: '',
payWay: '',
bookingType: '', // 修改字段名为bookingType与后端一致
resourceId: '', // 添加资源ID字段
state: 'S',
remark: '',
openTime: '',
openTimes: [],
spaces: [],
conflictWarning: null,
showBookingTypeSelect: true, // 控制是否显示场地类型选择框
}
},
_initMethod: function () {
// 确保addCommunitySpacePersonInfo对象已初始化
if (!vc.component.addCommunitySpacePersonInfo) {
vc.component.addCommunitySpacePersonInfo = {
cspId: '',
spaceId: '',
personName: '',
personTel: '',
appointmentDate: '',
appointmentTime: '',
receivableAmount: '',
receivedAmount: '',
payWay: '',
bookingType: '',
resourceId: '',
state: 'S',
remark: '',
openTime: '',
openTimes: [],
spaces: [],
conflictWarning: null,
showBookingTypeSelect: true,
};
}
},
_initEvent: function () {
vc.on('addCommunitySpacePerson', 'openAddCommunitySpacePersonModal', function (_param) {
// 确保addCommunitySpacePersonInfo对象已初始化
if (!vc.component.addCommunitySpacePersonInfo) {
vc.component.addCommunitySpacePersonInfo = {
cspId: '',
spaceId: '',
personName: '',
personTel: '',
appointmentDate: '',
appointmentTime: '',
receivableAmount: '',
receivedAmount: '',
payWay: '',
bookingType: '',
resourceId: '',
state: 'S',
remark: '',
openTime: '',
openTimes: [],
spaces: [],
conflictWarning: null,
showBookingTypeSelect: true,
};
}
$that.clearAddCommunitySpacePersonInfo();
// 如果 _param 包含 appointmentDate 或 appointmentTime赋值
vc.copyObject(_param, $that.addCommunitySpacePersonInfo);
if (_param.appointmentDate) {
$that.addCommunitySpacePersonInfo.appointmentDate = _param.appointmentDate;
}
if (_param.appointmentTime) {
$that.addCommunitySpacePersonInfo.appointmentTime = _param.appointmentTime;
}
// 将 openTime 添加到 openTimes
if (_param.openTime) {
$that.addCommunitySpacePersonInfo.openTimes.push({
hours: _param.openTime,
isOpen: 'Y'
});
}
// 自动将 resourceId 设置为 spaceId 的值
$that.addCommunitySpacePersonInfo.resourceId = $that.addCommunitySpacePersonInfo.spaceId;
// 根据球场的bookingType字段判断预约类型
let _spaceBookingType = _param.spaceBookingType || 'Full'; // 默认为全场
if (_spaceBookingType === 'Full') {
// 如果是全场预约设置bookingType为Full并隐藏场地类型选择
$that.addCommunitySpacePersonInfo.bookingType = 'Full';
$that.addCommunitySpacePersonInfo.showBookingTypeSelect = false; // 控制是否显示场地类型选择框
} else if (_spaceBookingType === 'Half') {
// 如果是半场预约,检查该时间段是否已有半场预约
let _hasHalfBooking = false;
let _existingResourceId = '';
// 获取reportCommunitySpace组件的persons数据添加空值检查
let _reportCommunitySpaceComponent = vc.getComponent('reportCommunitySpace', 'reportCommunitySpaceInfo');
let _persons = _reportCommunitySpaceComponent ? _reportCommunitySpaceComponent.persons : [];
if (_persons && _param.spaceId && _param.hours) {
_persons.forEach(item => {
if (item.spaceId == _param.spaceId) {
item.times.forEach(itemTime => {
if (itemTime.hours == _param.hours && item.bookingType === 'Half') {
_hasHalfBooking = true;
_existingResourceId = item.resourceId;
}
});
}
});
}
// 判断当前球场的类型:如果已有半场预约,则默认为半场预约模式
if (_hasHalfBooking) {
$that.addCommunitySpacePersonInfo.bookingType = 'Half';
// 提示用户已有半场预约
setTimeout(() => {
vc.toast('该时间段已有半场预约,您将预约另一半场');
}, 500);
} else {
// 如果没有半场预约,默认为全场预约模式
$that.addCommunitySpacePersonInfo.bookingType = 'Full';
}
$that.addCommunitySpacePersonInfo.showBookingTypeSelect = true; // 显示场地类型选择框
}
$('#addCommunitySpacePersonModel').modal('show');
});
},
methods: {
addCommunitySpacePersonValidate() {
return vc.validate.validate({
addCommunitySpacePersonInfo: vc.component.addCommunitySpacePersonInfo
}, {
'addCommunitySpacePersonInfo.spaceId': [{
limit: "required",
param: "",
errInfo: "场地ID不能为空"
},
{
limit: "maxLength",
param: "30",
errInfo: "场地ID不能超过30"
},
],
'addCommunitySpacePersonInfo.personName': [{
limit: "required",
param: "",
errInfo: "预约人不能为空"
},
{
limit: "maxLength",
param: "64",
errInfo: "预约人不能超过64"
},
],
'addCommunitySpacePersonInfo.personTel': [{
limit: "required",
param: "",
errInfo: "预约电话不能为空"
},
{
limit: "maxLength",
param: "11",
errInfo: "预约电话不能超过11"
},
],
'addCommunitySpacePersonInfo.appointmentTime': [{
limit: "required",
param: "",
errInfo: "预约时间不能为空"
},
{
limit: "maxLength",
param: "64",
errInfo: "预约时间不能超过64"
},
],
'addCommunitySpacePersonInfo.receivableAmount': [{
limit: "required",
param: "",
errInfo: "应收金额不能为空"
},
{
limit: "maxLength",
param: "10",
errInfo: "应收金额不能超过10"
},
],
'addCommunitySpacePersonInfo.receivedAmount': [{
limit: "required",
param: "",
errInfo: "实收金额不能为空"
},
{
limit: "maxLength",
param: "10",
errInfo: "实收金额不能超过10"
},
],
'addCommunitySpacePersonInfo.payWay': [{
limit: "required",
param: "",
errInfo: "支付方式不能为空"
},
{
limit: "maxLength",
param: "12",
errInfo: "支付方式不能超过12"
},
],
'addCommunitySpacePersonInfo.state': [{
limit: "required",
param: "",
errInfo: "state不能为空"
},
{
limit: "maxLength",
param: "12",
errInfo: "state不能超过12"
},
],
'addCommunitySpacePersonInfo.remark': [{
limit: "required",
param: "",
errInfo: "remark不能为空"
},
{
limit: "maxLength",
param: "512",
errInfo: "remark不能超过512"
},
],
});
},
saveCommunitySpacePersonInfo: function () {
if (!vc.component.addCommunitySpacePersonValidate()) {
vc.toast(vc.validate.errInfo);
return;
}
// 验证场地类型是否已选择
if (!vc.component.addCommunitySpacePersonInfo.bookingType) {
vc.toast("请选择场地类型");
return;
}
// 如果选择半场验证资源ID是否已填写
if (vc.component.addCommunitySpacePersonInfo.bookingType === 'Half' &&
!vc.component.addCommunitySpacePersonInfo.resourceId) {
vc.toast("半场预约必须指定资源ID");
return;
}
vc.component.addCommunitySpacePersonInfo.communityId = vc.getCurrentCommunity().communityId;
//不提交数据将数据 回调给侦听处理
if (vc.notNull($props.callBackListener)) {
vc.emit($props.callBackListener, $props.callBackFunction, vc.component.addCommunitySpacePersonInfo);
$('#addCommunitySpacePersonModel').modal('hide');
return;
}
const conflict = this.checkBookingConflict();
if (conflict) {
vc.toast(conflict.message);
return;
}
vc.http.apiPost(
'/communitySpace.saveCommunitySpacePerson',
JSON.stringify(vc.component.addCommunitySpacePersonInfo), {
emulateJSON: true
},
function (json, res) {
//vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
let _json = JSON.parse(json);
if (_json.code == 0) {
//关闭model
$('#addCommunitySpacePersonModel').modal('hide');
vc.component.clearAddCommunitySpacePersonInfo();
vc.emit('communitySpacePersonManage', 'listCommunitySpacePerson', {});
vc.emit('communitySpaceManage', 'listCommunitySpacePerson', {});
return;
}
vc.toast(_json.msg);
},
function(errInfo, error) {
let errorMsg = errInfo;
// 解析特定错误
if (errInfo.includes("已有半场预约,不能预约全场")) {
errorMsg = "无法预约全场:" + errInfo;
// 自动切换为半场模式
vc.component.addCommunitySpacePersonInfo.bookingType = 'Half';
vc.component.addCommunitySpacePersonInfo.resourceId = '';
vc.toast("已自动切换为半场预约模式");
}
else if (errInfo.includes("已被预约全场,不能预约半场")) {
errorMsg = "无法预约半场:" + errInfo;
}
else if (errInfo.includes("资源") && errInfo.includes("已被占用")) {
errorMsg = "资源冲突:" + errInfo;
// 提示用户选择其他资源
vc.emit('refreshResourceList', {});
}
vc.toast(errorMsg);
}
);
},
clearAddCommunitySpacePersonInfo: function () {
vc.component.addCommunitySpacePersonInfo = {
cspId: '',
spaceId: '',
personName: '',
personTel: '',
appointmentDate: '',
appointmentTime: '',
receivableAmount: '',
receivedAmount: '',
payWay: '',
bookingType: '', // 重置场地类型字段
resourceId: '', // 重置资源ID字段
state: 'S',
remark: '',
openTime: '',
openTimes: [],
spaces: [],
conflictWarning: null,
};
},
// 处理场地类型变化
onBookingTypeChange: function () {
// 如果从半场切换到全场清空资源ID
if (vc.component.addCommunitySpacePersonInfo.bookingType === 'Full') {
vc.component.addCommunitySpacePersonInfo.resourceId = '';
}else if (vc.component.addCommunitySpacePersonInfo.bookingType === 'Half'){
vc.component.addCommunitySpacePersonInfo.resourceId = vc.component.addCommunitySpacePersonInfo.spaceId;
}
const conflict = this.checkBookingConflict();
if (conflict) {
vc.toast(conflict.message);
if (conflict.type === 'HALF_CONFLICT') {
this.addCommunitySpacePersonInfo.bookingType = 'Half';
vc.toast('已自动切换为半场预约模式')
}
}
},
checkBookingConflict() {
const info = this.addCommunitySpacePersonInfo;
if (!info.spaceId || !info.appointmentDate || !info.openTimes) return null;
// 获取已存在的预约数据
const reportComp = vc.getComponent('reportCommunitySpace', 'reportCommunitySpaceInfo');
const persons = reportComp ? reportComp.persons : [];
// 检测每个时间段
for (const timeSlot of info.openTimes) {
if (timeSlot.isOpen !== 'Y') continue;
const hour = timeSlot.hours;
const existingBookings = persons.filter(p =>
p.spaceId === info.spaceId &&
p.appointmentDate === info.appointmentDate &&
p.times.some(t => t.hours == hour)
);
// 检测冲突类型
if (existingBookings.length > 0) {
const hasFullBooking = existingBookings.some(b => b.bookingType === 'Full');
const hasHalfBooking = existingBookings.some(b => b.bookingType === 'Half');
if (info.bookingType === 'Full' && (hasFullBooking || hasHalfBooking)) {
return {
type: 'HALF_CONFLICT',
message: `时间段 ${hour} 点已有预约,无法预约全场`,
hour
};
}
}
}
return null;
},
resolveConflict() {
vc.emit('showAvailableTimes', {
spaceId: this.addCommunitySpacePersonInfo.spaceId,
date: this.addCommunitySpacePersonInfo.appointmentDate
});
}
}
});
})(window.vc);