|
@@ -1,11 +1,13 @@
|
|
|
const mongoose = require('mongoose');
|
|
|
const uuidV1 = require('uuid/v1');
|
|
|
+const { CRAWL_LOG_KEY, ProcessStatus } = require('../../../public/constants/price_info_constant');
|
|
|
|
|
|
const priceInfoLibModel = mongoose.model('std_price_info_lib');
|
|
|
const priceInfoClassModel = mongoose.model('std_price_info_class');
|
|
|
const priceInfoItemModel = mongoose.model('std_price_info_items');
|
|
|
const priceInfoAreaModel = mongoose.model('std_price_info_areas');
|
|
|
const compilationModel = mongoose.model('compilation');
|
|
|
+const importLogsModel = mongoose.model('import_logs');
|
|
|
|
|
|
async function getLibs(query) {
|
|
|
return await priceInfoLibModel.find(query).lean();
|
|
@@ -36,6 +38,19 @@ async function deleteLib(libID) {
|
|
|
await priceInfoLibModel.remove({ ID: libID });
|
|
|
}
|
|
|
|
|
|
+async function processChecking(key) {
|
|
|
+ const logData = key
|
|
|
+ ? await importLogsModel.findOne({ key })
|
|
|
+ : await importLogsModel.findOne({ key: CRAWL_LOG_KEY });
|
|
|
+ if (!logData) {
|
|
|
+ return { status: ProcessStatus.FINISH };
|
|
|
+ }
|
|
|
+ if (logData.status === ProcessStatus.FINISH || logData.status === ProcessStatus.ERROR) {
|
|
|
+ await importLogsModel.remove({ key: logData.key });
|
|
|
+ }
|
|
|
+ return { status: logData.status, errorMsg: logData.errorMsg || '', key: logData.key };
|
|
|
+}
|
|
|
+
|
|
|
// 爬取数据
|
|
|
async function crawlDataByCompilation(compilationID, from, to) {
|
|
|
if (!compilationID) {
|
|
@@ -56,14 +71,36 @@ async function crawlDataByCompilation(compilationID, from, to) {
|
|
|
} catch (e) {
|
|
|
throw '该费用定额无可用爬虫方法。'
|
|
|
}
|
|
|
- await crawlData(from, to);
|
|
|
+ //await crawlData(from, to);
|
|
|
+ // 异步不等结果,结果由checking来获取
|
|
|
+ crawlDataByMiddleware(crawlData, from, to);
|
|
|
+}
|
|
|
+
|
|
|
+// 爬取数据中间件,主要处理checking初始化
|
|
|
+async function crawlDataByMiddleware(crawlFunc, from, to) {
|
|
|
+ const logUpdateData = { status: ProcessStatus.FINISH };
|
|
|
+ try {
|
|
|
+ const logData = {
|
|
|
+ key: CRAWL_LOG_KEY,
|
|
|
+ content: '正在爬取数据,请稍候……',
|
|
|
+ status: ProcessStatus.START,
|
|
|
+ create_time: Date.now()
|
|
|
+ };
|
|
|
+ await importLogsModel.create(logData);
|
|
|
+ await crawlFunc(from, to);
|
|
|
+ } catch (err) {
|
|
|
+ logUpdateData.errorMsg = err;
|
|
|
+ logUpdateData.status = ProcessStatus.ERROR;
|
|
|
+ } finally {
|
|
|
+ await importLogsModel.update({ key: CRAWL_LOG_KEY }, logUpdateData);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 导入excel数据,格式如下
|
|
|
// 格式1:
|
|
|
-//地区 分类 编码 名称 规格型号 不含税价 含税价
|
|
|
-//江北区 黑色及有色金属 热轧光圆钢筋 φ6(6.5) 3566.37 4030
|
|
|
-//江北区 木、竹材料及其制品 柏木门套线 60×10 8.76 9.9
|
|
|
+//地区 分类 编码 名称 规格型号 单位 不含税价 含税价
|
|
|
+//江北区 黑色及有色金属 热轧光圆钢筋 φ6(6.5) 3566.37 4030
|
|
|
+//江北区 木、竹材料及其制品 柏木门套线 60×10 8.76 9.9
|
|
|
// 格式2:
|
|
|
//地区 分类 编码 名称 规格型号 不含税价 含税价
|
|
|
//江北区 黑色及有色金属 热轧光圆钢筋 φ6(6.5) 3566.37 4030
|
|
@@ -72,7 +109,6 @@ async function crawlDataByCompilation(compilationID, from, to) {
|
|
|
//
|
|
|
//北碚区 木、竹材料及其制品 热轧光圆钢筋 φ6(6.5) 3566.37 4030
|
|
|
async function importExcelData(libID, sheetData) {
|
|
|
- console.log(sheetData);
|
|
|
const libs = await getLibs({ ID: libID });
|
|
|
const compilationID = libs[0].compilationID;
|
|
|
// 建立区映射表:名称-ID映射、ID-名称映射
|
|
@@ -292,6 +328,7 @@ module.exports = {
|
|
|
createLib,
|
|
|
updateLib,
|
|
|
deleteLib,
|
|
|
+ processChecking,
|
|
|
crawlDataByCompilation,
|
|
|
importExcelData,
|
|
|
getAreas,
|