/** * 版本管理业务逻辑模型 * * @author CaiAoLin * @date 2017/7/28 * @version */ import BaseModel from "../../common/base/base_model"; import VersionSchema from "./schemas/version"; class VersionModel extends BaseModel { /** * 构造函数 * * @return {void} */ constructor() { let parent = super(); parent.model = VersionSchema; parent.init(); } /** * 获取版本列表 * * @return {Promise} */ async getVersionList() { let versionData = await this.findDataByCondition({name: {$ne: ''}}, null, false); return versionData === null ? [] : versionData; } /** * 设置场景 * * @param {string} scene * @return {void} */ setScene(scene = '') { switch (scene) { // 新增 case 'add': this.model.schema.path('name').required(true); this.model.schema.path('standard_bill').required(true); this.model.schema.path('ration_lib').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 {Object} postData * @return {Promise} */ async addLib(postData) { if (postData.id === undefined || postData.model === undefined) { throw '参数错误'; } let model = postData.model; let id = postData.id; let standardBillId = postData.standard_bill_id; let standardBill = postData.standard_bill; let rationLibId = postData.ration_lib_id; let rationLib = postData.ration_lib; switch (model) { case 'bill': if (standardBillId === undefined || standardBill === undefined) { throw '参数错误'; } break; case 'ration': if (rationLibId === undefined || rationLib === undefined) { throw '参数错误'; } break; } let versionData = await this.findDataByCondition({_id: id}); if (versionData === null || versionData.standard_bill === undefined) { throw '没有找到对应的数据'; } let updateData = {}; let field = model === 'bill' ? 'standard_bill' : 'ration_lib'; let tmpData = { id: model === 'bill' ? standardBillId : rationLibId, name: model === 'bill' ? standardBill : rationLib }; // 判断是否有重复 if (versionData[field].length > 0) { for (let tmp of versionData[field]) { if (tmp.id === tmpData.id) { throw '已存在对应数据'; } } } versionData[field].push(tmpData); updateData[field] = versionData[field]; let result = await this.db.update({_id: id}, updateData); return result.ok === undefined ? false : result.ok; } /** * 删除对应的标准清单库/定额库 * * @param {Number} id * @param {Number} libId * @param {String} model * @return {Promise} */ async deleteLib(id, libId, model) { let versionData = await this.findDataByCondition({_id: id}); if (versionData === null || versionData.standard_bill === undefined) { throw '没有找到对应的数据'; } let field = model === 'bill' ? 'standard_bill' : 'ration_lib'; if (versionData[field].length <= 0) { throw '没有对应的库数据'; } let own = false; for (let index in versionData[field]) { if (versionData[field][index].id === libId) { versionData[field].splice(index, 1); own = true; break; } } if (!own) { throw '没有对应的库数据'; } let updateData = {}; updateData[field] = versionData[field]; let result = await this.db.update({_id: id}, updateData); return result.ok === undefined ? false : result.ok; } } export default VersionModel;