public function updateData($data) { $data = (array) $data; $old_data = array(); foreach (UnitData::search(array('id' => $this->id)) as $unitdata) { $old_data[$unitdata->column_id] = $unitdata->value; } $add_data = $delete_data = $modify_data = array(); foreach ($data as $column => $value) { $column_id = ColumnGroup::getColumnId($column); if (!array_key_exists($column_id, $old_data)) { $add_data[] = $column_id; } elseif (json_encode($value, JSON_UNESCAPED_UNICODE) != $old_data[$column_id]) { $modify_data[] = $column_id; } } foreach ($old_data as $column_id => $value) { if (!array_key_exists(ColumnGroup::getColumnName($column_id), $data)) { $delete_data[] = $column_id; } } if (count($add_data) + count($modify_data) + count($delete_data) == 0) { return; } $now = time(); foreach ($add_data as $column_id) { $value = json_encode($data[ColumnGroup::getColumnName($column_id)], JSON_UNESCAPED_UNICODE); UnitData::insert(array('id' => $this->id, 'column_id' => $column_id, 'value' => $value)); UnitChangeLog::insert(array('id' => $this->id, 'updated_at' => $now, 'column_id' => $column_id, 'old_value' => '', 'new_value' => $value)); } foreach ($modify_data as $column_id) { $value = json_encode($data[ColumnGroup::getColumnName($column_id)], JSON_UNESCAPED_UNICODE); $unitdata = UnitData::find(array($this->id, $column_id)); $unitdata->update(array('value' => json_encode($data[ColumnGroup::getColumnName($column_id)], JSON_UNESCAPED_UNICODE))); try { UnitChangeLog::insert(array('id' => $this->id, 'updated_at' => $now, 'column_id' => $column_id, 'old_value' => $old_data[$column_id], 'new_value' => $value)); } catch (Pix_Table_DuplicateException $e) { } } foreach ($delete_data as $column_id) { try { UnitChangeLog::insert(array('id' => $this->id, 'updated_at' => $now, 'column_id' => $column_id, 'old_value' => $old_data[$column_id], 'new_value' => '')); } catch (Pix_Table_DuplicateException $e) { } UnitData::find(array($this->id, $column_id))->delete(); } $this->update(array('updated_at' => $now)); }
public function dump() { $columns = array(); foreach (ColumnGroup::search(1) as $columngroup) { $columns[$columngroup->id] = $columngroup->name; } $delta = 10000000; for ($i = 0; $i * $delta < 99999999; $i++) { $start = $i * $delta; $end = $start + $delta - 1; $tmpname1 = tempnam('', ''); $tmpname2 = tempnam('', ''); $file_name1 = 'files/' . str_pad($i * $delta, 8, '0', STR_PAD_LEFT) . '.json.gz'; $file_name2 = 'files/bussiness-' . str_pad($i * $delta, 8, '0', STR_PAD_LEFT) . '.json.gz'; $fp[1] = $fp[3] = gzopen($tmpname1, 'w'); $fp[2] = gzopen($tmpname2, 'w'); $unit_id = null; $unit = new StdClass(); foreach (UnitData::search("`id` >= {$start} AND `id` <= {$end}")->order("`id`, `column_id`")->volumemode(10000) as $unit_data) { if (!is_null($unit_id) and $unit_data->id != $unit_id) { fwrite($fp[$this->getType($unit_id)], str_pad($unit_id, 8, '0', STR_PAD_LEFT) . ',' . json_encode($unit, JSON_UNESCAPED_UNICODE) . "\n"); $unit = new StdClass(); } $unit_id = $unit_data->id; $unit->{$columns[$unit_data->column_id]} = json_decode($unit_data->value); } if (!is_null($unit_id)) { fwrite($fp[$this->getType($unit_id)], str_pad($unit_id, 8, '0', STR_PAD_LEFT) . ',' . json_encode($unit, JSON_UNESCAPED_UNICODE) . "\n"); } fclose($fp[1]); fclose($fp[2]); DropboxLib::putFile($tmpname1, $file_name1); DropboxLib::putFile($tmpname2, $file_name2); unlink($tmpname1); unlink($tmpname2); } }
} if (count($unit_names)) { // 1 - 公司 foreach (UnitData::search(array('column_id' => 2))->searchIn('id', $units[1]) as $unitdata) { $unit_names[$unitdata->id] = $unitdata->value; } // 2 - 商業登記 foreach (UnitData::search(array('column_id' => 33))->searchIn('id', $units[2]) as $unitdata) { $unit_names[$unitdata->id] = $unitdata->value; } // 3 - 分公司 foreach (Unit::search(1)->searchIn('id', $units[3]) as $unit) { $unit_names[$unit->id] = json_encode($unit->name()); } // 4 - 教育部, 99 - 其他 foreach (UnitData::search(array('column_id' => 43))->searchIn('id', array_merge($units[4], $units[99])) as $unitdata) { $unit_names[$unitdata->id] = $unitdata->value; } foreach ($unit_names as $id => $value) { $rows = array(); $rows[] = str_pad($id, 8, '0', STR_PAD_LEFT); $rows[] = $type_names[$types[$id]]; $v = json_decode($value); if (is_array($v)) { $rows[] = $v[0]; } else { $rows[] = strval($v); } fputcsv($fp, $rows); } $units = $unit_names = $types = array();