风雨同行 发表于 3 天前

当地摆设使用deepSeek自己天生sql 自动连接mysql自动执行sql得到效果后天生大屏统计表教程趁热打铁 后续使用无需一行代码

先看我弄好的效果 

我的数据库表 三个表  一个用户 一个角色表 一个用户角色关联表
https://i-blog.csdnimg.cn/direct/6b06f8024ff2411ca019172874c17ae8.png
https://i-blog.csdnimg.cn/direct/be6bfe6435ea48d882a2ad2de3a0859c.png
https://i-blog.csdnimg.cn/direct/5ef1d49ec9124cad89fef4d40ca173be.png



https://i-blog.csdnimg.cn/direct/d6501827c35548f8bb7b873d6fea4c5f.png

输入:统计本月各种角色注册数量


它最终给的网站地址 打开的效果:

https://i-blog.csdnimg.cn/direct/4a271fa57f2e417f8e676d9f95cf4f05.png
它天生的sql 
SELECT r.role_id, r.role_name, COUNT(DISTINCT ur.user_id) AS count FROM sys_role r LEFT JOIN sys_user_role ur ON r.role_id = ur.role_id LEFT JOIN sys_user u ON ur.user_id = u.user_id WHERE u.create_time >= DATE_FORMAT(CURDATE(), '%Y-%m-01') AND u.create_time < DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL 1 MONTH), '%Y-%m-01') AND u.del_flag = '0' AND r.status = '0' GROUP BY r.role_id, r.role_name ORDER BY r.role_sort;

然后自动调用API执行的效果
https://i-blog.csdnimg.cn/direct/226923c65c8f4cc1b1b0356a2e4e808c.png

https://i-blog.csdnimg.cn/direct/dd14caaa76c8475b9ef293e29999730c.png
末了自动根据 json   SQL 执行效果: [
  { ROLE_NAME: '超级管理员', REGISTER_COUNT: 1 },
  { ROLE_NAME: '普通客户', REGISTER_COUNT: 3 }
]天生网站如图  
https://i-blog.csdnimg.cn/direct/4a271fa57f2e417f8e676d9f95cf4f05.png

附带表格效果
https://i-blog.csdnimg.cn/direct/253b00617688472c88098ce8fcc8c87f.png

所需要准备

1.当地搭建dify 
2.ollama 拉取 deepseek -R1至少14B以上版本体验才够好
3.准备两个api  (我是让AI直接给我天生的nodejs服务端)一个用于执行AI天生的sql  执行后返回json效果  一个用于将AI天生的html代码天生html文件天生网站
流程大致说明:

使用dify创建工作流
1.开始: 准备两个变量  你的描述 和你要天生的类型 表格代码 还是天生统计图
https://i-blog.csdnimg.cn/direct/9cd7e5ed479148568c4f2c4cd4f3d980.png

2.将mysql表布局给到deepseek 加入你的描述
https://i-blog.csdnimg.cn/direct/76cf53d4fbdc4094b60ea23a03b186e2.png
https://i-blog.csdnimg.cn/direct/1cd3c570edfe4b0d8a93079f5c22fb3e.png

3.提取ds天生的sql文件 拿到后哀求API执行 
https://i-blog.csdnimg.cn/direct/8903b9473ce043d0992019dcc339a864.png
https://i-blog.csdnimg.cn/direct/f0e717b887e84fb5b52f57f568c948ed.png
4.接下来创建条件判定 是否查到数据 查到继承调用ds 将内容天生html代码
https://i-blog.csdnimg.cn/direct/89c949ef6e214f978e8f735a0d9a4b64.png

这次 你选择的类型就有效了 
https://i-blog.csdnimg.cn/direct/fa61402cb16249f8ad701405658087d9.png

提取html代码 将特殊符号转换
https://i-blog.csdnimg.cn/direct/12872f3d40f348078f52de2a40761958.png
调用API天生网站
https://i-blog.csdnimg.cn/direct/0d2abbfb25844729bfacb529b4285f43.png

