Scratch: Updating Laravolt Indonesia
Coretan-coretan untuk memperbarui data Laravolt Indonesia berdasarkan data tahun 2022.

Qisthi Ramadhani / 01 Januari 2023
use AppModelsWilayah;
use IlluminateSupportCollection;
use IlluminateSupportFacadesDB;
use LaravoltIndonesiaModelsProvince;
use LaravoltIndonesiaModelsCity;
use LaravoltIndonesiaModelsDistrict;
use LaravoltIndonesiaModelsVillage;
// dd(
// Wilayah::query()->village()->whereCode('1771071006')->first()
// );
// Wilayah::query()->village()->get()->chunk(1000)->each(function (Collection $collect) {
// $villageCodes = $collect->pluck('code')->toArray();
// Village::query()->whereIn('code', $villageCodes)->get()
// ->each(function ($village) use ($collect) {
// $newVillage = $collect->where('code', $village->code)->first();
// $name = strtoupper($newVillage->nama);
// $village->name = $name;
// $village->save();
// })
// ;
// });
// dd('done');
// $cities = Wilayah::query()->city()
// ->whereIn('code', [
// "1172",
// "1221",
// "1471",
// "1501",
// "1503",
// "1771",
// "1808",
// "2105",
// "3516",
// "5305",
// "5314",
// "5317",
// "5319",
// "6111",
// "6302",
// "6310",
// "6402",
// "6503",
// "7402",
// "7471",
// "8201",
// "8207",
// "9101",
// "9118",
// "9171",
// "9201",
// ])
// ->get()
// ;
// $districts = Wilayah::query()->district()
// ->whereIn('city_code', $cities->pluck('code')->toArray())
// ->get()
// ;
// $newVillages = Wilayah::query()->village()
// ->whereIn('district_code', $districts->pluck('code')->toArray())
// ->get()
// ;
// $oldVillages = Village::query()
// ->whereIn('district_code', $districts->pluck('code')->toArray())
// ->get()
// ;
// $cities->each(function (Wilayah $city) use ($districts, $newVillages, $oldVillages) {
// $currentDistricts = $districts->whereIn('city_code', $city->code);
// $currentDistricts->each(function (Wilayah $district) use ($newVillages, $oldVillages) {
// if(
// $newVillages->whereIn('district_code', $district->code)->count()
// !== $oldVillages->whereIn('district_code', $district->code)->count()
// ) {
// if (in_array($district->code, [
// '117201',
// '117202',
// ])) {
// // do nothing
// } else {
// dd([
// $newVillages->whereIn('district_code', $district->code)->toArray(),
// $oldVillages->whereIn('district_code', $district->code)->toArray(),
// 'new' => $newVillages->whereIn('district_code', $district->code)->count(),
// 'old' => $oldVillages->whereIn('district_code', $district->code)->count()
// ]);
// }
// }
// });
// });
// dd('enough');
// $cities = Wilayah::query()->city()->get();
// $districts = Wilayah::query()->district()->get();
// $districtCodes = $districts->pluck('code');
// $villages = Village::query()
// ->whereIn('district_code', $districtCodes->toArray())
// ->get();
// $newVillages = Wilayah::query()->village()
// ->whereIn('district_code', $districtCodes->toArray())
// ->get();
// $counted = 0;
// $countedCityCode = collect([]);
// $cities->each(function (Wilayah $city) use ($districts, $villages, $newVillages, &$counted, &$countedCityCode) {
// $districtCodes = $districts
// ->where('city_code', $city->code)
// ->pluck('code')
// ->toArray();
// if (
// $villages->whereIn('district_code', $districtCodes)->count()
// !== $newVillages->whereIn('district_code', $districtCodes)->count()
// ) {
// $counted += 1;
// $countedCityCode->push($city->code);
// }
// });
// dd($counted, $countedCityCode);
// $villages = Village::all();
// $villages->chunk(1000)->each(function (Collection $collect) {
// $villageIds = $collect->pluck('code');
// Wilayah::query()->village()->whereIn('code', $villageIds->toArray())->delete();
// });
// dd('done');
// $villages = Wilayah::query()->get();
// DB::beginTransaction();
// $villages->each(function (Wilayah $item) {
// $village = Village::query()->whereCode($item->code)->first();
// if (!$village) {
// Village::query()->create([
// 'code' => $item->code,
// 'district_code' => $item->district_code,
// 'name' => strtoupper($item->nama),
// 'meta' => '{"lat":NULL,"long":NULL,"pos":NULL}',
// ]);
// }
// });
// DB::commit();
// dd('done');
// $districts->each(function (Wilayah $item) use ($villages, $newVillages, &$counted) {
// if (
// $villages->where('district_code', $item->code)->count()
// !== $newVillages->where('district_code', $item->code)->count()
// ) {
// $counted += 1;
// }
// });
// Wilayah::query()->village()->get()->each(function (Wilayah $item) {
// $kodes = explode('.', $item->kode);
// $districtCode = $kodes[0].$kodes[1].$kodes[2];
// $cityCode = $kodes[0].$kodes[1];
// $provinceCode = $kodes[0];
// $code = $kodes[0].$kodes[1].$kodes[2].$kodes[3];
// $item->update([
// 'province_code' => $provinceCode,
// 'city_code' => $cityCode,
// 'district_code' => $districtCode,
// 'code' => $code,
// ]);
// });
// dd('updated');
$newDistrict = Wilayah::query()->district()->count();
$district = District::count();
$newVillage = Wilayah::query()->village()->count();
$village = Village::count();
dd([
'district_2022' => $newDistrict,
'district_current' => $district,
'village_2022' => $newVillage,
'village_current' => $village,
'district_diff' => $newDistrict - $district,
'village_diff' => $newVillage - $village,
]);
// Wilayah::query()->village()->whereIn('code', Village::query()->pluck('code')->toArray())->delete();
// dd('hi');
// $districtIds = District::query()->pluck('code')->toArray();
// $districts = Wilayah::query()
// ->district()
// ->whereNotIn('code', $districtIds)
// ->get();
// $districts->each(function ($item) {
// $kode = explode('.', $item->kode);
// $item->code = $kode[0].$kode[1].$kode[2];
// $item->city_code = $kode[0].$kode[1];
// $item->save();
// });
// dd('done');
// DB::beginTransaction();
// $districts->each(function ($item) {
// District::query()->create([
// 'code' => $item->code,
// 'city_code' => $item->city_code,
// 'name' => $item->nama,
// 'meta' => '{"lat":"NULL","long":"NULL"}',
// ]);
// });
// DB::commit();
// dd(
// 'done'
// );
/** @var Collection $villages */
// $villages = Village::all();
// $villages->chunk(10000)->each(function (Collection $collect) {
// dd($collect->pluck('code'));
// });
/** @var Collection $newVillages */
// $newVillages = Wilayah::query()->village()->get();
// $newVillages->chunk(10000)->each(function (Collection $collect) {
// $collect->each(function (Wilayah $item) {
// $kode = explode('.', $item->kode);
// $item->city_code = $kode[0].$kode[1];
// $item->district_code = $kode[0].$kode[1].$kode[2];
// $item->code = $kode[0].$kode[1].$kode[2].$kode[3];
// $item->save();
// });
// });
// dd(
// 'done'
// );
// $villageIds = Village::query()->pluck('code')->toArray();
// $newVillageIds = Wilayah::query()->village()->pluck('code')->toArray();
// dd(
// count($newVillageIds),
// array_intersect($newVillageIds, $villageIds)
// );