添加,2=>修改,3=>相册操作, public $type = [ 1=>'楼盘参数', 2=>'楼盘相册', 3=>'楼盘户型', 4=>'周边配套', 5=>'预售证', 6=>'历史价格', ]; public $Operation = [ 1=>'添加', 2=>'修改' ]; public function actionIndex() { return $this->render('index'); } public function actionIndexform() { $response = $this->CurlRequest(static::URL.'sync/houseall',Yii::$app->request->post()); if(!empty($response)) { return Help::JsonData(0,Yii::t('app','get_success'),$response['count'],$response['data']); } } //楼盘信息 public function actionHouseinfo() { $row = $this->CurlRequest('http://oss.pf.com/sync/houseoneinfo',Yii::$app->request->get()); if($row['code'] == 200) { //查询是否同步过,无论修改或者添加 $Contrast = new ParamsHandle(); $unid = $Contrast->Existsunid($row['data']['uniqid']); $name = $Contrast->ExistsName($row['data']['name']); if($unid === false && $name === false) { $msg = [ 'code'=>1, 'msg'=>'未检测到该楼盘信息,可直接添加。' ]; return $this->render('houseinfo',['details'=>$row['data'],'msg'=>$msg]); } else { //查询出自己的楼盘信息 $query = \common\models\House::find(); $query->select(['pfg_house.*','pfg_house_detail.*']); $query->andWhere(['pfg_house.del'=>1]); $query->andWhere(['pfg_house.id'=>$name['id']]); $FindHouse = $query->leftJoin('pfg_house_detail','pfg_house.id=pfg_house_detail.hid')->asArray()->one(); $FindHouse = $this->convertLabel($FindHouse); //转换标签 $dirty = []; //远程楼盘 $sample = [];//本地楼盘 foreach ($FindHouse as $key=>$val) { if($key == 'id' || $key == 'update_at' || $key == 'create_at' || $key == 'hid') continue; if(isset($row['data'][$key])) { if($FindHouse[$key] != $row['data'][$key]) { $dirty[$key] = $row['data'][$key]; $sample[$key] = $FindHouse[$key]; } } } if(!empty($dirty) && !empty($sample)) { $this->HouseLabelsDiff($dirty,$sample); $m = new \common\models\House(); $f = new \common\models\HouseDetail(); $mLabels = $m->attributeLabels(); //获取数据表里字段的描述 $fLabels = $f->attributeLabels(); //获取数据表里字段的描述 $data['mLabels'] = $mLabels; $data['fLabels'] = $fLabels; $data['dirty'] = $dirty; $data['sample'] = $sample; if(isset($dirty['thumb']) && !empty($dirty['thumb'])) { // $dirty['thumb'] = Yii::$app-> } } $msg = [ 'code'=>2, 'msg'=>'该楼盘已存在,可直接修改。' ]; $data['details'] = $row['data']; $data['msg'] = $msg; $data['hid'] = $FindHouse['id']; return $this->render('houseinfoedit',$data); } } } //相册 public function actionHousealbum() { $row = $this->CurlRequest(static::URL.'sync/album',Yii::$app->request->get()); if($row['code'] == 200) { $data = $row['data']['data']; $dataArr = []; foreach ($data as $key=>$val) { $dataArr[$val['name']][] = $val; } return $this->render('housealbum',['hid'=>Yii::$app->request->get('hid'),'model'=>$dataArr,'url'=>$row['data']['url'],'style'=>$row['data']['style']]); } } //相册添加或修改 public function actionHouseallbumform() { $row = $this->CurlRequest(static::URL.'sync/album',Yii::$app->request->post('data')); if($row['code'] == 300) return Help::JsonCode(Help::ERROR,Yii::t('app','edit_error')); $data = $row['data']['data']; $unid = $data[0]['uniqid']; $Contrast = new ParamsHandle(); $unidex = $Contrast->Existsunid($unid); if(!empty($unidex)) { $hid = $unidex['id']; } else { $name = $Contrast->ExistsName($row['data']['name']); if(empty($name)) return Help::JsonCode(Help::ERROR,'未检测到该楼盘相关信息,请先添加楼盘信息。'); $hid = $name['id']; } $albumLabel = \common\models\CategoryAlbum::find()->select(['id','name'])->andWhere(['del'=>1])->asArray()->all(); $Label = array_column($albumLabel,'id','name'); $model = new \common\models\HouseAlbum(); $record = []; $Tran = Yii::$app->db->beginTransaction(); foreach ($data as &$val) { if(isset($Label[$val['name']])) { $_model = clone $model; $exists = $_model::find()->andWhere(['del'=>1])->andWhere(['img'=>$val['img']])->andWhere(['hid'=>$hid])->one(); if($exists == null) { $_model->title = $val['title']; $_model->img = $val['img']; $_model->album_id = $Label[$val['name']]; $_model->hid = $hid; if($_model->save() == false) { $Tran->rollBack(); } $val['aid'] = $Label[$val['name']]; $record[] = $val; } } else { return Help::JsonCode(Help::ERROR,'未检测到-'.$val['name'].'类目信息,请联系管理员添加。'); } } if(!empty($record)) { $rec = new \common\models\Syncrecord(); $inp['uid'] = Yii::$app->session['user_info']['uid']; $inp['hid'] = $hid; $inp['operation_type'] = 3; $inp['content'] = json_encode($record); $inp['unid'] = $unid; $rec->Record($inp); } $Tran->commit(); return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_success')); } //户型 public function actionHousetype() { } //户型修改或添加 public function actionHousetypeform() { } //添加楼盘信息 public function actionHouseadd() { $input = Yii::$app->request->post(); switch ($input['sync']) { case 1: $input = $this->convertLabel($input); $result = $this->HouseCreate($input); break; case 2: $result = $this->HouseEdit($input); break; } if($result === true) { return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_success')); } return Help::JsonCode(Help::ERROR,Yii::t('app','edit_error')); } public function HouseCreate($input) { if(!is_array($input)) return false; $model = new House(); unset($input['id']); $con = new ContrastLabel(); $input['characteristic'] = $con->addArrJson($input['characteristic']); $input['architecture_type'] = $con->addArrJson($input['architecture_type']); $input['property_tag'] = $con->addArrJson($input['property_tag']); $input['type'] = $con->addArrJson($input['type']); $city = $con->HouseCity($input['city']); if(!empty($city)) { $input['city'] = $city['id']; } $Transaction = Yii::$app->db->beginTransaction(); $model->load($input,''); $model->save(false); $details = new HouseDetail(); $details->load($input,''); $details->hid = $model->attributes['id']; $input['hid'] = $model->attributes['id']; $record = new \common\models\Syncrecord(); $recordArr = [ 'uid'=>Yii::$app->session['user_info']['uid'], 'hid'=>$model->attributes['id'], 'operation_type'=>1, 'content' =>json_encode($input), 'unid' => $input['uniqid'] ]; $record->uid = Yii::$app->session['user_info']['uid']; $record->hid = $model->attributes['id']; $record->operation_type = 1; $record->content = $record->create_at = $_SERVER['REQUEST_TIME']; $record->unid = $input['uniqid']; if($details->save(false) === true && is_numeric($model->attributes['id']) && $record->save(false) === true) { $Transaction->commit(); return true; } $Transaction->rollBack(); return false; } //修改逻辑 public function HouseEdit($input) { $model = new House(); $one = $model->FindById($input['hid']); $modelDetaols = new HouseDetail(); $oneDetails = $modelDetaols->FindById($input['hid']); $inputArr = $input['data']; $con = new ContrastLabel(); if(isset($inputArr['architecture_type']) && !empty($inputArr['architecture_type'])) { $inputArr['architecture_type'] = $con->HouseAlllabel(json_encode($inputArr['architecture_type']),2); $tag = array_flip($inputArr['architecture_type']); $jsonArr = json_decode($one->architecture_type,true); $input['data']['architecture_type'] = '["'.implode('","', array_unique(array_values(array_merge($jsonArr,$tag)))).'"]'; } if(isset($inputArr['type']) && !empty($inputArr['type'])) { $inputArr['type'] = $con->HouseAlllabel(json_encode($inputArr['type']),1); $tag = array_flip($inputArr['type']); $input['data']['type'] = '["'.implode('","', array_unique(array_values(array_merge($tag,json_decode($one->type,true))))).'"]'; } if(isset($inputArr['characteristic']) && !empty($inputArr['characteristic'])) { $inputArr['characteristic'] = $con->HouseCharacteristic(json_encode($inputArr['characteristic'])); $tag = array_flip($inputArr['characteristic']); $input['data']['characteristic'] = '["'.implode('","', array_unique(array_values(array_merge($tag,json_decode($one->characteristic,true))))).'"]'; } if(isset($inputArr['property_tag']) && !empty($inputArr['property_tag'])) { $inputArr['property_tag'] = $con->HouseAlllabel(json_encode($inputArr['property_tag']),3); $tag = array_flip($inputArr['property_tag']); if(is_numeric($oneDetails->property_tag)) { $proArr[] = $oneDetails->property_tag; $oneDetails->property_tag = json_encode($proArr); } $input['data']['property_tag'] = '["'.implode('","', array_values(array_unique(array_merge($tag,json_decode($oneDetails->property_tag,true)))) ).'"]'; } $one->load($input['data'],''); $oneDetails->load($input['data'],''); $resuleA = $one->save(false); $resuleB = $oneDetails->save(false); $recordArr = [ 'uid'=>Yii::$app->session['user_info']['uid'], 'hid'=>$input['hid'], 'operation_type'=>2, 'content' =>json_encode($input), 'unid' => $input['unid'] ]; $rec = new \common\models\Syncrecord(); $rec->Record($recordArr); if($resuleA || $resuleB) { return true; } } //对比楼盘标签,如果相同就删除掉 public function HouseLabelsDiff(&$a,&$b) { if(!empty($a['characteristic']) && !empty( $b['characteristic'])) { $sub = \common\models\Characteristic::find()->andWhere(['del'=>1])->andWhere(['name'=>$a['characteristic']])->asArray()->all(); $a['characteristic'] = array_column($sub,'name','id'); $char = array_diff($a['characteristic'],$b['characteristic']); if(empty($char)) { unset($a['characteristic']); unset($b['characteristic']); } } if(!empty($a['architecture_type']) && !empty( $b['architecture_type'])) { $sub = \common\models\CategoryLabel::find()->andWhere(['del'=>1])->andWhere(['type'=>2])->andFilterWhere(['in', 'name', $a['architecture_type']])->asArray()->all(); $a['architecture_type'] = array_column($sub,'name','id'); $char = array_diff($a['architecture_type'],$b['architecture_type']); if(empty($char)) { unset($a['architecture_type']); unset($b['architecture_type']); } } if(!empty($a['type']) && !empty( $b['type'])) { $sub = \common\models\CategoryLabel::find()->andWhere(['del'=>1])->andWhere(['type'=>1])->andFilterWhere(['in', 'name', $a['type']])->asArray()->all(); $a['type'] = array_column($sub,'name','id'); $char = array_diff($a['type'],$b['type']); if(empty($char)) { unset($a['type']); unset($b['type']); } } if(!empty($a['property_tag']) && !empty( $b['property_tag'])) { $sub = \common\models\CategoryLabel::find()->andWhere(['del'=>1])->andWhere(['type'=>3])->andFilterWhere(['in', 'name', $a['property_tag']])->asArray()->all(); $a['property_tag'] = array_column($sub,'name','id'); $char = array_diff($a['property_tag'],$b['property_tag']); if(empty($char)) { unset($a['property_tag']); unset($b['property_tag']); } } } private function convertLabel($FindHouse) { $con = new ContrastLabel(); $FindHouse['characteristic'] = $con->HouseCharacteristic($FindHouse['characteristic']); $FindHouse['architecture_type'] = $con->HouseAlllabel($FindHouse['architecture_type'],2); $FindHouse['property_tag'] = $con->HouseAlllabel($FindHouse['property_tag'],3); $FindHouse['type'] = $con->HouseAlllabel($FindHouse['type'],1); $FindHouse['hot'] = $con->HouseHot($FindHouse['hot']); $FindHouse['is_push'] = $con->HouseisPush($FindHouse['is_push']); $FindHouse['fit'] = $con->HouseFit($FindHouse['fit']); $FindHouse['state'] = $con->HouseState($FindHouse['state']); return $FindHouse; } //请求。 public function CurlRequest($url,$post = null) { $curl = new curl\Curl(); $response = $curl->reset() ->setOption( CURLOPT_POSTFIELDS, http_build_query( $post )) ->post($url); if(!empty($response)) { return json_decode($response,true); } } }