123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- <?php
- /**
- * Created by PhpStorm.
- * User: xiaofeng
- * Date: 2018/4/3
- * Time: 下午5:14
- * 邮箱设置
- */
- namespace backend\controllers;
- use Yii;
- use backend\base\Help;
- use backend\base\CommonController;
- use linslin\yii2\curl;
- use common\models\Syncrecord;
- use common\models\House;
- use common\models\HouseDetail;
- use common\models\HouseAlbum;
- use backend\synchouse\ParamsHandle;
- use backend\synchouse\ContrastLabel;
- class OthersyncController extends CommonController
- {
- const URL = 'http://oss.pf.com/';
- //operation_type 记录操作楼盘里 1=>添加,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);
- }
- }
- }
|