package com.ruoyi.cc.task;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.aicall.domain.CcCallPhone;
import com.ruoyi.aicall.domain.CcCallTask;
import com.ruoyi.aicall.domain.CcLlmAgentAccount;
import com.ruoyi.aicall.service.ICcCallPhoneService;
import com.ruoyi.aicall.service.ICcCallTaskService;
import com.ruoyi.aicall.service.ICcLlmAgentAccountService;
import com.ruoyi.common.utils.ExceptionUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * 处理客户意向打标的线程
 */
public class CustIntentionHandleQueue {
	private static  final Logger log = LoggerFactory.getLogger(CustIntentionHandleQueue.class);

	private static Object locker  = new Object();

	public static void start(){
		synchronized (locker) {
			new Thread(
					new Runnable() {
						@Override
						public void run() {
							CustIntentionHandleQueue();
						}
					} , "CustIntentionHandleQueue").start();
		}
	}

	private static void CustIntentionHandleQueue(){
		log.info("已经启动客户意向跑批线程!");
		while(true) {
			try {
				ICcCallPhoneService ccCallPhoneService = SpringUtils.getBean(ICcCallPhoneService.class);
				ICcLlmAgentAccountService llmAgentAccountService = SpringUtils.getBean(ICcLlmAgentAccountService.class);
				ICcCallTaskService ccCallTaskService = SpringUtils.getBean(ICcCallTaskService.class);

				// 1. 获取所有的已挂机未跑批客户意向的通话记录
				List<CcCallPhone> ccCallPhoneList = ccCallPhoneService.getCustIntentionList();
				log.info("run cust intentions phone records size：{}", ccCallPhoneList.size());
				if (ccCallPhoneList.size() > 0) {
					Map<Integer, CcLlmAgentAccount> intentionTipsMap = new HashMap<>(); // key:llmAccountId    value:intentionTips
					Map<Long, Integer> llmAccountIdMap = new HashMap<>(); // key:batchId    value:llmAccountId
					Map<String, List<String>> intentionMap = new HashMap<>(); // key:intention    value:phoneIdList
					for (CcCallPhone ccCallPhone: ccCallPhoneList) {
						try {
							// 2. 获取对应任务的对应大模型配置的客户意向提示词
							Long batchId = ccCallPhone.getBatchId();
							Integer llmAccountId = llmAccountIdMap.get(batchId);
							CcCallTask ccCallTask = null;
							if (null == llmAccountId) {
								ccCallTask = ccCallTaskService.selectCcCallTaskByBatchId(batchId);
								if (null != ccCallTask) {
									log.info("run cust intentions ccCallTask：{}-{}", ccCallPhone.getTelephone(), ccCallTask.getBatchId());
									llmAccountId = ccCallTask.getLlmAccountId();
									llmAccountIdMap.put(batchId, llmAccountId);
								} else {
									log.info("run cust intentions ccCallTask：{}-null", ccCallPhone.getTelephone());
								}
							}

							// 3. 未接通的标记为“未接通”，接通的根据提示词和对话内容调用大模型获取客户意向，如果没有提示词，默认客户意向为-
							String intention = "-";
							String intentionTips = "";
							if (StringUtils.isBlank(ccCallPhone.getDialogue()) || ccCallPhone.getDialogue().equals("[]")) {
								intention = "未接通";
							} else if (null != ccCallTask && ccCallTask.getTaskType() == 1) {
								CcLlmAgentAccount ccLlmAgentAccount = intentionTipsMap.get(llmAccountId);
								if (null == ccLlmAgentAccount) {
									ccLlmAgentAccount = llmAgentAccountService.selectCcLlmAgentAccountById(llmAccountId);
									if (null == ccLlmAgentAccount) {
										ccLlmAgentAccount = new CcLlmAgentAccount();
										ccLlmAgentAccount.setIntentionTips("");
									}
									log.info("run cust intentions ccLlmAgentAccount：{}-{}", ccCallPhone.getTelephone(), ccLlmAgentAccount.getId());
									intentionTipsMap.put(llmAccountId, ccLlmAgentAccount);
								}
								// 如果是对接本地模型，这里需要修改
								if ("DeepSeekChat".equals(ccLlmAgentAccount.getProviderClassName())) {
									intentionTips = ccLlmAgentAccount.getIntentionTips();
								}
								if (StringUtils.isBlank(intentionTips)) {
									intention = "-";
								} else {
									log.info("run cust intentions getIntenetionByDialogue：{}-{}", ccCallPhone.getTelephone(), ccLlmAgentAccount.getId());
									intention = ccCallPhoneService.getIntenetionByDialogue(ccLlmAgentAccount, ccCallPhone.getDialogue());
								}
							}
							List<String> phoneIds = intentionMap.getOrDefault(intention, new ArrayList<>());
							phoneIds.add(ccCallPhone.getId());
							intentionMap.put(intention, phoneIds);

						} catch (Exception e) {
							log.error("跑批客户意向异常:uuid:{}-msg:{}", ccCallPhone.getUuid(), ExceptionUtil.getExceptionMessage(e));
						}

					}
					for (String intention: intentionMap.keySet()) {
						try {
							List<String> phoneIds = intentionMap.getOrDefault(intention, new ArrayList<>());
							if (phoneIds.size() > 0) {
								ccCallPhoneService.updateIntentionByIds(intention, phoneIds);
							}
						} catch (Exception e) {
							log.error("客户意向更新入库异常:{}", intention);
						}
					}
				}
			} catch (Exception e) {
				log.error("客户意向跑批异常:{}", ExceptionUtil.getExceptionMessage(e));
			}
			try {
				Thread.sleep(60*1000L); // 每隔1分钟跑一次
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}