import CachePromise from '../../utils/cachepromise'; import { httpget } from "../../utils/request"; import apiurl from "../apiurl"; import { Wata1Promise, Wata2Promise, Wata3Promise } from './base'; function sortWsTree(roots) { const result = roots.sort((a, b) => { const lenA = a.children ? a.children.length : 0; const lenB = b.children ? b.children.length : 0; return lenB - lenA; }); for (const elem of roots) { elem.children = sortWsTree(elem.children); } return result; } export const WsDataPromise = new CachePromise( () => httpget(apiurl.ws.list).then(({ data }) => data).catch(() => null), 0); export async function wsDataEx() { const dataList = await WsDataPromise.get(); if (!dataList) { return {}; } const dataMap = {}; const ws2WataMap = {}; dataList.forEach(o => { dataMap[o.WSCD] = o; o.parents = []; }); dataList.forEach(o => { let c = o; while (c.PWSCD) { if (c.WSCD === c.PWSCD || !dataMap[c.PWSCD]) { console.log(JSON.stringify(c)); break; } if (o.parents.length > 5) { break; } o.parents.push(c.PWSCD); c = dataMap[c.PWSCD]; } }); dataList.forEach((o) => { if (o.WSTP !== 5) { return; } let curObj = o; let tryNumber = 10; // 避免死循环 while (curObj && curObj.PWSCD && --tryNumber) { ws2WataMap[curObj.PWSCD] = ws2WataMap[curObj.PWSCD] || []; ws2WataMap[curObj.PWSCD].push(o.WSCD); curObj = dataMap[curObj.PWSCD]; } }); return { dataMap, ws2WataMap, dataList }; } export async function wsDataTree() { const wsList = await WsDataPromise.get(); if (!wsList) { return []; } const dataList = wsList .map(({ WSCD, WSTP, NAME, PWSCD }) => ({ pId: PWSCD, WSCD, key: WSCD, title: NAME || WSCD, children: [], WSTP })); const dataMap = {}; dataList.forEach((o) => { dataMap[o.WSCD] = o; }); dataList.forEach((o) => { dataMap[o.pId] && dataMap[o.pId].children.push(o); }); let rootList = dataList.filter(o => o.WSTP === 1); if (rootList && rootList.length > 0) { rootList = sortWsTree(rootList); } return rootList; } export const WsDataExPromise = new CachePromise(wsDataEx, 0); export const WsDataTreePromise = new CachePromise(wsDataTree, 0); export async function wataGet(wscd) { const { data } = await httpget(apiurl.ws.wataGet, { wscd }) || {}; return data; } export async function wataUGet(index, WSCD) { const wataulistPromise = [null, Wata1Promise, Wata2Promise, Wata3Promise][index]; if (!wataulistPromise) { return null; } const data = await wataulistPromise.get(); if (!data) { return null; } const feature = data?.features?.find(o => o.properties.WSCD === WSCD); if (!feature) { return null; } return { ...feature.properties, geometry: feature.geometry }; } export async function wsStatIndex(wscd) { const { data } = await httpget(apiurl.ws.statIndex, { wscd }) || {}; if (!data) { //message.error('取得小流域统计列表失败'); return; } return data; } export async function wsStat(type, wscd) { const { data } = await httpget(apiurl.ws.stat, { type, wscd }) || {}; if (!data) { //message.error('取得小流域统计列表失败'); return; } return data; }