| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- const db = require('../../plugin/DataBase/db')
- const path = require('path')
- const Logger = require('../Logger')
- const mq = require('../../plugin/mq')
- const { mq: mqName } = require('../../plugin/mq/mqPrefix')
- const Redis = require('../../plugin/DataBase/Redis')
- const EmailTemplate = require('../../plugin/Email/emailTemplate')
- const { insertBindAudit, BindAuditAction, BindAuditSource } = require('./BindAudit')
- class WorkOrderMcp {
- constructor() {
- this.logger = new Logger(path.join(__dirname, '../logs/MCP.log'), 'INFO')
- this.messageQueue = mqName('runforge_message_queue')
- this.auto_day = [
- { label: '周一', value: 1 },
- { label: '周二', value: 2 },
- { label: '周三', value: 3 },
- { label: '周四', value: 4 },
- { label: '周五', value: 5 },
- { label: '周六', value: 6 },
- { label: '周日', value: 0 }
- ]
- this.area = ["兰花湖校区跑区", "主校区北跑区", "主校区南跑区", "重庆工商大学茶园校区", "随机分配"]
- this.auto_time = [
- { label: '随机分配', value: -1 },
- ...Array.from({ length: 17 }, (_, i) => {
- const hour = i + 7
- return { label: `${hour} ~ ${hour + 1}时`, value: hour }
- })
- ]
- this.autoTimeLabel = (record) => {
- if (record.auto_time === -1) {
- if (record.today_auto_time)
- return `随机-今日${record.today_auto_time}时`
- return '随机-待分配'
- }
- const match = this.auto_time.find(item => item.value === record.auto_time)
- return match ? match.label : '-'
- }
- this.emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
- this.banEmailList = ['icloud.com']
- this.dailyUnbindLimit = 5
- }
- async get_account_info({ sender, student_num }) {
- try {
- if ([sender, student_num].some(value => value === '' || value === null || value === undefined))
- return '缺少参数'
- this.logger.info(`MCP接收获取账号信息请求:${sender},student_num:${student_num}`)
- let sql = `
- SELECT
- l.name,
- l.student_num,
- l.state,
- l.area,
- l.auto_time,
- l.total_num,
- l.term_num,
- l.academy_name,
- l.sex,
- l.grade_id,
- l.email,
- l.auto_run,
- l.today_auto_time,
- l.target_count,
- l.auto_day,
- l.notice_type
- FROM
- lepao_account l
- WHERE
- l.student_num = ? AND l.create_user = ?
- `
- const rows = await db.query(sql, [student_num, sender])
- if (!rows || rows.length == 0) return '该账号未被您绑定,无法查看'
- let data = rows[0]
- let returnMsg = `
- 姓名:${data.name ?? '未更新,请使用乐跑登录器更新账号信息'};学号:${data.student_num};账号状态:${data.state === 1 ? '正常' : '需使用乐跑登录器更新账号信息'};乐跑跑区:${data.area == '' ? "随机分配" : data.area};自动乐跑状态:${data.auto_run === 1 ? '开启' : '关闭'}
- `
- if (data.auto_run === 1) {
- returnMsg += `自动乐跑时间:${this.autoTimeLabel(data)};`
- returnMsg += `自动乐跑星期:${data.auto_day.slice().sort((a, b) => {
- if (a === 0) return 1; if (b === 0) return -1; return a - b;
- }).map(day => this.auto_day.find(item => item.value === day)?.label).join(',')};`
- }
- if (data.sex) returnMsg += `性别:${data.sex === 1 ? '男' : '女'};`
- if (data.email) returnMsg += `邮箱:${data.email};`
- if (data.grade) returnMsg += `邮箱:${data.grade};`
- if (data.academy_name) returnMsg += `学院:${data.academy_name};`
- if (data.grade_id) returnMsg += `年级:${data.grade_id}级;`
- if (data.target_count) returnMsg += `目标乐跑次数:${data.target_count};`
- if (data.total_num) returnMsg += `累计乐跑次数:${data.total_num};`
- if (data.notice_type) returnMsg += `通知方式:${data.notice_type};`
- return returnMsg
- } catch (error) {
- this.logger.error(`MCP查询账号信息出错:${error.stack}`)
- return '系统出错,请稍后再试'
- }
- }
- getDailyUnbindRedisKey(sender) {
- const now = new Date()
- const year = now.getFullYear()
- const month = `${now.getMonth() + 1}`.padStart(2, '0')
- const day = `${now.getDate()}`.padStart(2, '0')
- return `mcp:unbind:daily:${sender}:${year}${month}${day}`
- }
- getSecondsToDayEnd() {
- const now = new Date()
- const tomorrow = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1, 0, 0, 0, 0)
- return Math.max(1, Math.floor((tomorrow.getTime() - now.getTime()) / 1000))
- }
- async unbind_account({ sender, student_num }) {
- try {
- if ([sender, student_num].some(value => value === '' || value === null || value === undefined))
- return '缺少参数'
- this.logger.info(`工单MCP接收解绑账号请求:${sender}`)
- const dailyUnbindKey = this.getDailyUnbindRedisKey(sender)
- const dailyUnbindCount = Number(await Redis.get(dailyUnbindKey) || 0)
- if (dailyUnbindCount >= this.dailyUnbindLimit) return '今日通过MCP解绑次数已超过限制,只能人工处理'
- let selectSql = `SELECT create_user, auto_run FROM lepao_account WHERE student_num = ? AND create_user IS NOT NULL`
- let selectRows = await db.query(selectSql, [student_num])
- if (!selectRows || selectRows.length === 0 || !selectRows[0]?.create_user) return '该账号未绑定,无需解绑'
- if (selectRows[0].auto_run === 1) return '该账号已开启自动乐跑,请用原绑定账号关闭自动乐跑后再解绑或联系人工客服处理'
- let updateSql = `UPDATE lepao_account SET create_user = NULL, auto_run = 0, update_time = ? WHERE student_num = ?`
- let updateRows = await db.query(updateSql, [Date.now(), student_num])
- if (!updateRows || updateRows.affectedRows !== 1)
- return '系统出错,请稍后再试'
- const auditOk = await insertBindAudit({
- studentNum: student_num,
- ownerUuid: selectRows[0].create_user || null,
- action: BindAuditAction.PLATFORM_UNBIND,
- source: BindAuditSource.MCP_WORK_ORDER,
- operatorUuid: sender || null,
- detail: { sender },
- createdAt: Date.now()
- })
- if (!auditOk) {
- this.logger.warn(`工单解绑审计写入失败 student_num=${student_num}`)
- }
- const latestUnbindCount = await Redis.incr(dailyUnbindKey)
- if (latestUnbindCount === 1) {
- await Redis.expire(dailyUnbindKey, this.getSecondsToDayEnd())
- }
- return `解绑成功,您现在可绑定该账号`
- } catch (error) {
- this.logger.error(`工单MCP解绑账号出错:${error.stack}`)
- return '系统出错,请稍后再试'
- }
- }
- async need_manual({ sender, order_id }) {
- try {
- if ([sender, order_id].some(value => value === '' || value === null || value === undefined))
- return '缺少参数'
- this.logger.info(`MCP接收转人工服务请求:${order_id}`)
- const selectSql = 'SELECT msg, state, email, need_manual FROM work_order WHERE id = ? AND create_user = ?'
- const selectRows = await db.query(selectSql, [order_id, sender])
- if (!selectRows || selectRows.length !== 1)
- return '未找到你创建的工单,请核对工单ID'
- if (selectRows[0].state === 2) return '工单已关闭,无法提交转人工服务,请提交新工单'
- if (selectRows[0].need_manual === 1) return '该工单已提交转人工服务,无需重复提交'
-
- let msg = selectRows[0].msg
- msg.push({
- time: Date.now(),
- content: '人工处理请求已提交,请耐心等待人工客服处理',
- uuid: 'e4fe0277-0b1a-41a1-b25f-8b6e4cec3281',
- type: 'system'
- })
- let updateSql = `UPDATE work_order SET msg = ?, need_manual = 1, update_time = ? WHERE id = ? AND create_user = ?`
- let updateRows = await db.query(updateSql, [msg, Date.now(), order_id, sender])
- if (!updateRows || updateRows.affectedRows !== 1) return '系统出错,请稍后再试'
-
- return `转人工服务请求已提交,后续消息AI助理小妍将不再回复`
- } catch (error) {
- this.logger.error(`MCP转人工服务出错:${error.stack}`)
- return '系统出错,请稍后再试'
- }
- }
- }
- const WORKORDERMCP = new WorkOrderMcp()
- module.exports.WORKORDERMCP = WORKORDERMCP
|