277 lines
9.0 KiB
TypeScript
277 lines
9.0 KiB
TypeScript
import { getCommunitylistAPI, getVillageByIdAPI } from '@/api/system/community';
|
|
import { communitylist_rows_type } from '@/api/system/community/type';
|
|
import { deleteHouseApi, getBuildinglistAPI, getHouselistAPI, getVillageTree } from '@/api/system/house';
|
|
import type { addBuildingType, BuildingUnit, BuildingVo } from '@/api/system/house/type';
|
|
import { convertBuildingToTree } from '@/utils/house';
|
|
import { defineStore } from 'pinia';
|
|
// 数字排序
|
|
function sortNumberStr(a: string, b: string) {
|
|
const numA = parseFloat(a);
|
|
const numB = parseFloat(b);
|
|
|
|
// 如果都是有效数字 → 按数字大小排(支持负数、小数)
|
|
if (!isNaN(numA) && !isNaN(numB)) {
|
|
return numA - numB;
|
|
}
|
|
|
|
// 否则按自然语言排
|
|
return a.localeCompare(b, 'zh-CN', { numeric: true });
|
|
}
|
|
export const useHouseStore = defineStore(
|
|
'house',
|
|
() => {
|
|
// 当前小区信息
|
|
const currentVilageInfo = ref<communitylist_rows_type>();
|
|
// 住宅类型
|
|
const buildingtype = ref<string>('0');
|
|
// 住宅列表
|
|
const buildingtypelist = ref([]);
|
|
// 房屋树状结构
|
|
const treedata = ref([]);
|
|
|
|
// 当前小区 所有楼栋信息
|
|
const currentBuildingInfoList = ref<BuildingVo[]>([]);
|
|
// 当前楼栋信息
|
|
const currentBuildingInfo = ref<BuildingVo>({
|
|
'id': null,
|
|
'villageId': null,
|
|
'villageName': '',
|
|
'buildingName': '',
|
|
'buildingUse': null,
|
|
'buildToward': '',
|
|
'buildTall': '',
|
|
'buildStructure': '',
|
|
'unitCount': null,
|
|
'floorCount': null,
|
|
'remark': ''
|
|
});
|
|
|
|
// 当前楼栋 所有房屋信息
|
|
const currentHouseInfoList = ref<BuildingUnit>(null);
|
|
|
|
// 当前楼栋 所有楼层数据
|
|
const currentFloorInfoList = computed(() => {
|
|
if (floorslist.value.size === 0) {
|
|
return [];
|
|
}
|
|
return [...floorslist.value].sort(sortNumberStr);
|
|
});
|
|
// 楼层列表 set
|
|
const floorslist = ref<Set<string>>(new Set());
|
|
|
|
//
|
|
// 查找当前小区
|
|
const getviliageinfo = async (id) => {
|
|
const res = await getVillageByIdAPI(id);
|
|
currentVilageInfo.value = res.data;
|
|
return res.data;
|
|
};
|
|
|
|
// 更新小区信息
|
|
const updateVilageinfo = (data: communitylist_rows_type) => {
|
|
currentVilageInfo.value = data;
|
|
};
|
|
|
|
// 初始化
|
|
const initHouse = async (currentbuildingtype: string) => {
|
|
currentBuildingInfoList.value = [];
|
|
// 获取当前小区 所有楼栋信息
|
|
const res2 = await getBuildinglistAPI(currentbuildingtype);
|
|
currentBuildingInfoList.value = res2.data;
|
|
|
|
// 获取当前小区 第一个楼栋信息
|
|
// ! 不同住宅类型
|
|
if ((currentBuildingInfo.value && currentBuildingInfo.value.id === null) || buildingtype.value !== currentbuildingtype) {
|
|
currentBuildingInfo.value = res2.data[0];
|
|
// ! 相同住宅类型
|
|
} else if (buildingtype.value === currentbuildingtype) {
|
|
// 更新当前楼栋的 信息
|
|
currentBuildingInfo.value = res2.data.find((item) => item.id === currentBuildingInfo.value.id);
|
|
}
|
|
|
|
if (currentBuildingInfo.value && currentBuildingInfo.value.id) {
|
|
// 获取当前 第一个楼栋的所有信息
|
|
const res3 = await getHouselistAPI(currentBuildingInfo.value.id);
|
|
currentHouseInfoList.value = res3.data;
|
|
floorslist.value = new Set();
|
|
// 得到所有的有房屋的 楼层 数组
|
|
currentHouseInfoList.value.units.forEach((item) => {
|
|
item.floors.forEach((floor) => {
|
|
floorslist.value.add(floor.floorNo);
|
|
});
|
|
});
|
|
if (buildingtype.value !== currentbuildingtype) {
|
|
buildingtype.value = currentbuildingtype;
|
|
}
|
|
}
|
|
};
|
|
|
|
// 选中的房屋
|
|
const checkoutHouses = ref<string[]>([]);
|
|
// 选中的房屋方法
|
|
const checkoutHousesFun = (data: string[]) => {
|
|
checkoutHouses.value = data;
|
|
};
|
|
|
|
// 切换住宅类型
|
|
const switchHouseType = async (housetype: string) => {
|
|
// 清空
|
|
currentBuildingInfoList.value = [];
|
|
// 获取当前小区 所有楼栋信息
|
|
const res2 = await getBuildinglistAPI(housetype);
|
|
currentBuildingInfoList.value = res2.data;
|
|
// 获取当前小区 第一个楼栋信息
|
|
if ((currentBuildingInfo.value && currentBuildingInfo.value.id === null) || buildingtype.value !== housetype) {
|
|
if (res2.data.length > 0) {
|
|
currentBuildingInfo.value = res2.data[0];
|
|
} else {
|
|
currentBuildingInfo.value = {
|
|
'id': null,
|
|
'villageId': null,
|
|
'villageName': '',
|
|
'buildingName': '',
|
|
'buildingUse': null,
|
|
'buildToward': '',
|
|
'buildTall': '',
|
|
'buildStructure': '',
|
|
'unitCount': null,
|
|
'floorCount': null,
|
|
'remark': ''
|
|
};
|
|
}
|
|
}
|
|
if (currentBuildingInfo.value.id !== null) {
|
|
switchBuilding(currentBuildingInfo.value.id);
|
|
} else {
|
|
floorslist.value = new Set();
|
|
}
|
|
buildingtype.value = housetype;
|
|
ElMessage.success('切换成功');
|
|
};
|
|
|
|
// 更新当前楼栋信息
|
|
const updateCureentBuilding = async () => {
|
|
currentHouseInfoList.value = null;
|
|
// 获取当前 楼栋的所有信息
|
|
const res3 = await getHouselistAPI(currentBuildingInfo.value.id);
|
|
currentHouseInfoList.value = res3.data;
|
|
floorslist.value = new Set();
|
|
// 得到所有的有房屋的 楼层 数组
|
|
currentHouseInfoList.value.units.forEach((item) => {
|
|
item.floors.forEach((floor) => {
|
|
floorslist.value.add(floor.floorNo);
|
|
});
|
|
});
|
|
};
|
|
// 根据房屋号,查询房屋
|
|
async function getHouseBybuildingNo(houseName: string) {
|
|
currentHouseInfoList.value = null;
|
|
// 获取当前 楼栋的所有信息
|
|
const res3 = await getHouselistAPI(currentBuildingInfo.value.id, houseName);
|
|
currentHouseInfoList.value = res3.data;
|
|
floorslist.value = null;
|
|
floorslist.value = new Set();
|
|
// 得到所有的有房屋的 楼层 数组
|
|
currentHouseInfoList.value.units.forEach((item) => {
|
|
item.floors.forEach((floor) => {
|
|
floorslist.value.add(floor.floorNo);
|
|
});
|
|
});
|
|
}
|
|
// 切换楼栋
|
|
const switchBuilding = (buildingId: string) => {
|
|
if (!buildingId) return;
|
|
// 切换之前 清空当前楼栋信息
|
|
currentBuildingInfo.value = {
|
|
'id': null,
|
|
'villageId': null,
|
|
'villageName': '',
|
|
'buildingName': '',
|
|
'buildingUse': null,
|
|
'buildToward': '',
|
|
'buildTall': '',
|
|
'buildStructure': '',
|
|
'unitCount': null,
|
|
'floorCount': null,
|
|
'remark': ''
|
|
};
|
|
currentHouseInfoList.value = null;
|
|
getHouselistAPI(buildingId).then((res) => {
|
|
currentHouseInfoList.value = res.data;
|
|
// 获取当前楼栋信息并 赋值
|
|
currentBuildingInfo.value = currentBuildingInfoList.value.find((item) => item.id === buildingId);
|
|
|
|
// 清空 楼层信息
|
|
floorslist.value.clear();
|
|
// 重新获取楼层信息
|
|
currentHouseInfoList.value.units.forEach((item) => {
|
|
item.floors.forEach((floor) => {
|
|
floorslist.value.add(floor.floorNo);
|
|
});
|
|
});
|
|
return;
|
|
});
|
|
};
|
|
|
|
// 更新数据
|
|
const updateHouse = () => {
|
|
initHouse(buildingtype.value);
|
|
};
|
|
// 删除指定楼栋
|
|
const deleteBuildingFun = (buildingId: string) => {
|
|
// 手动 从 楼栋列表中删除 指定楼栋
|
|
currentBuildingInfoList.value = currentBuildingInfoList.value.filter((item) => item.id !== buildingId);
|
|
// 判断指定楼栋是 当前楼栋
|
|
if (currentBuildingInfo.value.id === buildingId) {
|
|
// 获取楼栋列表中的第一个楼栋,并切换到第一个楼栋
|
|
if (currentBuildingInfoList.value[0]) {
|
|
currentBuildingInfo.value = currentBuildingInfoList.value[0];
|
|
switchBuilding(currentBuildingInfo.value.id);
|
|
}
|
|
}
|
|
};
|
|
|
|
// 房屋 树状结构类型
|
|
const getCurrentVilageTreeHouse = async () => {
|
|
const villageid = localStorage.getItem('villageid');
|
|
try {
|
|
treedata.value = [];
|
|
const res = await getVillageTree(villageid);
|
|
treedata.value = convertBuildingToTree(res.data.buildings || []);
|
|
return treedata.value;
|
|
} catch (err) {
|
|
console.error('获取楼栋树失败:', err);
|
|
treedata.value = [];
|
|
return treedata.value;
|
|
}
|
|
};
|
|
return {
|
|
currentVilageInfo,
|
|
currentBuildingInfoList,
|
|
currentBuildingInfo,
|
|
currentHouseInfoList,
|
|
currentFloorInfoList,
|
|
checkoutHouses,
|
|
buildingtype,
|
|
buildingtypelist,
|
|
treedata,
|
|
getHouseBybuildingNo,
|
|
updateVilageinfo,
|
|
updateCureentBuilding,
|
|
getviliageinfo,
|
|
checkoutHousesFun,
|
|
initHouse,
|
|
switchHouseType,
|
|
switchBuilding,
|
|
updateHouse,
|
|
deleteBuildingFun,
|
|
getCurrentVilageTreeHouse
|
|
};
|
|
},
|
|
{
|
|
persist: {
|
|
storage: sessionStorage
|
|
}
|
|
}
|
|
);
|