package shop.alien.job.store;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import shop.alien.entity.result.R;
import shop.alien.entity.store.*;
import shop.alien.entity.store.vo.AiApproveStoreInfo;
import shop.alien.mapper.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

import static com.alipay.api.internal.util.AlipayUtils.getFileSuffix;

/**
 * 调用AI标签数据服务类
 *
 * @author qxy
 * @date 2025/9/12 14:21
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class AiTagJob {

    private final RestTemplate restTemplate;

    private final TagsMainMapper tagsMainMapper;

    private final TagsSynonymMapper tagsSynonymMapper;

    private final StoreCommentSummaryMapper storeCommentSummaryMapper;

    private final StoreCommentSummaryInterestMapper storeCommentSummaryInterestMapper;

    private final LifeUserDynamicsMapper lifeUserDynamicsMapper;

    private final StoreClockInMapper storeClockInMapper;

    private final StoreInfoMapper storeInfoMapper;

    // 第三方接口地址 获取所有标签主表信息
    @Value("${third-party-tag.base-url}")
    private String tagMainUrl;

    // 第三方接口地址 获取所有标签子表信息
    @Value("${third-party-synonym.base-url}")
    private String tagsSynonymUrl;

    // 第三方接口地址 获取推荐信息数据 店铺评论总结表
    @Value("${third-party-summary.base-url}")
    private String allSummaryUrl;

    // 第三方接口地址 获取店铺趣味信息
    @Value("${third-party-interest.base-url}")
    private String allInterestUrl;

    // 第三方接口地址 登录接口URL
    @Value("${third-party-login.base-url}")
    private String loginUrl;

    // 第三方接口地址 创建Ai标签数据接口
    @Value("${third-party-savetag.base-url}")
    private String saveTagUrl;

    // 第三方接口地址 内容合规检测接口
    @Value("${third-party-contentcheck.base-url}")
    private String contentComplianceUrl;

    @Value("${third-party-getresult.base-url}")
    private String getResultUrl;

    /**
     * 店铺入驻 AI 审核提交接口，例如: http://host:port/ai/auto-review/api/v1/merchant-onboarding/applications
     * （与 {@link shop.alien.job.second.AiProductComplaintJob} 中 third-party-aiProductComplaintResultUrl 用法相同，在 Nacos 配置）
     */
    @Value("${third-party-aiMerchantOnboardingApplicationsUrl.base-url}")
    private String aiMerchantOnboardingApplicationsUrl;

    //用户名
    @Value("${third-party-user-name-admin.base-url}")
    private String userName;

    //密码
    @Value("${third-party-pass-word.base-url}")
    private String passWord;

    /**
     * 创建Ai标签评论数据
     */
    @XxlJob("getSaveTagsTask")
    public R<String> getSaveTagsTask() {
        log.info("登录Ai服务获取token..." + loginUrl);
        //构建请求参数
        MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
        formData.add("username", userName);    // 表单字段 1：用户名
        formData.add("password", passWord);    // 表单字段 2：密码

        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(formData, headers);
        ResponseEntity<String> postForEntity = null;
        try {
            log.info("请求Ai服务登录接口===================>");
            postForEntity = restTemplate.postForEntity(loginUrl, requestEntity, String.class);
        } catch (Exception e) {
            log.error("类:PostMethod 方法:post", e);
        }

        if (postForEntity != null) {
            if (postForEntity.getStatusCodeValue() == 200) {
                log.info("请求Ai服务登录成功 postForEntity.getBody()\t" + postForEntity.getBody());
                String responseBody = postForEntity.getBody();
                JSONObject jsonObject = JSONObject.parseObject(responseBody);
                JSONObject dataJson = jsonObject.getJSONObject("data");
                String accessToken = dataJson.getString("access_token");
                //调用 创建Ai标签评论数据方法
                getSaveTagsTaskInfo(accessToken);
                return R.success("任务执行成功");
            } else {
                log.error("请求AI服务 登录接口失败 http状态：" + postForEntity.getStatusCode());
                return R.fail("请求AI服务 登录接口失败 http状态:" + postForEntity.getStatusCode());
            }
        }
        XxlJobHelper.handleFail("登录任务执行失败 返回异常");
        return R.fail("登录任务执行失败 返回异常");
    }

    public R<String> getSaveTagsTaskInfo(String token) {
        log.info("开始从Ai服务创建店铺评价标签评论数据...");
        log.info("restTemplate:" + restTemplate + "url:" + saveTagUrl);

        // 获取当前日期
        LocalDate today = LocalDate.now();

        // 获取当月第一天
        LocalDate firstDayOfMonth = today.withDayOfMonth(1);

        // 获取当月最后一天
        LocalDate lastDayOfMonth = today.withDayOfMonth(today.lengthOfMonth());

        // 格式化为字符串（可根据需要调整格式）
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String startDate = firstDayOfMonth.format(formatter);
        String endDate = lastDayOfMonth.format(formatter);

        // 1. 构建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        // Token 认证
        headers.set("Authorization", "Bearer " + token);

        AnalysisRequest jsonBody = new AnalysisRequest();
        jsonBody.setStart_time(startDate + "T00:00:00");
        jsonBody.setEnd_time(endDate + "T23:59:59");

        HttpEntity<AnalysisRequest> request = new HttpEntity<>(jsonBody, headers);
        ResponseEntity<String> response = null;
        try {
            response = restTemplate.postForEntity(saveTagUrl, request, String.class);
        } catch (Exception e) {
            log.error("类:PostMethod 方法:post", e);
        }

        if (response != null) {
            if (response.getStatusCodeValue() == 200) {
                log.info("postForEntity.getBody()\t" + response.getStatusCode());
                XxlJobHelper.handleSuccess("创建标签成功 状态码" + response.getStatusCode());
                return R.success("创建成功");
            } else {
                log.error("请求AI服务 创建标签数据失败 http状态：" + response.getStatusCode());
                XxlJobHelper.handleFail("请求AI服务 创建标签数据失败 http状态" + response.getStatusCode());
                return R.fail("请求AI服务 创建标签数据失败 http状态：" + response.getStatusCode());
            }
        }
        log.error("创建标签任务执行失败 返回异常");
        XxlJobHelper.handleFail("创建标签任务执行失败 返回异常");
        return R.fail("创建标签任务执行失败 返回异常");
    }

    /**
     * 拉取Ai服务标签主表数据
     */
    @XxlJob("getTagMainTask")
    public R<String> getTagMainTask() {
        log.info("登录Ai服务获取token..." + loginUrl);
        //构建请求参数
        MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
        formData.add("username", userName);    // 表单字段 1：用户名
        formData.add("password", passWord);    // 表单字段 2：密码

        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(formData, headers);
        ResponseEntity<String> postForEntity = null;
        try {
            postForEntity = restTemplate.postForEntity(loginUrl, requestEntity, String.class);
        } catch (Exception e) {
            log.error("类:PostMethod 方法:post", e);
        }

        if (postForEntity != null) {
            if (postForEntity.getStatusCodeValue() == 200) {
                log.info("请求Ai服务登录成功 postForEntity.getBody()\t" + postForEntity.getBody());
                String responseBody = postForEntity.getBody();
                JSONObject jsonObject = JSONObject.parseObject(responseBody);
                JSONObject dataJson = jsonObject.getJSONObject("data");
                String accessToken = dataJson.getString("access_token");
                //调用 获取AI服务 标签主表get请求
                getTagMainTaskInfo(accessToken);
                return R.success("任务执行成功");
            } else {
                log.error("请求AI服务 登录接口失败 http状态：" + postForEntity.getStatusCode());
                return R.fail("请求AI服务 登录接口失败 http状态:" + postForEntity.getStatusCode());
            }
        }
        XxlJobHelper.handleFail("登录任务执行失败 返回异常");
        return R.fail("登录任务执行失败 返回异常");
    }

    public R<String> getTagMainTaskInfo(String token) {
        log.info("开始从Ai服务获取标签主表信息...");
        log.info("restTemplate:" + restTemplate + "url:" + tagMainUrl);
        // 1. 构建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer " + token); // Token 认证
        headers.set("accept", "*/*");
        headers.set("connection", "Keep-Alive");
        headers.set("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        // 2. 构建请求实体（仅包含头信息，GET 请求通常无请求体）
        HttpEntity<?> requestEntity = new HttpEntity<>(headers);

        // 发送 GET 请求，响应体为 String 类型
        ResponseEntity<String> responseEntity = restTemplate.exchange(
                tagMainUrl,
                HttpMethod.GET,  // 指定请求方法为 GET
                requestEntity,
                String.class     // 响应体类型
        );

        // 解析响应
        if (responseEntity.getStatusCodeValue() == 200) {
            String responseBody = responseEntity.getBody();
            JSONObject jsonObject = JSONObject.parseObject(responseBody);
            Integer code = jsonObject.getInteger("code");
            String message = jsonObject.getString("message");
            JSONArray dataArray = jsonObject.getJSONArray("data");
            List<TagsMain> tagsMainList = new ArrayList<>();
            for (int i = 0; i < dataArray.size(); i++) {
                JSONObject tagObj = dataArray.getJSONObject(i);
                TagsMain tagsMain = new TagsMain();
                tagsMain.setId(tagObj.getInteger("id"));
                tagsMain.setTagName(tagObj.getString("tag_name"));
                tagsMain.setStoreId(tagObj.getInteger("store_id"));
                tagsMain.setTagType(tagObj.getInteger("tag_type"));
                tagsMain.setDeleteFlag(tagObj.getInteger("delete_flag"));
                tagsMain.setCreatedTime(tagObj.getDate("created_time"));
                tagsMain.setCreatedUserId(tagObj.getInteger("created_user_id"));
                tagsMain.setUpdatedTime(tagObj.getDate("updated_time"));
                tagsMain.setUpdatedUserId(tagObj.getInteger("updated_user_id"));
                tagsMainList.add(tagsMain);
            }
            if (!CollectionUtils.isEmpty(tagsMainList)) {
                int num = tagsMainMapper.insertBatchTagsMain(tagsMainList);
                XxlJobHelper.handleSuccess("拉取主标签数据成功 执行" + num + "条");
                return R.success("拉取主标签数据成功 执行" + num + "条");
            } else {
                XxlJobHelper.handleSuccess("获取回显数据为空 状态码:" + responseEntity.getStatusCodeValue());
                return R.success("请求成功 获取回显数据为空 状态码:" + responseEntity.getStatusCodeValue());
            }
        }
        XxlJobHelper.handleFail("任务执行失败 状态码" + responseEntity.getStatusCodeValue());
        return R.fail("任务执行失败 状态码" + responseEntity.getStatusCodeValue());
    }


    /**
     * 拉取Ai服务子标签数据
     */
    @XxlJob("getTagsSynonymTask")
    public R<String> getTagsSynonymTask() {
        log.info("登录Ai服务获取token..." + loginUrl);
        //构建请求参数
        MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
        formData.add("username", userName);    // 表单字段 1：用户名
        formData.add("password", passWord);    // 表单字段 2：密码

        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(formData, headers);
        ResponseEntity<String> postForEntity = null;
        try {
            postForEntity = restTemplate.postForEntity(loginUrl, requestEntity, String.class);
        } catch (Exception e) {
            log.error("类:PostMethod 方法:post", e);
        }

        if (postForEntity != null) {
            if (postForEntity.getStatusCodeValue() == 200) {
                log.info("请求Ai服务登录成功 postForEntity.getBody()\t" + postForEntity.getBody());
                String responseBody = postForEntity.getBody();
                JSONObject jsonObject = JSONObject.parseObject(responseBody);
                JSONObject dataJson = jsonObject.getJSONObject("data");
                String accessToken = dataJson.getString("access_token");
                //调用 获取AI服务 标签子表get请求
                getTagsSynonymTaskInfo(accessToken);
                return R.success("任务执行成功");
            } else {
                log.error("请求AI服务 登录接口失败 http状态：" + postForEntity.getStatusCode());
                return R.fail("请求AI服务 登录接口失败 http状态:" + postForEntity.getStatusCode());
            }
        }
        XxlJobHelper.handleFail("登录任务执行失败 返回异常");
        return R.fail("登录任务执行失败 返回异常");
    }

    public R<String> getTagsSynonymTaskInfo(String token) {
        log.info("开始从Ai服务获取数据 获取所有标签子表信息...");
        log.info("restTemplate:" + restTemplate + "url:" + tagsSynonymUrl);
        // 1. 构建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer " + token); // Token 认证
        headers.set("accept", "*/*");
        headers.set("connection", "Keep-Alive");
        headers.set("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        // 2. 构建请求实体（仅包含头信息，GET 请求通常无请求体）
        HttpEntity<?> requestEntity = new HttpEntity<>(headers);

        // 发送 GET 请求，响应体为 String 类型
        ResponseEntity<String> responseEntity = restTemplate.exchange(
                tagsSynonymUrl,
                HttpMethod.GET,  // 指定请求方法为 GET
                requestEntity,
                String.class     // 响应体类型
        );

        // 解析响应
        if (responseEntity.getStatusCodeValue() == 200) {
            String responseBody = responseEntity.getBody();
            JSONObject jsonObject = JSONObject.parseObject(responseBody);
            Integer code = jsonObject.getInteger("code");
            String message = jsonObject.getString("message");
            JSONArray dataArray = jsonObject.getJSONArray("data");
            List<TagsSynonym> tagsSynonymList = new ArrayList<>();
            for (int i = 0; i < dataArray.size(); i++) {
                JSONObject tagObj = dataArray.getJSONObject(i);
                TagsSynonym tagsSynonym = new TagsSynonym();
                tagsSynonym.setId(tagObj.getInteger("id"));
                tagsSynonym.setMainTagId(tagObj.getInteger("main_tag_id"));
                tagsSynonym.setCommentId(tagObj.getInteger("comment_id"));
                tagsSynonym.setSynonymTag(tagObj.getString("synonym_tag"));
                tagsSynonym.setTagType(tagObj.getInteger("tag_type"));
                tagsSynonym.setTaskId(tagObj.getString("task_id"));
                tagsSynonym.setDeleteFlag(tagObj.getInteger("delete_flag"));
                tagsSynonym.setCreatedTime(tagObj.getDate("created_time"));
                tagsSynonym.setCreatedUserId(tagObj.getInteger("created_user_id"));
                tagsSynonym.setUpdatedTime(tagObj.getDate("updated_time"));
                tagsSynonym.setUpdatedUserId(tagObj.getInteger("updated_user_id"));
                tagsSynonymList.add(tagsSynonym);
            }
            if (!CollectionUtils.isEmpty(tagsSynonymList)) {
                int num = tagsSynonymMapper.insertBatchTagsSynonym(tagsSynonymList);
                XxlJobHelper.handleSuccess("拉取子标签数据成功 执行" + num + "条");
                return R.success("拉取子标签数据成功 执行" + num + "条");
            } else {
                XxlJobHelper.handleSuccess("获取回显数据为空" + "状态码:" + responseEntity.getStatusCodeValue());
                return R.success("请求成功 获取回显数据为空" + "状态码:" + responseEntity.getStatusCodeValue());
            }
        }
        XxlJobHelper.handleFail("任务执行失败 状态码" + responseEntity.getStatusCodeValue());
        return R.fail("任务执行失败 状态码" + responseEntity.getStatusCodeValue());
    }

    /**
     * 拉取Ai服务获取推荐信息数据
     */
    @XxlJob("getStoreCommentSummaryTask")
    public R<String> getStoreCommentSummaryTask() {
        log.info("登录Ai服务获取token..." + loginUrl);
        //构建请求参数
        MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
        formData.add("username", userName);    // 表单字段 1：用户名
        formData.add("password", passWord);    // 表单字段 2：密码

        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(formData, headers);
        ResponseEntity<String> postForEntity = null;
        try {
            postForEntity = restTemplate.postForEntity(loginUrl, requestEntity, String.class);
        } catch (Exception e) {
            log.error("类:PostMethod 方法:post", e);
        }

        if (postForEntity != null) {
            if (postForEntity.getStatusCodeValue() == 200) {
                log.info("请求Ai服务登录成功 postForEntity.getBody()\t" + postForEntity.getBody());
                String responseBody = postForEntity.getBody();
                JSONObject jsonObject = JSONObject.parseObject(responseBody);
                JSONObject dataJson = jsonObject.getJSONObject("data");
                String accessToken = dataJson.getString("access_token");
                //调用 获取AI服务 获取所有推荐信表 get请求
                getStoreCommentSummaryTaskInfo(accessToken);
                return R.success("任务执行成功");
            } else {
                log.error("请求AI服务 登录接口失败 http状态：" + postForEntity.getStatusCode());
                return R.fail("请求AI服务 登录接口失败 http状态:" + postForEntity.getStatusCode());
            }
        }
        XxlJobHelper.handleFail("登录任务执行失败 返回异常");
        return R.fail("登录任务执行失败 返回异常");
    }

    public R<String> getStoreCommentSummaryTaskInfo(String token) {
        log.info("开始从Ai服务获取数据 获取推荐信息数据 店铺评论总结表...");
        log.info("restTemplate:" + restTemplate + "url:" + allSummaryUrl);
        // 1. 构建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer " + token); // Token 认证
        headers.set("accept", "*/*");
        headers.set("connection", "Keep-Alive");
        headers.set("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        // 2. 构建请求实体（仅包含头信息，GET 请求通常无请求体）
        HttpEntity<?> requestEntity = new HttpEntity<>(headers);

        // 发送 GET 请求，响应体为 String 类型
        ResponseEntity<String> responseEntity = restTemplate.exchange(
                allSummaryUrl,
                HttpMethod.GET,  // 指定请求方法为 GET
                requestEntity,
                String.class     // 响应体类型
        );

        // 解析响应
        if (responseEntity.getStatusCodeValue() == 200) {
            String responseBody = responseEntity.getBody();
            JSONObject jsonObject = JSONObject.parseObject(responseBody);
            Integer code = jsonObject.getInteger("code");
            String message = jsonObject.getString("message");
            JSONArray dataArray = jsonObject.getJSONArray("data");
            List<StoreCommentSummary> storeCommentSummaries = new ArrayList<>();
            for (int i = 0; i < dataArray.size(); i++) {
                JSONObject tagObj = dataArray.getJSONObject(i);
                StoreCommentSummary summary = new StoreCommentSummary();
                summary.setId(tagObj.getInteger("id"));
                summary.setStoreId(tagObj.getInteger("store_id"));
                summary.setStoreName(tagObj.getString("store_name"));
                summary.setSummary(tagObj.getString("summary"));
                summary.setTaskId(tagObj.getString("task_id"));
                summary.setIsShow(tagObj.getInteger("is_show"));
                summary.setDeleteFlag(tagObj.getInteger("delete_flag"));
                summary.setCreatedTime(tagObj.getDate("created_time"));
                summary.setCreatedUserId(tagObj.getInteger("created_user_id"));
                summary.setUpdatedTime(tagObj.getDate("updated_time"));
                summary.setUpdatedUserId(tagObj.getInteger("updated_user_id"));
                storeCommentSummaries.add(summary);
            }
            if (!CollectionUtils.isEmpty(storeCommentSummaries)) {
                int num = storeCommentSummaryMapper.insertBatchStoreCommentSummary(storeCommentSummaries);
                XxlJobHelper.handleSuccess("拉取子标签数据成功 执行" + num + "条");
                return R.success("拉取子标签数据成功 执行" + num + "条");
            } else {
                XxlJobHelper.handleSuccess("获取回显数据为空" + "状态码:" + responseEntity.getStatusCodeValue());
                return R.success("请求成功 获取回显数据为空" + "状态码:" + responseEntity.getStatusCodeValue());
            }
        }
        log.info("AI服务 推荐信息数据请求失败，状态码：" + responseEntity.getStatusCodeValue());
        XxlJobHelper.handleFail("任务执行失败 状态码" + responseEntity.getStatusCodeValue());
        return R.success("任务执行失败 状态码" + responseEntity.getStatusCodeValue());
    }

    /**
     * 拉取Ai服务获取趣味推荐信息表数据
     */
    @XxlJob("getStoreCommentSummaryInterestTask")
    public R<String> getStoreCommentSummaryInterestTask() {
        log.info("登录Ai服务获取token..." + loginUrl);
        //构建请求参数
        MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
        formData.add("username", userName);    // 表单字段 1：用户名
        formData.add("password", passWord);    // 表单字段 2：密码

        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(formData, headers);
        ResponseEntity<String> postForEntity = null;
        try {
            postForEntity = restTemplate.postForEntity(loginUrl, requestEntity, String.class);
        } catch (Exception e) {
            log.error("类:PostMethod 方法:post", e);
        }

        if (postForEntity != null) {
            if (postForEntity.getStatusCodeValue() == 200) {
                log.info("请求Ai服务登录成功 postForEntity.getBody()\t" + postForEntity.getBody());
                String responseBody = postForEntity.getBody();
                JSONObject jsonObject = JSONObject.parseObject(responseBody);
                JSONObject dataJson = jsonObject.getJSONObject("data");
                String accessToken = dataJson.getString("access_token");
                //调用 获取AI服务 获取趣味推荐信息表 get请求
                getStoreCommentSummaryInterestInfo(accessToken);
                return R.success("任务执行成功");
            } else {
                log.error("请求AI服务 登录接口失败 http状态：" + postForEntity.getStatusCode());
                return R.fail("请求AI服务 登录接口失败 http状态:" + postForEntity.getStatusCode());
            }
        }
        XxlJobHelper.handleFail("登录任务执行失败 返回异常");
        return R.fail("登录任务执行失败 返回异常");
    }

    public R<String> getStoreCommentSummaryInterestInfo(String token) {
        log.info("开始从Ai服务获取数据 获取趣味推荐信息表...");
        log.info("restTemplate:" + restTemplate + "url:" + allInterestUrl);
        // 1. 构建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer " + token); // Token 认证
        headers.set("accept", "*/*");
        headers.set("connection", "Keep-Alive");
        headers.set("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        // 2. 构建请求实体（仅包含头信息，GET 请求通常无请求体）
        HttpEntity<?> requestEntity = new HttpEntity<>(headers);

        // 发送 GET 请求，响应体为 String 类型
        ResponseEntity<String> responseEntity = restTemplate.exchange(
                allInterestUrl,
                HttpMethod.GET,  // 指定请求方法为 GET
                requestEntity,
                String.class     // 响应体类型
        );

        // 解析响应
        if (responseEntity.getStatusCodeValue() == 200) {
            String responseBody = responseEntity.getBody();
            JSONObject jsonObject = JSONObject.parseObject(responseBody);
            Integer code = jsonObject.getInteger("code");
            String message = jsonObject.getString("message");
            JSONArray dataArray = jsonObject.getJSONArray("data");
            List<StoreCommentSummaryInterest> storeCommentSummaryInterests = new ArrayList<>();
            for (int i = 0; i < dataArray.size(); i++) {
                JSONObject tagObj = dataArray.getJSONObject(i);
                StoreCommentSummaryInterest summary = new StoreCommentSummaryInterest();
                summary.setId(tagObj.getInteger("id"));
                summary.setStoreId(tagObj.getInteger("store_id"));
                summary.setStoreName(tagObj.getString("store_name"));
                summary.setSummary(tagObj.getString("summary"));
                summary.setTaskId(tagObj.getString("task_id"));
                summary.setDeleteFlag(tagObj.getInteger("delete_flag"));
                summary.setCreatedTime(tagObj.getDate("created_time"));
                summary.setCreatedUserId(tagObj.getInteger("created_user_id"));
                summary.setUpdatedTime(tagObj.getDate("updated_time"));
                summary.setUpdatedUserId(tagObj.getInteger("updated_user_id"));
                storeCommentSummaryInterests.add(summary);
            }
            if (!CollectionUtils.isEmpty(storeCommentSummaryInterests)) {
                int num = storeCommentSummaryInterestMapper.insertBatchStoreCommentSummaryInterest(storeCommentSummaryInterests);
                XxlJobHelper.handleSuccess("拉取子标签数据成功 执行" + num + "条");
                return R.success("拉取子标签数据成功 执行" + num + "条");
            } else {
                XxlJobHelper.handleSuccess("获取回显数据为空" + "状态码:" + responseEntity.getStatusCodeValue());
                return R.success("请求成功 获取回显数据为空" + "状态码:" + responseEntity.getStatusCodeValue());
            }
        }
        log.info("AI服务 获取趣味推荐信息数据请求失败，状态码：" + responseEntity.getStatusCodeValue());
        XxlJobHelper.handleFail("任务执行失败 状态码" + responseEntity.getStatusCodeValue());
        return R.success("任务执行失败 状态码" + responseEntity.getStatusCodeValue());
    }

    /**
     * 利用AI审核违规内容
     */
    @XxlJob("contentComplianceCheckTask")
    public R<String> contentComplianceCheckTask() {
        List<LifeUserDynamics> lifeUserDynamics = lifeUserDynamicsMapper.selectList(new LambdaQueryWrapper<LifeUserDynamics>()
                .eq(LifeUserDynamics::getCheckFlag, 0).eq(LifeUserDynamics::getDeleteFlag, 0));

        // 只依赖数据库字段即可判定待审核的动态，避免重复提交
        // 常见图片后缀（可按需添加，如 .heic、.svg 等）
        HashSet<String> IMAGE_SUFFIXES = new HashSet<>(Arrays.asList(
                "jpg", "jpeg", "png", "gif", "bmp", "webp", "heic", "svg", "tiff"
        ));
        // 常见视频后缀（可按需添加，如 .avi、.flv 等）
        HashSet<String> VIDEO_SUFFIXES = new HashSet<>(Arrays.asList(
                "mp4", "mov", "mkv", "avi", "flv", "wmv", "mpeg", "mpg", "webm"
        ));

        log.info("登录Ai服务获取token..." + loginUrl);
        MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
        formData.add("username", userName);
        formData.add("password", passWord);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(formData, headers);
        ResponseEntity<String> postForEntity = null;
        try {
            log.info("请求Ai服务登录接口===================>");
            postForEntity = restTemplate.postForEntity(loginUrl, requestEntity, String.class);
        } catch (Exception e) {
            log.error("请求AI服务登录接口失败", e);
        }
        HttpHeaders aiHeaders = new HttpHeaders();
        if (postForEntity != null && postForEntity.getStatusCodeValue() == 200) {
            log.info("请求Ai服务登录成功 postForEntity.getBody()\t" + postForEntity.getBody());
            String responseBody = postForEntity.getBody();
            JSONObject jsonObject = JSONObject.parseObject(responseBody);
            if (jsonObject != null) {
                JSONObject dataJson = jsonObject.getJSONObject("data");
                String accessToken = dataJson.getString("access_token");

                aiHeaders.setContentType(MediaType.APPLICATION_JSON);
                aiHeaders.set("Authorization", "Bearer " + accessToken);
            }
        }

        List<StoreClockIn> storeClockIns = storeClockInMapper.selectList(new LambdaQueryWrapper<StoreClockIn>()
                .eq(StoreClockIn::getDeleteFlag, 0).eq(StoreClockIn::getCheckFlag, 0));
        for (StoreClockIn storeClockIn : storeClockIns) {
            String imagePath = storeClockIn.getImgUrl() != null ? storeClockIn.getImgUrl() : "";
            List<String> imageList = new ArrayList<>();
            List<String> videoList = new ArrayList<>();
            // 按分隔符拆分路径数组（处理连续分隔符如 ",," 产生的空字符串）
            String[] allPaths = imagePath.split(",");
            for (String path : allPaths) {
                // 去除路径前后空格（避免 " a.jpg " 这类情况）
                String trimmedPath = path.trim();
                if (trimmedPath.isEmpty()) {
                    continue; // 跳过空路径
                }
                // 获取文件后缀（忽略大小写）
                // 找到最后一个 "." 的位置
                int lastDotIndex = trimmedPath.lastIndexOf('.');
                // 截取后缀（从 "." 后一位到结尾）
                String suffix = trimmedPath.substring(lastDotIndex + 1);
                // 分类添加到对应列表
                if (IMAGE_SUFFIXES.contains(suffix)) {
                    imageList.add(trimmedPath);
                } else if (VIDEO_SUFFIXES.contains(suffix)) {
                    videoList.add(trimmedPath);
                }
            }
            // 对每一条动态逐条申请 token 并提交，避免批量失败导致重试逻辑复杂化
            try {
                Map<String, Object> jsonBody = new HashMap<>();
                // text/img/video 三种维度一起传入 AI，便于一次完成合规审查
                jsonBody.put("text", storeClockIn.getContent());
                jsonBody.put("image_urls", imageList);
                jsonBody.put("video_urls", videoList);

                HttpEntity<Map<String, Object>> request = new HttpEntity<>(jsonBody, aiHeaders);
                ResponseEntity<String> response = null;
                try {
                    response = restTemplate.postForEntity(contentComplianceUrl, request, String.class);
                    if (response.getStatusCodeValue() != 200) {
                        log.error("AI内容审核接口调用失败 http状态：" + response.getStatusCode());
                    }
                    JSONObject responseNode = JSONObject.parseObject(response.getBody());
                    if (responseNode == null) {
                        log.error("AI接口调用失败，响应内容为空");
                    }
                    Integer code = null;
                    if (responseNode != null) {
                        code = responseNode.getInteger("code");
                        if (code == 200) {
                            JSONObject dataNode = JSONObject.from(responseNode.get("data"));
                            // 审核发起后仅标记 checkFlag=1 并保存任务号，等待回调或后续轮询更新
                            StoreClockIn clockIn = new StoreClockIn();
                            clockIn.setId(storeClockIn.getId());
                            clockIn.setCheckFlag(1);
                            clockIn.setAiTaskId(dataNode.get("task_id").toString());
                            storeClockInMapper.updateById(clockIn);
                            log.info("动态审核成功，AI返回内容: {}", response.getBody());
                            XxlJobHelper.handleSuccess("动态内容审核任务执行成功");
                        }
                    } else {
                        log.error("AI接口调用失败，错误码: " + code);
                    }
                } catch (Exception e) {
                    log.error("调用AI内容审核接口失败", e);
                }
            } catch (RuntimeException ex) {
                XxlJobHelper.handleFail("动态内容审核任务执行失败:" + ex.getMessage());
                return R.fail("动态内容审核任务执行失败:" + ex.getMessage());
            }
        }

        for (LifeUserDynamics lifeUserDynamic : lifeUserDynamics) {
            String imagePath = lifeUserDynamic.getImagePath();
            List<String> imageList = new ArrayList<>();
            List<String> videoList = new ArrayList<>();
            // 按分隔符拆分路径数组（处理连续分隔符如 ",," 产生的空字符串）
            String[] allPaths = imagePath.split(",");
            for (String path : allPaths) {
                // 去除路径前后空格（避免 " a.jpg " 这类情况）
                String trimmedPath = path.trim();
                if (trimmedPath.isEmpty()) {
                    continue; // 跳过空路径
                }
                // 获取文件后缀（忽略大小写）
                // 找到最后一个 "." 的位置
                int lastDotIndex = trimmedPath.lastIndexOf('.');
                // 截取后缀（从 "." 后一位到结尾）
                String suffix = trimmedPath.substring(lastDotIndex + 1);
                // 分类添加到对应列表
                if (IMAGE_SUFFIXES.contains(suffix)) {
                    imageList.add(trimmedPath);
                } else if (VIDEO_SUFFIXES.contains(suffix)) {
                    videoList.add(trimmedPath);
                }
            }
            try {
                Map<String, Object> jsonBody = new HashMap<>();
                // text/img/video 三种维度一起传入 AI，便于一次完成合规审查
                jsonBody.put("text", lifeUserDynamic.getContext());
                jsonBody.put("image_urls", imageList);
                jsonBody.put("video_urls", videoList);

                HttpEntity<Map<String, Object>> request = new HttpEntity<>(jsonBody, aiHeaders);
                ResponseEntity<String> response = null;
                try {
                    response = restTemplate.postForEntity(contentComplianceUrl, request, String.class);
                    if (response.getStatusCodeValue() != 200) {
                        log.error("AI内容审核接口调用失败 http状态：" + response.getStatusCode());
                    }
                    JSONObject responseNode = JSONObject.parseObject(response.getBody());
                    if (responseNode == null) {
                        log.error("AI接口调用失败，响应内容为空");
                    }
                    Integer code = null;
                    if (responseNode != null) {
                        code = responseNode.getInteger("code");
                        if (code == 200) {
                            JSONObject dataNode = JSONObject.from(responseNode.get("data"));
                            // 审核发起后仅标记 checkFlag=1 并保存任务号，等待回调或后续轮询更新
                            LifeUserDynamics dynamics = new LifeUserDynamics();
                            dynamics.setId(lifeUserDynamic.getId());
                            dynamics.setCheckFlag(1);
                            dynamics.setAiTaskId(dataNode.get("task_id").toString());
                            lifeUserDynamicsMapper.updateById(dynamics);
                            log.info("动态审核成功，AI返回内容: {}", response.getBody());
                            XxlJobHelper.handleSuccess("动态内容审核任务执行成功");
                        }
                    } else {
                        log.error("AI接口调用失败，错误码: " + code);
                    }
                } catch (Exception e) {
                    log.error("调用AI内容审核接口失败", e);
                }
            } catch (RuntimeException ex) {
                XxlJobHelper.handleFail("动态内容审核任务执行失败:" + ex.getMessage());
                return R.fail("动态内容审核任务执行失败:" + ex.getMessage());
            }
        }
        return R.success("动态内容审核任务执行成功");
    }

    /**
     * 获取审核违规内容结果
     */
    @XxlJob("getCheckTask")
    public R<String> getCheckTask() {
        List<LifeUserDynamics> lifeUserDynamics = lifeUserDynamicsMapper.selectList(new LambdaQueryWrapper<LifeUserDynamics>()
                .eq(LifeUserDynamics::getCheckFlag, 1).eq(LifeUserDynamics::getDeleteFlag, 0));
        log.info("登录Ai服务获取token..." + loginUrl);
        MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
        formData.add("username", userName);
        formData.add("password", passWord);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(formData, headers);
        ResponseEntity<String> postForEntity = null;
        try {
            log.info("请求Ai服务登录接口===================>");
            postForEntity = restTemplate.postForEntity(loginUrl, requestEntity, String.class);
        } catch (Exception e) {
            log.error("请求AI服务登录接口失败", e);
        }
        RestTemplate restTemplateWithAuth = new RestTemplate();
        if (postForEntity != null && postForEntity.getStatusCodeValue() == 200) {
            log.info("请求Ai服务登录成功 postForEntity.getBody()\t" + postForEntity.getBody());
            String responseBody = postForEntity.getBody();
            JSONObject jsonObject = JSONObject.parseObject(responseBody);
            if (jsonObject != null) {
                JSONObject dataJson = jsonObject.getJSONObject("data");
                String accessToken = dataJson.getString("access_token");
                List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
                interceptors.add((request, body, execution) -> {
                    request.getHeaders().set("Authorization", "Bearer " + accessToken);
                    return execution.execute(request, body);
                });
                restTemplateWithAuth.setInterceptors(interceptors);
            }
        }

        List<StoreClockIn> storeClockIns = storeClockInMapper.selectList(new LambdaQueryWrapper<StoreClockIn>().eq(StoreClockIn::getCheckFlag, 1));
        for (StoreClockIn storeClockIn : storeClockIns) {
            // 针对已提交且未删除的动态轮询查询结果
            try {
                ResponseEntity<String> response = null;
                try {
                    response = restTemplateWithAuth.getForEntity(getResultUrl + "?task_id=" + storeClockIn.getAiTaskId(), String.class);
                    if (response.getStatusCodeValue() != 200) {
                        log.error("AI内容审核结果获取接口调用失败 http状态：" + response.getStatusCode());
                    }
                    JSONObject responseNode = JSONObject.parseObject(response.getBody());
                    if (responseNode == null) {
                        log.error("AI接口调用失败，响应内容为空");
                    }
                    Integer code = null;
                    if (responseNode != null) {
                        code = responseNode.getInteger("code");
                        if (code == 200) {
                            JSONObject dataNode = JSONObject.from(responseNode.get("data"));
                            StoreClockIn clockIn = new StoreClockIn();
                            clockIn.setId(storeClockIn.getId());
                            if ("completed".equals(dataNode.get("status"))) {
                                if (!(boolean) dataNode.get("is_compliant")) {
                                    // 只要 AI 判定不合规，立即禁用动态并记录原因
                                    clockIn.setDeleteFlag(1);
                                    clockIn.setReason(String.valueOf(dataNode.get("failure_reason")));
                                }
                                clockIn.setCheckFlag(2);
                                storeClockInMapper.updateById(clockIn);
                                if (!(boolean) dataNode.get("is_compliant")) {
                                    storeClockInMapper.deleteById(clockIn);
                                }
                                log.info("动态审核结果获取成功，AI返回内容: {}", response.getBody());
                                XxlJobHelper.handleSuccess("动态内容审核任务结果获取执行成功");
                            } else {
                                log.info("动态审核未完成，AI返回内容: {}", response.getBody());
                            }
                        } else {
                            log.error("AI接口调用失败，错误码: " + code);
                        }
                    }
                } catch (Exception e) {
                    log.error("调用AI内容审核结果获取接口失败", e);
                }
            } catch (RuntimeException ex) {
                XxlJobHelper.handleFail("动态内容审核任务结果获取执行失败:" + ex.getMessage());
                return R.fail("动态内容审核任务结果获取执行失败:" + ex.getMessage());
            }
        }

        for (LifeUserDynamics lifeUserDynamic : lifeUserDynamics) {
            // 针对已提交且未删除的动态轮询查询结果
            try {
                ResponseEntity<String> response = null;
                try {
                    response = restTemplateWithAuth.getForEntity(getResultUrl + "?task_id=" + lifeUserDynamic.getAiTaskId(), String.class);
                    if (response.getStatusCodeValue() != 200) {
                        log.error("AI内容审核结果获取接口调用失败 http状态：" + response.getStatusCode());
                    }
                    JSONObject responseNode = JSONObject.parseObject(response.getBody());
                    if (responseNode == null) {
                        log.error("AI接口调用失败，响应内容为空");
                    }
                    Integer code = null;
                    if (responseNode != null) {
                        code = responseNode.getInteger("code");
                        if (code == 200) {
                            JSONObject dataNode = JSONObject.from(responseNode.get("data"));
                            LifeUserDynamics dynamics = new LifeUserDynamics();
                            dynamics.setId(lifeUserDynamic.getId());
                            if ("completed".equals(dataNode.get("status"))) {
                                if (!(boolean) dataNode.get("is_compliant")) {
                                    // 只要 AI 判定不合规，立即禁用动态并记录原因
                                    dynamics.setEnableStatus(1);
                                    dynamics.setReason(String.valueOf(dataNode.get("failure_reason")));
                                }
                                dynamics.setCheckFlag(2);
                                lifeUserDynamicsMapper.updateById(dynamics);
                                log.info("动态审核结果获取成功，AI返回内容: {}", response.getBody());
                                XxlJobHelper.handleSuccess("动态内容审核任务结果获取执行成功");
                            } else {
                                log.info("动态审核未完成，AI返回内容: {}", response.getBody());
                            }
                        } else {
                            log.error("AI接口调用失败，错误码: " + code);
                        }
                    }
                } catch (Exception e) {
                    log.error("调用AI内容审核结果获取接口失败", e);
                }
            } catch (RuntimeException ex) {
                XxlJobHelper.handleFail("动态内容审核任务结果获取执行失败:" + ex.getMessage());
                return R.fail("动态内容审核任务结果获取执行失败:" + ex.getMessage());
            }
        }
        return R.success("动态内容审核任务结果获取执行成功");
    }

    /**
     * 店铺AI审核任务
     * <p>
     * 定时查询 store_info 表中审核状态为 0（待审核）的门店，
     * 后续可在此处调用 AI 审核接口进行自动审核。
     * </p>
     */
    @XxlJob("aiApproveStoreInfoTask")
    public R<String> aiApproveStoreInfoTask() {
        log.info("开始执行店铺AI审核任务...");
        try {
            // 1. 查询待审核门店：store_application_status = 0 且 未删除
            List<StoreInfo> pendingStores = storeInfoMapper.selectList(
                    new LambdaQueryWrapper<StoreInfo>()
                            .eq(StoreInfo::getStoreApplicationStatus, 0)
                            .eq(StoreInfo::getDeleteFlag, 0)
            );

            if (pendingStores == null || pendingStores.isEmpty()) {
                log.info("当前无待审核门店，任务结束。");
                XxlJobHelper.handleSuccess("当前无待审核门店");
                return R.success("当前无待审核门店");
            }

            log.info("本次待审核门店数量：{}", pendingStores.size());

            // 2. 登录 AI 服务获取 token（如需调用 AI 审核接口，可在此处复用 token）
            MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
            formData.add("username", userName);
            formData.add("password", passWord);

            HttpHeaders loginHeaders = new HttpHeaders();
            loginHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity<MultiValueMap<String, String>> loginRequest = new HttpEntity<>(formData, loginHeaders);

            ResponseEntity<String> loginResponse = null;
            try {
                log.info("请求Ai服务登录接口===================> {}", loginUrl);
                loginResponse = restTemplate.postForEntity(loginUrl, loginRequest, String.class);
            } catch (Exception e) {
                log.error("请求AI服务登录接口失败", e);
            }

            String accessToken = null;
            if (loginResponse != null && loginResponse.getStatusCodeValue() == 200) {
                log.info("请求Ai服务登录成功 body={}", loginResponse.getBody());
                String body = loginResponse.getBody();
                if (body != null) {
                    JSONObject jsonObject = JSONObject.parseObject(body);
                    if (jsonObject != null) {
                        JSONObject dataJson = jsonObject.getJSONObject("data");
                        if (dataJson != null) {
                            accessToken = dataJson.getString("access_token");
                        }
                    }
                }
            }

            if (accessToken == null) {
                log.error("获取AI服务 access_token 失败，终止店铺AI审核任务");
                XxlJobHelper.handleFail("获取AI服务 access_token 失败");
                return R.fail("获取AI服务 access_token 失败");
            }

            // 3. 遍历待审核门店，构建 AiApproveStoreInfo 并预留调用 AI 审核接口（具体字段后续补充）
            HttpHeaders aiHeaders = new HttpHeaders();
            aiHeaders.setContentType(MediaType.APPLICATION_JSON);
            aiHeaders.set("Authorization", "Bearer " + accessToken);

            for (StoreInfo storeInfo : pendingStores) {
                AiApproveStoreInfo aiApproveStoreInfo = new AiApproveStoreInfo();

                HttpEntity<AiApproveStoreInfo> request = new HttpEntity<>(aiApproveStoreInfo, aiHeaders);
                ResponseEntity<String> response = null;
                try {
                    response = restTemplate.postForEntity(aiMerchantOnboardingApplicationsUrl, request, String.class);
                    if (response.getStatusCodeValue() != 200) {
                        log.error("店铺AI审核接口调用失败 storeId={}, http状态={}", storeInfo.getId(), response.getStatusCode());
                        continue;
                    }
                    String respBody = response.getBody();
                    log.info("店铺AI审核返回结果 storeId={}, body={}", storeInfo.getId(), respBody);
                    // 如需根据 AI 返回结果更新 store_info 审核状态，可在此处解析 respBody 并更新 DB
                } catch (Exception e) {
                    log.error("调用店铺AI审核接口异常, storeId={}", storeInfo.getId(), e);
                }
            }

            XxlJobHelper.handleSuccess("店铺AI审核任务执行完成");
            return R.success("店铺AI审核任务执行完成");
        } catch (Exception e) {
            log.error("店铺AI审核任务执行异常", e);
            XxlJobHelper.handleFail("店铺AI审核任务执行异常: " + e.getMessage());
            return R.fail("店铺AI审核任务执行异常: " + e.getMessage());
        }
    }



    class AnalysisRequest {
        private String start_time;
        private String end_time;

        public String getStart_time() {
            return start_time;
        }

        public void setStart_time(String start_time) {
            this.start_time = start_time;
        }

        public String getEnd_time() {
            return end_time;
        }

        public void setEnd_time(String end_time) {
            this.end_time = end_time;
        }
    }
}
