contract_audit.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 = 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. const pauditList = await this.getAllDataByCondition({ where: options });
  33. const pushData = [];
  34. for (const a of this._.uniqBy(accountList, 'id')) {
  35. if (this._.findIndex(pauditList, { uid: a.id }) === -1) {
  36. const data = {
  37. spid: options.spid || null,
  38. tid: options.tid || null,
  39. uid: a.id,
  40. create_time: new Date(),
  41. };
  42. pushData.push(data);
  43. }
  44. }
  45. if (pushData.length > 0) {
  46. return transaction ? await transaction.insert(this.tableName, pushData) : await this.db.insert(this.tableName, pushData);
  47. }
  48. return false;
  49. }
  50. async delAudit(id) {
  51. return await this.db.delete(this.tableName, { id });
  52. }
  53. async updatePermission(updateData) {
  54. if (!updateData.id) {
  55. return false;
  56. }
  57. return await this.db.update(this.tableName, updateData);
  58. }
  59. async checkPermission(options, uid) {
  60. if (this.ctx.session.sessionUser.is_admin) {
  61. return true;
  62. }
  63. let flag = false;
  64. const cloneOptions = this._.cloneDeep(options);
  65. cloneOptions.uid = uid;
  66. const info = await this.getDataByCondition(cloneOptions);
  67. if (info) {
  68. flag = true;
  69. }
  70. return flag;
  71. }
  72. async getUserPermissionEdit(options, uid) {
  73. const cloneOptions = this._.cloneDeep(options);
  74. cloneOptions.uid = uid;
  75. const info = await this.getDataByCondition(cloneOptions);
  76. return info && info.permission_edit;
  77. }
  78. async getUserList(tid, is_report = null) {
  79. const reportSql = is_report !== null ? ' AND ca.`is_report` = ' + is_report : '';
  80. const sql = 'SELECT ca.*, pa.name as user_name FROM ?? AS ca LEFT JOIN ?? AS pa ON ca.`uid` = pa.`id` ' +
  81. 'WHERE ca.`tid` = ?' + reportSql + ' ORDER BY ca.`id` DESC';
  82. const params = [this.tableName, this.ctx.service.projectAccount.tableName, tid];
  83. return await this.db.query(sql, params);
  84. }
  85. async setOtherTender(tidList, userList) {
  86. // 根据标段找出创建人去除,已存在的先删除再插入
  87. const transaction = await this.db.beginTransaction();
  88. try {
  89. const tenderList = await this.ctx.service.tender.getAllDataByCondition({
  90. columns: ['id', 'user_id'],
  91. where: { id: tidList.split(',') },
  92. });
  93. const oldTouristList = await this.getAllDataByCondition({ where: { tid: tidList.split(',') } });
  94. const insertData = [];
  95. const deleteIdData = [];
  96. for (const user of userList) {
  97. for (const t of tenderList) {
  98. const delId = this._.find(oldTouristList, { tid: t.id, uid: user.uid });
  99. if (delId) deleteIdData.push(delId.id);
  100. if (user.uid !== t.user_id) {
  101. insertData.push({
  102. tid: t.id,
  103. uid: user.uid,
  104. is_report: user.is_report,
  105. in_time: new Date(),
  106. });
  107. }
  108. }
  109. }
  110. if (deleteIdData.length > 0) await transaction.delete(this.tableName, { id: deleteIdData });
  111. if (insertData.length > 0) await transaction.insert(this.tableName, insertData);
  112. await transaction.commit();
  113. return true;
  114. } catch (err) {
  115. await transaction.rollback();
  116. throw err;
  117. }
  118. }
  119. }
  120. return ContractAudit;
  121. };