package com.tzld.piaoquan.api;

import com.aliyun.openservices.shade.com.google.common.collect.Sets;
import com.tzld.piaoquan.api.dao.mapper.contentplatform.ContentPlatformAccountMapper;
import com.tzld.piaoquan.api.dao.mapper.contentplatform.ContentPlatformGzhPlanVideoMapper;
import com.tzld.piaoquan.api.dao.mapper.contentplatform.ContentPlatformIllegalMsgMapper;
import com.tzld.piaoquan.api.job.contentplatform.ContentPlatformDatastatJob;
import com.tzld.piaoquan.api.model.po.contentplatform.*;
import com.tzld.piaoquan.growth.common.model.bo.VideoDetail;
import com.tzld.piaoquan.growth.common.service.MessageAttachmentService;
import com.tzld.piaoquan.growth.common.utils.DateUtil;
import com.tzld.piaoquan.growth.common.utils.MessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SpringBootTest(classes = GrowthServerApplication.class)
@Slf4j
public class ContentPlatformTest {

    @Autowired
    ContentPlatformDatastatJob datastatJob;
    @Autowired
    MessageAttachmentService messageAttachmentService;
    @Autowired
    ContentPlatformAccountMapper accountMapper;
    @Autowired
    private ContentPlatformIllegalMsgMapper contentPlatformIllegalMsgMapper;
    @Autowired
    private ContentPlatformGzhPlanVideoMapper gzhPlanVideoMapper;

    @Test
    public void testSyncContentPlatformQwDatastatReplyTotalJob() {
        List<String> dtList = DateUtil.getBeforeDays(7);
        for (String dt : dtList) {
            datastatJob.syncContentPlatformGzhDatastatJob(dt);
            datastatJob.syncContentPlatformGzhVideoDatastatJob(dt);
            datastatJob.syncContentPlatformGzhDatastatTotalJob(dt);
            datastatJob.syncContentPlatformFwhDatastatJob(dt);
            datastatJob.syncContentPlatformFwhDatastatTotalJob(dt);
            datastatJob.syncContentPlatformGzhPushDatastatJob(dt);
            datastatJob.syncContentPlatformGzhPushDatastatTotalJob(dt);
            datastatJob.syncContentPlatformQwDatastatJob(dt);
            datastatJob.syncContentPlatformQwDatastatTotalJob(dt);
            datastatJob.syncContentPlatformQwDatastatReplyTotalJob(dt);
            datastatJob.syncContentPlatformQwDatastatSubChannelJob(dt);
        }
    }

    @Test
    public void testMessageAttachmentService() {
        Set<Long> videoIds = Sets.newHashSet(56978204L);
        Map<Long, VideoDetail> videoDetailMap = messageAttachmentService.getVideoDetail(videoIds);
        log.info("videoDetailMap:{}", videoDetailMap);
    }

    @Test
    public void changeAccountPrice() {
        List<String> channelList = Arrays.asList("gzh", "fwh", "qw");
        ContentPlatformAccountExample example = new ContentPlatformAccountExample();
        example.createCriteria().andChannelIn(channelList);
        List<ContentPlatformAccount> accountList = accountMapper.selectByExample(example);
        accountList.forEach(account -> {
            String price = updateJsonValue(account.getPrice(), "qiweihezuo_static_price", "0.25");
            account.setPrice(price);
            //accountMapper.updateByPrimaryKeySelective(account);
        });
    }

    /**
     * 仅通过字符串匹配方式修改指定 key 的 value
     *
     * @param jsonStr 原始 JSON 字符串
     * @param key     要修改的字段名，例如 "qiweihezuo_static_price"
     * @param newValue 新的值，例如 "0.25"
     * @return 修改后的 JSON 字符串
     */
    public String updateJsonValue(String jsonStr, String key, String newValue) {
        // 匹配规则： "qiweihezuo_static_price": 任意数值
        // 支持整数、小数，并允许空格
        String regex = "(\"" + Pattern.quote(key) + "\"\\s*:\\s*)([0-9]+(?:\\.[0-9]+)?)";

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(jsonStr);

        // 替换为新的值
        return matcher.replaceAll("$1" + newValue);
    }

    @Test
    public void sendIllegalMsg() {
        List<String> channels = Arrays.asList("yy","yygz","ls","xs","zqh","cy","qs","bh","jxyy","yqst","sw","sxzq","bz","gzdd","gzbb","gzxts","gzyhc","gzlq","wxm","hc","hzrf","ywlx","zc","hzqf","qd","zh","pj","szhx","zzws","llf","shx","lq","zzbf","xsy","ml","xmayd","wx","hnld","gzash","xf","jyxx","htlz","hzlcz","gzyd");
        ContentPlatformAccountExample example = new ContentPlatformAccountExample();
        example.createCriteria().andChannelIn(channels);
        List<ContentPlatformAccount> accountList = accountMapper.selectByExample(example);
        for (ContentPlatformAccount contentPlatformAccount : accountList) {
            ContentPlatformIllegalMsg msg = new ContentPlatformIllegalMsg();
            msg.setAccountId(contentPlatformAccount.getId());
            msg.setStatus(0);
            msg.setIllegalTitle("平台【本渠道裂变率】概念调整通知");
            msg.setIllegalContent("尊敬的用户：\n" +
                    "\n" +
                    "为优化体验、规范信息传递，平台将公众号-自动回复的【本渠道裂变率】调整为【本渠道质量分】。新字段整合裂变、arpu等维度的效率数值，综合反映渠道质量表现。\n" +
                    "\n" +
                    "本次调整于2026年1月8日正式生效，不影响用户核心权益。如有疑问，可联系商务反馈。\n" +
                    "\n" +
                    "2026年1月8日");
            msg.setCreateTimestamp(System.currentTimeMillis());
            msg.setUpdateTimestamp(System.currentTimeMillis());
            contentPlatformIllegalMsgMapper.insertSelective(msg);
        }
    }

    @Test
    public void setRootSourceId() {
        ContentPlatformGzhPlanVideoExample example = new ContentPlatformGzhPlanVideoExample();
        example.createCriteria().andPageUrlIsNotNull().andRootSourceIdIsNull();
        List<ContentPlatformGzhPlanVideo> list = gzhPlanVideoMapper.selectByExample(example);
        for (ContentPlatformGzhPlanVideo item : list) {
            item.setRootSourceId(MessageUtil.getRootSourceId(item.getPageUrl()));
            gzhPlanVideoMapper.updateByPrimaryKeySelective(item);
        }
    }

}
