123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- /**
- * 编办管理业务逻辑模型
- *
- * @author CaiAoLin
- * @date 2017/7/28
- * @version
- */
- import BaseModel from "../../common/base/base_model";
- import CompilationSchema from "./schemas/compilation";
- class CompilationModel extends BaseModel {
- /**
- * 允许的块
- *
- * @var {Array}
- */
- sectionList = ['bill', 'ration'];
- /**
- * 构造函数
- *
- * @return {void}
- */
- constructor() {
- let parent = super();
- parent.model = CompilationSchema;
- parent.init();
- }
- /**
- * 获取编办列表
- *
- * @return {Promise}
- */
- async getCompilationList() {
- // 筛选字段
- let field = {_id: 1, name: 1, is_release: 1, "ration_valuation._id": 1, "ration_valuation.name": 1, "ration_valuation.enable": 1,
- "bill_valuation._id": 1, "bill_valuation.name": 1, "bill_valuation.enable": 1};
- let compilationData = await this.findDataByCondition({name: {$ne: ''}}, field, false);
- return compilationData === null ? [] : compilationData;
- }
- /**
- * 设置场景
- *
- * @param {string} scene
- * @return {void}
- */
- setScene(scene = '') {
- switch (scene) {
- // 新增
- case 'add':
- this.model.schema.path('name').required(true);
- this.model.schema.path('creator').required(true);
- this.model.schema.path('create_time').required(true);
- break;
- }
- }
- /**
- * 新增编办
- *
- * @param {Object} data
- * @return {Promise}
- */
- async add(data) {
- let result = false;
- if (Object.keys(data).length <= 0) {
- return result;
- }
- this.setScene('add');
- data.create_time = new Date().getTime();
- result = this.db.create(data);
- return result;
- }
- /**
- * 新增计价规则
- *
- * @param {String} id
- * @param {String} section
- * @param {Object} data
- * @return {Promise}
- */
- async addValuation(id, section, data) {
- let condition = {_id: id};
- let compilationData = await this.findDataByCondition(condition);
- if (compilationData === null || compilationData.name === undefined) {
- throw '没有找到对应的数据';
- }
- if (data.name === undefined || data.name === '') {
- throw '计价规则名称为空';
- }
- if (this.sectionList.indexOf(section) < 0) {
- throw '数据有误';
- }
- let insertData = {};
- insertData[section + '_valuation'] = data;
- let result = await this.db.addToSet(condition, insertData);
- return result.ok === undefined ? false : result.ok;
- }
- /**
- * 保存计价数据
- *
- * @param {String} valuationId
- * @param {Object} data
- * @return {Promise}
- */
- async saveValuation(valuationId, data) {
- data = this._filterValuationData(data);
- let sectionString = data.section + "_valuation";
- let condition = {};
- condition[sectionString + "._id"] = valuationId;
- let updateData = {};
- updateData[sectionString + ".$.bill_lib"] = data.bill_lib;
- updateData[sectionString + ".$.ration_lib"] = data.ration_lib;
- updateData[sectionString + ".$.name"] = data.name;
- updateData[sectionString + ".$.engineering"] = data.engineering;
- updateData[sectionString + ".$.main_tree_col"] = JSON.parse(data.main_tree_col);
- let result = await this.db.update(condition, updateData);
- return result !== null && result.ok === 1;
- };
- /**
- * 更改启用/禁用
- *
- * @param {String} valuationId
- * @param {String} section
- * @param {String} enable
- * @return {Promise}
- */
- async switchEnable(valuationId, section, enable) {
- let sectionString = section + "_valuation";
- let condition = {};
- condition[sectionString + "._id"] = valuationId;
- let updateData = {};
- updateData[sectionString + ".$.enable"] = enable === "true";
- let result = await this.db.update(condition, updateData);
- return result !== null && result.ok === 1;
- }
- /**
- * 过滤计价数据
- *
- * @param {Object} data
- * @return {Object}
- */
- _filterValuationData(data) {
- if (Object.keys(data).length <= 0) {
- throw '数据有误';
- }
- if (data.section === undefined || data.section === '' || this.sectionList.indexOf(data.section) < 0) {
- throw '类型错误';
- }
- // 判断名称
- if (data.name === undefined || data.name === '') {
- throw '名称不能为空';
- }
- // 判断工程专业
- if (data.engineering === undefined || data.engineering === '') {
- throw '名称不能为空';
- }
- data.engineering = parseInt(data.engineering);
- // 判断标准清单
- if (data.bill_lib === undefined || data.bill_lib === '') {
- throw '判断标准清单不能为空';
- }
- data.bill_lib = data.bill_lib instanceof Array ? data.bill_lib : [data.bill_lib];
- for(let tmp in data.bill_lib) {
- data.bill_lib[tmp] = JSON.parse(data.bill_lib[tmp]);
- }
- // 判断定额清单
- if (data.ration_lib === undefined || data.ration_lib === '') {
- throw '判断标准清单不能为空';
- }
- data.ration_lib = data.ration_lib instanceof Array ? data.ration_lib : [data.ration_lib];
- for(let tmp in data.ration_lib) {
- data.ration_lib[tmp] = JSON.parse(data.ration_lib[tmp]);
- }
- return data;
- }
- /**
- * 获取计价规则数据
- *
- * @param {String} compilationId
- * @param {String} id
- * @param {String} section
- * @return {Promise|Array}
- */
- async getValuation(compilationId, id, section) {
- if (this.sectionList.indexOf(section) < 0) {
- throw '数据有误';
- }
- let compilationData = await this.findDataByCondition({_id: compilationId});
- if (Object.keys(compilationData).length <= 0) {
- throw '编办数据有误';
- }
- let result = {};
- let sectionString = section + '_valuation';
- for(let valuation of compilationData[sectionString]) {
- if (valuation._id.toString() === id) {
- result = valuation;
- break;
- }
- }
- return [result, compilationData[sectionString]];
- /* 数据库获取编办
- let condition = {_id: versionId};
- let childCondition = {};
- childCondition[sectionString] = {$elemMatch: {_id: id}};
- let result = await this.db.findOne(condition, childCondition);
- return result !== null && result.bill_valuation.length > 0 ? result.bill_valuation[0] : {};
- */
- }
- /**
- * 删除计价规则
- *
- * @param {String} compilationId
- * @param {String} valuationId
- * @param {String} section
- * @return {Promise}
- */
- async deleteValuation(compilationId, valuationId, section) {
- let condition = {_id: compilationId};
- let sectionString = section + '_valuation';
- let deleteData = {};
- deleteData[sectionString] = {_id: valuationId};
- // 利用pull删除嵌套数据
- let result = await this.db.deleteSet(condition, deleteData);
- return result !== null && result.ok === 1;
- }
- /**
- * 发布编办
- *
- * @param {String} id
- * @param {Number} status
- * @return {Promise}
- */
- async release(id, status) {
- // 如果是发布编办则需要判断配置的内容是否满足发布条件
- if (status) {
- let compilationData = await this.findDataByCondition({_id: id});
- // 最少需要有一个计价规则存在
- if (compilationData.bill_valuation.length <= 0 && compilationData.ration_valuation.length <= 0) {
- throw '至少需要一个计价规则';
- }
- // 判断是否全部禁止
- let hasEnable = false;
- let valuationList = compilationData.bill_valuation.concat(compilationData.ration_valuation);
- for (let valuation of valuationList) {
- if (valuation.enable) {
- hasEnable = true;
- break;
- }
- }
- if (!hasEnable) {
- throw '至少需要一个可用的计价规则';
- }
- }
- let updateData = {
- is_release: status === 1,
- release_time: new Date().getTime()
- };
- return this.updateById(id, updateData);
- }
- }
- export default CompilationModel;
|