'use strict'; /** * 审批信息重新发送modal * * @author EllisRan * @date 2024/01/18 * @version */ const smsTypeConst = require('../const/sms_type'); module.exports = app => { class NoticeAgain extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'notice_again'; } // 入库并判断 async addNoticeAgain(transaction, type, datas) { // 判断用户是否已绑定公众号或者企业微信, 并判断用户是否开启了重新发送并且钩上对应的审批 const userInfo = await this.ctx.service.projectAccount.getDataById(datas.uid); if (!userInfo.wx_type || userInfo.wx_type === '') return; const wxType = JSON.parse(userInfo.wx_type); // 没开启发送功能不应该生成重新发送入库数据 if (wxType[type] && wxType[type].indexOf(smsTypeConst.judge.approval.toString()) !== -1) { datas.status = 0; const notice_again = userInfo.notice_again ? JSON.parse(userInfo.notice_again) : null; if ((userInfo.wx_openid || userInfo.qywx_userid) && notice_again && notice_again.checked && notice_again.sp[datas.sp_type]) { datas.status = 1; } datas.in_time = new Date(); datas.last_time = this.ctx.helper.dateTran(new Date(), 'YYYY-MM-DD HH:mm'); datas.times = 0; datas.wx_data = JSON.stringify(datas.wx_data); datas.sms_type = type; datas.origin_url = JSON.stringify({ host: this.ctx.host, protocol: 'https', // 改为固定防止发送有问题 }); await transaction.insert(this.tableName, datas); } } async stopNoticeAgain(transaction, table_name, sp_id, status = 2) { // 判断sp_id是字符还是数组 // if (sp_id instanceof Array) { // for (const id of sp_id) { // await transaction.update(this.tableName, { status }, { where: { table_name, sp_id: id } }); // } // } else { await transaction.update(this.tableName, { status }, { where: { table_name, sp_id } }); // } } // 针对撤回删除对应的重新发送数据 async deleteNoticeAgain(transaction, table_name, sp_id) { // 判断sp_id是字符还是数组 await transaction.delete(this.tableName, { table_name, sp_id }); } async getSpResult(table_name, sp_id) { const sql = 'SELECT * FROM ?? WHERE id = ?'; const sqlParam = [table_name, sp_id]; return await this.db.queryOne(sql, sqlParam); } async updateUserNoticeAgain(transaction, uid, notice_again) { const list = await transaction.select(this.tableName, { where: { uid } }); const uncheckedList = this._.filter(list, function(item) { return item.status !== 2; }); if (uncheckedList.length > 0) { if (!notice_again.checked) { // 关闭时所有数据暂停为0 const updateData = uncheckedList.map(x => { return { id: x.id, status: 0 }; }); await transaction.updateRows(this.tableName, updateData); } else { const updateData = []; // 分批次查看并是否插入 for (const sp in notice_again.sp) { const splist = this._.filter(uncheckedList, function(item) { return item.sp_type === sp; }); if (splist.length > 0) { const spUpData = splist.map(x => { return { id: x.id, status: notice_again.sp[sp] ? 1 : 0 }; }); updateData.push(...spUpData); } } if (updateData.length > 0) { await transaction.updateRows(this.tableName, updateData); } } } } } return NoticeAgain; };