/** * Execute the console command. * * @return mixed */ public function fire() { $file = public_path('region.js'); try { $json = file_get_contents($file); } catch (\Exception $e) { echo $e->getMessage(); } \DB::table('addresses')->truncate(); eval($json); //province foreach ($province as $p) { $address = new Address(); $address->id = $p[0]; $address->guid = Uuid::v4(false); $address->address = $p[1][0]; $address->parent_id = 0; $address->level = $p[2]; $address->save(); } $i = 0; $arrId = array(); foreach ($json as $v) { if ($v[3] == 3) { continue; } $address = new Address(); if (in_array($v[0], $arrId)) { rsort($arrId); $id = $arrId[0] + 1; \Log::info(sprintf("%s in arr ,+1 = %s", $v[0], $id)); } else { $id = $v[0]; } $address->id = $id; $address->guid = Uuid::v4(false); $address->address = $v[1][0]; $address->parent_id = $v[2]; $address->level = $v[3]; array_push($arrId, $id); try { $address->save(); } catch (\Exception $e) { \Log::info(sprintf("erro msg: %s ", $e->getMessage())); continue; } echo sprintf("success insert one raw,ID: %s", $address->id); $i++; echo "\n"; } echo sprintf("complete !! %s raws affected\n", $i); }
public function create() { $buyerId = $this->buyerId; $inputData = $this->inputData->all(); $validator = Validator::make($inputData, ['countyId' => 'required', 'address' => 'required', 'postcode' => '', 'receiver' => 'required', 'mobile' => 'required', 'default' => 'required']); $countyId = $inputData['countyId']; if (!($county = Address::find($countyId))) { return RestHelp::encodeResult(23002, "地址不合法,请重新输入"); } try { $city = $county->getFather(); $province = $city->getFather(); } catch (\Exception $e) { return RestHelp::encodeResult(23002, "地址不合法,请重新输入"); } if ($validator->fails()) { return RestHelp::parametersIllegal($validator->messages()->first()); } $newDeliverAddressid = $this->buyerAddressService->create($buyerId, $inputData['countyId'], $inputData['address'], isset($inputData['postcode']) ? $inputData['postcode'] : '', $inputData['receiver'], $inputData['mobile'], $inputData['default'] == true ? 1 : 0); if ($inputData['default'] == true) { BuyerAddress::whereRaw("id != {$newDeliverAddressid} and deleted_at is null")->where('buyer_id', $buyerId)->update(['default' => 0]); } return RestHelp::success(['id' => $newDeliverAddressid]); }
public function countyIndex() { $cityId = $this->inputData->get('cityId'); if (!$cityId) { return RestHelp::parametersIllegal("city_id is required"); } $county = Address::where('parent_id', $cityId)->get(); $arrRet = array(); foreach ($county as $k => $p) { $arrRet[$k]['id'] = $p->id; $arrRet[$k]['name'] = $p->address; } return RestHelp::success($arrRet); }