chenzx
2024-12-11 4043a2fe2103c7960b74acbccdcc876d11f83f0f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommonHelper;
using Dapper;
using GasolineBlend.Entity;
using Google.Protobuf.WellKnownTypes;
using Microsoft.Office.Interop.Excel;
using MySql.Data.MySqlClient;
 
namespace GasolineBlend.DAL
{
    public class AgentDataDAL : BaseMySQLDAL
    {
        public List<AgentDataPage> GetAgentDataList(string Keyword, int PageNum, int PageSize, string AgentType, string CreatorId, string FavoriteFlag)
        {
            using (IDbConnection connection = new MySqlConnection(connectionString))
            {
                var sql = $"SELECT a.id,a.name,a.prompt,a.permission,a.creationtime,a.modificationtime,a.avatarlink,a.creatorid,a.agenttype,a.description,a.userno,a.questionno,a.modelapi,a.modelkey,CASE WHEN s.agentid IS NOT NULL THEN 1 ELSE 0 END AS favoriteflag FROM agentdata a LEFT JOIN staragent s ON a.id = s.agentid AND s.userid = '{CreatorId}' WHERE 1=1";
                if (!string.IsNullOrEmpty(Keyword)) {
                    sql += $" and a.name like '%{Keyword.Trim()}%' ";
                }
                if (!string.IsNullOrEmpty(FavoriteFlag)&& FavoriteFlag=="1")
                {
                    sql += $" and s.userid = '{CreatorId.Trim()}' and (CASE WHEN s.agentid IS NOT NULL THEN 1 ELSE 0 END) = 1 ";
                }
                else {
                    if (!string.IsNullOrEmpty(AgentType))
                    {
                        sql += $" and a.agenttype = '{AgentType.Trim()}' ";
                    }
                    sql += $" and a.permission = 1 "; 
                }
                int offset = (PageNum - 1) * PageSize;
                sql += $" ORDER BY a.userno DESC  LIMIT {PageSize} OFFSET {offset}";
 
                return connection.Query<AgentDataPage>(sql).ToList();
            }
        }
      
        public int GetAgentDataCount(string Keyword, string AgentType, string CreatorId, string FavoriteFlag)
        {
            using (IDbConnection connection = new MySqlConnection(connectionString))
            {
                var sql = $"SELECT COUNT(*) FROM agentdata a LEFT JOIN staragent s ON a.id = s.agentid AND s.userid = '{CreatorId}' WHERE 1=1 ";
                if (!string.IsNullOrEmpty(Keyword))
                {
                    sql += $" and a.name like '%{Keyword.Trim()}%' ";
                }
                if (!string.IsNullOrEmpty(FavoriteFlag) && FavoriteFlag == "1")
                {
                    sql += $" and s.userid = '{CreatorId.Trim()}' and (CASE WHEN s.agentid IS NOT NULL THEN 1 ELSE 0 END) = 1 ";
                }
                else
                {
                    if (!string.IsNullOrEmpty(AgentType))
                    {
                        sql += $" and a.agenttype = '{AgentType.Trim()}' ";
                    }
                    sql += $" and a.permission = 1 ";
                }
                sql += $"  ORDER BY modificationtime DESC ";
                int Count = connection.Query<int>(sql).FirstOrDefault();
                return Count;
            }
        }
 
