'use strict'; /** * Created by EllisRan on 2020/3/3. */ const BaseService = require('../base/base_service'); module.exports = app => { class ContractTreeAudit extends BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'contract_tree_audit'; this.dataId = 'id'; } async add(options, select, uid) { if (!options.contract_type || !select.contract_id || !uid) { throw '参数有误'; } const sql = 'SELECT * FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND contract_id = ? AND uid = ?'; const sqlParam = [this.tableName, select.contract_id, uid]; const resultData = await this.db.queryOne(sql, sqlParam); if (resultData) { throw '该节点下已经存在该用户,请勿重复添加'; } const data = { spid: options.spid || null, tid: options.tid || null, contract_type: options.contract_type, contract_id: select.contract_id, uid: uid, create_time: new Date(), }; const result = await this.db.insert(this.tableName, data); return { id: result.insertId, ...data, } } async dels(options, select, uid) { if (!options.contract_type || !select.contract_id) { throw '参数有误'; } const uids = Array.isArray(uid) ? uid : [uid]; const sql = 'SELECT * FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND contract_id = ? AND uid in (' + this.ctx.helper.getInArrStrSqlFilter(uids) + ')'; const sqlParam = [this.tableName, select.contract_id]; const resultData = await this.db.query(sql, sqlParam); if (resultData.length === 0 || resultData.length !== uids.length) { throw '该节点下已不存在该用户'; } // for (const u of uids) { // const sql = 'SELECT * FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND contract_id = ? AND uid = ?'; // const sqlParam = [this.ctx.service.contract.tableName, select.contract_id, u]; // const resultData = await this.db.queryOne(sql, sqlParam); // if (resultData) { // throw '该用户已添加过合同,不可删除'; // } // } const result = await this.db.delete(this.tableName, { id: this._.map(resultData, 'id') }); return await this.getAllDataByCondition({ where: options }); } } return ContractTreeAudit; };