SyncLive.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/3/11
  6. * Time: 下午3:08
  7. */
  8. namespace backend\server;
  9. use yii\base\Component;
  10. use common\models\Syncliverecord;
  11. use common\models\LivesyncRecord;
  12. use common\models\Live;
  13. use common\models\Synchouserecord;
  14. use Yii;
  15. use yii\db\Exception;
  16. class SyncLive extends Component
  17. {
  18. public function LiveOpe($input)
  19. {
  20. $operator = $input['operator'];
  21. $title = $input['title'];
  22. $livecontent = json_decode($input['livecontent'], true);
  23. $HouseInfo = $this->LerifyHouse($livecontent);
  24. if (!$HouseInfo) {
  25. return '同步目标站点不存在该楼盘';
  26. }
  27. $liveexs = Live::find()->select('id as lid')->where(['hid' => $HouseInfo['hid']])->asArray()->one();
  28. //楼盘直播视频是否存在
  29. if (empty($liveexs)) {
  30. //该直播视频是否同步过
  31. if(!empty($input['un_id'])){
  32. $liveexs = Syncliverecord::find()->select('own_lid as lid')->where(['unid' => $input['un_id']])->orderBy(['create_at' => SORT_DESC])->asArray()->one();
  33. }
  34. if (empty($liveexs)) {
  35. $liveexs = Live::find()->select('id as lid')->where(['title' => $title])->asArray()->one();
  36. // 未同步过但是相同视频是否存在
  37. if (empty($liveexs)) { //未同步过,并且直播视频不存在
  38. return $this->LiveToneOne($operator, $livecontent, $HouseInfo);
  39. }
  40. }
  41. }
  42. // 同步过或者直播视频存在或者楼盘直播视频
  43. return $this->LiveToneTwo($operator, $livecontent, $HouseInfo, $liveexs['lid']);//修改
  44. }
  45. /**
  46. *同步新增
  47. * */
  48. public function LiveToneOne($operator, $livecontent, $HouseInfo)
  49. {
  50. $Transaction = Yii::$app->db->beginTransaction();
  51. $liveInfo = [];
  52. try {
  53. $liveInfo['hid'] = $HouseInfo['hid'];
  54. $liveInfo['city'] = $HouseInfo['city'];
  55. $liveInfo['title'] = $livecontent['title'];
  56. $liveInfo['video_url'] = $livecontent['video_url'];
  57. $liveInfo['rtmp_url'] = $livecontent['rtmp_url'];
  58. $liveInfo['img'] = $livecontent['img'];
  59. $liveInfo['time'] = $livecontent['time'];
  60. $LiveModel = new Live();
  61. $LiveModel->load($liveInfo, '');
  62. $t = $LiveModel->save();
  63. if ($t != true) return '直播添加失败';
  64. $lid = $LiveModel->attributes['id'];
  65. $this->SubjoinInfo($operator, $livecontent, $lid, 'add');
  66. $Transaction->commit();
  67. return true;
  68. } catch (Exception $e) {
  69. $Transaction->rollBack();
  70. return '同步失败1';
  71. }
  72. }
  73. /**
  74. *同步修改
  75. * */
  76. public function LiveToneTwo($operator, $livecontent, $HouseInfo, $lid)
  77. {
  78. $Transaction = Yii::$app->db->beginTransaction();
  79. $liveInfo = [];
  80. $thisLiveInfo = null;
  81. try {
  82. $LiveModel = Live::findOne($lid);
  83. if (!empty($LiveModel)) {
  84. //更新之前先获取原数据
  85. $thisLiveInfo = $LiveModel->attributes;
  86. $LiveModel->hid = $HouseInfo['hid'];
  87. $LiveModel->city = $HouseInfo['city'];
  88. $LiveModel->title = $livecontent['title'];
  89. $LiveModel->video_url = $livecontent['video_url'];
  90. $LiveModel->rtmp_url = $livecontent['rtmp_url'];
  91. $LiveModel->img = $livecontent['img'];
  92. $LiveModel->time = $livecontent['time'];
  93. $t = $LiveModel->save();
  94. if ($t != true) return '直播更新失败';
  95. $mode = 'edit';
  96. } else {
  97. $liveInfo['hid'] = $HouseInfo['hid'];
  98. $liveInfo['city'] = $HouseInfo['city'];
  99. $liveInfo['title'] = $livecontent['title'];
  100. $liveInfo['video_url'] = $livecontent['video_url'];
  101. $liveInfo['rtmp_url'] = $livecontent['rtmp_url'];
  102. $liveInfo['img'] = $livecontent['img'];
  103. $liveInfo['time'] = $livecontent['time'];
  104. $LiveModel = new Live();
  105. $LiveModel->load($liveInfo, '');
  106. $t = $LiveModel->save();
  107. if ($t != true) return '直播更新失败';
  108. $lid = $LiveModel->attributes['id'];
  109. $mode = 'add';
  110. }
  111. $this->SubjoinInfo($operator, $livecontent, $lid, $mode, $thisLiveInfo);
  112. $Transaction->commit();
  113. return true;
  114. } catch (Exception $e) {
  115. $Transaction->rollBack();
  116. return '同步失败1';
  117. }
  118. }
  119. /**
  120. * 附加表信息
  121. * */
  122. public function SubjoinInfo($operator, $livecontent, $lid, $mode, $thisLiveInfo = null)
  123. {
  124. $syncliverecordInfo = [];
  125. $livesyncRecordInfo = [];
  126. $syncliverecordInfo['own_lid'] = $lid;//本地直播视频id
  127. $syncliverecordInfo['unid'] = $livecontent['uniqid'];
  128. $syncliverecordInfo['state'] = 1;
  129. $syncliverecordInfo['content'] = json_encode($livecontent);
  130. $syncliverecordInfo['name'] = $operator;
  131. $syncliverecordInfo['create_at'] = time();
  132. $syncliverecordInfo['ip'] = $ip = Yii::$app->request->userIP;
  133. if (!empty($thisLiveInfo)) {
  134. $syncliverecordInfo['edit_content'] = json_encode($thisLiveInfo['liveinfo']);
  135. }
  136. $SyncliverecordModel = new Syncliverecord();
  137. $SyncliverecordModel->load($syncliverecordInfo, '');
  138. $t = $SyncliverecordModel->save();
  139. if ($t != true) return 'Syncliverecord纪录添加失败';
  140. $livesyncRecordInfo['mode'] = $mode;
  141. $livesyncRecordInfo['operator'] = $operator;
  142. $livesyncRecordInfo['oss_live_name'] = $livecontent['title'];
  143. $livesyncRecordInfo['oss_unid'] = $livecontent['uniqid'];
  144. $livesyncRecordInfo['create_at'] = time();
  145. $livesyncRecordInfo['oss_live_data'] = json_encode($livecontent);
  146. if (!empty($thisLiveInfo)) {
  147. $livesyncRecordInfo['this_live_name'] = json_encode($thisLiveInfo['housename']);
  148. $livesyncRecordInfo['this_live_data'] = json_encode($thisLiveInfo['liveinfo']);
  149. }
  150. $LivesyncRecordModel = new LivesyncRecord();
  151. $LivesyncRecordModel->load($livesyncRecordInfo, '');
  152. $t = $LivesyncRecordModel->save();
  153. if ($t != true) return 'LivesyncRecord纪录添加失败';
  154. }
  155. public function getThisLiveInfo($lid)
  156. {
  157. $liveInfo = Live::find()->where(['id' => $lid])->asArray()->one();
  158. $housename = (\common\models\house::findOne($liveInfo['hid']))['name'];
  159. return ['liveinfo' => $liveInfo, 'housename' => $housename];
  160. }
  161. /**
  162. * 检测同步的直播楼盘是否存在
  163. * */
  164. public function LerifyHouse($livecontent)
  165. {
  166. $h_name = $livecontent['h_name'];
  167. $h_unid = $livecontent['h_unid'];
  168. $city_name = $livecontent['city_name'];
  169. $city_level = $livecontent['city_level'];
  170. $hosueInfo = (new Synchouserecord())->getLerifyHouse($h_unid);
  171. if (empty($hosueInfo)) {
  172. $hosueInfo = \common\models\House::find()->where(['name' => $h_name, 'del' => 1])->select('id as hid,city')->asArray()->one();
  173. if (empty($hosueInfo)) {
  174. return false;
  175. }
  176. }
  177. $cityInfo = \common\models\CategoryCity::find()->where(['city_name' => $city_name, 'del' => 1, 'level' => $city_level])->select('id')->one();
  178. if (empty($cityInfo)) {
  179. $city = $hosueInfo['city'];
  180. } else {
  181. $city = $cityInfo['id'];
  182. }
  183. return ['hid' => $hosueInfo['hid'], 'city' => $city];
  184. }
  185. }