package cn.qinys.platform.admin.service;

import cn.qinys.platform.admin.mapper.WishingTreeExtensionMapper;
import cn.qinys.platform.admin.mapper.WishingTreeMapper;
import cn.qinys.platform.entity.wishing.WishingTree;
import cn.qinys.platform.entity.wishing.WishingTreeExtension;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

/**
 * @author lie tan
 * @description
 * @date 2026-06-13 11:26
 **/

@SpringBootTest
class GaoDeServiceTest {

    @Resource
    private GaoDeService gaoDeService;
    @Resource
    WishingTreeMapper treeMapper;
    @Resource
    WishingTreeExtensionMapper treeExtensionMapper;

    @Test
    void getGaoDeResult() {
        QueryWrapper<WishingTree> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("id", 0);
        List<WishingTree> trees = treeMapper.selectList(queryWrapper);
        trees.forEach(tree -> {
            JSONObject gaoDeResult = gaoDeService.getGaoDeResult(tree.getLongitude(), tree.getLatitude(), 5);
            JSONObject addressComponent = gaoDeResult.getJSONObject("addressComponent");
            Integer adcode = addressComponent.getInteger("adcode");
            UpdateWrapper<WishingTreeExtension> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("tree_id", tree.getId());
            updateWrapper.set("adcode", adcode);
            updateWrapper.set("regeocode", JSON.toJSONString(gaoDeResult));
            int update = treeExtensionMapper.update(updateWrapper);
            if(update == 0){
                WishingTreeExtension extension = new WishingTreeExtension();
                extension.setTreeId(tree.getId());
                extension.setTotalCount(0);
                extension.setPublicCount(0);
                extension.setPrivateCount(0);
                extension.setAdcode(adcode);
                extension.setRegeocode(JSON.toJSONString(gaoDeResult));
                treeExtensionMapper.insert(extension);
            }

        });

    }
}