|
@@ -808,6 +808,96 @@ async function batchUpdate(priceItem, prop, val) {
|
|
|
|
|
|
}
|
|
|
|
|
|
+// 根据期数范围,获取期数数据
|
|
|
+function getPeriodData(from, to) {
|
|
|
+ const monthMap = {
|
|
|
+ '1': '01月',
|
|
|
+ '2': '02月',
|
|
|
+ '3': '03月',
|
|
|
+ '4': '04月',
|
|
|
+ '5': '05月',
|
|
|
+ '6': '06月',
|
|
|
+ '7': '07月',
|
|
|
+ '8': '08月',
|
|
|
+ '9': '09月',
|
|
|
+ '10': '10月',
|
|
|
+ '11': '11月',
|
|
|
+ '12': '12月',
|
|
|
+ };
|
|
|
+ if (from > to) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ const reg = /(\d+)-(\d+)/;
|
|
|
+ const fromMatch = from.match(reg);
|
|
|
+ const fromYear = +fromMatch[1];
|
|
|
+ const fromMonth = +fromMatch[2];
|
|
|
+ const toMatch = to.match(reg);
|
|
|
+ const toYear = +toMatch[1];
|
|
|
+ const toMonth = +toMatch[2];
|
|
|
+ let curYear = fromYear;
|
|
|
+ let curMonth = fromMonth;
|
|
|
+ const periods = [];
|
|
|
+ while ((curYear <= toYear && curMonth <= toMonth) || curYear < toYear) {
|
|
|
+ periods.push(`${curYear}年-${monthMap[curMonth]}`);
|
|
|
+ if (curMonth === 12) {
|
|
|
+ curYear++;
|
|
|
+ curMonth = 1;
|
|
|
+ } else {
|
|
|
+ curMonth++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return periods;
|
|
|
+}
|
|
|
+
|
|
|
+// 导出浙江信息价excel数据
|
|
|
+async function getZheJiangSheetData(from, to) {
|
|
|
+ const periods = getPeriodData(from, to);
|
|
|
+ if (!periods) {
|
|
|
+ throw '无效的期数区间。';
|
|
|
+ }
|
|
|
+ const compilationID = '5de61133d46f6f000d15d347';
|
|
|
+ const libs = await priceInfoLibModel.find({ compilationID, period: { $in: periods } }, '-_id ID name period').lean();
|
|
|
+ console.log(libs);
|
|
|
+ const areas = await priceInfoAreaModel.find({ compilationID }, '-_id ID name').lean();
|
|
|
+ const zjAreas = areas.filter(area => /浙江/.test(area.name) && /全市平均/.test(area.name));
|
|
|
+ // 地区按照serialNo排序
|
|
|
+ zjAreas.sort((a, b) => a.serialNo - b.serialNo);
|
|
|
+ const areaIDs = zjAreas.map(area => area.ID);
|
|
|
+ const libIDs = libs.map(lib => lib.ID);
|
|
|
+ const allPriceItems = await priceInfoItemModel.find({ compilationID, areaID: { $in: areaIDs }, libID: { $in: libIDs } }, '-_id libID areaID code name noTaxPrice').lean();
|
|
|
+ // 材料大类配置
|
|
|
+ const priceClasses = [
|
|
|
+ { name: '水泥', codes: ['5509003', '5509001', '5509002', '5509006'] },
|
|
|
+ { name: '钢材', codes: ['2001006', '2003004', '2003008', '2001001', '2001008'] },
|
|
|
+ { name: '砂石料', codes: ['5503005', '5503007', '5503013', '5503014', '5505005', '5505016', '5505025', '5505012', '5505013', '1516001', '5503006', '5505019001', '5505019'] },
|
|
|
+ ]
|
|
|
+ const excelData = [['日期', '地区', '材料大类', '编码', '名称', '价格']];
|
|
|
+ libs.forEach(lib => {
|
|
|
+ // 日期
|
|
|
+ excelData.push([lib.period || '', '', '', '', '', '']);
|
|
|
+ zjAreas.forEach(area => {
|
|
|
+ // 地区
|
|
|
+ const areaName = area.name.replace('浙江省-', '').replace('全市平均', '');
|
|
|
+ excelData.push(['', areaName || '', '', '', '', '']);
|
|
|
+ // 材料大类
|
|
|
+ let priceItems = allPriceItems.filter(item => item.libID === lib.ID && item.areaID === area.ID);
|
|
|
+ priceItems = _.sortBy(priceItems, 'code');
|
|
|
+ priceClasses.forEach(priceClass => {
|
|
|
+ excelData.push(['', '', priceClass.name || '', '', '', '']);
|
|
|
+ priceItems.forEach(item => {
|
|
|
+ const code = item.code ? item.code.trim() : '';
|
|
|
+ const name = item.name ? item.name.trim() : '';
|
|
|
+ const price = item.noTaxPrice ? +item.noTaxPrice : '';
|
|
|
+ if (priceClass.codes.includes(code)) {
|
|
|
+ excelData.push(['', '', '', code, name, price]);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+ });
|
|
|
+ });
|
|
|
+ return excelData;
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
module.exports = {
|
|
|
getLibs,
|
|
@@ -835,4 +925,5 @@ module.exports = {
|
|
|
exportInfoPriceByCompilation,
|
|
|
getAllLibs,
|
|
|
batchUpdate,
|
|
|
+ getZheJiangSheetData
|
|
|
}
|