notice_again.js 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. 'use strict';
  2. /**
  3. * 审批信息重新发送modal
  4. *
  5. * @author EllisRan
  6. * @date 2024/01/18
  7. * @version
  8. */
  9. const smsTypeConst = require('../const/sms_type');
  10. module.exports = app => {
  11. class NoticeAgain extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'notice_again';
  21. }
  22. // 入库并判断
  23. async addNoticeAgain(transaction, type, datas) {
  24. // 判断用户是否已绑定公众号或者企业微信, 并判断用户是否开启了重新发送并且钩上对应的审批
  25. const userInfo = await this.ctx.service.projectAccount.getDataById(datas.uid);
  26. if (!userInfo.wx_type || userInfo.wx_type === '') return;
  27. const wxType = JSON.parse(userInfo.wx_type);
  28. // 没开启发送功能不应该生成重新发送入库数据
  29. if (wxType[type] && wxType[type].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  30. datas.status = 0;
  31. const notice_again = userInfo.notice_again ? JSON.parse(userInfo.notice_again) : null;
  32. if ((userInfo.wx_openid || userInfo.qywx_userid) && notice_again && notice_again.checked && notice_again.sp[datas.sp_type]) {
  33. datas.status = 1;
  34. }
  35. datas.in_time = new Date();
  36. datas.last_time = this.ctx.helper.dateTran(new Date(), 'YYYY-MM-DD HH:mm');
  37. datas.times = 0;
  38. datas.wx_data = JSON.stringify(datas.wx_data);
  39. datas.sms_type = type;
  40. await transaction.insert(this.tableName, datas);
  41. }
  42. }
  43. async stopNoticeAgain(transaction, table_name, sp_id, status = 2) {
  44. // 判断sp_id是字符还是数组
  45. // if (sp_id instanceof Array) {
  46. // for (const id of sp_id) {
  47. // await transaction.update(this.tableName, { status }, { where: { table_name, sp_id: id } });
  48. // }
  49. // } else {
  50. await transaction.update(this.tableName, { status }, { where: { table_name, sp_id } });
  51. // }
  52. }
  53. // 针对撤回删除对应的重新发送数据
  54. async deleteNoticeAgain(transaction, table_name, sp_id) {
  55. // 判断sp_id是字符还是数组
  56. await transaction.delete(this.tableName, { table_name, sp_id });
  57. }
  58. async getSpResult(table_name, sp_id) {
  59. const sql = 'SELECT * FROM ?? WHERE id = ?';
  60. const sqlParam = [table_name, sp_id];
  61. return await this.db.queryOne(sql, sqlParam);
  62. }
  63. async updateUserNoticeAgain(transaction, uid, notice_again) {
  64. const list = await transaction.select(this.tableName, { where: { uid } });
  65. const uncheckedList = this._.filter(list, function(item) {
  66. return item.status !== 2;
  67. });
  68. if (uncheckedList.length > 0) {
  69. if (!notice_again.checked) { // 关闭时所有数据暂停为0
  70. const updateData = uncheckedList.map(x => { return { id: x.id, status: 0 }; });
  71. await transaction.updateRows(this.tableName, updateData);
  72. } else {
  73. const updateData = [];
  74. // 分批次查看并是否插入
  75. for (const sp in notice_again.sp) {
  76. const splist = this._.filter(uncheckedList, function(item) {
  77. return item.sp_type === sp;
  78. });
  79. if (splist.length > 0) {
  80. const spUpData = splist.map(x => { return { id: x.id, status: notice_again.sp[sp] ? 1 : 0 }; });
  81. updateData.push(...spUpData);
  82. }
  83. }
  84. if (updateData.length > 0) {
  85. await transaction.updateRows(this.tableName, updateData);
  86. }
  87. }
  88. }
  89. }
  90. }
  91. return NoticeAgain;
  92. };