contract_tree_audit.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. 'use strict';
  2. /**
  3. * Created by EllisRan on 2020/3/3.
  4. */
  5. const BaseService = require('../base/base_service');
  6. module.exports = app => {
  7. class ContractTreeAudit extends BaseService {
  8. /**
  9. * 构造函数
  10. *
  11. * @param {Object} ctx - egg全局变量
  12. * @return {void}
  13. */
  14. constructor(ctx) {
  15. super(ctx);
  16. this.tableName = 'contract_tree_audit';
  17. this.dataId = 'id';
  18. }
  19. async add(options, select, uid) {
  20. if (!options.contract_type || !select.contract_id || !uid) {
  21. throw '参数有误';
  22. }
  23. const sql = 'SELECT * FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND contract_id = ? AND uid = ?';
  24. const sqlParam = [this.tableName, select.contract_id, uid];
  25. const resultData = await this.db.queryOne(sql, sqlParam);
  26. if (resultData) {
  27. throw '该节点下已经存在该用户,请勿重复添加';
  28. }
  29. const data = {
  30. spid: options.spid || null,
  31. tid: options.tid || null,
  32. contract_type: options.contract_type,
  33. contract_id: select.contract_id,
  34. uid: uid,
  35. create_time: new Date(),
  36. };
  37. const result = await this.db.insert(this.tableName, data);
  38. return {
  39. id: result.insertId,
  40. ...data,
  41. }
  42. }
  43. async dels(options, select, uid) {
  44. if (!options.contract_type || !select.contract_id) {
  45. throw '参数有误';
  46. }
  47. const uids = Array.isArray(uid) ? uid : [uid];
  48. const sql = 'SELECT * FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND contract_id = ? AND uid in (' + this.ctx.helper.getInArrStrSqlFilter(uids) + ')';
  49. const sqlParam = [this.tableName, select.contract_id];
  50. const resultData = await this.db.query(sql, sqlParam);
  51. if (resultData.length === 0 || resultData.length !== uids.length) {
  52. throw '该节点下已不存在该用户';
  53. }
  54. // for (const u of uids) {
  55. // const sql = 'SELECT * FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND contract_id = ? AND uid = ?';
  56. // const sqlParam = [this.ctx.service.contract.tableName, select.contract_id, u];
  57. // const resultData = await this.db.queryOne(sql, sqlParam);
  58. // if (resultData) {
  59. // throw '该用户已添加过合同,不可删除';
  60. // }
  61. // }
  62. const result = await this.db.delete(this.tableName, { id: this._.map(resultData, 'id') });
  63. return await this.getAllDataByCondition({ where: options });
  64. }
  65. }
  66. return ContractTreeAudit;
  67. };