/** * 单价文件显示业务模型(用于选择单价文件操作) * * @author CaiAoLin * @date 2017/7/5 * @version */ const mongoose = require("mongoose"); const BaseModel = require("../../common/base/base_model"); const CounterModel = require("./counter_model"); let collectionName = 'unit_price_file'; let Projects = mongoose.model('projects'); class UnitPriceFileModel extends BaseModel { /** * 构造函数 * * @return {void} */ constructor() { let parent = super(); parent.model = mongoose.model(collectionName); parent.init(); } /** * 设置场景 * * @param {string} scene * @return {void} */ setScene(scene = '') { switch (scene) { // 新增数据的验证规则 case 'add': /* this.model.schema.path('name').required(true); this.model.schema.path('project_id').required(true);*/ break; } } /** * 新增单价文件 * * @param {object} data * @return {Promise} */ async add(data) { let result = false; try { if (Object.keys(data).length <= 0) { throw '数据为空'; } result = await this.db.create(data); } catch (error) { result = false; } return result; } /** * 根据项目id获取单价文件数据 * * @param {Number} projectId * @return {Promise} */ async getDataByProject(projectId) { let result = null; projectId = parseInt(projectId); try { if (isNaN(projectId) || projectId <= 0) { throw '标段id有误'; } let unitPriceFileId =await this.getUnitPriceFileId(projectId); if (unitPriceFileId <= 0) { throw '没有对应的单价文件'; } result = await this.findDataByCondition({id: unitPriceFileId}); } catch (error) { console.log('error:' + error); throw '没有对应的单价文件 ' } return result; } async getUnitPriceFileId(projectId){ let result = 0; let startTime = +new Date(); let projectData = await Projects.find({ID: projectId},['property.unitPriceFile']); if (projectData === null) { return result; } let endTime = +new Date(); console.log("取单价文件列表id时间-----"+(endTime - startTime)); projectData = projectData[0]; result = projectData.property.unitPriceFile !== undefined ? projectData.property.unitPriceFile.id : 0; return result; } /** * 新增单条工料机数据 * * @param {object} data * @return {Promise} */ async add(data) { if (Object.keys(data).length <= 0) { throw '新增数据为空'; } let counterModel = new CounterModel(); data.id = await counterModel.getId(collectionName); this.setScene('add'); let result = await this.db.create(data); return result; } /** * 根据单位工程获取对应单价文件 * * @param {Array} idList * @return {Promise|Array} */ async getDataByTenderId(idList) { let result = []; if (idList.length <= 0) { return result; } let condition = {project_id: {$in: idList},deleteInfo:null}; result = await this.findDataByCondition(condition, null, false); return result; } /** * 根据建设项目id获取单价文件数据 * * @param {Number} root_project_id * @return {Promise} */ async getDataByRootProject(root_project_id){ let condition = {root_project_id: root_project_id, deleteInfo: null}; return await this.findDataByCondition(condition, null, false); } /** * 根据用户获取对应被删除的单价文件 * * @param {String} userID * @return {Promise} */ async getGCUnitPriceFiles(userID){ let condition = {user_id: userID, 'deleteInfo.deleted': true, '$or': [{'deleteInfo.completeDeleted': false}, {'deleteInfo.completeDeleted': null}]}; let result = await this.findDataByCondition(condition, null, false); return result; } } module.exports = UnitPriceFileModel;