123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- const mongoose = require('mongoose');
- const uuidV1 = require('uuid/v1');
- 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');
- async function getLibs(query) {
- return await priceInfoLibModel.find(query).lean();
- }
- async function createLib(name, period, compilationID) {
- // 将2020-01变成2020年01月
- const reg = /(\d{4})-(\d{2})/;
- const formattedPeriod = period.replace(reg, '$1年$2月');
- const lib = {
- ID: uuidV1(),
- name,
- period: formattedPeriod,
- compilationID,
- createDate: Date.now(),
- };
- await priceInfoLibModel.create(lib);
- return lib;
- }
- async function updateLib(query, updateData) {
- await priceInfoLibModel.update(query, updateData);
- }
- async function deleteLib(libID) {
- await priceInfoClassModel.remove({ libID });
- await priceInfoItemModel.remove({ libID });
- await priceInfoLibModel.remove({ ID: libID });
- }
- // 爬取数据
- async function crawlDataByCompilation(compilationID, from, to) {
- if (!compilationID) {
- throw '无有效费用定额。';
- }
- const compilationData = await compilationModel.findOne({ _id: mongoose.Types.ObjectId(compilationID) }, 'overWriteUrl').lean();
- if (!compilationData || !compilationData.overWriteUrl) {
- throw '无有效费用定额。';
- }
- // 从overWriteUrl提取并组装爬虫文件
- const reg = /\/([^/]+)\.js/;
- const matched = compilationData.overWriteUrl.match(reg);
- const crawlURL = `${matched[1]}_price_crawler.js`;
- let crawlData;
- try {
- const crawler = require(`../../../web/over_write/js/${crawlURL}`);
- crawlData = crawler.crawlData;
- } catch (e) {
- throw '该费用定额无可用爬虫方法。'
- }
- await crawlData(from, to);
- }
- // 获取费用定额的地区数据
- async function getAreas(compilationID) {
- return await priceInfoAreaModel.find({ compilationID }, '-_id ID name').lean();
- }
- async function updateAres(updateData) {
- const bulks = [];
- updateData.forEach(({ ID, name }) => bulks.push({
- updateOne: {
- filter: { ID },
- update: { name }
- }
- }));
- if (bulks.length) {
- await priceInfoAreaModel.bulkWrite(bulks);
- }
- }
- async function insertAreas(insertData) {
- await priceInfoAreaModel.insertMany(insertData);
- }
- async function deleteAreas(deleteData) {
- await priceInfoAreaModel.remove({ ID: { $in: deleteData } });
- }
- async function getClassData(libID, areaID) {
- return await priceInfoClassModel.find({ libID, areaID }, '-_id').lean();
- }
- async function getPriceData(classID) {
- return await priceInfoItemModel.find({ classID }, '-_id').lean();
- }
- async function editPriceData(postData) {
- const bulks = [];
- postData.forEach(data => {
- if (data.type === 'update') {
- bulks.push({
- updateOne: {
- filter: { ID: data.ID },
- update: { ...data.data }
- }
- });
- } else if (data.type === 'delete') {
- bulks.push({
- deleteOne: {
- filter: { ID: data.ID }
- }
- });
- } else {
- bulks.push({
- insertOne: {
- document: data.data
- }
- });
- }
- });
- if (bulks.length) {
- await priceInfoItemModel.bulkWrite(bulks);
- }
- }
- module.exports = {
- getLibs,
- createLib,
- updateLib,
- deleteLib,
- crawlDataByCompilation,
- getAreas,
- updateAres,
- insertAreas,
- deleteAreas,
- getClassData,
- getPriceData,
- editPriceData
- }
|