qfrjava
2025-03-26 37f98a71f6c11a6522655de97d1bac54f2e4d960
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
package com.smtaiserver.smtaiserver.control;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import org.springframework.web.servlet.ModelAndView;
 
import com.smtaiserver.smtaiserver.core.SMTAIServerApp;
import com.smtaiserver.smtaiserver.core.SMTAIServerRequest;
import com.smtaiserver.smtaiserver.database.SMTDatabase;
import com.smtaiserver.smtaiserver.database.SMTDatabase.DBRecord;
import com.smtaiserver.smtaiserver.database.SMTDatabase.DBRecords;
import com.smtservlet.core.SMTApp;
import com.smtservlet.util.SMTJsonWriter;
import com.smtservlet.util.SMTStatic;
 
public class SMTSystemManagerControl
{
    public ModelAndView getOperateLog(SMTAIServerRequest tranReq) throws Exception
    {
        Date startTime = tranReq.convParamToDate("start_time", false);
        Date endTime = tranReq.convParamToDate("end_time", false);
        String opType = tranReq.convParamToString("type", false);
        String opUser = tranReq.convParamToString("user", false);
        String opFrom = tranReq.convParamToString("from", false);
        
           SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {
            SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
            
            String sql = "SELECT A.*, B.user_name FROM sys_operate_log A LEFT JOIN sys_user_info B ON A.op_user=B.user_id WHERE 1=1 ";
            List<Object> sqlParams = new ArrayList<>();
            if(startTime != null)
            {
                sql += " AND op_time >=?";
                sqlParams.add(startTime);
            }
 
            if(endTime != null)
            {
                sql += " AND op_time <=?";
                sqlParams.add(endTime);
            }
            
            if(opType != null)
            {
                sql += " AND op_type in (SELECT regexp_split_to_table(?, ','))";
                sqlParams.add(opType);
            }
            
            if(opUser != null)
            {
                sql += " AND B.user_name LIKE ?";
                sqlParams.add("%" + opUser + "%");
            }
            
            if(opFrom != null)
            {
                sql += " AND op_from in (SELECT regexp_split_to_table(?, ','))";
                sqlParams.add(opType);
            }
            
            sql += " ORDER BY op_time DESC LIMIT 1000";
            
            DBRecords recs = db.querySQL(sql, sqlParams.toArray(new Object[sqlParams.size()]));
            
            jsonWr.beginArray("values");
            for(DBRecord rec : recs.getRecords())
            {
                jsonWr.beginMap(null);
                {
                    jsonWr.addKeyValue("id", rec.getString("op_id"));
                    jsonWr.addKeyValue("type", rec.getString("op_type"));
                    jsonWr.addKeyValue("time", rec.getString("op_time"));
                    jsonWr.addKeyValue("user", rec.getString("user_name"));
                    jsonWr.addKeyValue("note", rec.getString("op_note"));
                    jsonWr.addKeyValue("from", rec.getString("op_from"));
                    jsonWr.addKeyValue("client_ip", rec.getString("client_ip"));
                }
                jsonWr.endMap();
            }
            jsonWr.endArray();
            
            return tranReq.returnJson(jsonWr);
        }
        finally
        {
            db.close();
        }    
    }
    
    public ModelAndView updateUserRoles(SMTAIServerRequest tranReq) throws Exception
    {
        String userId = tranReq.convParamToString("user_id", true);
        String[] userRoles = tranReq.convParamToStringArray("user_roles", false);
        
        SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {
            DBRecords recs = db.querySQL("SELECT user_name FROM sys_user_info WHERE user_id=?", new Object[] {userId});
            if(recs.getRowCount() == 0)
                return tranReq.returnJsonState(false, "未发现用户", null);
            String userName = recs.getRecord(0).getString(0);
            
            db.executeSQL("BEGIN", null);
            try
            {
                db.executeSQL("DELETE FROM sys_role_user WHERE user_id=?", new Object[] {userId});
                if(userRoles != null)
                {
                    for(String userRole : userRoles)
                    {
                        db.executeSQL("INSERT INTO sys_role_user(user_id, role_id)VALUES(?,?)", new Object[] {userId, userRole});
                    }
                }
                
                tranReq.appendOperateLog(db, "更新角色", "更新用户[" + userName + "]的角色");
                
                db.executeSQL("COMMIT", null);
                return tranReq.returnJsonState(true, null, null);
 
            }
            catch(Exception ex)
            {
                db.rollbackDB(ex);
                return null;
            }
                
            
        }
        finally
        {
            db.close();
        }
    }
    
