123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608 |
- /**
- * 项目工料机列表数据模型
- *
- * @author CaiAoLin
- * @date 2017/6/22
- * @version
- */
- import BaseModel from "../../common/base/base_model";
- import {default as GLJSchemas, collectionName as gljCollectionName} from "./schemas/glj";
- import CounterModel from "./counter_model";
- import UnitPriceModel from "./unit_price_model";
- import STDMixRatioModel from "../../common/std/std_mix_ratio_model";
- import UnitPriceFileModel from "./unit_price_file_model";
- import GLJTypeConst from "../../common/const/glj_type_const";
- import RationGLJFacade from "../../ration_glj/facade/ration_glj_facade";
- import STDRationLibGLJListModel from "../../common/std/std_ration_lib_glj_list_model";
- import STDGLJType from "../../../public/cache/std_glj_type_util";
- import MixRatioModel from "./mix_ratio_model";
- class GLJListModel extends BaseModel {
- /**
- * 材料类型id
- *
- * @var {Array}
- */
- materialIdList = [GLJTypeConst.GENERAL_MATERIAL, GLJTypeConst.CONCRETE, GLJTypeConst.MORTAR, GLJTypeConst.MIX_RATIO,
- GLJTypeConst.COMMERCIAL_CONCRETE, GLJTypeConst.COMMERCIAL_MORTAR];
- /**
- * 拥有组成物的工料机类型id
- *
- * @var {Array}
- */
- ownCompositionTypes = [GLJTypeConst.CONCRETE, GLJTypeConst.MORTAR, GLJTypeConst.MIX_RATIO,
- GLJTypeConst.COMMERCIAL_CONCRETE, GLJTypeConst.COMMERCIAL_MORTAR];
- /**
- * 构造函数
- *
- * @return {void}
- */
- constructor() {
- let parent = super();
- parent.model = GLJSchemas;
- parent.init();
- }
- /**
- * 设置场景
- *
- * @param {string} scene
- * @return {void}
- */
- setScene(scene = '') {
- switch (scene) {
- // 新增数据的验证规则
- case 'add':
- this.model.schema.path('glj_id').required(true);
- this.model.schema.path('project_id').required(true);
- this.model.schema.path('code').required(true);
- this.model.schema.path('name').required(true);
- break;
- }
- }
- /**
- * 根据标段对应项目工料机列表
- *
- * @param {Number} projectId
- * @param {Number} unitPriceFileId
- * @return {Promise}
- */
- async getListByProjectId(projectId, unitPriceFileId) {
- let gljData = null;
- try {
- // 首先获取对应标段下所有的项目工料机数据
- let condition = {project_id: projectId};
- let fields = {_id: 0};
- gljData = await this.db.find(condition, fields);
- // 没有数据则直接返回空
- if (gljData.length <= 0) {
- throw '无数据';
- }
- // 获取标段设置的单价文件数据
- let unitPriceModel = new UnitPriceModel();
- let unitPriceList = await unitPriceModel.getDataByFileId(unitPriceFileId);
- // 整理获取工料机ID list
- let gljIdList = [];
- // 整理获取有组成物的项目工料机编码
- let projectGLJCode = [];
- for(let tmp of gljData) {
- gljIdList.push(tmp.id);
- // 有组成物的类型才入数组
- if (this.ownCompositionTypes.indexOf(tmp.type)) {
- projectGLJCode.push(tmp.code);
- }
- }
- // 从定额工料机库中获取消耗量
- condition = {
- projectID: projectId,
- projectGLJIDList: gljIdList
- };
- let quantityData = await RationGLJFacade.getQuantityByProjectGLJ(condition);
- let quantityList = {};
- // 整理数据
- for (let tmp of quantityData) {
- quantityList[tmp.projectGLJID] = tmp.quantity;
- }
- // 查找组成物的消耗量
- let totalComposition = {};
- if (projectGLJCode.length > 0) {
- let mixRatioModel = new MixRatioModel();
- condition = {connect_code: {"$in": projectGLJCode}, unit_price_file_id: unitPriceFileId};
- let mixRatioList = await mixRatioModel.findDataByCondition(condition, null, false);
- for (let tmp of mixRatioList) {
- totalComposition[tmp.connect_code] = totalComposition[tmp.connect_code] === undefined ? tmp.consumption :
- totalComposition[tmp.connect_code] + tmp.consumption;
- totalComposition[tmp.connect_code] = Number(totalComposition[tmp.connect_code].toFixed(2));
- }
- }
- // 组合单价数据
- this.combineData(gljData, unitPriceList, quantityList, [], totalComposition);
- // 排序
- gljData.sort(function (a, b) {
- return a.unit_price.type - b.unit_price.type;
- });
- } catch (error) {
- console.log("glj_list_model:" + error);
- gljData = [];
- }
- return gljData;
- }
- /**
- * 组合工料机数据和单价文件数据
- *
- * @param {object} gljList
- * @param {object} unitPriceList
- * @param {object} quantityList
- * @param {object} mixRatioData 组合物明细数据
- * @param {object} totalComposition 组合物父工料机统计数据
- * @return {void}
- */
- combineData(gljList, unitPriceList, quantityList = {}, mixRatioData = {}, totalComposition = {}) {
- // 循环组合数据
- for(let glj of gljList) {
- if (glj.code === undefined) {
- continue;
- }
- glj.unit_price = unitPriceList !== null && unitPriceList[glj.code + glj.name] !== undefined ? unitPriceList[glj.code + glj.name] : null;
- if (glj.unit_price === null) {
- continue;
- }
- // 消耗量赋值
- glj.quantity = quantityList[glj.id] !== undefined ? quantityList[glj.id] : 0;
- glj.quantity = totalComposition[glj.code] !== undefined ? totalComposition[glj.code] : glj.quantity;
- // 组成物消耗量
- let gljId = glj.glj_id + '';
- glj.consumption = mixRatioData[gljId] !== undefined ? mixRatioData[gljId].consumption : 0;
- glj.mix_ratio_id = mixRatioData[gljId] !== undefined ? mixRatioData[gljId].id : 0;
- // 计算调整基价
- switch (glj.unit_price.type + '') {
- // 人工: 调整基价=基价单价*调整系数
- case GLJTypeConst.LABOUR:
- glj.adjust_price = glj.adjustment * glj.unit_price.base_price;
- break;
- // 机械类型的算法
- case GLJTypeConst.MACHINE:
- console.log('机械');
- break;
- // 材料、主材、设备
- default:
- glj.adjust_price = glj.unit_price.base_price;
- }
- }
- }
- /**
- * 新增项目工料机数据(包括新增单价文件) 定额工料机新增时调用
- *
- * @param {object} data
- * @return {Promise} 返回插入成功的数据id
- */
- async addList(data) {
- let result = null;
- try {
- if (Object.keys(data).length <= 0) {
- throw '新增数据为空';
- }
- // 首先查找是否有同编码同名称的工料机数据
- let projectGljData = await this.findDataByCondition({code: data.code, project_id: data.project_id});
- let isAddProjectGLJ = false;
- // 如果找不到数据则新增
- if (!projectGljData) {
- // 新增单条记录 (两个操作本来应该是事务操作,然而mongodb事务支持比较弱,就当作是都可以顺利执行)
- let gljInsertData = await this.add(data);
- if (!gljInsertData) {
- throw '新增项目工料机失败!';
- }
- isAddProjectGLJ = true;
- projectGljData = gljInsertData;
- }
- // 获取标段对应的单价文件id
- let unitPriceFileModel = new UnitPriceFileModel();
- let unitPriceFile = await unitPriceFileModel.getDataByProject(data.project_id);
- if (!unitPriceFile) {
- throw '没有对应的单价文件';
- }
- let unitPriceFileId = unitPriceFile.id;
- // 判断类型,如果是混凝土、砂浆或者配合比则查找对应的组成物(前提是没有对应的项目工料机数据)
- if (isAddProjectGLJ && (data.type === GLJTypeConst.CONCRETE || data.type === GLJTypeConst.MORTAR ||
- data.type === GLJTypeConst.MIX_RATIO)) {
- this.compositionInit(data.code, data.project_id, unitPriceFileId);
- }
- // 新增单价文件
- let unitPriceModel = new UnitPriceModel();
- let [unitPriceInsertData, isAdd] = await unitPriceModel.addUnitPrice(data, unitPriceFileId);
- if (!unitPriceInsertData) {
- throw '新增单价失败!';
- }
- projectGljData.unit_price = unitPriceInsertData;
- result = projectGljData;
- } catch (error) {
- console.log(error);
- result = null;
- }
- return result;
- }
- /**
- * 新增单条工料机数据
- *
- * @param {object} data
- * @return {Promise}
- */
- async add(data) {
- if (Object.keys(data).length <= 0) {
- throw '新增数据为空';
- }
- let counterModel = new CounterModel();
- if (data instanceof Array) {
- // 如果是批量新增
- for(let tmp in data) {
- data[tmp].id = await counterModel.getId(gljCollectionName);
- }
- } else {
- data.id = await counterModel.getId(gljCollectionName);
- }
- this.setScene('add');
- let result = await this.db.create(data);
- return result;
- }
- /**
- * 根据工料机id修改市场单价
- *
- * @param {Object} updateData
- * @return {Promise}
- */
- async modifyMarketPrice(updateData) {
- let result = {};
- try {
- if (updateData.code === undefined || updateData.market_price === undefined ||
- updateData.name === undefined || updateData.project_id === undefined) {
- throw '参数有误!';
- }
- // 先查是否有对应code的数据
- let gljListData = await this.findDataByCondition({code: updateData.code,
- project_id: updateData.project_id}, {_id: 0}, false);
- if (!gljListData) {
- throw '不存在对应code数据';
- }
- // 获取标段对应的单价文件id
- let unitPriceFileModel = new UnitPriceFileModel();
- let unitPriceFile = await unitPriceFileModel.getDataByProject(updateData.project_id);
- if (!unitPriceFile) {
- throw '没有对应的单价文件';
- }
- let unitPriceFileId = unitPriceFile.id;
- let unitPriceModel = new UnitPriceModel();
- let gljCount = gljListData.length;
- let [unitPriceData, isAdd] = await unitPriceModel.addUnitPrice(updateData, unitPriceFileId, gljCount);
- // 判断是否已存在对应数据
- let includeField = [
- {field: 'name', value: unitPriceData.name}
- ];
- let gljIndex = this.isIncluded(gljListData, includeField);
- let gljData = isAdd ? {} : gljListData[gljIndex];
- // 如果单价数据新增则工料机也需要新增
- if (isAdd) {
- // 如果没有对应的记录则新增一条工料机数据,并更改name
- let regular = /\(\d\)/;
- let changeString = '(' + gljCount + ')';
- updateData.name = regular.test(updateData.name) ? updateData.name.replace(regular, changeString) :
- updateData.name + changeString;
- // 获取第一条数据作为数据源
- let originalData = gljListData[0];
- // 更改名称
- originalData.name = updateData.name;
- originalData = JSON.stringify(originalData);
- gljData = await this.add(JSON.parse(originalData));
- if (!gljData) {
- throw '新增工料机数据失败!';
- }
- }
- gljData.unit_price = unitPriceData;
- result = gljData;
- } catch (error) {
- console.log(error);
- result = {};
- }
- return result;
- }
- /**
- * 判断数据中是否包含某个数据
- *
- * @param {Array} data
- * @param {Array} includeField
- * @return {Number}
- */
- isIncluded(data, includeField) {
- let index = -1;
- if (data.length <= 0) {
- return index;
- }
- for(let tmp in data) {
- let counter = 0;
- for (let includeTmp of includeField) {
- if (data[tmp][includeTmp.field] === includeTmp.value) {
- counter++;
- }
- }
- if (counter === includeField.length) {
- index = tmp;
- break;
- }
- }
- return index;
- }
- /**
- * 工料机中组成物操作
- * 该方法只在确保没有对应项目工料机的时候才会调用
- *
- * @param {String} code
- * @param {Number} projectId
- * @param {Number} unitPriceFileId
- * @return {void}
- */
- async compositionInit(code, projectId, unitPriceFileId) {
- // 查找对应组成物的项目工料机数据
- let [projectGljList, compositionList] = await this.getCompositionGLJList(code, projectId, 'name');
- // 整理配合比待插入数据
- let mixRatioInsertData = [];
- for (let tmp of compositionList) {
- // 配合比数据插入
- let mixRatioData = {
- consumption: tmp.consumption,
- glj_id: tmp.glj_id,
- unit_price_file_id: unitPriceFileId,
- connect_code: tmp.connect_code
- };
- mixRatioInsertData.push(mixRatioData);
- }
- // 插入配合比表
- // 因为有可能项目工料机与单价数据已存在,但配合比数据不存在,所以先插入配合比,后续判断如果存在项目工料机则可以省下数据库操作
- let mixRatioModel = new MixRatioModel();
- let addMixRatioResult = await mixRatioModel.add(mixRatioInsertData);
- if (!addMixRatioResult) {
- throw '组成物插入单价数据失败!';
- }
- // 如果已经存在则后续操作停止
- if(projectGljList.length === compositionList.length) {
- return;
- }
- // 插入不存在的项目工料机数据
- let notInGLJId = [];
- let compositionData = {};
- for(let tmp of compositionList) {
- compositionData[tmp.glj_id] = tmp;
- if (projectGljList[tmp.name] !== undefined) {
- continue;
- }
- // 把不存在的工料机总库id加入数组
- notInGLJId.push(tmp.glj_id);
- }
- // 查找对应工料机总库数据
- let stdRationLibGLJListModel = new STDRationLibGLJListModel();
- let stdGLJData = await stdRationLibGLJListModel.getDataById(notInGLJId);
- if (stdGLJData === null) {
- throw '没有找到对应的工料机总库数据';
- }
- // 获取工料机类型以及整理数据
- let gljTypeList = STDGLJType.getStdGljTypeCacheObj().toArray();
- let gljType = {};
- for (let tmp of gljTypeList) {
- gljType[tmp.fullName] = tmp.ID;
- }
- // 整理插入的数据
- let gljInsertData = [];
- let unitPriceInsertData = [];
- for (let tmp of stdGLJData) {
- // 项目工料机插入的数据
- let gljData = {
- glj_id: tmp.ID,
- project_id: projectId,
- code: tmp.code,
- name: tmp.name
- };
- gljInsertData.push(gljData);
- // 单价文件插入的数据
- let unitPriceData = {
- base_price: tmp.basePrice,
- // 初始市场价=基价
- market_price: tmp.basePrice,
- code: tmp.code,
- name: tmp.name,
- specs: tmp.specs,
- unit: tmp.unit,
- unit_price_file_id: unitPriceFileId,
- // 如果没有对应的工料机类型则默认设置为普通材料
- type: gljType[tmp.unit] !== undefined ? gljType[tmp.unit] : GLJTypeConst.MAIN_MATERIAL
- };
- unitPriceInsertData.push(unitPriceData);
- }
- // 整理完后开始插入数据
- let addResult = await this.add(gljInsertData);
- if (!addResult) {
- throw '组成物插入项目工料机失败!';
- }
- // 插入单价数据表
- let unitPriceModel = new UnitPriceModel();
- let addUnitPriceResult = await unitPriceModel.add(unitPriceInsertData);
- if (!addUnitPriceResult) {
- throw '组成物插入单价数据失败!';
- }
- }
- /**
- * 获取组成物具体数据
- *
- * @param {Number} projectGLJId
- * @param {Number} unitPriceFileId
- * @return {Promise}
- */
- async getCompositionList(projectGLJId, unitPriceFileId) {
- let result = [];
- try {
- // 查找对应的项目工料机数据
- let projectGLJData = await this.getDataById(projectGLJId);
- let allowType = [GLJTypeConst.MIX_RATIO, GLJTypeConst.CONCRETE, GLJTypeConst.MORTAR];
- if (projectGLJData.unit_price === null || allowType.indexOf(projectGLJData.unit_price.type) < 0) {
- throw '找不到相关项目工料机';
- }
- // 查找对应的项目工料机数据
- let [gljData, compositionList] = await this.getCompositionGLJList(projectGLJData.code, projectGLJData.project_id);
- if (gljData.length <= 0) {
- throw '没有对应的组成物项目工料机';
- }
- // 整理出code和name查找相关单价数据
- let codeList = [];
- let nameList = [];
- let gljIdList = [];
- for(let tmp of gljData) {
- codeList.push(tmp.code);
- nameList.push(tmp.name);
- gljIdList.push(tmp.glj_id);
- }
- // 查找对应的单价数据
- let unitPriceModel = new UnitPriceModel();
- let condition = {code: {"$in": codeList}, name: {"$in": nameList}, unit_price_file_id: unitPriceFileId};
- let unitPriceList = await unitPriceModel.findDataByCondition(condition, {_id: 0}, false);
- // 查找对应的配合比数据
- let mixRatioModel = new MixRatioModel();
- condition = {glj_id: {"$in": gljIdList}, connect_code: projectGLJData.code, unit_price_file_id: unitPriceFileId};
- let mixRatioData = await mixRatioModel.findDataByCondition(condition, {_id: 0}, false, 'glj_id');
- // 整理数据
- let unitPriceData = {};
- for(let tmp of unitPriceList) {
- unitPriceData[tmp.code + tmp.name] = tmp;
- }
- this.combineData(gljData, unitPriceData, [], mixRatioData);
- // 排序
- gljData.sort(function (a, b) {
- return parseInt(a.code) - parseInt(b.code);
- });
- result = gljData;
- } catch (error) {
- console.log(error);
- result = [];
- }
- return result;
- }
- /**
- * 获取混凝土等有组成物相关工料机对应的组成物项目工料机数据
- *
- * @param {String} code
- * @param {Number} projectId
- * @param {String} indexBy
- * @return {Promise} 返回组成物工料机数据和组成物列表数据
- */
- async getCompositionGLJList(code, projectId, indexBy = null) {
- // 首先获取对应组成物列表
- let stdMixRatioModel = new STDMixRatioModel();
- let compositionList = await stdMixRatioModel.getDataByCode(code);
- if (compositionList.length <= 0) {
- throw '不存在对应的组成物';
- }
- let codeList = [];
- let nameList = [];
- for(let tmp of compositionList) {
- codeList.push(tmp.code);
- nameList.push(tmp.name);
- }
- // 查找对应的项目工料机数据
- let condition = {code: {"$in": codeList}, name: {"$in": nameList}, project_id: projectId};
- let gljData = await this.findDataByCondition(condition, {_id: 0}, false, indexBy);
- return [gljData, compositionList];
- }
- /**
- * 根据条件获取对应项目工料机数据
- *
- * @param {Number} id
- * @return {Promise}
- */
- async getDataById(id) {
- // 查找对应的项目工料机数据
- let projectGLJData = await this.findDataByCondition({id: id});
- if (projectGLJData === null) {
- throw '没有找到对应数据';
- }
- // 查找对应的单价数据
- let unitPriceModel = new UnitPriceModel();
- let unitPrice = await unitPriceModel.findDataByCondition({code: projectGLJData.code, name: projectGLJData.name});
- projectGLJData.unit_price = unitPrice;
- return projectGLJData;
- }
- }
- export default GLJListModel;
|