construction_audit.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 ConstructionAudit extends BaseService {
  8. /**
  9. * 构造函数
  10. *
  11. * @param {Object} ctx - egg全局变量
  12. * @return {void}
  13. */
  14. constructor(ctx) {
  15. super(ctx);
  16. this.tableName = 'construction_audit';
  17. this.dataId = 'id';
  18. }
  19. async getList(tid) {
  20. const list = await this.db.select(this.tableName, { where: { tid }, 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. }
  27. return list;
  28. }
  29. async saveAudits(tid, accountList, transaction = null) {
  30. // 判断是否已存在该用户,存在则不插入
  31. const pauditList = await this.getAllDataByCondition({ where: { tid } });
  32. const pushData = [];
  33. for (const a of this._.uniqBy(accountList, 'id')) {
  34. if (this._.findIndex(pauditList, { uid: a.id }) === -1) {
  35. const data = {
  36. tid,
  37. uid: a.id,
  38. in_time: new Date(),
  39. };
  40. pushData.push(data);
  41. }
  42. }
  43. if (pushData.length > 0) {
  44. return transaction ? await transaction.insert(this.tableName, pushData) : await this.db.insert(this.tableName, pushData);
  45. }
  46. return false;
  47. }
  48. async delAudit(id) {
  49. return await this.db.delete(this.tableName, { id });
  50. }
  51. async updateReport(updateData) {
  52. if (!updateData.id || updateData.is_report === undefined) {
  53. return false;
  54. }
  55. return await this.db.update(this.tableName, updateData);
  56. }
  57. async checkPermission(tender, uid) {
  58. let flag = false;
  59. const accountInfo = await this.ctx.service.projectAccount.getDataById(uid);
  60. const permission = accountInfo !== undefined && accountInfo.permission !== ''
  61. ? JSON.parse(accountInfo.permission) : null;
  62. if (accountInfo.project_id === tender.project_id && (accountInfo.is_admin || (permission !== null && permission.construction !== undefined && permission.construction.indexOf('1') !== -1))) {
  63. flag = true;
  64. } else {
  65. const info = await this.getDataByCondition({ tid: tender.id, uid });
  66. if (info) {
  67. flag = true;
  68. }
  69. }
  70. return flag;
  71. }
  72. async getUserList(tid, is_report = null) {
  73. const reportSql = is_report !== null ? ' AND ca.`is_report` = ' + is_report : '';
  74. const sql = 'SELECT ca.*, pa.name as user_name FROM ?? AS ca LEFT JOIN ?? AS pa ON ca.`uid` = pa.`id` ' +
  75. 'WHERE ca.`tid` = ?' + reportSql + ' ORDER BY ca.`id` DESC';
  76. const params = [this.tableName, this.ctx.service.projectAccount.tableName, tid];
  77. return await this.db.query(sql, params);
  78. }
  79. async setOtherTender(tidList, userList) {
  80. // 根据标段找出创建人去除,已存在的先删除再插入
  81. const transaction = await this.db.beginTransaction();
  82. try {
  83. const tenderList = await this.ctx.service.tender.getAllDataByCondition({
  84. columns: ['id', 'user_id'],
  85. where: { id: tidList.split(',') },
  86. });
  87. const oldTouristList = await this.getAllDataByCondition({ where: { tid: tidList.split(',') } });
  88. const insertData = [];
  89. const deleteIdData = [];
  90. for (const user of userList) {
  91. for (const t of tenderList) {
  92. const delId = this._.find(oldTouristList, { tid: t.id, uid: user.uid });
  93. if (delId) deleteIdData.push(delId.id);
  94. if (user.uid !== t.user_id) {
  95. insertData.push({
  96. tid: t.id,
  97. uid: user.uid,
  98. is_report: user.is_report,
  99. in_time: new Date(),
  100. });
  101. }
  102. }
  103. }
  104. if (deleteIdData.length > 0) await transaction.delete(this.tableName, { id: deleteIdData });
  105. if (insertData.length > 0) await transaction.insert(this.tableName, insertData);
  106. await transaction.commit();
  107. return true;
  108. } catch (err) {
  109. await transaction.rollback();
  110. throw err;
  111. }
  112. }
  113. }
  114. return ConstructionAudit;
  115. };