    public ModelAndView getRoleInfoList(SMTAIServerRequest tranReq) throws Exception
    {
        SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {
            SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
            DBRecords recsRoles = db.querySQL("SELECT * FROM sys_role_info", null);
            
            jsonWr.beginArray("values");
            for(DBRecord recRole : recsRoles.getRecords())
            {
                jsonWr.beginMap(null);
                {
                    jsonWr.addKeyValue("id", recRole.getString("role_id"));
                    jsonWr.addKeyValue("title", recRole.getString("role_title"));
                }
                jsonWr.endMap();
            }
            jsonWr.endArray();
                
            
            return tranReq.returnJson(jsonWr);
        }
        finally
        {
            db.close();
        }
    }    
    
    public ModelAndView addUserInfo(SMTAIServerRequest tranReq) throws Exception
    {
        String userName = tranReq.convParamToString("user_name", true);
        String password = SMTStatic.convStrToMD5(tranReq.convParamToString("password", true));
        String userPhone = tranReq.convParamToString("phone", false);
        String realName = tranReq.convParamToString("real_name", false);
        String userPart = tranReq.convParamToString("part", false);
        String userSex = tranReq.convParamToString("sex", false);
        String userEmail = tranReq.convParamToString("email", false);
        String userNote = tranReq.convParamToString("note", false);
        String[] userRoles = tranReq.convParamToStringArray("user_roles", false);
        
        SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {
            String userId = SMTStatic.newUUID();
            
            DBRecords recs = db.querySQL("SELECT 1 FROM sys_user_info WHERE user_name=? AND user_state_code='Y'", new Object[] {userName});
            if(recs.getRowCount() != 0)
                return tranReq.returnJsonState(false, "用户已经存在", null);
            
            db.executeSQL("BEGIN", null);
            try
            {
                SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
                db.executeSQL("INSERT INTO sys_user_info(user_id, user_name, user_phone, real_name, user_part, user_sex, user_email, note,user_pass,user_state_code,create_time)VALUES(?,?,?,?,?,?,?,?,?,?,?)", new Object[] {
                        userId,
                        userName,
                        userPhone, 
                        realName,
                        userPart,
                        userSex,
                        userEmail,
                        userNote,
                        password,
                        "Y",
                        new Date()
                        });
                
                if(userRoles != null)
                {
                    for(String userRole : userRoles)
                    {
                        db.executeSQL("INSERT INTO sys_role_user(user_id, role_id)VALUES(?,?)", new Object[] {userId, userRole});
                    }
                }
                
                tranReq.appendOperateLog(db, "创建用户", "创建用户:" + userName);
                
                db.executeSQL("COMMIT", null);
                
                jsonWr.addKeyValue("user_id", userId);
                
                return tranReq.returnJson(jsonWr);
            }
            catch(Exception ex)
            {
                db.executeSQL("ROLLBACK", null);
                throw ex;
            }           
        }
        finally
        {
            db.close();
        }
    }
    
