contract_audit.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 ContractAudit extends BaseService {
  8. /**
  9. * 构造函数
  10. *
  11. * @param {Object} ctx - egg全局变量
  12. * @return {void}
  13. */
  14. constructor(ctx) {
  15. super(ctx);
  16. this.tableName = 'contract_audit';
  17. this.dataId = 'id';
  18. }
  19. async getList(options) {
  20. const list = options.spid ? await this.ctx.service.subProjPermission.getContractAuditList(options.spid) : await this.db.select(this.tableName, { where: options, orders: [['id', 'desc']] });
  21. for (const l of list) {
  22. const accountInfo = await this.ctx.service.projectAccount.getDataById(l.uid);
  23. l.name = accountInfo.name;
  24. l.role = accountInfo.role;
  25. l.company = accountInfo.company;
  26. l.mobile = accountInfo.mobile;
  27. }
  28. return list;
  29. }
  30. async saveAudits(options, accountList, transaction = null) {
  31. // 判断是否已存在该用户,存在则不插入
  32. if (options.spid) {
  33. const spAudits = await this.ctx.service.subProjPermission.getAllDataByCondition({ where: { spid: options.spid, uid: this._.map(accountList, 'id') } });
  34. const insertData = [];
  35. for (const a of spAudits) {
  36. if (!a.contract_permission) {
  37. insertData.push({
  38. id: a.id,
  39. contract_permission: '5',
  40. });
  41. }
  42. }
  43. if (insertData.length > 0) {
  44. return transaction ? await transaction.updateRows(this.ctx.service.subProjPermission.tableName, insertData) : await this.db.updateRows(this.ctx.service.subProjPermission.tableName, insertData);
  45. }
  46. return false;
  47. }
  48. const pauditList = await this.getAllDataByCondition({ where: options });
  49. const pushData = [];
  50. for (const a of this._.uniqBy(accountList, 'id')) {
  51. if (this._.findIndex(pauditList, { uid: a.id }) === -1) {
  52. const data = {
  53. spid: options.spid || null,
  54. tid: options.tid || null,
  55. uid: a.id,
  56. create_time: new Date(),
  57. };
  58. pushData.push(data);
  59. }
  60. }
  61. if (pushData.length > 0) {
  62. return transaction ? await transaction.insert(this.tableName, pushData) : await this.db.insert(this.tableName, pushData);
  63. }
  64. return false;
  65. }
  66. async delAudit(options, ids) {
  67. if (options.spid) {
  68. const updateData = [];
  69. for (const id of ids) {
  70. updateData.push({
  71. id,
  72. contract_permission: '',
  73. });
  74. }
  75. return await this.db.updateRows(this.ctx.service.subProjPermission.tableName, updateData);
  76. }
  77. return await this.db.delete(this.tableName, { id: ids });
  78. }
  79. async updatePermission(options, updateData) {
  80. if (!updateData.uid) {
  81. return false;
  82. }
  83. if (options.spid) {
  84. const spAudit = await this.ctx.service.subProjPermission.getDataByCondition({ spid: options.spid, uid: updateData.uid });
  85. if (!spAudit) {
  86. return false;
  87. }
  88. const newContractPermission = await this.ctx.service.subProjPermission.getNewContractPermission(spAudit.contract_permission, updateData);
  89. return await this.db.update(this.ctx.service.subProjPermission.tableName, { id: spAudit.id, contract_permission: newContractPermission });
  90. }
  91. const contractAudit = await this.getDataByCondition({ tid: options.tid, uid: updateData.uid });
  92. if (!contractAudit) {
  93. return false;
  94. }
  95. updateData.id = contractAudit.id;
  96. return await this.db.update(this.tableName, updateData);
  97. }
  98. async getUserPermissionEdit(options, uid) {
  99. const cloneOptions = this._.cloneDeep(options);
  100. cloneOptions.uid = uid;
  101. const info = await this.getDataByCondition(cloneOptions);
  102. return info && info.permission_edit;
  103. }
  104. async getUserList(tid, is_report = null) {
  105. const reportSql = is_report !== null ? ' AND ca.`is_report` = ' + is_report : '';
  106. const sql = 'SELECT ca.*, pa.name as user_name FROM ?? AS ca LEFT JOIN ?? AS pa ON ca.`uid` = pa.`id` ' +
  107. 'WHERE ca.`tid` = ?' + reportSql + ' ORDER BY ca.`id` DESC';
  108. const params = [this.tableName, this.ctx.service.projectAccount.tableName, tid];
  109. return await this.db.query(sql, params);
  110. }
  111. async setOtherTender(tidList, userList) {
  112. // 根据标段找出创建人去除,已存在的先删除再插入
  113. const transaction = await this.db.beginTransaction();
  114. try {
  115. const tenderList = await this.ctx.service.tender.getAllDataByCondition({
  116. columns: ['id', 'user_id'],
  117. where: { id: tidList.split(',') },
  118. });
  119. const oldTouristList = await this.getAllDataByCondition({ where: { tid: tidList.split(',') } });
  120. const insertData = [];
  121. const deleteIdData = [];
  122. for (const user of userList) {
  123. for (const t of tenderList) {
  124. const delId = this._.find(oldTouristList, { tid: t.id, uid: user.uid });
  125. if (delId) deleteIdData.push(delId.id);
  126. if (user.uid !== t.user_id) {
  127. insertData.push({
  128. spid: null,
  129. tid: t.id,
  130. uid: user.uid,
  131. permission_add: user.permission.add,
  132. permission_edit: user.permission.edit,
  133. permission_show_unit: user.permission.showUnit,
  134. permission_show_node: user.permission.showNode,
  135. permission_att: user.permission_att,
  136. create_time: new Date(),
  137. });
  138. }
  139. }
  140. }
  141. if (deleteIdData.length > 0) await transaction.delete(this.tableName, { id: deleteIdData });
  142. if (insertData.length > 0) await transaction.insert(this.tableName, insertData);
  143. await transaction.commit();
  144. return true;
  145. } catch (err) {
  146. await transaction.rollback();
  147. throw err;
  148. }
  149. }
  150. }
  151. return ContractAudit;
  152. };