输出效果
https://i-blog.csdnimg.cn/direct/20d430e210d746a8ab04bc636cba58ab.png
两个API如下: app.js用于执行AI天生的sql 然后返回json效果
//数据库配置文件
module.exports= {
host: 'localhost',
user: 'root',
password: 'root',
database: 'ry-vue',
port: 3306,
dialect: 'mysql'
}; // app.js
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const mysql = require('mysql2/promise'); // 使用Promise接口
const dbConfig = require('./config/db');

const app = express();
app.use(bodyParser.json());
app.use(cors());

// 配置MySQL连接池(推荐)
const pool = mysql.createPool({
host: dbConfig.host,
port: dbConfig.port,
user: dbConfig.user,
password: dbConfig.password,
database: dbConfig.database,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
ssl: { rejectUnauthorized: false } // MySQL 8.0可能需要SSL配置
});

// API端点
const allowedSQLOperations = ['SELECT', 'SHOW']; // 仅允许查询操作
app.post('/execute-sql', async (req, res) => {
try {
    //const { sql } = req.body;
    const sql = req.body.sql.trim().toUpperCase();
    const token = req.body.token;

    // 基础token校验(生产环境需加强)
    if (!token || typeof token !== 'string') {
      return res.status(400).json({ error: 'Invalid token statement' });
    }

    // 基础SQL校验(生产环境需加强)
    if (!sql || typeof sql !== 'string') {
      return res.status(400).json({ error: 'Invalid SQL statement' });
    }
    if (!allowedSQLOperations.some(op => sql.startsWith(op))) {
      return res.status(403).json({ error: 'Operation not allowed' });
    }

    const connection = await pool.getConnection();
    try {
      console.log('执行的SQL:', sql);
      const = await connection.query(sql);
      console.log('SQL 执行结果:', results);
      res.json({
      success: true,
      data: results,
      meta: {
          affectedRows: results.affectedRows,
          insertId: results.insertId
      }
      });
    } finally {
      connection.release();// 释放连接回连接池
    }
} catch (error) {
    console.error('SQLError:', error);
    res.status(500).json({
      success: false,
      error: {
      code: error.code,
      message: error.sqlMessage || error.message
      }
    });
}
});

// 启动服务
const PORT = 3000;
app.listen(PORT, () => {
console.log(`API 服务已启动:http://localhost:${PORT}`);
});
//用于将生成的html代码生成html文件生成访问地址
const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();
const port = 3001;

// 解析 JSON 格式的请求体
app.use(express.json());

// 处理 POST 请求
app.post('/create-html',(req, res) => {
    // 从请求体中获取 HTML 代码
    var htmlCode = req.body.html;
   htmlCode = htmlCode.replace(/#XP#/g,'"');

    if (!htmlCode) {
      return res.status(400).send(' 请提供有效的 HTML 代码');
    }

    // 生成一个唯一的文件名
    const fileName = `generated-${Date.now()}.html`;
    const filePath = path.join(__dirname,'html-files', fileName);

    // 创建保存 HTML 文件的目录(如果不存在)
    const dir = path.dirname(filePath);
    if (!fs.existsSync(dir)){
      fs.mkdirSync(dir,{ recursive: true });
    }

    // 将 HTML 代码写入文件
    fs.writeFile(filePath,htmlCode, (err) => {
      if (err) {
            console.error(err);
            return res.status(500).send(' 保存 HTML 文件时出错');
      }

      // 返回文件路径
      const fileUrl = `http://10.10.7.196:3001/html-files/${fileName}`;
      res.status(200).send(`网站已经生成地址是:${fileUrl}`);
    });
});

// 静态文件服务,用于提供生成的 HTML 文件
app.use('/html-files',express.static(path.join(__dirname,'html-files')));

// 启动服务器
app.listen(port,() => {
    console.log(` 服务器运行在 http://localhost:${port}`);
});

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 当地摆设使用deepSeek自己天生sql 自动连接mysql自动执行sql得到效果后天生大屏统计表教程趁热打铁 后续使用无需一行代码