    public ModelAndView deleteUserInfo(SMTAIServerRequest tranReq) throws Exception
    {
        String userId = tranReq.convParamToString("user_id", true);
        
        SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {
            SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
            
            DBRecords recs = db.querySQL("SELECT user_name FROM sys_user_info WHERE user_id=?", new Object[] {userId});
            if(recs.getRowCount() == 0)
                return tranReq.returnJsonState(false, "未发现用户", null);
            String userName = recs.getRecord(0).getString(0);
            
            db.executeSQL("BEGIN", null);
            try
            {
                db.executeSQL("UPDATE sys_user_info SET user_state_code='N' WHERE user_id=?", new Object[] {userId});
                db.executeSQL("DELETE FROM sys_role_user WHERE user_id=?", new Object[] {userId});
                
                tranReq.appendOperateLog(db, "删除用户", "删除用户:" + userName);
                
                db.executeSQL("COMMIT", null);
            }
            catch(Exception ex)
            {
                db.rollbackDB(ex);
                return null;
            }
            
            return tranReq.returnJson(jsonWr);
        }
        finally
        {
            db.close();
        }
    }    
    public ModelAndView updateUserInfo(SMTAIServerRequest tranReq) throws Exception
    {
        String userId = tranReq.convParamToString("user_id", true);
        String userPhone = tranReq.convParamToString("phone", false);
        String realName = tranReq.convParamToString("real_name", false);
        String userPart = tranReq.convParamToString("part", false);
        String userSex = tranReq.convParamToString("sex", false);
        String userEmail = tranReq.convParamToString("email", false);
        String userNote = tranReq.convParamToString("note", false);
        
        SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {
            DBRecords recs = db.querySQL("SELECT user_name FROM sys_user_info WHERE user_id=?", new Object[] {userId});
            if(recs.getRowCount() == 0)
                return tranReq.returnJsonState(false, "未发现用户", null);
            String userName = recs.getRecord(0).getString(0);
            
            SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
            db.executeSQL("UPDATE sys_user_info SET user_phone=?,real_name=?,user_part=?,user_sex=?,user_email=?,note=? WHERE user_id=?", new Object[] {
                    userPhone, 
                    realName,
                    userPart,
                    userSex,
                    userEmail,
                    userNote,
                    userId
                    });
            
            tranReq.appendOperateLog(db, "更新用户", "更新用户[" + userName + "]的信息");
            
            return tranReq.returnJson(jsonWr);
        }
        finally
        {
            db.close();
        }
    }
    
    public ModelAndView changeUserPassword(SMTAIServerRequest tranReq) throws Exception
    {
        String userId = tranReq.convParamToString("user_id", true);
        String password = SMTStatic.convStrToMD5(tranReq.convParamToString("password", true));
        
        SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {
            DBRecords recs = db.querySQL("SELECT user_name FROM sys_user_info WHERE user_id=?", new Object[] {userId});
            if(recs.getRowCount() == 0)
                return tranReq.returnJsonState(false, "未发现用户", null);
            String userName = recs.getRecord(0).getString(0);
            
            SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
            db.executeSQL("UPDATE sys_user_info SET user_pass=? WHERE user_id=?", new Object[] {password, userId});
            tranReq.appendOperateLog(db, "修改密码", "修改用户[" + userName + "]的密码");
            
            return tranReq.returnJson(jsonWr);
        }
        finally
        {
            db.close();
        }
    }
    
    public ModelAndView getUserInfoList(SMTAIServerRequest tranReq) throws Exception
    {
        SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {
            SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
            DBRecords recsUser = db.querySQL("SELECT U.*, P.department_name FROM sys_user_info U LEFT JOIN sys_department_info P ON U.user_part=P.department_id  WHERE user_state_code='Y'", null);
            DBRecords recsRoles = db.querySQL("SELECT * FROM sys_role_user", null);
            
            Map<String, Set<String>> mapUserId2RoleSet = new HashMap<>();
            for(DBRecord recRole : recsRoles.getRecords())
            {
                String roleId = recRole.getString("role_id");
                String userId = recRole.getString("user_id");
                Set<String> setRole = mapUserId2RoleSet.get(userId);
                if(setRole == null)
                {
                    setRole = new HashSet<>();
                    mapUserId2RoleSet.put(userId, setRole);
                }
                setRole.add(roleId);
            }
            
            jsonWr.beginArray("values");
            for(DBRecord recUser : recsUser.getRecords())
            {
                jsonWr.beginMap(null);
                {
                    String userId = recUser.getString("user_id");
                    jsonWr.addKeyValue("id", userId);
                    jsonWr.addKeyValue("phone", recUser.getString("user_phone"));
                    jsonWr.addKeyValue("name", recUser.getString("user_name"));
                    jsonWr.addKeyValue("real_name", recUser.getString("real_name"));
                    jsonWr.addKeyValue("part_id", recUser.getString("user_part"));
                    jsonWr.addKeyValue("part_name", recUser.getString("department_name"));
                    jsonWr.addKeyValue("sex", recUser.getString("user_sex"));
                    jsonWr.addKeyValue("email", recUser.getString("user_email"));
                    jsonWr.addKeyValue("note", recUser.getString("note"));    
                    jsonWr.addKeyValue("create_time", recUser.getString("create_time"));
                    
                    Set<String> setRole = mapUserId2RoleSet.get(userId);
                    if(setRole != null)
                    {
                        jsonWr.beginArray("roles");
                        for(String role : setRole)
                        {
                            jsonWr.addKeyValue(null, role);
                        }
                        jsonWr.endArray();
                    }
                }
                jsonWr.endMap();
            }
            jsonWr.endArray();
                
            
            return tranReq.returnJson(jsonWr);
        }
        finally
        {
            db.close();
        }
    }
    
