SyncVr.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/3/12
  6. * Time: 上午9:57
  7. * 同步资讯
  8. */
  9. namespace backend\server;
  10. use Yii;
  11. use yii\db\Exception;
  12. use linslin\yii2\curl;
  13. use common\models\Vr;
  14. use common\models\Syncvrrecord;
  15. class SyncVr
  16. {
  17. public function userOpe($input)
  18. {
  19. try {
  20. $Transaction = Yii::$app->db->beginTransaction();
  21. if ($this->isNoll($input['data']) || $this->isNoll($input['append'])) {
  22. throw new Exception('数据不完整,无法进行同步,请检查数据完整性!');
  23. }
  24. $dataCode = $this->operation($input['data'], $input['append']);
  25. if ($dataCode === true) {
  26. $Transaction->commit();
  27. } else {
  28. $Transaction->rollBack();
  29. }
  30. return $dataCode;
  31. } catch (Exception $e) {
  32. $Transaction->rollBack();
  33. return $e->getMessage();
  34. }
  35. }
  36. /**
  37. * 同步VR信息数据
  38. * */
  39. private function operation($data, $append)
  40. {
  41. $data = json_decode($data, true);
  42. $append = json_decode($append, true);
  43. if ($this->isNoll($data['img']) || $this->isNoll($append['img_url'])) {
  44. throw new Exception('封面图数据不完整,无法进行同步,请检查封面图数据完整性!');
  45. }
  46. // return [$data,$append];
  47. $vrInfo = [];
  48. $vrInfo['img'] = $this->PullImg($data['img'], $append['img_url']);
  49. // $vrInfo['img'] = $data['img'];
  50. $vrInfo['name'] = $data['name'];
  51. $vrInfo['abstarct'] = $data['abstarct'];
  52. $vrInfo['covered_area'] = $data['covered_area'];
  53. $vrInfo['house_type'] = $this->getHouseTypeId($data);
  54. $vrInfo['hid'] = $this->getHouseInfo($append);
  55. $vrInfo['path'] = $data['path'];
  56. $vrInfo['type'] = $data['type'];
  57. $vrInfo['state'] = $data['state'];
  58. $vrInfo['sort'] = $data['sort'];
  59. $vrInfo['uuid'] = $data['uuid'];
  60. $this_content = [];
  61. $this_content['oss_content'] = json_encode($vrInfo, JSON_UNESCAPED_UNICODE);
  62. $this_content['auditor_name'] = $append['user_name'];
  63. $sync = $this->isSyncEd($data['uuid']);
  64. $syncModel = new Syncvrrecord();
  65. if ($sync === false) {
  66. $vrModel = new Vr();
  67. if ($vrModel->load($vrInfo, '') && $vrModel->validate() && $vrModel->save()) {
  68. $this_content['this_vrid'] = $vrModel->attributes['id'];
  69. $this_content['code'] = 'add';
  70. } else {
  71. throw new Exception('同步添加失败!请联系管理员');
  72. }
  73. } else {
  74. $vrRow = Vr::findOne($sync['id']);
  75. if ($vrRow->load($vrInfo, '') && $vrRow->update(false)) {
  76. $this_content['this_vrid'] = $sync['id'];
  77. $this_content['code'] = 'edit';
  78. $this_content['this_content'] = json_encode($sync, JSON_UNESCAPED_UNICODE);
  79. } else {
  80. throw new Exception('同步更新失败!请联系管理员');
  81. }
  82. }
  83. $syncModel->load($this_content, '');
  84. if (!$syncModel->save()) {
  85. throw new Exception('同步纪录失败!请联系管理员');
  86. }
  87. return true;
  88. }
  89. /**
  90. * 获取楼盘信息
  91. * */
  92. private function getHouseInfo($data)
  93. {
  94. $house_name = $data['house_name'];
  95. if ($data['type'] == 1) {
  96. $houseInfo = \common\models\House::find()->where(['name' => $house_name, 'del' => 1])->one();
  97. if (!empty($houseInfo)) {
  98. return $houseInfo['id'];
  99. }
  100. } else {
  101. }
  102. throw new Exception('VR对应楼盘不存在,请先同步楼盘信息!');
  103. }
  104. /**
  105. * 获取户型信息
  106. * */
  107. public function getHouseTypeId($data)
  108. {
  109. if ($data['type'] == 1) {
  110. $houseType = \common\models\CategoryHousetype::find()->where(['huxing_name' => $data['huxing_name'], 'del' => 1])->one();
  111. if (!empty($houseType)) {
  112. return $houseType['id'];
  113. }
  114. } else {
  115. }
  116. throw new Exception('VR对应楼盘户型不存在,请先同步楼盘户型信息!');
  117. }
  118. /**
  119. * 拉取图片
  120. */
  121. private function PullImg($img, $oos)
  122. {
  123. if (is_string($img)) {
  124. $curl = new curl\Curl();
  125. $resultImg = $curl->get($oos . $img);
  126. $imgUrl = Yii::$app->params['img_url']['video'];
  127. if (!is_file($imgUrl . $img)) {
  128. if (!file_put_contents($imgUrl . $img, $resultImg)) {
  129. throw new Exception('图片添加失败,请联系管理员');
  130. }
  131. }
  132. return $img;
  133. } else {
  134. throw new Exception('封面图数据不完整,无法进行同步,请检查封面图数据完整性!');
  135. }
  136. }
  137. /**
  138. * 检测VR是否已同步过
  139. * */
  140. private function isSyncEd($uuid)
  141. {
  142. $row = (new Vr())->getFindByUuid($uuid);
  143. if (!empty($row)) {
  144. return $row;
  145. } else {
  146. return false;
  147. }
  148. }
  149. /**
  150. * 检测数据存在并且不为空
  151. * */
  152. private function isNoll($data)
  153. {
  154. if (!isset($data) || empty($data)) {
  155. return true;
  156. }
  157. return false;
  158. }
  159. }