package com.fs.app.controller;


import cn.hutool.core.date.DateUtil;
import com.fs.app.taskService.SopLogsChatTaskService;
import com.fs.app.taskService.SopLogsTaskService;
import com.fs.common.core.domain.R;
import com.fs.course.mapper.FsCourseWatchLogMapper;
import com.fs.course.service.*;
import com.fs.qw.mapper.QwExternalContactMapper;
import com.fs.qw.service.IQwExternalContactService;
import com.fs.sop.mapper.QwSopLogsMapper;
import com.fs.sop.mapper.QwSopMapper;
import com.fs.sop.mapper.SopUserLogsMapper;
import com.fs.sop.service.IQwSopLogsService;
import com.fs.sop.service.IQwSopTagService;
import com.fs.sop.service.IQwSopTempRulesService;
import com.fs.sop.service.IQwSopTempVoiceService;
import com.fs.sop.vo.QwSopLogsDoSendListTVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api("公共接口")
@RestController
@RequestMapping(value="/app/common")
@Slf4j
public class CommonController {

    @Autowired
    private SopLogsTaskService service;
    @Autowired
    private SopLogsTaskService sopLogsTaskService;
    @Autowired
    private IQwExternalContactService qwExternalContactService;
    @Autowired
    private IFsUserVideoService fsUserVideoService;
    @Autowired
    private IHuaweiObsService huaweiObsService;
    @Autowired
    private IFsCourseWatchLogService watchLogService;
    @Autowired
    private QwExternalContactMapper qwExternalContactMapper;

    @Autowired
    private IQwSopLogsService iQwSopLogsService;

    @Autowired
    private QwSopMapper qwSopMapper;

    @Autowired
    private FsCourseWatchLogMapper fsCourseWatchLogMapper;

    @Autowired
    private IFsCourseLinkService courseLinkService;

    @Autowired
    private SopUserLogsMapper sopUserLogsMapper;

    @Autowired
    private QwSopLogsMapper qwSopLogsMapper;
    @Autowired
    private IQwSopTempRulesService tempRulesService;
    @Autowired
    private IQwSopTempVoiceService qwSopTempVoiceService;

    @GetMapping("/testSop")
    public R testSop() throws Exception {

        List<QwSopLogsDoSendListTVO> expireded = iQwSopLogsService.expiredMessagesByQwSopLogs();
        if (!expireded.isEmpty()) {
            processAndInsertQwSopLogs(expireded);
        }

        // 将标签字符串解析为 List    //客户总标签
        List<String> tagIdsList = new ArrayList<>();
//        tagIdsList.add("etW3ylWQAAcHQ4wJdAOMMZRfQnn3Sp2w");
//        tagIdsList.add("etW3ylWQAAdRYbwUx0P1kkL7695Vxn3g");
//        tagIdsList.add("etW3ylWQAAZyu1C9XwtKNaCLJDE8PlBQ");

        return R.ok().put("data",tagIdsList);
    }

    // 定义一个方法来批量处理插入逻辑，支持每 500 条数据一次的批量插入
    private void processAndInsertQwSopLogs(List<QwSopLogsDoSendListTVO> logsByJsApiNotExtId) {
        // 定义批量插入的大小
        int batchSize = 500;

        // 循环处理外部用户 ID，每次处理批量大小的子集
        for (int i = 0; i < logsByJsApiNotExtId.size(); i += batchSize) {

            int endIndex = Math.min(i + batchSize, logsByJsApiNotExtId.size());
            List<QwSopLogsDoSendListTVO> batchList = logsByJsApiNotExtId.subList(i, endIndex);  // 获取当前批次的子集

            // 直接使用批次数据进行批量更新，不需要额外的 List
            try {
                qwSopLogsMapper.batchUpdateQwSopLogsBySendTime(batchList);
            } catch (Exception e) {
                // 记录异常日志，方便后续排查问题
                log.error("批量更新数据时发生异常，处理的批次起始索引为: " + i, e);
            }
        }
    }

    @GetMapping("/test")
    public R test(String time) throws Exception {
        log.info("进入sop任务");
//        LocalDateTime currentTime = DateUtil.parseLocalDateTime(time);
//        // 计算下一个整点时间
//        LocalDateTime nextHourTime = currentTime.withMinute(0).withSecond(0).withNano(0).plusHours(1);
//
//        // 打印日志，确认时间
//        log.info("任务实际执行时间: {}", currentTime);
//        log.info("传递给任务的时间参数: {}", nextHourTime);
        sopLogsTaskService.selectSopUserLogsListByTime(DateUtil.parseLocalDateTime(time));
        return R.ok();
    }
    @GetMapping("/testVideo")
    public R testVideo(String sopId) throws Exception {
        qwSopTempVoiceService.synchronous(sopId, Arrays.asList(Arrays.asList(2020L, 100L), Arrays.asList(2758L, 170L)));
        return R.ok();
    }
    @GetMapping("/testSop2")
    public R testSop2() throws Exception {
        sopLogsTaskService.createCourseFinishMsg();
        return R.ok();
    }

    @Autowired
    IQwSopTagService qwSopTagService;
    @GetMapping("/tag")
    public R tag() throws Exception {
        qwSopTagService.addTag();
        return R.ok();
    }


    @Autowired
    private IFsCourseWatchLogService courseWatchLogService;
    @GetMapping("/del")
    public R del(){
        courseWatchLogService.del();
        return R.ok();
    }


    @Autowired
    private SopLogsChatTaskService sopLogsChatTaskService;
    @GetMapping("/test2")
    public String selectChatSopUserLogsListByTime() throws Exception {
        System.out.println("test2");
       // sopLogsChatTaskService.createAiChatSopLogs();
        return "s";
    }

    @PostMapping("/updateUrl")
    public R updateUrl()
    {
        log.info("开始更新URL");
        try {
            fsUserVideoService.updateVideoUrl();
            huaweiObsService.uploadByCOS();
            log.info("更新URL成功完成");


        } catch (Exception e) {
            log.error("开始更新URL执行失败", e);
        }
        return R.ok();
    }
}
