project_log.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 'use strict';
  2. /**
  3. * 项目操作日志-数据模型
  4. *
  5. * @author ellisran
  6. * @date 2021/01/12
  7. * @version
  8. */
  9. module.exports = app => {
  10. class projectLog extends app.BaseService {
  11. constructor(ctx) {
  12. super(ctx);
  13. this.tableName = 'project_log';
  14. }
  15. /**
  16. * 创建登录日志
  17. * @return {Boolean} 日志是否创建成功
  18. * @param {Number} type - 登录类型
  19. * @param {Number} status - 是否显示记录
  20. */
  21. async addProjectLog(transaction, type, status, msg, tid = 0) {
  22. const { ctx } = this;
  23. const ipMsg = await ctx.helper.getUserIPMsg();
  24. const payload = {
  25. uid: ctx.session.sessionUser.accountId,
  26. pid: ctx.session.sessionProject.id,
  27. tid: ctx.tender.id ? ctx.tender.id : tid,
  28. type,
  29. status,
  30. msg,
  31. };
  32. this._.assign(payload, ipMsg);
  33. return await transaction.insert(this.tableName, payload);
  34. }
  35. /**
  36. * 获取操作日志
  37. * @param {Number} pid - 项目id
  38. * @param {Number} type - 类型
  39. * @return {Promise<Array>} 日志数组
  40. */
  41. async getLogs(pid, type = 0) {
  42. const typeSql = parseInt(type) !== 0 ? ' AND A.`type` = ' + type : '';
  43. const sql = 'SELECT A.*, B.`name` as `username`, B.`mobile` FROM ?? as A LEFT JOIN ?? as B ON A.`uid` = B.`id` WHERE A.`pid` = ?' + typeSql + ' AND TO_DAYS(NOW()) - TO_DAYS(A.`create_time`) <= 30 ORDER BY A.`id` DESC';
  44. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, pid];
  45. return await this.db.query(sql, sqlParam);
  46. }
  47. }
  48. return projectLog;
  49. };