construction_audit.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. }
  80. return ConstructionAudit;
  81. };