酒店住宿,分旺季和非旺季,费用不一致。根据选择的时间来判断所处时间是否在旺季期间,算出旺季时间,非旺季时间,再分别乘旺季酒店 费用和非旺季酒店费用算出酒店费用。这是我写的方法,因为要求只能用es5写,用了不少for循环,没用filter过滤,希望有大佬能看看,帮忙改进一下,精简一下,还存在缺陷。
// 调用方法,可用以下参数,因为后端返回值是时间戳,wjyf可设置酒店旺季的月份,是后端接口返回值。
this.getzsfy(1656950400000, 1665763200000, 1, 10, ‘11’)
getzsfy = (ksrq, jsrq, wjbz, fwjbz, wjyf) => {
let kstime = new Date(ksrq);
let jstime = new Date(jsrq);
let ksmonth = kstime.getMonth() + 1 < 10 ? (kstime.getMonth() + 1) : kstime.getMonth() + 1;
let ksmonth1 = ksmonth
let ksday = kstime.getDate();
let jsmonth = jstime.getMonth() + 1 < 10 ? (jstime.getMonth() + 1) : jstime.getMonth() + 1;
let jsmonth1 = jsmonth
let start = kstime.getFullYear() + '-' + ksmonth
let end = jstime.getFullYear() + '-' + jsmonth
// 用来存选择时间内包含的月份
let result = [];
while (ksmonth1 <= jsmonth1) {
result.push(ksmonth1 + '');
ksmonth1++
}
let jsday = jstime.getDate();
let ksts = 0, jsts = 0
let arr = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
let totalday = (jsrq - ksrq) / (60000 * 60 * 24) + 1
let allday = totalday
console.log('总出差时间===zsfy', allday)
console.log('最初算出天数==zsfy', arr[ksmonth - 1] - ksday, jsday)
let zsfy = 0
let wjyfarr = wjyf.split(',');
let yflist = []
wjyfarr.filter(items => {
console.log('item===zsfy', items, wjyfarr, result, result.indexOf(items))
if (result.indexOf(items) > -1) {
yflist.push(items)
}
})
console.log('月份list===zsf', yflist)
// 判断首月是否包含在筛选出的数组中,算出首月时间
// 算出旺季时间,以总时间-旺季时间=非旺季时间
let wjts = 0
let fwjts = 0
console.log('打印=====zsfy', yflist.indexOf(ksmonth + ''), yflist.indexOf(jsmonth + ''))
if (yflist.indexOf(ksmonth + '') > -1 && yflist.indexOf(jsmonth + '') > -1) {
if (ksmonth !== jsmonth) {
ksts = arr[ksmonth - 1] - ksday;
jsts = jsday;
wjts = ksts + jsts;
let gvts = []
// let gvts = yflist.filter(item => (item !== ksmonth + '' && item !== jsmonth + ''))
for (let i = 0; i < yflist.length; i++) {
console.log('forxunhuan====', yflist[i])
if (yflist[i] !== ksmonth + '' && yflist[i] !== jsmonth + '') {
gvts.push(yflist[i])
}
}
for (let i = 0; i < gvts.length; i++) {
wjts = wjts + arr[gvts[i] - 1];
fwjts = allday - wjts;
console.log('过滤数据中每一月份的天数==zsfy', arr[gvts[i] - 1])
}
console.log('过滤首月和末月的天数===zsfy', gvts, wjts, fwjts)
} else {
wjts = allday
}
console.log('11111==zsfy', ksts, jsts, wjts, fwjts, allday)
} else if (yflist.indexOf(ksmonth + '') > -1 && yflist.indexOf(jsmonth + '') < 0) {
// 过滤掉首月,算后面月份的天数加首月天数
console.log('进入zsfy===首月旺季')
// let gvts = yflist.filter(item => item !== ksmonth + '')
let gvts = []
for (let i = 0; i < yflist.length; i++) {
console.log('forxunhuan====', yflist[i])
if (yflist[i] !== ksmonth + '' && yflist[i] !== jsmonth + '') {
gvts.push(yflist[i])
}
}
console.log('gvts===zsfy', gvts)
wjts = arr[ksmonth - 1] - ksday;
fwjts = allday - wjts
for (let i = 0; i < gvts.length; i++) {
wjts = wjts + arr[gvts[i] - 1];
fwjts = allday - wjts;
console.log('过滤数据中每一月份的天数==zsfy', arr[gvts[i] - 1])
}
console.log('gvts====zsfy', ksts, gvts, wjts, fwjts, allday)
} else if (yflist.indexOf(ksmonth + '') < 0 && yflist.indexOf(jsmonth + '') > -1) {
// 过滤掉末月,算前面月份的天数加末月天数
console.log('进入zsfy11')
// let gvts = yflist.filter(item => item !== jsmonth + '')
let gvts = []
for (let i = 0; i < yflist.length; i++) {
console.log('forxunhuan====', yflist[i])
if (yflist[i] !== ksmonth + '' && yflist[i] !== jsmonth + '') {
gvts.push(yflist[i])
}
}
wjts = jsday;
fwjts = allday - wjts;
for (let i = 0; i < gvts.length; i++) {
wjts = wjts + arr[gvts[i] - 1];
fwjts = allday - wjts;
console.log('过滤数据中每一月份的天数==zsfy11', arr[gvts[i] - 1])
}
console.log('gvts====zsfy111', jsday, gvts, wjts, fwjts, allday)
} else {
console.log('yflist===zsfy', yflist)
fwjts = allday - wjts
for (let i = 0; i < yflist.length; i++) {
wjts = wjts + arr[yflist[i] - 1];
fwjts = allday - wjts;
console.log('zsfy===111', arr[yflist[i] - 1], wjts, fwjts, allday)
}
}
zsfy = wjts * wjbz + fwjts * fwjbz
console.log('旺季标准===非旺季==zsfy', wjbz, fwjbz, wjts, fwjts)
console.log('旺季费用====zsfy', zsfy)
return zsfy
}