package com.ruoyi.web.controller.back;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.Region;
import com.ruoyi.common.utils.RegionDataImporter;
import com.ruoyi.system.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RequestMapping("/back/region")
@RestController
public class RegionController {

    @Autowired
    private IRegionService regionService;

    @RequestMapping("/add")
    public AjaxResult add() throws IOException {
        File file = new File("D:\\region_tree_data.json");
        if (!file.exists()) {
            System.err.println("文件不存在: " + file.getAbsolutePath());
        }
        // 读取JSON文件内容
        String jsonContent = org.apache.commons.io.FileUtils.readFileToString(file, "UTF-8");
        System.out.println("JSON文件读取成功，内容长度: " + jsonContent.length());

        // 导入数据
        List<Region> regionList = RegionDataImporter.importRegionData(jsonContent);
        regionService.batchInsert(regionList);
        return AjaxResult.success();
    }

}
