construction_log.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. 'use strict';
  2. /**
  3. * Created by EllisRan on 2020/3/3.
  4. */
  5. const BaseService = require('../base/base_service');
  6. const constructionConst = require('../const/construction');
  7. module.exports = app => {
  8. class ConstructionLog extends BaseService {
  9. /**
  10. * 构造函数
  11. *
  12. * @param {Object} ctx - egg全局变量
  13. * @return {void}
  14. */
  15. constructor(ctx) {
  16. super(ctx);
  17. this.tableName = 'construction_log';
  18. }
  19. async getCount(params) {
  20. return await this.count(params);
  21. }
  22. async getLogList(params) {
  23. // 获取所有标段的成员管理人员
  24. // const userList = await this.ctx.service.constructionAudit.getUserList(params.tid);
  25. const list = await this.getList({ where: params });
  26. if (list.length > 0) {
  27. const userIdList = this._.uniq(this._.concat(this._.map(list, 'report_uid'), this._.map(list, 'shenpi_uid')));
  28. const userList = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { id: userIdList } });
  29. for (const l of list) {
  30. l.report_username = this._.find(userList, { id: l.report_uid }) ? this._.find(userList, { id: l.report_uid }).name : '';
  31. l.shenpi_username = l.shenpi_uid && this._.find(userList, { id: l.shenpi_uid }) ? this._.find(userList, { id: l.shenpi_uid }).name : '';
  32. }
  33. }
  34. return list;
  35. }
  36. async getUncheckedLogList(tid, uid) {
  37. const uncheckList = await this.getAllDataByCondition({ where: { tid, report_uid: uid, status: constructionConst.status.uncheck }, orders: [['id', 'desc']] });
  38. const list = this._.filter(uncheckList, item => item.shenpi_uid !== null);
  39. if (list.length > 0) {
  40. const userIdList = this._.uniq(this._.concat(this._.map(list, 'report_uid'), this._.map(list, 'shenpi_uid')));
  41. const userList = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { id: userIdList } });
  42. for (const l of list) {
  43. l.report_username = this._.find(userList, { id: l.report_uid }) ? this._.find(userList, { id: l.report_uid }).name : '';
  44. l.shenpi_username = l.shenpi_uid && this._.find(userList, { id: l.shenpi_uid }) ? this._.find(userList, { id: l.shenpi_uid }).name : '';
  45. }
  46. }
  47. return list;
  48. }
  49. async getDateCodeList(tid) {
  50. const sql = 'SELECT `date_code`, count(*) AS num FROM ?? WHERE `tid` = ? GROUP BY `date_code` ORDER BY `date_code`';
  51. const params = [this.tableName, tid];
  52. return await this.db.query(sql, params);
  53. }
  54. async addLog(tid, uid, data) {
  55. if (!data) throw '参数有误';
  56. if (!data.code || !data.period || data.type === undefined) throw '参数有误';
  57. const newData = {
  58. tid,
  59. report_uid: uid,
  60. create_time: new Date(),
  61. type: data.type ? data.type : 0,
  62. period: data.period,
  63. code: data.code,
  64. date_code: data.code.substring(0, 8),
  65. };
  66. const result = await this.db.insert(this.tableName, newData);
  67. return result.insertId;
  68. }
  69. async updateLogJson(id, json, data) {
  70. json[data.key] = data.value;
  71. const updateData = {
  72. id,
  73. log_json: JSON.stringify(json),
  74. };
  75. return await this.db.update(this.tableName, updateData);
  76. }
  77. async setShenpiUser(tid, id, shenpi_uid) {
  78. const transaction = await this.db.beginTransaction();
  79. try {
  80. const userInfo = await this.ctx.service.projectAccount.getDataById(shenpi_uid);
  81. if (!userInfo) throw '该用户不存在';
  82. await transaction.update(this.tableName, { id, shenpi_uid });
  83. // 判断有无,无则插入到constructionAudit成员管理表里
  84. const audit = await this.ctx.service.constructionAudit.getDataByCondition({ tid, uid: shenpi_uid });
  85. if (!audit) {
  86. const addAudit = {
  87. tid,
  88. uid: shenpi_uid,
  89. in_time: new Date(),
  90. };
  91. await transaction.insert(this.ctx.service.constructionAudit.tableName, addAudit);
  92. }
  93. await transaction.commit();
  94. return { shenpi_uid, shenpi_username: userInfo.name };
  95. } catch (err) {
  96. await transaction.rollback();
  97. throw err;
  98. }
  99. }
  100. async removeShenpiUser(id) {
  101. return await this.db.update(this.tableName, { id, shenpi_uid: null });
  102. }
  103. async start(id) {
  104. return await this.db.update(this.tableName, { id, status: constructionConst.status.checking, start_time: new Date() });
  105. }
  106. async startMulti(ids, uid) {
  107. const list = await this.getAllDataByCondition({ where: { id: ids } });
  108. if (list.length > 0) {
  109. const updateList = [];
  110. for (const l of list) {
  111. if (l.report_uid !== uid) {
  112. throw '非填报人无法提交日志';
  113. }
  114. if (l.status === constructionConst.status.uncheck) {
  115. updateList.push({
  116. id: l.id,
  117. status: constructionConst.status.checking,
  118. start_time: new Date(),
  119. });
  120. }
  121. }
  122. if (updateList.length > 0) await this.db.updateRows(this.tableName, updateList);
  123. }
  124. return true;
  125. }
  126. async checked(id) {
  127. return await this.db.update(this.tableName, { id, status: constructionConst.status.checked, checked_time: new Date() });
  128. }
  129. async checkNo(id) {
  130. return await this.db.update(this.tableName, { id, status: constructionConst.status.uncheck });
  131. }
  132. async deleteLog(id) {
  133. const transaction = await this.db.beginTransaction();
  134. try {
  135. // 先删附件,再删数据记录
  136. const attList = await this.ctx.service.constructionAtt.getAllDataByCondition({ where: { log_id: id } });
  137. await this.ctx.helper.delFiles(attList);
  138. await transaction.delete(this.ctx.service.constructionAtt.tableName, { log_id: id });
  139. await transaction.delete(this.tableName, { id });
  140. await transaction.commit();
  141. return true;
  142. } catch (err) {
  143. await transaction.rollback();
  144. throw err;
  145. }
  146. }
  147. }
  148. return ConstructionLog;
  149. };