|
@@ -0,0 +1,251 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+class JyzlCronAction extends Action {
|
|
|
+
|
|
|
+
|
|
|
+ public $size = 1000;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public function getList( $model, $field ){
|
|
|
+ $list = $model->field($field)->select();
|
|
|
+ if(!$list){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $jms_city_list = array();
|
|
|
+ foreach ($list as $key => $val) {
|
|
|
+ $jms_city_list[$val['ID']] = $this->md5_id_base($val);
|
|
|
+ }
|
|
|
+ return $jms_city_list;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public function md5_id_base( $data, $length = -6 ){
|
|
|
+ $str = '';
|
|
|
+ foreach ($data as $val){
|
|
|
+ $str.=$val;
|
|
|
+ }
|
|
|
+ return substr(md5($str),$length);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public function sync( ){
|
|
|
+ echo "同步数据开始 time:".date("Y-m-d H:i:s")."\n";
|
|
|
+ $this->syncJyzlCity();
|
|
|
+ $this->syncJyzlStation();
|
|
|
+ $this->syncJyzlVehicle();
|
|
|
+ echo "同步数据结束 time:".date("Y-m-d H:i:s")."\n";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public function getSignsList( $ids, $model, $field ){
|
|
|
+ //$where = array('ID' => array('IN',$ids));
|
|
|
+ //$list = $model->field($field)->where($where)->order('ID ASC')->select();
|
|
|
+ $list = $model->field($field)->order('ID ASC')->select();
|
|
|
+ $jyzl_city_list = array();
|
|
|
+ if ($list) {
|
|
|
+ foreach ($list as $key => $val) {
|
|
|
+ $jyzl_city_list[$val['ID']] = $this->md5_id_base($val);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $jyzl_city_list;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public function getDetailDiff( $data, $model, $field ){
|
|
|
+ $list = array();
|
|
|
+ if ($data['update']) {
|
|
|
+ foreach ($data['update'] as $update) {
|
|
|
+ $where = array('ID' => $update);
|
|
|
+ $list['update'][] = $model->field($field)->where($where)->find();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ($data['add']) {
|
|
|
+ foreach ($data['add'] as $add) {
|
|
|
+ $where = array('ID' => $add);
|
|
|
+ $list['add'][] = $model->field($field)->where($where)->find();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if($data['del']){
|
|
|
+ $list['del'] = $data['del'];
|
|
|
+ }
|
|
|
+ return $list;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public function _initialize( ){
|
|
|
+ $this->field_jms_city = "ID,ParentId,OrderNum,CityName,ProvinceName,LicensePlatePrefix,FullName,AreaCode,RzxFtpServer,SiteDomain,RegionId,ProvinceId,DefaultZoomFactor,MinZoomFactor,CenterLng,CenterLat";
|
|
|
+ $this->model_jms_city = M("jms_city");//数据源
|
|
|
+ $this->model_jyzl_city = M("jyzl_city");
|
|
|
+
|
|
|
+ $this->field_jyzl_vehicle = "";
|
|
|
+ $this->model_jms_vehicle = M("jms_vehicle");//数据源
|
|
|
+ $this->model_jyzl_vehicle = M("jyzl_vehicle");
|
|
|
+
|
|
|
+ $this->field_jzgl_station = "";
|
|
|
+ $this->model_jzgl_station = M("jzgl_station");//数据源
|
|
|
+ $this->model_jyzl_station = M("jyzl_station");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public function submitDetailDiff( $list, $model ){
|
|
|
+ /*if (!$list) {
|
|
|
+ $result = array('success'=>true,'message'=>'no sync data','error'=>'');
|
|
|
+ echo json_encode($result);exit;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!$model) {
|
|
|
+ $result = array("success" => true, "message" => "model not found");
|
|
|
+ echo json_encode($result);exit;
|
|
|
+ }*/
|
|
|
+
|
|
|
+ //修改
|
|
|
+ $error = array();
|
|
|
+ if ($list['update']) {
|
|
|
+ foreach ($list['update'] as $val) {
|
|
|
+ $where = array('ID'=>$val['ID']);
|
|
|
+ $res = $model->createSave($where,$val);
|
|
|
+ if ($res == false){
|
|
|
+ $error['update'][] = $model->getLastSql();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //新增
|
|
|
+ if ($list['add']) {
|
|
|
+ foreach ($list['add'] as $val) {
|
|
|
+ $res = $model->createAdd($val);
|
|
|
+ if($res == false){
|
|
|
+ $error['add'][] = $model->getLastSql();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //删除
|
|
|
+ /*if ($list['del']) {
|
|
|
+ foreach ($list['del'] as $val) {
|
|
|
+ $where = array( 'ID' => $val );
|
|
|
+ //$data['IsDel'] = C('删除状态_是');
|
|
|
+ //$res = $model->createSave($where,$data);
|
|
|
+ $res = $model->where($where)->delete();
|
|
|
+ if ($res == false) {
|
|
|
+ $error['del'][] = $model->getLastSql();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }*/
|
|
|
+ $result = array( 'success' => true, 'message' => 'sync data success', 'error'=>$error );
|
|
|
+ echo json_encode($result)."\n";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public function syncJyzlCity( ){
|
|
|
+ //获取城市数据列表及基本字段签名 (id=>签名 键值对)
|
|
|
+ $jms_city_list = $this->getList($this->model_jms_city, $this->field_jms_city);
|
|
|
+ if(!$jms_city_list)
|
|
|
+ return false;
|
|
|
+ //获取同部数据表城市数据列表及基本字段签名 ( id=>签名 键值对)
|
|
|
+ //$jyzl_city_list = $this->getSignsList(array_keys($jms_city_list), $this->model_jyzl_city, $this->field_jms_city);
|
|
|
+ $jyzl_city_list = $this->getSignsList([], $this->model_jyzl_city, $this->field_jms_city);
|
|
|
+ //上述两组数据比较,获取差异的(增加,删除,修改)
|
|
|
+ // id=> 差异(add,del,update) 键值对
|
|
|
+ $diff_info_list = $this->query_diff($jyzl_city_list, $jms_city_list);
|
|
|
+ //获取差异的用户详细信息
|
|
|
+ $diff_jyzl_city_list = $this->getDetailDiff($diff_info_list, $this->model_jms_city, $this->field_jms_city);
|
|
|
+ //同步差异的用户详细信息
|
|
|
+ $this->submitDetailDiff($diff_jyzl_city_list, $this->model_jyzl_city);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public function query_diff( $server, $local ){
|
|
|
+ $diff = array_keys(array_diff($local,$server));
|
|
|
+ $result['add'] = array_values(array_diff(array_keys($local),array_keys($server)));
|
|
|
+ $result['del'] = array_values(array_diff(array_keys($server),array_keys($local)));
|
|
|
+ $result['update'] =array_diff($diff,$result['add']);
|
|
|
+ if(!$result){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public function syncJyzlVehicle( ){
|
|
|
+ ini_set('memory_limit', '256M');
|
|
|
+ $count = $this->model_jms_vehicle->count();
|
|
|
+ $pageCount = ceil($count/$this->size);//总页数
|
|
|
+ $jms_vehicle_list_data = [];//数据源
|
|
|
+ $jyzl_vehicle_list_data = [];//同步数据
|
|
|
+ $diff_jyzl_vehicle_list_data = [];
|
|
|
+ for ($i = 1; $i < $pageCount + 1; $i++) {
|
|
|
+ $p = ($i-1)*$this->size;
|
|
|
+ //获取车辆列表及基本字段签名 (id=>签名 键值对)
|
|
|
+ $jms_vehicle_list = $this->getVehicleList($this->model_jms_vehicle, $this->field_jyzl_vehicle,"$p,".$this->size);
|
|
|
+ // 设置缓存
|
|
|
+ //S('VehicleCount',$p);
|
|
|
+ $jms_vehicle_list_ids = array_merge($jms_vehicle_list_ids,$jms_vehicle_list);
|
|
|
+ if(!$jms_vehicle_list) continue;
|
|
|
+ //获取同步数据表车辆列表及基本字段签名 ( id=>签名 键值对)
|
|
|
+ $jyzl_vehicle_list = $this->getVehicleSignsList(array_keys($jms_vehicle_list), $this->model_jyzl_vehicle, $this->field_jyzl_vehicle);
|
|
|
+
|
|
|
+ //上述两组数据比较,获取差异的(增加,删除,修改) id=> 差异(add,del,update) 键值对
|
|
|
+ $diff_info_list = $this->query_diff($jyzl_vehicle_list, $jms_vehicle_list);
|
|
|
+ //获取差异的用户详细信息
|
|
|
+ $diff_jyzl_vehicle_list = $this->getDetailDiff($diff_info_list, $this->model_jms_vehicle, $this->field_jyzl_vehicle);
|
|
|
+ //sleep(5);
|
|
|
+ $diff_jyzl_vehicle_list_data = array_merge($diff_jyzl_vehicle_list_data, $diff_jyzl_vehicle_list);
|
|
|
+ }
|
|
|
+ //不考虑jzyl_vehicle 里面有数据 但是jms_vehicle 无数据的情况
|
|
|
+ //$jyzl_vehicle_ids = $this->model_jyzl_vehicle->getField("ID",true);//获取同步数据表所有的ID
|
|
|
+ //$jms_vehicle_list_ids = $this->model_jms_vehicle->getField("ID",true);//获取数据源表所有的ID
|
|
|
+ //$diff_ids = array_diff($jyzl_vehicle_ids, $jms_vehicle_list_ids);
|
|
|
+ //$diff_jyzl_vehicle_list_data['del'] = empty($diff_ids)?[]:array_values($diff_ids);
|
|
|
+
|
|
|
+ $this->submitDetailDiff($diff_jyzl_vehicle_list_data, $this->model_jyzl_vehicle);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public function syncJyzlStation( ){
|
|
|
+ //获取基站列表及基本字段签名 (id=>签名 键值对)
|
|
|
+ $jzgl_station_list = $this->getList($this->model_jzgl_station, $this->field_jzgl_station);
|
|
|
+ if(!$jzgl_station_list)
|
|
|
+ return false;
|
|
|
+ //获取同步数据表基站列表及基本字段签名 ( id=>签名 键值对)
|
|
|
+ //$jyzl_station_list = $this->getSignsList(array_keys($jzgl_station_list), $this->model_jyzl_station, $this->field_jzgl_station);
|
|
|
+ $jyzl_station_list = $this->getSignsList([], $this->model_jyzl_station, $this->field_jzgl_station);
|
|
|
+ //上述两组数据比较,获取差异的(增加,删除,修改)
|
|
|
+ // id=> 差异(add,del,update) 键值对
|
|
|
+ $diff_info_list = $this->query_diff($jyzl_station_list, $jzgl_station_list);
|
|
|
+ //获取差异的用户详细信息
|
|
|
+ $diff_jyzl_station_list = $this->getDetailDiff($diff_info_list, $this->model_jzgl_station, $this->field_jzgl_station);
|
|
|
+ //同步差异的用户详细信息
|
|
|
+ $this->submitDetailDiff($diff_jyzl_station_list, $this->model_jyzl_station);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public function getVehicleList( $model, $field, $limit ){
|
|
|
+ $list = $model->field($field)->limit($limit)->select();
|
|
|
+ if(!$list){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $jms_city_list = array();
|
|
|
+ foreach ($list as $key => $val) {
|
|
|
+ $jms_city_list[$val['ID']] = $this->md5_id_base($val);
|
|
|
+ }
|
|
|
+ return $jms_city_list;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public function getVehicleSignsList( $ids, $model, $field ){
|
|
|
+ $where = array('ID' => array('IN',$ids));
|
|
|
+ $list = $model->field($field)->where($where)->order('ID ASC')->select();
|
|
|
+ $jyzl_city_list = array();
|
|
|
+ if ($list) {
|
|
|
+ foreach ($list as $key => $val) {
|
|
|
+ $jyzl_city_list[$val['ID']] = $this->md5_id_base($val);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $jyzl_city_list;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|