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

698 lines
33 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, vm) {
vc.extends({
data: {
editCommunitySpaceInfo: {
spaceId: '',
venueId: '',
name: '',
startTime: '',
endTime: '',
bookingType: '',
adminName: '',
tel: '',
state: '',
communityId: '',
pricingByDay: 'N', // 是否按天定义收费,默认为否
uniformFullPrice: 0, // 统一全场价格
uniformHalfPrice: 0, // 统一半场价格
dayPricing: [
{ dayOfWeek: 'Monday', fullPrice: 0, halfPrice: 0 },
{ dayOfWeek: 'Tuesday', fullPrice: 0, halfPrice: 0 },
{ dayOfWeek: 'Wednesday', fullPrice: 0, halfPrice: 0 },
{ dayOfWeek: 'Thursday', fullPrice: 0, halfPrice: 0 },
{ dayOfWeek: 'Friday', fullPrice: 0, halfPrice: 0 },
{ dayOfWeek: 'Saturday', fullPrice: 0, halfPrice: 0 },
{ dayOfWeek: 'Sunday', fullPrice: 0, halfPrice: 0 }
] // 按天定义的价格规则
}
},
_initMethod: function () {
let that = vc.component;
// 获取传递的参数
let _spaceInfo = vc.param('spaceInfo');
if (_spaceInfo) {
that.editCommunitySpaceInfo = _spaceInfo;
// 保存原始收费模式,防止在编辑时被修改
that.editCommunitySpaceInfo._originalPricingByDay = _spaceInfo.pricingByDay;
}
// 初始化价格规则数据
that._initPriceRuleData();
},
_initEvent: function () {
vc.on('editCommunitySpace', 'openEditCommunitySpaceModal', function (_params) {
vc.component.refreshEditCommunitySpaceInfo();
$('#editCommunitySpaceModel').modal('show');
// 先复制参数确保priceRules数据被正确复制
vc.copyObject(_params, vc.component.editCommunitySpaceInfo);
vc.component.editCommunitySpaceInfo.communityId = vc.getCurrentCommunity().communityId;
// 打印接收到的参数,以便调试
console.log('接收到的参数:', _params);
console.log('复制后的editCommunitySpaceInfo:', vc.component.editCommunitySpaceInfo);
// 确保在复制数据后立即初始化价格规则数据
vc.component._initPriceRuleData();
});
},
methods: {
/**
* 初始化价格规则数据
*/
_initPriceRuleData: function () {
const that = vc.component;
let priceRules = that.editCommunitySpaceInfo.priceRules;
console.log('初始化价格规则数据:', priceRules);
// 重置价格数据,避免旧数据干扰
that.editCommunitySpaceInfo.uniformFullPrice = 0;
that.editCommunitySpaceInfo.uniformHalfPrice = 0;
that.editCommunitySpaceInfo.dayPricing.forEach(day => {
day.fullPrice = 0;
day.halfPrice = 0;
});
if (priceRules && Array.isArray(priceRules) && priceRules.length > 0) {
console.log('开始处理价格规则,数量:', priceRules.length);
// 智能判断收费模式
let hasDayPricing = priceRules.some(rule =>
rule.ruleName && (rule.ruleName.includes('星期') || rule.ruleName.includes('周'))
);
if (hasDayPricing) {
that.editCommunitySpaceInfo.pricingByDay = 'Y';
console.log('检测到按天收费规则,设置为按天收费模式');
} else {
that.editCommunitySpaceInfo.pricingByDay = 'N';
console.log('未检测到按天收费规则,设置为统一收费模式');
}
// 处理价格规则
priceRules.forEach((rule, index) => {
console.log(`处理规则 ${index}:`, rule);
if (that.editCommunitySpaceInfo.pricingByDay === 'Y') {
// 按天收费模式
let dayOfWeek = null;
// 多种方式提取星期信息
if (rule.day) {
dayOfWeek = rule.day;
} else if (rule.ruleName) {
// 从ruleName中提取
if (rule.ruleName.includes('一') || rule.ruleName.includes('MONDAY') || rule.ruleName.includes('Monday')) {
dayOfWeek = 'Monday';
} else if (rule.ruleName.includes('二') || rule.ruleName.includes('TUESDAY') || rule.ruleName.includes('Tuesday')) {
dayOfWeek = 'Tuesday';
} else if (rule.ruleName.includes('三') || rule.ruleName.includes('WEDNESDAY') || rule.ruleName.includes('Wednesday')) {
dayOfWeek = 'Wednesday';
} else if (rule.ruleName.includes('四') || rule.ruleName.includes('THURSDAY') || rule.ruleName.includes('Thursday')) {
dayOfWeek = 'Thursday';
} else if (rule.ruleName.includes('五') || rule.ruleName.includes('FRIDAY') || rule.ruleName.includes('Friday')) {
dayOfWeek = 'Friday';
} else if (rule.ruleName.includes('六') || rule.ruleName.includes('SATURDAY') || rule.ruleName.includes('Saturday')) {
dayOfWeek = 'Saturday';
} else if (rule.ruleName.includes('日') || rule.ruleName.includes('天') || rule.ruleName.includes('SUNDAY') || rule.ruleName.includes('Sunday')) {
dayOfWeek = 'Sunday';
}
}
if (dayOfWeek) {
let dayIndex = that._getDayIndex(dayOfWeek);
if (dayIndex >= 0) {
if (rule.ruleType === '1001' || rule.ruleName.includes('全场')) {
that.editCommunitySpaceInfo.dayPricing[dayIndex].fullPrice = parseFloat(rule.price) || 0;
console.log(`设置 ${dayOfWeek} 全场价格:`, rule.price);
} else if (rule.ruleType === '2002' || rule.ruleName.includes('半场')) {
that.editCommunitySpaceInfo.dayPricing[dayIndex].halfPrice = parseFloat(rule.price) || 0;
console.log(`设置 ${dayOfWeek} 半场价格:`, rule.price);
}
}
}
} else {
// 统一收费模式
if (rule.ruleType === '1001' || rule.ruleName.includes('全场')) {
that.editCommunitySpaceInfo.uniformFullPrice = parseFloat(rule.price) || 0;
console.log('设置统一全场价格:', rule.price);
} else if (rule.ruleType === '2002' || rule.ruleName.includes('半场')) {
that.editCommunitySpaceInfo.uniformHalfPrice = parseFloat(rule.price) || 0;
console.log('设置统一半场价格:', rule.price);
}
}
});
} else {
console.log('没有priceRules数据使用默认值');
}
console.log('初始化后的价格数据:', {
uniformFullPrice: that.editCommunitySpaceInfo.uniformFullPrice,
uniformHalfPrice: that.editCommunitySpaceInfo.uniformHalfPrice,
dayPricing: that.editCommunitySpaceInfo.dayPricing
});
},
/**
* 根据星期几获取索引
* @param {String} dayOfWeek 星期几
* @return {Number} 索引
*/
_getDayIndex: function (dayOfWeek) {
const dayMap = {
'Monday': 0,
'Tuesday': 1,
'Wednesday': 2,
'Thursday': 3,
'Friday': 4,
'Saturday': 5,
'Sunday': 6,
'星期一': 0,
'星期二': 1,
'星期三': 2,
'星期四': 3,
'星期五': 4,
'星期六': 5,
'星期日': 6,
'星期天': 6,
'MONDAY': 0,
'TUESDAY': 1,
'WEDNESDAY': 2,
'THURSDAY': 3,
'FRIDAY': 4,
'SATURDAY': 5,
'SUNDAY': 6
};
// 清理dayOfWeek参数去除前后空格
const cleanedDayOfWeek = dayOfWeek ? dayOfWeek.trim() : dayOfWeek;
console.log('_getDayIndex调用: dayOfWeek="' + dayOfWeek + '", 清理后:"' + cleanedDayOfWeek + '", 类型:', typeof dayOfWeek);
console.log('dayMap检查:', dayMap[cleanedDayOfWeek]);
console.log('dayMap中MONDAY的值:', dayMap['MONDAY']);
console.log('dayMap中MONDAY是否存在:', 'MONDAY' in dayMap);
// 使用清理后的参数进行查找
const result = dayMap[cleanedDayOfWeek] !== undefined ? dayMap[cleanedDayOfWeek] : -1;
console.log('_getDayIndex返回结果:', result);
return result;
},
/**
* 改变预约类型
*/
_changeBookingType: function () {
// 这里可以添加预约类型改变时的逻辑
},
editCommunitySpaceValidate: function () {
return vc.validate.validate({
editCommunitySpaceInfo: vc.component.editCommunitySpaceInfo
}, {
'editCommunitySpaceInfo.name': [
{
limit: "required",
param: "",
errInfo: "名称不能为空"
},
{
limit: "maxLength",
param: "50",
errInfo: "名称不能超过50"
}
],
'editCommunitySpaceInfo.startTime': [
{
limit: "required",
param: "",
errInfo: "预约开始时间不能为空"
},
{
limit: "maxLength",
param: "64",
errInfo: "预约开始时间不能超过64"
}
],
'editCommunitySpaceInfo.endTime': [
{
limit: "required",
param: "",
errInfo: "预约结束时间不能为空"
},
{
limit: "maxLength",
param: "64",
errInfo: "预约结束时间不能超过64"
}
],
'editCommunitySpaceInfo.bookingType': [
{
limit: "required",
param: "",
errInfo: "预约类型不能为空"
},
{
limit: "maxLength",
param: "12",
errInfo: "预约类型不能超过12"
}
],
'editCommunitySpaceInfo.adminName': [
{
limit: "required",
param: "",
errInfo: "管理员不能为空"
},
{
limit: "maxLength",
param: "64",
errInfo: "管理员不能超过64"
}
],
'editCommunitySpaceInfo.tel': [
{
limit: "required",
param: "",
errInfo: "管理员电话不能为空"
},
{
limit: "maxLength",
param: "11",
errInfo: "管理员电话不能超过11"
}
],
'editCommunitySpaceInfo.state': [
{
limit: "required",
param: "",
errInfo: "状态不能为空"
},
{
limit: "maxLength",
param: "12",
errInfo: "状态不能超过12"
}
],
'editCommunitySpaceInfo.spaceId': [
{
limit: "required",
param: "",
errInfo: "编号不能为空"
}
]
});
},
editCommunitySpace: function () {
if (!vc.component.editCommunitySpaceValidate()) {
vc.toast(vc.validate.errInfo);
return;
}
// 验证价格规则
if (vc.component.editCommunitySpaceInfo.pricingByDay === 'Y') {
// 按天定义收费
let hasPrice = false;
vc.component.editCommunitySpaceInfo.dayPricing.forEach(day => {
if ((vc.component.editCommunitySpaceInfo.bookingType === 'Full' || vc.component.editCommunitySpaceInfo.bookingType === 'Half') && day.fullPrice > 0) {
hasPrice = true;
}
if (vc.component.editCommunitySpaceInfo.bookingType === 'Half' && day.halfPrice > 0) {
hasPrice = true;
}
});
if (!hasPrice) {
vc.toast("请设置至少一天的价格");
return;
}
} else {
// 统一收费
if ((vc.component.editCommunitySpaceInfo.bookingType === 'Full' || vc.component.editCommunitySpaceInfo.bookingType === 'Half') && vc.component.editCommunitySpaceInfo.uniformFullPrice <= 0) {
vc.toast("请设置全场收费标准");
return;
}
if (vc.component.editCommunitySpaceInfo.bookingType === 'Half' && vc.component.editCommunitySpaceInfo.uniformHalfPrice <= 0) {
vc.toast("请设置半场收费标准");
return;
}
}
vc.http.apiPost(
'/communitySpace.updateCommunitySpace',
JSON.stringify(vc.component.editCommunitySpaceInfo), {
emulateJSON: true
},
function (json, res) {
//vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
let _json = JSON.parse(json);
if (_json.code == 0) {
// 保存价格规则
vc.component._savePriceRule();
} else {
vc.toast(_json.msg);
}
},
function (errInfo, error) {
console.log('请求失败处理');
vc.message(errInfo);
});
},
/**
* 生成规则ID
*/
generateRuleId: function() {
// 生成一个基于时间戳和随机数的唯一ID
return '14' + new Date().getTime() + Math.floor(Math.random() * 10000);
},
/**
* 保存价格规则
*/
_savePriceRule: function () {
let that = this;
// 直接从组件数据中获取现有规则
let existingPriceRules = vc.component.editCommunitySpaceInfo.priceRules || [];
console.log('组件中的现有规则:', existingPriceRules);
// 获取收费模式(不可修改,使用原始值)
let pricingByDay = vc.component.editCommunitySpaceInfo.pricingByDay;
console.log('当前收费模式(不可修改):', pricingByDay);
// 构建新的价格规则数组
let priceRules = [];
if (pricingByDay === 'Y') {
// 按天定义收费 - 只能更新现有的按天收费规则
vc.component.editCommunitySpaceInfo.dayPricing.forEach(day => {
console.log('处理按天收费规则,星期:', day.dayOfWeek, '全场价:', day.fullPrice, '半场价:', day.halfPrice);
// 处理全场价格规则
if (vc.component.editCommunitySpaceInfo.bookingType === 'Full' ||
vc.component.editCommunitySpaceInfo.bookingType === 'Half') {
// 全场价格规则
let fullPriceRule = {
ruleType: 'DAY_PRICING_FULL',
feeType: '1001',
price: day.fullPrice || 0,
day: day.dayOfWeek // 添加day字段移除ruleName由后端生成
};
// 改进的匹配逻辑:通过规则名称中的星期几信息来匹配
let existingFullRule = that._findPriceRuleByDay(existingPriceRules, day.dayOfWeek, '1001');
if (existingFullRule) {
fullPriceRule.ruleId = existingFullRule.ruleId;
console.log('找到星期' + that._getDayChineseName(day.dayOfWeek) + '的现有全场规则ruleId:', existingFullRule.ruleId);
// 只有当有现有规则时才添加(确保只更新,不插入)
priceRules.push(fullPriceRule);
} else {
console.log('未找到星期' + that._getDayChineseName(day.dayOfWeek) + '的现有全场规则,跳过该规则');
}
}
// 处理半场价格规则
if (vc.component.editCommunitySpaceInfo.bookingType === 'Half') {
// 半场价格规则
let halfPriceRule = {
ruleType: 'DAY_PRICING_HALF',
feeType: '1001',
price: day.halfPrice || 0,
day: day.dayOfWeek // 添加day字段移除ruleName由后端生成
};
// 改进的匹配逻辑:通过规则名称中的星期几信息来匹配
let existingHalfRule = that._findPriceRuleByDay(existingPriceRules, day.dayOfWeek, '2002');
if (existingHalfRule) {
halfPriceRule.ruleId = existingHalfRule.ruleId;
console.log('找到星期' + that._getDayChineseName(day.dayOfWeek) + '的现有半场规则ruleId:', existingHalfRule.ruleId);
// 只有当有现有规则时才添加(确保只更新,不插入)
priceRules.push(halfPriceRule);
} else {
console.log('未找到星期' + that._getDayChineseName(day.dayOfWeek) + '的现有半场规则,跳过该规则');
}
}
});
} else {
// 统一收费 - 只能更新现有的统一收费规则
if (vc.component.editCommunitySpaceInfo.bookingType === 'Full' ||
vc.component.editCommunitySpaceInfo.bookingType === 'Half') {
let fullPriceRule = {
ruleType: 'UNIFORM_PRICING_FULL',
feeType: '1001',
price: vc.component.editCommunitySpaceInfo.uniformFullPrice || 0
// 移除ruleName由后端生成
};
// 查找现有规则中的全场规则
let existingFullRule = existingPriceRules.find(rule => rule.ruleType === '1001');
if (existingFullRule) {
fullPriceRule.ruleId = existingFullRule.ruleId;
console.log('找到现有统一全场规则ruleId:', existingFullRule.ruleId);
// 只有当有现有规则时才添加(确保只更新,不插入)
priceRules.push(fullPriceRule);
} else {
console.log('未找到现有的统一全场规则,跳过该规则');
}
}
if (vc.component.editCommunitySpaceInfo.bookingType === 'Half') {
let halfPriceRule = {
ruleType: 'UNIFORM_PRICING_HALF',
feeType: '1001',
price: vc.component.editCommunitySpaceInfo.uniformHalfPrice || 0
// 移除ruleName由后端生成
};
// 查找现有规则中的半场规则
let existingHalfRule = existingPriceRules.find(rule => rule.ruleType === '2002');
if (existingHalfRule) {
halfPriceRule.ruleId = existingHalfRule.ruleId;
console.log('找到现有统一半场规则ruleId:', existingHalfRule.ruleId);
// 只有当有现有规则时才添加(确保只更新,不插入)
priceRules.push(halfPriceRule);
} else {
console.log('未找到现有的统一半场规则,跳过该规则');
}
}
}
// 构建请求数据
let requestData = {
spaceId: vc.component.editCommunitySpaceInfo.spaceId,
venueId: vc.component.editCommunitySpaceInfo.venueId,
communityId: vc.component.editCommunitySpaceInfo.communityId,
rules: priceRules
};
console.log('准备发送的价格规则数据:', JSON.stringify(requestData, null, 2));
// 检查是否有规则需要发送
if (priceRules.length === 0) {
console.log('没有价格规则需要保存,直接关闭模态框');
// 如果没有规则需要保存,直接关闭模态框
$('#editCommunitySpaceModel').modal('hide');
vc.emit('communitySpaceManage', 'listCommunitySpace', {});
vc.toast("修改成功");
return;
}
// 发送请求
vc.http.apiPost(
'/communitySpacePriceRule.saveCommunitySpacePriceRules',
JSON.stringify(requestData), {
emulateJSON: true,
headers: {
'Content-Type': 'application/json'
}
},
function (json, res) {
let _json = JSON.parse(json);
if (_json.code == 0) {
//关闭modal
$('#editCommunitySpaceModel').modal('hide');
vc.emit('communitySpaceManage', 'listCommunitySpace', {});
vc.toast("修改成功");
} else {
vc.toast(_json.msg || "价格规则保存失败");
}
},
function (errInfo, error) {
console.log('请求失败处理');
vc.message(errInfo);
});
},
_findExistingRule: function(dayOfWeek, ruleType) {
let existingRules = vc.component.editCommunitySpaceInfo.priceRules;
if (!existingRules || !Array.isArray(existingRules)) {
return null;
}
return existingRules.find(rule => {
if (rule.ruleType !== ruleType) {
return false;
}
if (dayOfWeek) {
// 按天收费规则,需要匹配星期
return rule.dayOfWeek === dayOfWeek ||
(rule.ruleName && rule.ruleName.includes(that._getDayChineseName(dayOfWeek)));
} else {
// 统一收费规则
return !rule.dayOfWeek &&
(rule.ruleName && (rule.ruleName.includes('统一') || rule.ruleName.includes('全场') || rule.ruleName.includes('半场')));
}
});
},
/**
* 根据星期几和规则类型查找价格规则
*/
_findPriceRuleByDay: function(existingPriceRules, dayOfWeek, ruleType) {
let that = this;
// 将星期几转换为大写英文格式(因为规则名称中使用的是英文)
let englishDay = dayOfWeek.toUpperCase();
console.log('查找规则: 星期英文名=' + englishDay + ', 规则类型=' + ruleType);
return existingPriceRules.find(function(rule) {
console.log('检查规则: ruleId=' + rule.ruleId + ', ruleName=' + rule.ruleName + ', ruleType=' + rule.ruleType);
// 1. 首先检查规则类型是否匹配
if (rule.ruleType !== ruleType) {
console.log('规则类型不匹配: 期望=' + ruleType + ', 实际=' + rule.ruleType);
return false;
}
// 2. 检查规则名称是否包含英文星期名称
if (rule.ruleName && rule.ruleName.includes(englishDay)) {
console.log('规则名称匹配成功: ' + rule.ruleName + ' 包含 ' + englishDay);
return true;
}
// 3. 检查规则名称是否包含中文星期名称(兼容性检查)
let chineseDay = that._getDayChineseName(dayOfWeek);
if (rule.ruleName && rule.ruleName.includes('星期' + chineseDay)) {
console.log('规则名称匹配成功(中文): ' + rule.ruleName + ' 包含 星期' + chineseDay);
return true;
}
// 4. 如果有dayOfWeek字段直接比较
if (rule.dayOfWeek && rule.dayOfWeek.toUpperCase() === englishDay) {
console.log('dayOfWeek字段匹配成功: ' + rule.dayOfWeek + ' = ' + englishDay);
return true;
}
console.log('规则不匹配');
return false;
});
},
/**
* 修改收费模式改变事件
*/
_changePricingByDay: function() {
// 在编辑模式下,不允许修改收费模式
if (vc.component.editCommunitySpaceInfo.spaceId) {
// 如果是编辑模式,恢复原来的收费模式
let originalPricingByDay = vc.component.editCommunitySpaceInfo._originalPricingByDay;
if (originalPricingByDay) {
vc.component.editCommunitySpaceInfo.pricingByDay = originalPricingByDay;
}
vc.toast("编辑模式下不允许修改收费模式");
return;
}
// 新增模式下,正常处理收费模式改变
vc.component._initPriceRuleData();
},
/**
* 将日期类型与星期几匹配
*/
_matchDateTypeWithDay: function(dateType, dayOfWeek) {
// 将星期几转换为标准格式
let normalizedDay = dayOfWeek.toUpperCase();
// 匹配周末
if (dateType === 'WEEKEND' && (normalizedDay === 'SATURDAY' || normalizedDay === 'SUNDAY')) {
return true;
}
// 匹配工作日
if (dateType === 'WEEKDAY' &&
(normalizedDay === 'MONDAY' || normalizedDay === 'TUESDAY' ||
normalizedDay === 'WEDNESDAY' || normalizedDay === 'THURSDAY' ||
normalizedDay === 'FRIDAY')) {
return true;
}
// 可以添加其他日期类型匹配逻辑
return false;
},
/**
* 将英文星期转换为中文数字
*/
_getDayChineseName: function(dayOfWeek) {
const dayMap = {
'Monday': '一',
'Tuesday': '二',
'Wednesday': '三',
'Thursday': '四',
'Friday': '五',
'Saturday': '六',
'Sunday': '日',
'MONDAY': '一',
'TUESDAY': '二',
'WEDNESDAY': '三',
'THURSDAY': '四',
'FRIDAY': '五',
'SATURDAY': '六',
'SUNDAY': '日'
};
return dayMap[dayOfWeek] || dayOfWeek;
},
refreshEditCommunitySpaceInfo: function () {
// 保存现有的priceRules数据
const existingPriceRules = vc.component.editCommunitySpaceInfo ? vc.component.editCommunitySpaceInfo.priceRules : null;
vc.component.editCommunitySpaceInfo = {
spaceId: '',
venueId: '',
name: '',
startTime: '',
endTime: '',
bookingType: '',
adminName: '',
tel: '',
state: '',
communityId: '',
pricingByDay: 'N', // 是否按天定义收费,默认为否
uniformFullPrice: 0, // 统一全场价格
uniformHalfPrice: 0, // 统一半场价格
dayPricing: [
{ dayOfWeek: 'Monday', fullPrice: 0, halfPrice: 0 },
{ dayOfWeek: 'Tuesday', fullPrice: 0, halfPrice: 0 },
{ dayOfWeek: 'Wednesday', fullPrice: 0, halfPrice: 0 },
{ dayOfWeek: 'Thursday', fullPrice: 0, halfPrice: 0 },
{ dayOfWeek: 'Friday', fullPrice: 0, halfPrice: 0 },
{ dayOfWeek: 'Saturday', fullPrice: 0, halfPrice: 0 },
{ dayOfWeek: 'Sunday', fullPrice: 0, halfPrice: 0 }
], // 按天定义的价格规则
priceRules: existingPriceRules // 保留现有的priceRules数据
}
}
}
});
})(window.vc, window.vc.component);