    public ModelAndView getChatHistoryAnswer(SMTAIServerRequest tranReq) throws Exception
    {
        String historyId = tranReq.convParamToString("id", true);
        
           SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {
            SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);    
            
            DBRecords recs = db.querySQL("SELECT ask_json, trace_llm FROM chat_history_detail WHERE history_id=?", new Object[] {historyId});
            if(recs.getRowCount() == 0)
                return tranReq.returnJsonState(false, "未找到对话id", null);
            
            DBRecord rec = recs.getRecord(0);
            String sAskJson = rec.getString("ask_json");
            if(!SMTStatic.isNullOrEmpty(sAskJson))
                jsonWr.addKeyRaw("ask_json", sAskJson);
            
            jsonWr.addKeyValue("ask_log", rec.getString("trace_llm"));
            
            return tranReq.returnJson(jsonWr);
        }
        finally
        {
            db.close();
        }
    }
    
    public ModelAndView getChatHistoryList(SMTAIServerRequest tranReq) throws Exception
    {
        Date startTime = tranReq.convParamToDate("start_time", false);
        Date endTime = tranReq.convParamToDate("end_time", false);
        String groupType = tranReq.convParamToString("group_type", false);
        String opUser = tranReq.convParamToString("user", false);
        String question = tranReq.convParamToString("question", false);
        String state = tranReq.convParamToString("state", false);
        
           SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {
            SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
            
            String sql = 
                " SELECT A.history_id, A.group_id, A.question, A.create_time, A.run_ms, A.chat_state, A.group_type, B.user_name"
              +    " FROM chat_history_detail A"
              + " INNER JOIN chat_history_group C ON A.group_id=C.group_id"
              + " LEFT JOIN sys_user_info B ON C.user_id=B.user_id"
              + " WHERE 1=1 "
              ;
            List<Object> sqlParams = new ArrayList<>();
            if(startTime != null)
            {
                sql += " AND A.create_time >=?";
                sqlParams.add(startTime);
            }
 
            if(endTime != null)
            {
                sql += " AND A.create_time <=?";
                sqlParams.add(endTime);
            }
            
            if(groupType != null)
            {
                sql += " AND A.group_type in (SELECT regexp_split_to_table(?, ','))";
                sqlParams.add(groupType);
            }
            
            if(opUser != null)
            {
                sql += " AND B.user_name LIKE ?";
                sqlParams.add("%" + opUser + "%");
            }
            
            if(question != null)
            {
                sql += " AND A.question LIKE ?";
                sqlParams.add("%" + question + "%");
            }
            
            if(state != null)
            {
                sql += " AND A.chat_state = ?";
                sqlParams.add(state);
            }
            
            
            sql += " ORDER BY create_time DESC LIMIT 1000";
            
            DBRecords recs = db.querySQL(sql, sqlParams.toArray(new Object[sqlParams.size()]));
            
            jsonWr.beginArray("values");
            for(DBRecord rec : recs.getRecords())
            {
                jsonWr.beginMap(null);
                {
                    jsonWr.addKeyValue("id", rec.getString("history_id"));
                    jsonWr.addKeyValue("group_id", rec.getString("group_id"));
                    jsonWr.addKeyValue("time", rec.getString("create_time"));
                    jsonWr.addKeyValue("user", rec.getString("user_name"));
                    jsonWr.addKeyValue("question", rec.getString("question"));
                    jsonWr.addKeyValue("run_ms", rec.getString("run_ms"));
                    jsonWr.addKeyValue("state", rec.getString("chat_state"));
                    jsonWr.addKeyValue("group_type", rec.getString("group_type"));
                }
                jsonWr.endMap();
            }
            jsonWr.endArray();
            
            return tranReq.returnJson(jsonWr);
        }
        finally
        {
            db.close();
        }    
        
    }
    
    public ModelAndView getDepartmentList(SMTAIServerRequest tranReq) throws Exception
    {
           SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {
            SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
            
            DBRecords recs = db.querySQL("SELECT D.*, U.user_name FROM sys_department_info D LEFT JOIN sys_user_info U ON U.user_id=D.create_user", null);
            
            jsonWr.beginArray("values");
            for(DBRecord rec : recs.getRecords())
            {
                jsonWr.beginMap(null);
                {
                    jsonWr.addKeyValue("id", rec.getString("department_id"));
                    jsonWr.addKeyValue("parent_id", rec.getString("department_p_id"));
                    jsonWr.addKeyValue("name", rec.getString("department_name"));
                    jsonWr.addKeyValue("create_user", rec.getString("user_name"));
                    jsonWr.addKeyValue("create_time", rec.getString("create_time"));
                }
                jsonWr.endMap();
            }
            jsonWr.endArray();
            
            return tranReq.returnJson(jsonWr);
        }
        finally
        {
            db.close();
        }
 
    }
    
    public ModelAndView addDepartmentInfo(SMTAIServerRequest tranReq) throws Exception
    {
        String partName = tranReq.convParamToString("name", false);
        String parentId = tranReq.convParamToString("parent_id", false);
           SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {
            SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
            String partId = SMTStatic.newUUID();
            
            db.executeSQL(
                "INSERT INTO sys_department_info(department_id, department_p_id, department_name, create_user, create_time)VALUES(?,?,?,?,?)", 
                new Object[] {
                    partId,
                    parentId,
                    partName,
                    tranReq.getLoginUserId(),
                    new Date()
                });
            
            jsonWr.addKeyValue("id", partId);
            return tranReq.returnJson(jsonWr);
        }
        finally
        {
            db.close();
        }        
    }
    
    public ModelAndView deleteDepartment(SMTAIServerRequest tranReq) throws Exception
    {
        String partId = tranReq.convParamToString("id", true);
           SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {
            DBRecords recs = db.querySQL("SELECT COUNT(*) FROM sys_department_info WHERE department_p_id=?", new Object[] {partId});
            if(recs.getRecord(0).getInteger(0) > 0)
                return tranReq.returnJsonState(false, "当前部门有下属子部门", null);
            
            recs = db.querySQL("SELECT COUNT(*) FROM sys_user_info WHERE user_part=?", new Object[] {partId});
            if(recs.getRecord(0).getInteger(0) > 0)
                return tranReq.returnJsonState(false, "当前部门有所属用户", null);       
            
            db.executeSQL("DELETE FROM sys_department_info WHERE department_id=?", new Object[] {partId});
            
            return tranReq.returnJsonState(true, null, null);
        }
        finally
        {
            db.close();
        }  
    }
    
    public ModelAndView updateDepartmentInfo(SMTAIServerRequest tranReq) throws Exception
    {
        String partId = tranReq.convParamToString("id", true);
        String partName = tranReq.convParamToString("name", true);
           SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {
            db.executeSQL("UPDATE sys_department_info SET department_name=? WHERE department_id=?", new Object[] {partName, partId});
            
            return tranReq.returnJsonState(true, null, null);
        }
        finally
        {
            db.close();
        }  
    }
 
    public ModelAndView getUserSampleList(SMTAIServerRequest tranReq) throws Exception
    {
        String userId = tranReq.convParamToString("user_id", true);
           SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {
            String sql = 
                  "SELECT\r\n"
                + "    group_id, sample_id, sample_question, sample_title\r\n"
                + "FROM\r\n"
                + "    ai_scene_sample\r\n"
                + "WHERE\r\n"
                + "   sample_enable='Y' AND \n" 
                + "      group_id IN (\r\n"
                + "        SELECT\r\n"
                + "            group_id\r\n"
                + "        FROM\r\n"
                + "            sys_role_group\r\n"
                + "        WHERE\r\n"
                + "            role_id IN (\r\n"
                + "                SELECT\r\n"
                + "                    role_id\r\n"
                + "                FROM\r\n"
                + "                    sys_role_user\r\n"
                + "                WHERE\r\n"
                + "                    user_id=?\r\n"
                + "            )\r\n"
                + "    \r\n"
                + "    )";
            
            DBRecords recs = db.querySQL(sql, new Object[] {userId});
            
            SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
            jsonWr.beginArray("values");
            for(DBRecord rec : recs.getRecords())
            {
                jsonWr.beginMap(null);
                {
                    jsonWr.addKeyValue("sample_id", rec.getString("sample_id"));
                    jsonWr.addKeyValue("group_id", rec.getString("group_id"));
                    jsonWr.addKeyValue("question", rec.getString("sample_question"));
                    jsonWr.addKeyValue("title", rec.getString("sample_title"));
                }
                jsonWr.endMap();
            }
            jsonWr.endArray();
            
            return tranReq.returnJson(jsonWr);
        }
        finally
        {
            db.close();
        }  
    }
    
    public ModelAndView getLLMInfoList(SMTAIServerRequest tranReq) throws Exception
    {
           SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
        try
        {
            SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
            
            DBRecords recsFactory = db.querySQL("SELECT * FROM ai_llm_factory", null);
            DBRecords recsConnect = db.querySQL("SELECT * FROM ai_llm_connect", null);
            
            jsonWr.beginMap("values");
            for(DBRecord rec : recsFactory.getRecords())
            {
                jsonWr.beginMap(rec.getString("factory_id"));
                {
                    jsonWr.addKeyValue("title", rec.getString("factory_title"));
                    
                    String sJsonConfig = rec.getString("factory_args");
                    if(!SMTStatic.isNullOrEmpty(sJsonConfig))
                        jsonWr.addKeyRaw("config", sJsonConfig);
                }
                jsonWr.endMap();
            }
            
            for(DBRecord rec : recsConnect.getRecords())
            {
                jsonWr.beginMap(rec.getString("factory_id"));
                {
                    jsonWr.beginMap("connects");
                    {
                        jsonWr.beginMap(rec.getString("connect_id"));
                        {
                            jsonWr.addKeyValue("title", rec.getString("connect_title"));
                            jsonWr.addKeyValue("class", rec.getString("class_name"));
                            String sJsonConfig = rec.getString("connect_args");
                            if(!SMTStatic.isNullOrEmpty(sJsonConfig))
                                jsonWr.addKeyRaw("config", sJsonConfig);
                        }
                        jsonWr.endMap();
                    }
                    jsonWr.endMap();
                }
                jsonWr.endMap();
            }
            
            jsonWr.endMap();
            
            return tranReq.returnJson(jsonWr);
        }
        finally
        {
            db.close();
        }
    }
    
    
    public ModelAndView clearSystemCache(SMTAIServerRequest tranReq) throws Exception
    {
        SMTApp.getThis().clearEhCacheManager();
        return tranReq.returnJsonState(true, null, null);
    }
    
    
    public ModelAndView getQuestionReplaceList(SMTAIServerRequest tranReq) throws Exception
    {
        SMTJsonWriter jsonWr = tranReq.newReturnJsonWriter(true, null, null);
        try(SMTDatabase db = SMTAIServerApp.getApp().allocDatabase())
        {
            DBRecords recs = db.querySQL("SELECT * FROM ai_question_replace", null);
            
            jsonWr.beginArray("values");
            for(DBRecord rec : recs.getRecords())
            {
                jsonWr.beginMap(null);
                {
                    jsonWr.addKeyValue("id", rec.getString("replace_id"));
                    jsonWr.addKeyValue("question", rec.getString("question_text"));
                    jsonWr.addKeyValue("replace", rec.getString("replace_text"));
                    jsonWr.addKeyValue("order", rec.getString("replace_order"));
                    jsonWr.addKeyValue("state", rec.getString("replace_state"));
                    jsonWr.addKeyValue("group", rec.getString("group_type"));
                    jsonWr.addKeyValue("create_time", rec.getString("create_time"));
                }
                jsonWr.endMap();
            }
            jsonWr.endArray();
        }
        return tranReq.returnJson(jsonWr);
    }
}