123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- /**
- * 版本管理业务逻辑模型
- *
- * @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;
|