datacollect_audit.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. 'use strict';
  2. /**
  3. * 决策大屏用户查看权限-数据模型
  4. *
  5. * @author ellisran
  6. * @date 2021/09/23
  7. * @version
  8. */
  9. const accountGroup = require('../const/account_group').group;
  10. module.exports = app => {
  11. class datacollectAudit extends app.BaseService {
  12. constructor(ctx) {
  13. super(ctx);
  14. this.tableName = 'datacollect_audit';
  15. }
  16. async getGroupInfo(pid, groupid) {
  17. const sql = 'SELECT * FROM ?? WHERE pid = ? AND groupid = ? AND uid is NULL';
  18. const sqlParam = [this.tableName, pid, groupid];
  19. return await this.db.queryOne(sql, sqlParam);
  20. }
  21. async getCompanyInfo(pid, companyId) {
  22. const sql = 'SELECT * FROM ?? WHERE pid = ? AND company_id = ? AND uid is NULL';
  23. const sqlParam = [this.tableName, pid, companyId];
  24. return await this.db.queryOne(sql, sqlParam);
  25. }
  26. async saveAudit(pid, uid) {
  27. const data = {
  28. pid,
  29. uid,
  30. create_time: new Date(),
  31. };
  32. return await this.db.insert(this.tableName, data);
  33. }
  34. async saveGroup(pid, groupid) {
  35. const transaction = await this.db.beginTransaction();
  36. try {
  37. // 删除所在组的用户
  38. await transaction.delete(this.tableName, { pid, groupid });
  39. const data = {
  40. pid,
  41. groupid,
  42. create_time: new Date(),
  43. };
  44. await transaction.insert(this.tableName, data);
  45. await transaction.commit();
  46. return true;
  47. } catch (err) {
  48. await transaction.rollback();
  49. throw err;
  50. }
  51. }
  52. async saveCompany(pid, companyId) {
  53. const transaction = await this.db.beginTransaction();
  54. try {
  55. // const companyInfo = await this.ctx.service.constructionUnit.getDataById(companyId);
  56. // if (!companyInfo) throw '单位不存在';
  57. // await transaction.delete(this.tableName, { pid, company_id: companyId });
  58. // const nullCompanyList = await this.getAllDataByCondition({ where: { pid, company_id: null } });
  59. // if (nullCompanyList.length > 0) {
  60. // const userList = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { company: companyInfo.name } });
  61. // await transaction.delete(this.tableName, { pid, uid: this._.map(userList, 'id') });
  62. // }
  63. const data = {
  64. pid,
  65. company_id: companyId,
  66. create_time: new Date(),
  67. };
  68. await transaction.insert(this.tableName, data);
  69. await transaction.commit();
  70. return true;
  71. } catch (err) {
  72. await transaction.rollback();
  73. throw err;
  74. }
  75. }
  76. async delAudit(id) {
  77. return await this.db.delete(this.tableName, { id });
  78. }
  79. async getList(pid) {
  80. const list = await this.db.select(this.tableName, { where: { pid } });
  81. for (const l of list) {
  82. if (l.uid) {
  83. const accountInfo = await this.ctx.service.projectAccount.getDataById(l.uid);
  84. l.name = accountInfo.name;
  85. } else if (l.groupid) {
  86. l.name = accountGroup[l.groupid];
  87. } else if (l.company_id) {
  88. const companyInfo = await this.ctx.service.constructionUnit.getDataById(l.company_id);
  89. l.name = companyInfo ? companyInfo.name : null;
  90. }
  91. }
  92. return list;
  93. }
  94. }
  95. return datacollectAudit;
  96. };