123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- /**
- * 单价文件业务模型
- *
- * @author CaiAoLin
- * @date 2017/6/30
- * @version
- */
- import BaseModel from "../../common/base/base_model"
- import CounterModel from "./counter_model"
- import GLJListModel from "./glj_list_model";
- import {default as UnitPriceSchema, collectionName as collectionName} from "./schemas/unit_price";
- class UnitPriceModel extends BaseModel {
- /**
- * 构造函数
- *
- * @return {void}
- */
- constructor() {
- let parent = super();
- parent.model = UnitPriceSchema;
- parent.init();
- }
- /**
- * 根据标段获取对应单价数据
- *
- * @param {Number} tenderId
- * @return {Promise}
- */
- async getDataByTenderId(tenderId) {
- tenderId = parseInt(tenderId);
- if (isNaN(tenderId) || tenderId <= 0) {
- return null;
- }
- let unitPriceList = await this.db.model.find({tender_id: tenderId});
- if (unitPriceList.length <= 0) {
- return null;
- }
- // 整理数据
- let result = {};
- for(let tmp of unitPriceList) {
- result[tmp.code + tmp.name] = tmp;
- }
- return result;
- }
- /**
- * 设置场景
- *
- * @param {string} scene
- * @return {void}
- */
- setScene(scene = '') {
- switch (scene) {
- // 新增数据的验证规则
- case 'add':
- this.model.schema.path('project_id').required(true);
- this.model.schema.path('tender_id').required(true);
- this.model.schema.path('base_price').required(true);
- this.model.schema.path('market_price').required(true);
- this.model.schema.path('name').required(true);
- this.model.schema.path('code').required(true);
- this.model.schema.path('unit').required(true);
- this.model.schema.path('type').required(true);
- }
- }
- /**
- * 新增单价文件
- *
- * @param {Object} data
- * @return {Promise}
- */
- async addData(data) {
- let result = false;
- try {
- // 首先查找是否有相同的记录
- let unitPriceData = await this.db.model.findOne({code: data.code, market_price: data.market_price});
- if (unitPriceData && unitPriceData.id > 0) {
- return true;
- }
- // 没有则新增数据
- result = this.db.model.create(data);
- } catch (error) {
- result = false;
- }
- return result;
- }
- /**
- * 更新单价文件(定额中修改价格时调用)
- *
- * @param {Object} data
- * @param {Number} unitPriceTenderId
- * @return {Promise}
- */
- async updateUnitPrice(data, unitPriceTenderId) {
- if (data.code === undefined || data.tender_id === undefined || data.name === undefined
- || data.market_price === undefined) {
- return null;
- }
- let marketPrice = data.market_price !== undefined ? data.market_price : data.base_price;
- // 先查找是否有同code的单价记录 @todo 后续可能会加入单位这个字段进一步确定唯一性
- let unitPriceData = await this.findDataByCondition({code: data.code, tender_id: unitPriceTenderId}, null, false);
- // 如果有记录,判断是否存在一样的市场单价,有则直接返回数据
- if (unitPriceData && this.isPriceIncluded(unitPriceData, data.market_price)) {
- return unitPriceData;
- }
- // 如果不存在基价单价,则在数据源中获取
- if (data.base_price === undefined) {
- let firstUnitPrice = unitPriceData[0] !== undefined ? unitPriceData[0] : [];
- data.base_price = firstUnitPrice.base_price !== undefined ? firstUnitPrice.base_price : 0;
- data.type = firstUnitPrice.type !== undefined ? firstUnitPrice.type : 0;
- data.unit = firstUnitPrice.unit !== undefined ? firstUnitPrice.unit : 0;
- }
- let insertData = {
- code: data.code,
- base_price: data.base_price,
- market_price: marketPrice,
- project_id: data.project_id,
- tender_id: unitPriceTenderId,
- name: data.name,
- type: data.type,
- unit: data.unit
- };
- // 统计当前同code的数量
- let sameCount = await this.count({code: data.code});
- if (sameCount > 0) {
- // 如果存在有别的同code的数据,则新增一条项目工料机,并更改name
- let regular = /\(\d\)/;
- let changeString = '(' + sameCount + ')';
- insertData.name = regular.test(insertData.name) ? insertData.name.replace(regular, changeString) :
- insertData.name + changeString;
- // 然后再插入一条项目工料机数据
- let gljListModel = new GLJListModel();
- // 首先先查找原有数据
- let originalData = await gljListModel.findDataByCondition({code: data.code, tender_id: data.tender_id}, {_id: 0});
- // 查不到数据直接抛出错误
- if(!originalData) {
- throw '没有找到code为:' + data.code + '的数据';
- }
- // 新增一条新name的项目工料机
- originalData.name = insertData.name;
- // 这里由于查出来的数据带有隐藏属性,所以先用json转一下
- originalData = JSON.stringify(originalData);
- let addGLJResult = await gljListModel.add(JSON.parse(originalData));
- if (!addGLJResult) {
- throw '新增工料机数据失败!';
- }
- }
- return this.add(insertData);
- }
- /**
- * 新增记录
- *
- * @param {object} data
- * @return {Promise}
- */
- async add(data) {
- let counterModel = new CounterModel();
- let unitPriceId = await counterModel.getId(collectionName);
- data.id = unitPriceId;
- this.setScene('add');
- return this.db.model.create(data);
- }
- /**
- * 判断数据中是否包含某个市场价格的记录
- *
- * @param {Array} data
- * @param {Number} price
- * @return boolean
- */
- isPriceIncluded(data, price) {
- let result = false;
- if (data.length <= 0) {
- return result;
- }
- for(let tmp of data) {
- if (tmp.market_price === price) {
- return true;
- }
- }
- return result;
- }
- }
- export default UnitPriceModel;
|