const fs = require('fs');
const path = require('path');

// 读取JSON文件内容
function readJsonFile(filePath) {
  try {
    const data = fs.readFileSync(filePath, 'utf8');
    return JSON.parse(data);
  } catch (error) {
    console.error(`Error reading ${filePath}:`, error.message);
    return [];
  }
}

// 处理省级数据
function processProvinceData(provinceData) {
  return provinceData.map(item => ({
    id: item.code,
    pid: 0,
    tier: 1,
    district_name: item.name,
    sort: 0
  }));
}

// 处理市级数据，补全直辖市的虚拟市级数据
function processCityData(cityData, provinceData) {
  const cities = cityData.map(item => ({
    id: item.code,
    pid: item.province + '0000',
    tier: 2,
    district_name: item.name,
    sort: 0
  }));

  // 找出所有需要补全市级数据的省份（没有对应的市级数据的省份）
  const provincesNeedingVirtualCity = provinceData.filter(province =>
      !cityData.some(city => city.province === province.province)
  );

  // 为每个需要补全的省份创建虚拟的市级数据，代码为省份代码后两位改为01
  const virtualCities = provincesNeedingVirtualCity.map(province => {
    const provinceCode = province.code;
    // 生成虚拟市级代码，例如将110000改为110100
    const virtualCityCode = provinceCode.slice(0, 2) + '0100';

    return {
      id: virtualCityCode,
      pid: provinceCode,
      tier: 2,
      district_name: province.name,
      sort: 0
    };
  });

  return [...cities, ...virtualCities];
}

// 处理区县级数据
function processAreaData(areaData, cityData, provinceData) {
  return areaData.map(item => {
    // 尝试查找对应的市级数据
    const city = cityData.find(city =>
        city.province === item.province && city.city === item.city
    );

    // 如果找到了对应的市级数据，使用市级ID作为父ID
    if (city) {
      return {
        id: item.code,
        pid: city.code,
        tier: 3,
        district_name: item.name,
        sort: 0
      };
    }

    // 否则，使用省级ID作为父ID（可能是直辖市的情况）
    const province = provinceData.find(p => p.province === item.province);
    return {
      id: item.code,
      pid: province ? province.province + '0100' : 0,
      tier: 3,
      district_name: item.name,
      sort: 0
    };
  });
}

// 处理乡镇级数据
function processTownData(townData, areaData) {
  return townData.map(item => {
    // 查找对应的区县级数据
    const area = areaData.find(area =>
        area.province === item.province &&
        area.city === item.city &&
        area.area === item.area
    );

    return {
      id: item.province + item.city + item.area + item.town,
      pid: area ? area.code : 0,
      tier: 4,
      district_name: item.name,
      sort: 0
    };
  });
}

// 生成SQL插入语句
function generateSqlStatements(data, tableName, batchSize = 500) {
  const sqlStatements = [];

  for (let i = 0; i < data.length; i += batchSize) {
    const batch = data.slice(i, i + batchSize);
    const values = batch.map(item => {
      const escapedName = item.district_name.replace(/'/g, "''");
      return `('${item.id}', '${item.pid}', ${item.tier}, '${escapedName}', ${item.sort})`;
    }).join(',');

    sqlStatements.push(`INSERT INTO ${tableName} (id, pid, tier, district_name, sort)
                        VALUES ${values};  `);
  }

  return sqlStatements;
}

// 主函数
function main() {
  // 读取数据
  const provinceData = readJsonFile(path.join(__dirname, 'province.json'));
  const cityData = readJsonFile(path.join(__dirname, 'city.json'));
  const areaData = readJsonFile(path.join(__dirname, 'area.json'));
  const townData = readJsonFile(path.join(__dirname, 'town.json'));

  // 处理数据
  const processedProvinces = processProvinceData(provinceData);
  const processedCities = processCityData(cityData, provinceData);
  const processedAreas = processAreaData(areaData, cityData, provinceData);
  const processedTowns = processTownData(townData, areaData);

  // 生成SQL语句
  const tableName = 'sys_district';
  const provinceSql = generateSqlStatements(processedProvinces, tableName);
  const citySql = generateSqlStatements(processedCities, tableName);
  const areaSql = generateSqlStatements(processedAreas, tableName);
  const townSql = generateSqlStatements(processedTowns, tableName);

  // 写入SQL文件
  const sqlContent = [
    '-- 省级数据',
    ...provinceSql,
    '',
    '-- 市级数据',
    ...citySql,
    '',
    '-- 区县级数据',
    ...areaSql,
    '',
    '-- 乡镇级数据',
    ...townSql,
    '',
    'UPDATE sys_district\n' +
    'SET province = CONCAT(SUBSTRING(id, 1, 2), \'0000\'),\n' +
    '    city     = IF(tier >= 2, CONCAT(SUBSTRING(id, 1, 2), SUBSTRING(id, 3, 2), \'00\'), \'\'),\n' +
    '    area     = IF(tier >= 3, CONCAT(SUBSTRING(id, 1, 2), SUBSTRING(id, 3, 2), SUBSTRING(id, 5, 2)), \'\'),\n' +
    '    town     = IF(tier >= 4, CONCAT(SUBSTRING(id, 1, 2), SUBSTRING(id, 3, 2), SUBSTRING(id, 5, 2), SUBSTRING(id, 7, 6)), \'\')'
  ].join('\n');

  fs.writeFileSync(path.join(__dirname, 'district_data.sql'), sqlContent, 'utf8');
  console.log('SQL文件生成成功！');
}

// 执行主函数
main();
