/** * Created by zhang on 2020/7/20 */ module.exports={ getOptions, getDataByCondition, mutiApplyInfoPrice }; const mongoose = require('mongoose'); let infoLibModel = mongoose.model("std_price_info_lib"); let infoItemsModel = mongoose.model("std_price_info_items"); let infoAreasModel = mongoose.model("std_price_info_areas"); let unitPriceModel = mongoose.model("unit_price"); let _ = require("lodash"); let gljUtil = require('../../../public/web/gljUtil'); const scMathUtil = require('../../../public/scMathUtil').getUtil(); // 载入模块 var Segment = require('segment'); // 创建实例 var segment = new Segment(); // 使用默认的识别模块及字典,载入字典文件需要1秒,仅初始化时执行一次即可 segment.useDefault(); async function getOptions(data,compilation){//data 是预留对象,暂时不用 let compilationID = compilation._id; let areaMap={}; let periodMap={}; let areas =await infoAreasModel.find({"compilationID":compilationID}).lean(); let libList = await infoLibModel.find({"compilationID":compilationID}).lean(); for(let l of libList){ // for(let area of l.areas){ // if(!areaMap[area]) areas.push(area); // } //2020-05 let periodArray = l.period.split("-"); periodMap[periodArray[0]]?periodMap[periodArray[0]].push(periodArray[1]):periodMap[periodArray[0]]=[periodArray[1]] } for(let key in periodMap){ periodMap[key] = _.sortBy(periodMap[key]); } return {areas:areas,periodMap:periodMap} } async function getDataByCondition(data,compilation){ let result = {}; data.condition["compilationID"] = compilation._id; //特殊处理重庆的,地区选择非“通用”时,搜索范围应是当前选择的地区,加上“通用”中的信息价。 if (data.condition.commonInfoPriceID) { let idArray = [data.condition.areaID,data.condition.commonInfoPriceID]; data.condition.areaID = {$in: idArray} delete data.condition.commonInfoPriceID; } //根据地区+期数+材料编号的前4位与信息价材料的分类编号匹配,如果有数据,则显示数据出来。 //先按编号匹配 if (data.code) { result = await getDataByCode(data.code, data); if (result.totalSize > 0) return result; } //编号匹配不上的情况: //有关键字的情况 if (data.keyWord) { return await getDataByKeyWord(data.keyWord,data); } //查询所有的情况 if(data.lastID){ //有最后一行说明是查询下一页 data.condition["_id"] = {$gt:mongoose.Types.ObjectId(data.lastID)}; }else{ result.totalSize = await infoItemsModel.find(data.condition).count(); } result.items = await infoItemsModel.find(data.condition).lean().sort({"_id":1}).limit(50); return result; } async function getDataByCode(code, data) { let condition = { ...data.condition }; condition.code = code; let totalSize = await infoItemsModel.find(condition).count(); if (data.lastID) { //有最后一行说明是查询下一页 condition["_id"] = {$gt:mongoose.Types.ObjectId(data.lastID)}; } let items = []; if (totalSize > 0) { items = await infoItemsModel.find(condition).lean().sort({"_id":1}).limit(50); } return {totalSize,items} } async function getDataByKeyWord(keyword, data) { let items = []; let nameArray = []; //混凝土 和 砼 认成一个 // keyword = keyword.replace(/混凝土/g, "砼"); if (keyword.length < 3) { nameArray.push(keyword) } else { nameArray = segment.doSegment(keyword, { simple: true, //不返回词性 stripPunctuation: true //去除标点符号 }); } let temArr = []; for (let a of nameArray) { if (a == "混凝土") a = '砼'; if (a == '砼' || a.length > 1) temArr.push(a); } if (keyword.length == 1 && temArr.length == 0) temArr.push(keyword); nameArray = temArr; console.log(nameArray); let allInfoPrice = await infoItemsModel.find(data.condition).lean().sort({"_id":1}); let maxNum = 0;//最大匹配数 let matchMap = {};//匹配储存 for (let info of allInfoPrice) { //specs let mstring = info.name + info.spec; mstring = mstring.replace(/混凝土/g, "砼"); let matchCount = 0; for (let na of nameArray) { if (mstring.indexOf(na) != -1) { matchCount++; } } if (matchCount > 0) { matchMap[matchCount] ? matchMap[matchCount].push(info) : matchMap[matchCount] = [info]; if (matchCount > maxNum) maxNum = matchCount; } } if (maxNum > 0) items = matchMap[maxNum]; totalSize = items.length return {totalSize,items} } async function mutiApplyInfoPrice(data,compilation){ data.condition["compilationID"] = compilation._id; let infoPrices = await infoItemsModel.find(data.condition).lean(); let tasks = []; let projectGLJMap = {}; for(let info of infoPrices){ let index = gljUtil.getIndex(info,["name","specs","unit"]); if(data.pgljMap[index]){ for(let obj of data.pgljMap[index]){ let infoPrice = gljUtil.getInfoMarketPrice(info,data.taxType); infoPrice = scMathUtil.roundToString(infoPrice,data.decimal); let doc = {'market_price':infoPrice,'priceFrom':data.priceFrom}; let task = { updateOne:{ filter:{'id':obj.unitPriceID}, update:doc } }; tasks.push(task); projectGLJMap[obj.pgljID] = {index:obj.fullIndex,doc:doc}; } } } if(tasks.length > 0) await unitPriceModel.bulkWrite(tasks); return projectGLJMap; }