2222from openpyxl .cell .cell import ILLEGAL_CHARACTERS_RE
2323from rest_framework import serializers
2424
25- from application .models import Chat , Application
25+ from application .models import Chat , Application , ChatRecord
2626from common .db .search import get_dynamics_model , native_search , native_page_search
2727from common .exception .app_exception import AppApiException
2828from common .utils .common import get_file_content
2929from maxkb .conf import PROJECT_DIR
30- from maxkb .settings import TIME_ZONE
30+ from maxkb .settings import TIME_ZONE , edition
3131
3232
3333class ApplicationChatResponseSerializers (serializers .Serializer ):
@@ -120,20 +120,17 @@ def get_query_set(self, select_ids=None):
120120 condition = base_condition & min_trample_query
121121 else :
122122 condition = base_condition
123- inner_queryset = QuerySet (Chat ).filter (application_id = self .data .get ("application_id" ))
124- if 'abstract' in self .data and self .data .get ('abstract' ) is not None :
125- inner_queryset = inner_queryset .filter (abstract__icontains = self .data .get ('abstract' ))
126123
127124 return {
128- 'inner_queryset' : inner_queryset ,
129125 'default_queryset' : query_set .filter (condition ).order_by ("-application_chat.update_time" )
130126 }
131127
132128 def list (self , with_valid = True ):
133129 if with_valid :
134130 self .is_valid (raise_exception = True )
135131 return native_search (self .get_query_set (), select_string = get_file_content (
136- os .path .join (PROJECT_DIR , "apps" , "application" , 'sql' , 'list_application_chat.sql' )),
132+ os .path .join (PROJECT_DIR , "apps" , "application" , 'sql' , ('list_application_chat_ee.sql' if ['PE' , 'EE' ].__contains__ (
133+ edition ) else 'list_application_chat.sql' ))),
137134 with_table_name = False )
138135
139136 @staticmethod
@@ -144,7 +141,7 @@ def paragraph_list_to_string(paragraph_list):
144141
145142 @staticmethod
146143 def to_row (row : Dict ):
147- details = row .get ('details' )
144+ details = row .get ('details' ) or {}
148145 padding_problem_text = ' ' .join (node .get ("answer" , "" ) for key , node in details .items () if
149146 node .get ("type" ) == 'question-node' )
150147 search_dataset_node_list = [(key , node ) for key , node in details .items () if
@@ -161,26 +158,28 @@ def to_row(row: Dict):
161158 'name' ) + ':\n ' + ApplicationChatQuerySerializers .paragraph_list_to_string (node .get ('paragraph_list' ,
162159 [])) for
163160 key , node in search_dataset_node_list ])
164- improve_paragraph_list = row .get ('improve_paragraph_list' )
161+ improve_paragraph_list = row .get ('improve_paragraph_list' ) or []
165162 vote_status_map = {'-1' : '未投票' , '0' : '赞同' , '1' : '反对' }
166163 return [str (row .get ('chat_id' )), row .get ('abstract' ), row .get ('problem_text' ), padding_problem_text ,
167164 row .get ('answer_text' ), vote_status_map .get (row .get ('vote_status' )), reference_paragraph_len ,
168165 reference_paragraph ,
169166 "\n " .join ([
170167 f"{ improve_paragraph_list [index ].get ('title' )} \n { improve_paragraph_list [index ].get ('content' )} "
171168 for index in range (len (improve_paragraph_list ))]),
172- row .get ('message_tokens' ) + row .get ('answer_tokens' ), row .get ('run_time' ),
169+ ( row .get ('message_tokens' ) or 0 ) + ( row .get ('answer_tokens' ) or 0 ), row .get ('run_time' ),
173170 str (row .get ('create_time' ).astimezone (pytz .timezone (TIME_ZONE )).strftime ('%Y-%m-%d %H:%M:%S' )
174- )]
171+ if row . get ( 'create_time' ) is not None else None )]
175172
176173 def export (self , data , with_valid = True ):
177174 if with_valid :
178175 self .is_valid (raise_exception = True )
179176 ApplicationChatRecordExportRequest (data = data ).is_valid (raise_exception = True )
177+
180178 data_list = native_search (self .get_query_set (data .get ('select_ids' )),
181179 select_string = get_file_content (
182180 os .path .join (PROJECT_DIR , "apps" , "application" , 'sql' ,
183- 'export_application_chat.sql' )),
181+ ('export_application_chat_ee.sql' if ['PE' , 'EE' ].__contains__ (
182+ edition ) else 'export_application_chat.sql' ))),
184183 with_table_name = False )
185184
186185 batch_size = 500
@@ -232,5 +231,26 @@ def page(self, current_page: int, page_size: int, with_valid=True):
232231 if with_valid :
233232 self .is_valid (raise_exception = True )
234233 return native_page_search (current_page , page_size , self .get_query_set (), select_string = get_file_content (
235- os .path .join (PROJECT_DIR , "apps" , "application" , 'sql' , 'list_application_chat.sql' )),
234+ os .path .join (PROJECT_DIR , "apps" , "application" , 'sql' ,
235+ ('list_application_chat_ee.sql' if ['PE' , 'EE' ].__contains__ (
236+ edition ) else 'list_application_chat.sql' ))),
236237 with_table_name = False )
238+
239+
240+ class ChatCountSerializer (serializers .Serializer ):
241+ chat_id = serializers .UUIDField (required = True , label = _ ("Conversation ID" ))
242+
243+ def get_query_set (self ):
244+ return QuerySet (ChatRecord ).filter (chat_id = self .data .get ('chat_id' ))
245+
246+ def update_chat (self ):
247+ self .is_valid (raise_exception = True )
248+ count_chat_record = native_search (self .get_query_set (), get_file_content (
249+ os .path .join (PROJECT_DIR , "apps" , "application" , 'sql' , 'count_chat_record.sql' )), with_search_one = True )
250+ QuerySet (Chat ).filter (id = self .data .get ('chat_id' )).update (star_num = count_chat_record .get ('star_num' , 0 ) or 0 ,
251+ trample_num = count_chat_record .get ('trample_num' ,
252+ 0 ) or 0 ,
253+ chat_record_count = count_chat_record .get (
254+ 'chat_record_count' , 0 ) or 0 ,
255+ mark_sum = count_chat_record .get ('mark_sum' , 0 ) or 0 )
256+ return True
0 commit comments