        public List<AgentDataPage> GetMyAgentDataList(string Keyword, int PageNum, int PageSize, string AgentType, string CreatorId)
        {
            using (IDbConnection connection = new MySqlConnection(connectionString))
            {
                var sql = $"SELECT a.id,a.name,a.prompt,a.permission,a.creationtime,a.modificationtime,a.avatarlink,a.creatorid,a.agenttype,a.description,a.userno,a.questionno,a.modelapi,a.modelkey,CASE WHEN s.agentid IS NOT NULL THEN 1 ELSE 0 END AS favoriteflag FROM agentdata a LEFT JOIN staragent s ON a.id = s.agentid AND s.userid = '{CreatorId}' WHERE a.creatorid = '{CreatorId.Trim()}' ";
                if (!string.IsNullOrEmpty(Keyword))
                {
                    sql += $" and a.name like '%{Keyword.Trim()}%' ";
                }
                if (!string.IsNullOrEmpty(AgentType))
                {
                        sql += $" and a.agenttype = '{AgentType.Trim()}' ";
                }
                
                int offset = (PageNum - 1) * PageSize;
                sql += $" ORDER BY a.userno DESC  LIMIT {PageSize} OFFSET {offset}";
 
                return connection.Query<AgentDataPage>(sql).ToList();
            }
        }
        public int GetMyAgentDataCount(string Keyword, string AgentType, string CreatorId)
        {
            using (IDbConnection connection = new MySqlConnection(connectionString))
            {
                var sql = $"SELECT COUNT(*) FROM agentdata a LEFT JOIN staragent s ON a.id = s.agentid AND s.userid = '{CreatorId}' WHERE  a.creatorid = '{CreatorId.Trim()}' ";
                if (!string.IsNullOrEmpty(Keyword))
                {
                    sql += $" and a.name like '%{Keyword.Trim()}%' ";
                }
                if (!string.IsNullOrEmpty(AgentType))
                 {
                        sql += $" and a.agenttype = '{AgentType.Trim()}' ";
                 }
                sql += $"  ORDER BY modificationtime DESC ";
                int Count = connection.Query<int>(sql).FirstOrDefault();
                return Count;
            }
        }
 
        public bool DelAgentDataById(int id)
        {
            using (IDbConnection connection = new MySqlConnection(connectionString))
            {
                var sql = $"delete from agentdata where id={id}";
                connection.Open();
                return connection.Execute(sql) > 0;
            }
        }
 
        public bool UpdateAgentdata(AgentDataPage agentDataPage)
        {
            using (IDbConnection connection = new MySqlConnection(connectionString))
            {
                var sql = @"
                UPDATE agentdata
                SET
                    name = @Name,
                    prompt = @Prompt,
                    permission = @Permission,
                    agenttype = @AgentType,
                    modificationTime = NOW(),
                    avatarLink = @AvatarLink,
                    description = @Description,
                    modelapi = @ModelApi,
                    modelkey = @ModelKey
                WHERE id = @Id";
 
                var parameters = new
                {
                    Name = agentDataPage.Name,
                    Prompt = agentDataPage.Prompt,
                    Permission = agentDataPage.Permission,
                    AvatarLink = agentDataPage.AvatarLink,
                    Id = agentDataPage.Id,
                    AgentType = agentDataPage.AgentType,
                    Description = agentDataPage.Description,
                    ModelApi = agentDataPage.ModelApi,
                    ModelKey = agentDataPage.ModelKey
                };
 
                connection.Open(); // 打开数据库连接
                int affectedRows = connection.Execute(sql, parameters); // 执行更新操作
                return affectedRows > 0;
            }
        }
 
        public bool AddAgentData(AgentDataPage agentDataPage)
        {
            using (IDbConnection connection = new MySqlConnection(connectionString))
            {
                var sql = @"
                INSERT INTO agentdata (name, prompt, permission, creationtime, modificationtime, avatarLink, creatorid,agenttype,description,modelapi,modelkey)
                VALUES ( @Name, @Prompt, @Permission,NOW(), NOW(), @AvatarLink, @CreatorId,@AgentType,@Description,@ModelApi,@ModelKey)";
 
                var parameters = new 
                {
                    Name = agentDataPage.Name,
                    Description = agentDataPage.Description,
                    Permission = agentDataPage.Permission,
                    AvatarLink = agentDataPage.AvatarLink,
                    CreatorId = agentDataPage.CreatorId,
                    AgentType = agentDataPage.AgentType,
                    Prompt = agentDataPage.Prompt,
                    ModelApi = agentDataPage.ModelApi,
                    ModelKey = agentDataPage.ModelKey
                };
 
                connection.Open(); // 打开数据库连接
                int affectedRows = connection.Execute(sql, parameters); // 执行更新操作
                return affectedRows > 0;
            }
        }
 
    }
}