123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- /**
- * 计价规则标准库业务逻辑
- *
- * @author CaiAoLin
- * @date 2017/8/31
- * @version
- */
- import mongoose from "mongoose";
- import BaseModel from "../../common/base/base_model";
- import CompilationModel from "./compilation_model";
- import {default as EngineeringConst, List as EngineeringList} from "../../common/const/engineering";
- class EngineeringLibModel extends BaseModel {
- /**
- * 构造函数
- *
- * @return {void}
- */
- constructor() {
- let parent = super();
- parent.model = mongoose.model("engineering_lib");
- parent.init();
- }
- /**
- * 获取标准库数据
- *
- * @param {Object} data
- * @param {Number} engineering
- * @return {Promise}
- */
- async getLib(data, engineering) {
- let result = {};
- if (data.length <= 0) {
- return result;
- }
- let id = '';
- for(let tmp of data) {
- if (tmp.engineering === engineering) {
- id = tmp.engineering_id;
- break;
- }
- }
- if (id === '') {
- return result;
- }
- let condition = {_id: id};
- return this.findDataByCondition(condition);
- }
- /**
- * 新增标准库
- *
- * @param {String} valuationId
- * @param {Object} data
- * @return {Promise}
- */
- async addLib(valuationId, data) {
- if (data.main_tree_col) {
- data.main_tree_col = JSON.parse(data.main_tree_col);
- } else {
- data.main_tree_col = {
- emptyRows: 3,
- headRows: 0,
- treeCol: 0,
- headRowHeight: [],
- cols:[]
- }
- }
- if(data.glj_col){
- data.glj_col = JSON.parse(data.glj_col);
- }
- let result = false;
- data = this.filterLibData(data);
- try {
- // 查找计价规则表中是否有对应工程专业标准库的数据
- let compilationModel = new CompilationModel();
- let engineeringLib = await compilationModel.getEngineeringLib(valuationId, data.section, data.engineering);
- if (engineeringLib === null) {
- // 不存在则插入
- result = await this.db.create(data);
- } else {
- delete data.id;
- delete data.section;
- delete data.engineering;
- // 存在则直接更新
- let condition = {_id: engineeringLib.engineering_id};
- result = await this.db.update(condition, data);
- result = result.ok === 1;
- }
- // 失败直接返回
- if (!result) {
- throw '操作失败';
- }
- // 新增后更新编办数据表中专业工程字段
- if (result && engineeringLib === null) {
- let insertData = {
- engineering: data.engineering,
- engineering_id: result._id
- };
- let updateResult = await compilationModel.addEngineeringLib(valuationId, data.section, insertData);
- if (!updateResult) {
- throw '新增编办数据中的专业工程失败!';
- }
- }
- } catch (error) {
- console.log(error);
- result = false;
- }
- return result;
- }
- /**
- * 过滤计价数据
- *
- * @param {Object} data
- * @return {Object}
- */
- filterLibData(data) {
- if (Object.keys(data).length <= 0 || data.section === undefined) {
- console.log('1');
- throw '数据有误';
- }
- // 检测专业工程是否合法
- data.engineering = parseInt(data.engineering);
- let match = false;
- for(let index in EngineeringConst) {
- if (EngineeringConst[index] === data.engineering) {
- match = true;
- break;
- }
- }
- if (!match) {
- throw '工程专业错误';
- }
- // 判断标准清单
- data.bill_lib = this._validLib(data.bill_lib);
- // 判断定额库
- data.ration_lib = this._validLib(data.ration_lib);
- // 判断工料机库
- data.glj_lib = this._validLib(data.glj_lib);
- // 判断费率标准
- data.fee_lib = this._validLib(data.fee_lib);
- // 判断人工系数
- data.artificial_lib = this._validLib(data.artificial_lib);
- // 判断计算程序
- data.program_lib = this._validLib(data.program_lib);
- return data;
- }
- /**
- * 校验库数据
- *
- * @param {Object} libData
- * @return {Object}
- */
- _validLib(libData) {
- let result = [];
- // 判断标准库
- if (libData === undefined || libData === '') {
- throw '标准库不能为空';
- }
- libData = libData instanceof Array ? libData : [libData];
- for(let tmp in libData) {
- result[tmp] = JSON.parse(libData[tmp]);
- }
- return result;
- }
- /**
- * 获取对应标准库数量
- *
- * @param {Object} valuationData
- * @return {Object}
- */
- async getLibCount(valuationData) {
- let result = {};
- if (valuationData.engineering_list === undefined || valuationData.engineering_list.length <= 0) {
- return result;
- }
- // 整理需要查找的数据
- let findIdList = [];
- for(let engineering of valuationData.engineering_list) {
- findIdList.push(engineering.engineering_id);
- }
- let condition = {_id: {$in: findIdList}};
- let libData = await this.findDataByCondition(condition, null, false);
- if (libData === null) {
- return result;
- }
- // 整理数据
- let countData = {};
- for(let tmp of libData) {
- countData[tmp._id] = {
- bill_count: tmp.bill_lib.length,
- ration_count: tmp.ration_lib.length,
- glj_count: tmp.glj_lib.length,
- fee_count: tmp.fee_lib.length,
- artificial_count: tmp.artificial_lib.length,
- program_count: tmp.program_lib.length,
- };
- }
- for(let engineering of valuationData.engineering_list) {
- if (countData[engineering.engineering_id] !== undefined) {
- result[engineering.engineering] = countData[engineering.engineering_id];
- }
- }
- return result;
- }
- }
- export default EngineeringLibModel;
|