import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @ClassName: AreaTest
 * @Description: 地区测试
 * @Author: wanggeng
 * @Date: 2021/11/24 3:25 下午
 * @Version: 1.0
 */
public class AreaTest {

    private String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/db_smart_city_usercenter?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=UTC&nullCatalogMeansCurrent=true";

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void removeUnnecessaryAreas() throws SQLException {
        Connection connection = DriverManager.getConnection(jdbcUrl, "root", "root");
        String SQL = "DELETE FROM data_area WHERE area_code NOT LIKE ?";
        try (PreparedStatement preparedStatement = connection.prepareStatement(SQL)) {
            // 保留的根节点编码
            preparedStatement.setString(1, "1509%");
            System.out.println(preparedStatement);
            int removeCount = preparedStatement.executeUpdate();
            System.out.println("删除总数：" + removeCount);
        }
    }

    @Test
    public void updateAreaRootParentId() throws SQLException {
        Connection connection = DriverManager.getConnection(jdbcUrl, "root", "root");
        String SQL = "UPDATE data_area SET area_parent_id = 0 WHERE area_code = ?";
        try (PreparedStatement preparedStatement = connection.prepareStatement(SQL)) {
            // 设置更新的地区编码
            preparedStatement.setString(1, "150900000000");
            System.out.println(preparedStatement);
            int updateCount = preparedStatement.executeUpdate();
            System.out.println("更新成功：" + updateCount);
        }
    }


}
