OthersyncController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/4/3
  6. * Time: 下午5:14
  7. * 邮箱设置
  8. */
  9. namespace backend\controllers;
  10. use Yii;
  11. use backend\base\Help;
  12. use backend\base\CommonController;
  13. use linslin\yii2\curl;
  14. use common\models\Syncrecord;
  15. use common\models\House;
  16. use common\models\HouseDetail;
  17. use common\models\HouseAlbum;
  18. use backend\synchouse\ParamsHandle;
  19. use backend\synchouse\ContrastLabel;
  20. class OthersyncController extends CommonController
  21. {
  22. const URL = 'http://oss.pf.com/';
  23. //operation_type 记录操作楼盘里 1=>添加,2=>修改,3=>相册操作,
  24. public $type = [
  25. 1=>'楼盘参数',
  26. 2=>'楼盘相册',
  27. 3=>'楼盘户型',
  28. 4=>'周边配套',
  29. 5=>'预售证',
  30. 6=>'历史价格',
  31. ];
  32. public $Operation = [
  33. 1=>'添加',
  34. 2=>'修改'
  35. ];
  36. public function actionIndex()
  37. {
  38. return $this->render('index');
  39. }
  40. public function actionIndexform()
  41. {
  42. $response = $this->CurlRequest(static::URL.'sync/houseall',Yii::$app->request->post());
  43. if(!empty($response))
  44. {
  45. return Help::JsonData(0,Yii::t('app','get_success'),$response['count'],$response['data']);
  46. }
  47. }
  48. //楼盘信息
  49. public function actionHouseinfo()
  50. {
  51. $row = $this->CurlRequest('http://oss.pf.com/sync/houseoneinfo',Yii::$app->request->get());
  52. if($row['code'] == 200)
  53. {
  54. //查询是否同步过,无论修改或者添加
  55. $Contrast = new ParamsHandle();
  56. $unid = $Contrast->Existsunid($row['data']['uniqid']);
  57. $name = $Contrast->ExistsName($row['data']['name']);
  58. if($unid === false && $name === false)
  59. {
  60. $msg = [
  61. 'code'=>1,
  62. 'msg'=>'未检测到该楼盘信息,可直接添加。'
  63. ];
  64. return $this->render('houseinfo',['details'=>$row['data'],'msg'=>$msg]);
  65. }
  66. else
  67. {
  68. //查询出自己的楼盘信息
  69. $query = \common\models\House::find();
  70. $query->select(['pfg_house.*','pfg_house_detail.*']);
  71. $query->andWhere(['pfg_house.del'=>1]);
  72. $query->andWhere(['pfg_house.id'=>$name['id']]);
  73. $FindHouse = $query->leftJoin('pfg_house_detail','pfg_house.id=pfg_house_detail.hid')->asArray()->one();
  74. $FindHouse = $this->convertLabel($FindHouse); //转换标签
  75. $dirty = []; //远程楼盘
  76. $sample = [];//本地楼盘
  77. foreach ($FindHouse as $key=>$val)
  78. {
  79. if($key == 'id' || $key == 'update_at' || $key == 'create_at' || $key == 'hid') continue;
  80. if(isset($row['data'][$key]))
  81. {
  82. if($FindHouse[$key] != $row['data'][$key])
  83. {
  84. $dirty[$key] = $row['data'][$key];
  85. $sample[$key] = $FindHouse[$key];
  86. }
  87. }
  88. }
  89. if(!empty($dirty) && !empty($sample))
  90. {
  91. $this->HouseLabelsDiff($dirty,$sample);
  92. $m = new \common\models\House();
  93. $f = new \common\models\HouseDetail();
  94. $mLabels = $m->attributeLabels(); //获取数据表里字段的描述
  95. $fLabels = $f->attributeLabels(); //获取数据表里字段的描述
  96. $data['mLabels'] = $mLabels;
  97. $data['fLabels'] = $fLabels;
  98. $data['dirty'] = $dirty;
  99. $data['sample'] = $sample;
  100. if(isset($dirty['thumb']) && !empty($dirty['thumb']))
  101. {
  102. // $dirty['thumb'] = Yii::$app->
  103. }
  104. }
  105. $msg = [
  106. 'code'=>2,
  107. 'msg'=>'该楼盘已存在,可直接修改。'
  108. ];
  109. $data['details'] = $row['data'];
  110. $data['msg'] = $msg;
  111. $data['hid'] = $FindHouse['id'];
  112. return $this->render('houseinfoedit',$data);
  113. }
  114. }
  115. }
  116. //相册
  117. public function actionHousealbum()
  118. {
  119. $row = $this->CurlRequest(static::URL.'sync/album',Yii::$app->request->get());
  120. if($row['code'] == 200)
  121. {
  122. $data = $row['data']['data'];
  123. $dataArr = [];
  124. foreach ($data as $key=>$val)
  125. {
  126. $dataArr[$val['name']][] = $val;
  127. }
  128. return $this->render('housealbum',['hid'=>Yii::$app->request->get('hid'),'model'=>$dataArr,'url'=>$row['data']['url'],'style'=>$row['data']['style']]);
  129. }
  130. }
  131. //相册添加或修改
  132. public function actionHouseallbumform()
  133. {
  134. $row = $this->CurlRequest(static::URL.'sync/album',Yii::$app->request->post('data'));
  135. if($row['code'] == 300) return Help::JsonCode(Help::ERROR,Yii::t('app','edit_error'));
  136. $data = $row['data']['data'];
  137. $unid = $data[0]['uniqid'];
  138. $Contrast = new ParamsHandle();
  139. $unidex = $Contrast->Existsunid($unid);
  140. if(!empty($unidex))
  141. {
  142. $hid = $unidex['id'];
  143. }
  144. else
  145. {
  146. $name = $Contrast->ExistsName($row['data']['name']);
  147. if(empty($name)) return Help::JsonCode(Help::ERROR,'未检测到该楼盘相关信息,请先添加楼盘信息。');
  148. $hid = $name['id'];
  149. }
  150. $albumLabel = \common\models\CategoryAlbum::find()->select(['id','name'])->andWhere(['del'=>1])->asArray()->all();
  151. $Label = array_column($albumLabel,'id','name');
  152. $model = new \common\models\HouseAlbum();
  153. $record = [];
  154. $Tran = Yii::$app->db->beginTransaction();
  155. foreach ($data as &$val)
  156. {
  157. if(isset($Label[$val['name']]))
  158. {
  159. $_model = clone $model;
  160. $exists = $_model::find()->andWhere(['del'=>1])->andWhere(['img'=>$val['img']])->andWhere(['hid'=>$hid])->one();
  161. if($exists == null)
  162. {
  163. $_model->title = $val['title'];
  164. $_model->img = $val['img'];
  165. $_model->album_id = $Label[$val['name']];
  166. $_model->hid = $hid;
  167. if($_model->save() == false)
  168. {
  169. $Tran->rollBack();
  170. }
  171. $val['aid'] = $Label[$val['name']];
  172. $record[] = $val;
  173. }
  174. }
  175. else
  176. {
  177. return Help::JsonCode(Help::ERROR,'未检测到-'.$val['name'].'类目信息,请联系管理员添加。');
  178. }
  179. }
  180. if(!empty($record))
  181. {
  182. $rec = new \common\models\Syncrecord();
  183. $inp['uid'] = Yii::$app->session['user_info']['uid'];
  184. $inp['hid'] = $hid;
  185. $inp['operation_type'] = 3;
  186. $inp['content'] = json_encode($record);
  187. $inp['unid'] = $unid;
  188. $rec->Record($inp);
  189. }
  190. $Tran->commit();
  191. return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_success'));
  192. }
  193. //户型
  194. public function actionHousetype()
  195. {
  196. }
  197. //户型修改或添加
  198. public function actionHousetypeform()
  199. {
  200. }
  201. //添加楼盘信息
  202. public function actionHouseadd()
  203. {
  204. $input = Yii::$app->request->post();
  205. switch ($input['sync'])
  206. {
  207. case 1:
  208. $input = $this->convertLabel($input);
  209. $result = $this->HouseCreate($input);
  210. break;
  211. case 2:
  212. $result = $this->HouseEdit($input);
  213. break;
  214. }
  215. if($result === true)
  216. {
  217. return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_success'));
  218. }
  219. return Help::JsonCode(Help::ERROR,Yii::t('app','edit_error'));
  220. }
  221. public function HouseCreate($input)
  222. {
  223. if(!is_array($input)) return false;
  224. $model = new House();
  225. unset($input['id']);
  226. $con = new ContrastLabel();
  227. $input['characteristic'] = $con->addArrJson($input['characteristic']);
  228. $input['architecture_type'] = $con->addArrJson($input['architecture_type']);
  229. $input['property_tag'] = $con->addArrJson($input['property_tag']);
  230. $input['type'] = $con->addArrJson($input['type']);
  231. $city = $con->HouseCity($input['city']);
  232. if(!empty($city))
  233. {
  234. $input['city'] = $city['id'];
  235. }
  236. $Transaction = Yii::$app->db->beginTransaction();
  237. $model->load($input,'');
  238. $model->save(false);
  239. $details = new HouseDetail();
  240. $details->load($input,'');
  241. $details->hid = $model->attributes['id'];
  242. $input['hid'] = $model->attributes['id'];
  243. $record = new \common\models\Syncrecord();
  244. $recordArr = [
  245. 'uid'=>Yii::$app->session['user_info']['uid'],
  246. 'hid'=>$model->attributes['id'],
  247. 'operation_type'=>1,
  248. 'content' =>json_encode($input),
  249. 'unid' => $input['uniqid']
  250. ];
  251. $record->uid = Yii::$app->session['user_info']['uid'];
  252. $record->hid = $model->attributes['id'];
  253. $record->operation_type = 1;
  254. $record->content =
  255. $record->create_at = $_SERVER['REQUEST_TIME'];
  256. $record->unid = $input['uniqid'];
  257. if($details->save(false) === true && is_numeric($model->attributes['id']) && $record->save(false) === true)
  258. {
  259. $Transaction->commit();
  260. return true;
  261. }
  262. $Transaction->rollBack();
  263. return false;
  264. }
  265. //修改逻辑
  266. public function HouseEdit($input)
  267. {
  268. $model = new House();
  269. $one = $model->FindById($input['hid']);
  270. $modelDetaols = new HouseDetail();
  271. $oneDetails = $modelDetaols->FindById($input['hid']);
  272. $inputArr = $input['data'];
  273. $con = new ContrastLabel();
  274. if(isset($inputArr['architecture_type']) && !empty($inputArr['architecture_type']))
  275. {
  276. $inputArr['architecture_type'] = $con->HouseAlllabel(json_encode($inputArr['architecture_type']),2);
  277. $tag = array_flip($inputArr['architecture_type']);
  278. $jsonArr = json_decode($one->architecture_type,true);
  279. $input['data']['architecture_type'] = '["'.implode('","', array_unique(array_values(array_merge($jsonArr,$tag)))).'"]';
  280. }
  281. if(isset($inputArr['type']) && !empty($inputArr['type']))
  282. {
  283. $inputArr['type'] = $con->HouseAlllabel(json_encode($inputArr['type']),1);
  284. $tag = array_flip($inputArr['type']);
  285. $input['data']['type'] = '["'.implode('","', array_unique(array_values(array_merge($tag,json_decode($one->type,true))))).'"]';
  286. }
  287. if(isset($inputArr['characteristic']) && !empty($inputArr['characteristic']))
  288. {
  289. $inputArr['characteristic'] = $con->HouseCharacteristic(json_encode($inputArr['characteristic']));
  290. $tag = array_flip($inputArr['characteristic']);
  291. $input['data']['characteristic'] = '["'.implode('","', array_unique(array_values(array_merge($tag,json_decode($one->characteristic,true))))).'"]';
  292. }
  293. if(isset($inputArr['property_tag']) && !empty($inputArr['property_tag']))
  294. {
  295. $inputArr['property_tag'] = $con->HouseAlllabel(json_encode($inputArr['property_tag']),3);
  296. $tag = array_flip($inputArr['property_tag']);
  297. if(is_numeric($oneDetails->property_tag))
  298. {
  299. $proArr[] = $oneDetails->property_tag;
  300. $oneDetails->property_tag = json_encode($proArr);
  301. }
  302. $input['data']['property_tag'] = '["'.implode('","', array_values(array_unique(array_merge($tag,json_decode($oneDetails->property_tag,true)))) ).'"]';
  303. }
  304. $one->load($input['data'],'');
  305. $oneDetails->load($input['data'],'');
  306. $resuleA = $one->save(false);
  307. $resuleB = $oneDetails->save(false);
  308. $recordArr = [
  309. 'uid'=>Yii::$app->session['user_info']['uid'],
  310. 'hid'=>$input['hid'],
  311. 'operation_type'=>2,
  312. 'content' =>json_encode($input),
  313. 'unid' => $input['unid']
  314. ];
  315. $rec = new \common\models\Syncrecord();
  316. $rec->Record($recordArr);
  317. if($resuleA || $resuleB)
  318. {
  319. return true;
  320. }
  321. }
  322. //对比楼盘标签,如果相同就删除掉
  323. public function HouseLabelsDiff(&$a,&$b)
  324. {
  325. if(!empty($a['characteristic']) && !empty( $b['characteristic']))
  326. {
  327. $sub = \common\models\Characteristic::find()->andWhere(['del'=>1])->andWhere(['name'=>$a['characteristic']])->asArray()->all();
  328. $a['characteristic'] = array_column($sub,'name','id');
  329. $char = array_diff($a['characteristic'],$b['characteristic']);
  330. if(empty($char))
  331. {
  332. unset($a['characteristic']);
  333. unset($b['characteristic']);
  334. }
  335. }
  336. if(!empty($a['architecture_type']) && !empty( $b['architecture_type']))
  337. {
  338. $sub = \common\models\CategoryLabel::find()->andWhere(['del'=>1])->andWhere(['type'=>2])->andFilterWhere(['in', 'name', $a['architecture_type']])->asArray()->all();
  339. $a['architecture_type'] = array_column($sub,'name','id');
  340. $char = array_diff($a['architecture_type'],$b['architecture_type']);
  341. if(empty($char))
  342. {
  343. unset($a['architecture_type']);
  344. unset($b['architecture_type']);
  345. }
  346. }
  347. if(!empty($a['type']) && !empty( $b['type']))
  348. {
  349. $sub = \common\models\CategoryLabel::find()->andWhere(['del'=>1])->andWhere(['type'=>1])->andFilterWhere(['in', 'name', $a['type']])->asArray()->all();
  350. $a['type'] = array_column($sub,'name','id');
  351. $char = array_diff($a['type'],$b['type']);
  352. if(empty($char))
  353. {
  354. unset($a['type']);
  355. unset($b['type']);
  356. }
  357. }
  358. if(!empty($a['property_tag']) && !empty( $b['property_tag']))
  359. {
  360. $sub = \common\models\CategoryLabel::find()->andWhere(['del'=>1])->andWhere(['type'=>3])->andFilterWhere(['in', 'name', $a['property_tag']])->asArray()->all();
  361. $a['property_tag'] = array_column($sub,'name','id');
  362. $char = array_diff($a['property_tag'],$b['property_tag']);
  363. if(empty($char))
  364. {
  365. unset($a['property_tag']);
  366. unset($b['property_tag']);
  367. }
  368. }
  369. }
  370. private function convertLabel($FindHouse)
  371. {
  372. $con = new ContrastLabel();
  373. $FindHouse['characteristic'] = $con->HouseCharacteristic($FindHouse['characteristic']);
  374. $FindHouse['architecture_type'] = $con->HouseAlllabel($FindHouse['architecture_type'],2);
  375. $FindHouse['property_tag'] = $con->HouseAlllabel($FindHouse['property_tag'],3);
  376. $FindHouse['type'] = $con->HouseAlllabel($FindHouse['type'],1);
  377. $FindHouse['hot'] = $con->HouseHot($FindHouse['hot']);
  378. $FindHouse['is_push'] = $con->HouseisPush($FindHouse['is_push']);
  379. $FindHouse['fit'] = $con->HouseFit($FindHouse['fit']);
  380. $FindHouse['state'] = $con->HouseState($FindHouse['state']);
  381. return $FindHouse;
  382. }
  383. //请求。
  384. public function CurlRequest($url,$post = null)
  385. {
  386. $curl = new curl\Curl();
  387. $response = $curl->reset()
  388. ->setOption(
  389. CURLOPT_POSTFIELDS,
  390. http_build_query(
  391. $post
  392. ))
  393. ->post($url);
  394. if(!empty($response))
  395. {
  396. return json_decode($response,true);
  397. }
  398. }
  399. }