'use strict';
exports.main = async (event, context) => {
	//event为客户端上传的参数
	const db = uniCloud.database()
	let res = await db.collection("standardV2").field({"classify_id": true, "system_id": true, "brand_id": true}).get();
	let data = {}
	let id = []
	res.data.map(item => {
		id.push(item.classify_id)
		id.push(item.system_id)
		id.push(item.brand_id)
		if (data[item.classify_id]) {
			if (data[item.classify_id][item.system_id]) {
				if (data[item.classify_id][item.system_id].indexOf(item.brand_id) < 0)
					data[item.classify_id][item.system_id].push(item.brand_id)
			} else {
				data[item.classify_id][item.system_id] = [item.brand_id]
			}
		} else {
			let obj = {}
			obj[item.system_id] = [item.brand_id]
			data[item.classify_id] = obj
		}
	})
	let dic = await db.collection("dictionary").where({
		_id: db.command.in(id)
	}).get()
	let dic_data = {}
	dic.data.map(item => {
		dic_data[item._id] = item.name
	})
	let result = []
	for (let first in data) {
		let secondArr = []
		for (let second in data[first]) {
			let thirdArr = []
			data[first][second].map(third => {
				thirdArr.push({id: third, name: dic_data[third]})
			})
			secondArr.push({id: second, name: dic_data[second], child: thirdArr})
		}
		result.push({id: first, name: dic_data[first], child: secondArr})
	}
	return result;
};
