package com.lanyuanxiaoyao.digtal.market

import cn.hutool.json.JSONUtil
import com.lanyuanxiaoyao.digtal.market.runner.NewsRunner
import com.lanyuanxiaoyao.squirrel.core.common.Management
import jakarta.annotation.Resource
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Test
import org.slf4j.LoggerFactory
import org.springframework.boot.test.context.SpringBootTest

@SpringBootTest(
    classes = [Application::class],
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
)
class TestManagement {
    private val logger = LoggerFactory.getLogger(javaClass)

    @Resource
    private lateinit var management: Management

    @Resource
    private lateinit var newsRunner: NewsRunner

    @Test
    fun testParse() {
        parseArticleLink(
            management,
            "9a7f1d8f-4f39-4120-adeb-7435339b97bb",
            "https://www.gov.cn/zhengce/content/202409/content_6977766.htm",
        )
            .forEach { link ->
                val article = parseArticle(
                    management,
                    "9a7f1d8f-4f39-4120-adeb-7435339b97bb",
                    link.url,
                    "",
                    link.title,
                    link.datetime,
                )
                logger.info("{} {} {} {}", article?.title, article?.createTime, article?.author, article?.text)
            }
    }

    @Test
    fun testDownload() {
        val page = runBlocking {
            management.download(
                "74ee6b33-c1a3-41f9-b947-acd0bebd0e6e",
                "https://zfsg.gd.gov.cn/gkmlpt/content/4/4514/post_4514242.html#2589",
            )
        }
        logger.info("Page: {}", page)
    }

    @Test
    fun testParseList() {
        parseArticleLink(
            management,
            "cee7f242-668b-41fb-adbc-96fb27d4bf35",
            "https://zwfwj.beijing.gov.cn/so/ss/query/s?qt=(\"数据要素\")&sort=dateDesc&siteCode=1100000248&tab=&page=1&pageSize=20",
            true,
        )
            // .let { logger.info("{}", JSONUtil.toJsonPrettyStr(it)) }
            .let { it.forEach { logger.info("{}", it.url) } }
    }

    @Test
    fun testParseArticle() {
        parseArticle(
            management,
            "00bfe1f5-7662-4ea5-ada3-6e9dfc19f0be",
            "https://www.gov.cn/zhengce/zhengceku/202411/content_6984322.htm",
            "",
            "工业和信息化部关于印发《工业和信息化领域数据安全事件应急预案（试行）》的通知",
            "2024-11-01 12:48:26",
        )
            ?.let { logger.info("{}", JSONUtil.toJsonPrettyStr(it)) }
    }
}