VideoController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 阿源
  5. * Date: 2020/8/31
  6. * Time: 11:21
  7. */
  8. namespace addons\ShortVideo\backend\controllers;
  9. use addons\ShortVideo\common\enums\AddonsShortVideoEnum;
  10. use addons\ShortVideo\common\models\ShortVideo;
  11. use addons\ShortVideo\common\models\ShortVideoBrowse;
  12. use addons\ShortVideo\common\models\ShortVideoBrowseAward;
  13. use addons\ShortVideo\common\models\ShortVideoCommissionItemSetting;
  14. use addons\ShortVideo\common\models\ShortVideoConfig;
  15. use addons\ShortVideo\common\models\ShortVideoGoods;
  16. use addons\ShortVideo\common\models\ShortVideoOrderAward;
  17. use addons\ShortVideo\common\models\ShortVideoTag;
  18. use common\enums\OrderStatusEnum;
  19. use common\enums\StatusEnum;
  20. use common\models\goods\Goods;
  21. use common\models\order\Order;
  22. use common\models\user\User;
  23. class VideoController extends BaseController
  24. {
  25. /**
  26. * @name:
  27. * @msg: 视频置顶/取消置顶操作
  28. * @param {*}
  29. * @return {*}
  30. */
  31. public function actionTop()
  32. {
  33. if (!\Yii::$app->request->isGet) {
  34. return $this->error('请求方式错误');
  35. }
  36. $id = \Yii::$app->request->get('id');
  37. $is_top = \Yii::$app->request->get('is_top');
  38. if (empty($id) || (empty($is_top) && !is_numeric($is_top))) {
  39. return $this->error('参数错误');
  40. }
  41. $params = ['id' => $id, 'is_top' => $is_top];
  42. if (1 == $is_top) {
  43. $params['top_time'] = time();
  44. } else {
  45. $params['top_time'] = 0;
  46. }
  47. try {
  48. $res = ShortVideo::setData($this->mall_id, $params);
  49. if (false === $res) {
  50. return $this->error('操作失败');
  51. } else {
  52. return $this->success('操作成功');
  53. }
  54. } catch (\Exception $e) {
  55. return $this->error($e->getMessage());
  56. }
  57. }
  58. /**
  59. * @name:
  60. * @msg: 视频拉黑
  61. * @param {integer} $id 视频id
  62. * @return {*}
  63. */
  64. public function actionJoinBlacklist()
  65. {
  66. if (!\Yii::$app->request->isGet) {
  67. return $this->error('请求方式错误');
  68. }
  69. $id = \Yii::$app->request->get('id');
  70. if (empty($id)) return $this->error('参数错误');
  71. $params = ['id' => $id, 'is_blacklist' => 1, 'blacklist_time' => time()];
  72. try {
  73. $res = ShortVideo::setData($this->mall_id, $params);
  74. if (false === $res) {
  75. return $this->error('操作失败');
  76. } else {
  77. return $this->success('操作成功');
  78. }
  79. } catch (\Exception $e) {
  80. return $this->error($e->getMessage());
  81. }
  82. }
  83. /**
  84. * @name:
  85. * @msg: 视频相关统计数据
  86. * @param {integer} $id 视频id
  87. * @return {*}
  88. */
  89. public function actionVideoData()
  90. {
  91. if (!\Yii::$app->request->isGet) {
  92. return $this->error('请求方式错误');
  93. }
  94. $id = \Yii::$app->request->get('id');
  95. $start_time = \Yii::$app->request->get('start_time');
  96. $end_time = \Yii::$app->request->get('end_time');
  97. if (!empty($start_time)) $start_time = strtotime($start_time . ' 00:00:00');
  98. if (!empty($end_time)) $end_time = strtotime($end_time . ' 23:59:59');
  99. if (empty($id)) return $this->error('参数错误');
  100. $video_info = ShortVideo::find()
  101. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $id])
  102. ->asArray()
  103. ->one();
  104. if (empty($video_info)) return $this->error('视频不存在');
  105. $params[] = ['video_id' => $id];
  106. if (!empty($start_time)) $params[] = ['>=', 'created_at', $start_time];
  107. if (!empty($end_time)) $params[] = ['<=', 'created_at', $end_time];
  108. //累计订单金额
  109. $video_info['order_sum_money'] = ShortVideoOrderAward::getStatData($this->mall_id, 'order_sum_money', $params);
  110. //订单数量
  111. $video_info['order_num'] = ShortVideoOrderAward::getStatData($this->mall_id, 'order_num', $params);
  112. //订单金额
  113. // $video_info['order_money'] = $video_info['order_sum_money'];
  114. // 下单人数
  115. $video_info['order_people'] = ShortVideoOrderAward::getStatData($this->mall_id, 'order_people', $params);
  116. $query = ShortVideoBrowseAward::find()->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'video_id' => $id]);
  117. if (!empty($start_time)) $query->andWhere(['>=', 'created_at', $start_time]);
  118. if (!empty($end_time)) $query->andWhere(['<=', 'created_at', $end_time]);
  119. $awards = $query->all();
  120. $look_integral_own = $look_integral_one = $look_integral_two = $look_money_own = $look_money_one = $look_money_two = 0;
  121. if (!empty($awards)) {
  122. foreach ($awards as $award) {
  123. if ($award->award_type == AddonsShortVideoEnum::VIDEO_REWARD_TYPE0) {
  124. // 奖励积分
  125. if ($award->user_type == AddonsShortVideoEnum::VIDEO_REWARD_USER_TYPE0) $look_integral_own += $award->award_integral;
  126. if ($award->user_type == AddonsShortVideoEnum::VIDEO_REWARD_USER_TYPE1) $look_integral_one += $award->award_integral;
  127. // if ($award->user_type == AddonsShortVideoEnum::VIDEO_REWARD_USER_TYPE2) $look_integral_two += $award->award_integral;
  128. } elseif ($award->award_type == AddonsShortVideoEnum::VIDEO_REWARD_TYPE1) {
  129. // 奖励现金
  130. if ($award->user_type == AddonsShortVideoEnum::VIDEO_REWARD_USER_TYPE0) $look_money_own += $award->award_money;
  131. if ($award->user_type == AddonsShortVideoEnum::VIDEO_REWARD_USER_TYPE1) $look_money_one += $award->award_money;
  132. // if ($award->user_type == AddonsShortVideoEnum::VIDEO_REWARD_USER_TYPE2) $look_money_two += $award->award_money;
  133. }
  134. }
  135. }
  136. //观看积分
  137. $video_info['look_integral_own'] = floatval(round($look_integral_own, 2));
  138. $video_info['look_integral_one'] = floatval(round($look_integral_one, 2));
  139. $video_info['look_integral_two'] = floatval(round($look_integral_two, 2));
  140. //观看金额
  141. $video_info['look_money_own'] = floatval(round($look_money_own, 2));
  142. $video_info['look_money_one'] = floatval(round($look_money_one, 2));
  143. $video_info['look_money_two'] = floatval(round($look_money_two, 2));
  144. //分红奖励
  145. $query_vo = ShortVideoOrderAward::find()->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'video_id' => $id]);
  146. if (!empty($start_time)) $query_vo->andWhere(['>=', 'created_at', $start_time]);
  147. if (!empty($end_time)) $query_vo->andWhere(['<=', 'created_at', $end_time]);
  148. $video_info['dividend_money'] = $query_vo->sum('money');
  149. return $this->success('success', $video_info);
  150. }
  151. /**
  152. * @name:
  153. * @msg: 视频转化相关数据
  154. * @param {integer} $id 视频id
  155. * @return {*}
  156. */
  157. public function actionVideoTransaction()
  158. {
  159. if (!\Yii::$app->request->isGet) {
  160. return $this->error('请求方式错误');
  161. }
  162. $id = \Yii::$app->request->get('id');
  163. if (empty($id)) return $this->error('参数错误');
  164. $start_time = \Yii::$app->request->get('start_time');
  165. $end_time = \Yii::$app->request->get('end_time');
  166. if (!empty($start_time)) $start_time = strtotime($start_time);
  167. if (!empty($end_time)) $end_time = strtotime($end_time);
  168. $video_info = ShortVideo::find()
  169. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $id])
  170. ->asArray()
  171. ->one();
  172. if (empty($video_info)) return $this->error('视频不存在');
  173. // dd($video_info);
  174. //完播比例
  175. $video_info['look_proportion'] = $video_info['look_complete_num'] == 0 ? 0 : round($video_info['look_complete_num'] / $video_info['look_people_num'] * 100, 2);
  176. $params[] = ['video_id' => $id];
  177. if (!empty($start_time)) $params[] = ['>=', 'created_at', $start_time];
  178. if (!empty($end_time)) $params[] = ['<=', 'created_at', $end_time];
  179. //订单金额
  180. $video_info['order_money'] = ShortVideoOrderAward::getStatData($this->mall_id, 'order_sum_money', $params);
  181. // 下单人数
  182. $video_info['order_people'] = ShortVideoOrderAward::getStatData($this->mall_id, 'order_people', $params);
  183. //下单百分比
  184. if (0 == $video_info['order_people'] || 0 == $video_info['look_people_num']) {
  185. $video_info['order_proportion'] = 0;
  186. } else {
  187. $video_info['order_proportion'] = round($video_info['order_people'] / $video_info['look_people_num'] * 100, 2);
  188. }
  189. // $params[] = ['is_settlement' => 1];
  190. //支付人数
  191. $video_info['pay_people'] = ShortVideoOrderAward::getStatData($this->mall_id, 'pay_people', $params);
  192. //支付金额
  193. $video_info['pay_money'] = ShortVideoOrderAward::getStatData($this->mall_id, 'pay_money', $params);
  194. //客单价
  195. if (0 == $video_info['pay_money'] || 0 == $video_info['order_people']) {
  196. $video_info['pay_average_money'] = 0;
  197. } else {
  198. $video_info['pay_average_money'] = round($video_info['pay_money'] / $video_info['pay_people'], 2);
  199. }
  200. //支付转化率
  201. if (0 == $video_info['pay_people'] || 0 == $video_info['look_people_num']) {
  202. $video_info['pay_proportion'] = 0;
  203. } else {
  204. $video_info['pay_proportion'] = round($video_info['pay_people'] / $video_info['look_people_num'] * 100, 2);
  205. }
  206. return $this->success('success', $video_info);
  207. }
  208. /**
  209. * @name:
  210. * @msg: 视频详情页订单列表
  211. * @param {*}
  212. * @return {*}
  213. */
  214. public function actionOrderList()
  215. {
  216. if (!\Yii::$app->request->isGet) {
  217. return $this->error('请求方式错误');
  218. }
  219. $id = \Yii::$app->request->get('video_id');
  220. if (empty($id)) return $this->error('参数错误');
  221. $start_time = \Yii::$app->request->get('start_time');
  222. $end_time = \Yii::$app->request->get('end_time');
  223. $nickname = \Yii::$app->request->get('nickname');
  224. $page = \Yii::$app->request->get('page', 1);
  225. $limit = \Yii::$app->request->get('limit', $this->pageSize);
  226. $wheres[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'video_id' => $id];
  227. if (!empty($start_time)) $wheres[] = ['>=', 'created_at', strtotime($start_time . ' 00:00:00')];
  228. if (!empty($end_time)) $wheres[] = ['<=', 'created_at', strtotime($end_time . ' 23:59:59')];
  229. if (!empty($nickname)) {
  230. $users = User::find()
  231. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  232. ->andWhere(['like', 'nickname', $nickname])
  233. ->asArray()
  234. ->indexBy('id')
  235. ->all();
  236. $order_ids = Order::find()
  237. ->select('id')
  238. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'user_id' => array_column($users, 'id')])
  239. ->asArray()
  240. ->column();
  241. $wheres[] = ['order_id' => $order_ids];
  242. }
  243. $params = [
  244. 'select' => 'id,user_id,created_at,video_id,goods_id,award_type,money,award_percentage,award_money,is_settlement,order_id',
  245. 'where' => $wheres,
  246. 'order' => 'created_at desc,id desc',
  247. 'page' => $page,
  248. 'limit' => $limit,
  249. ];
  250. $order_awards = ShortVideoOrderAward::listPage($params);
  251. if (empty($order_awards['list'])) {
  252. if (is_array($order_awards['list'])) {
  253. return $this->success('success', $order_awards);
  254. } else {
  255. return $this->error(ShortVideoOrderAward::getStaticError());
  256. }
  257. }
  258. $order_ids = array_column($order_awards['list'], 'order_id');
  259. $orders = Order::find()
  260. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $order_ids])
  261. ->asArray()
  262. ->indexBy('id')
  263. ->all();
  264. if (empty($users)) {
  265. $user_ids = array_column($orders, 'user_id');
  266. $users = User::find()
  267. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $user_ids])
  268. ->asArray()
  269. ->indexBy('id')
  270. ->all();
  271. }
  272. $good_ids = array_column($order_awards['list'], 'goods_id');
  273. $goods = Goods::find()
  274. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $good_ids])
  275. ->asArray()
  276. ->indexBy('id')
  277. ->all();
  278. foreach ($order_awards['list'] as $index => &$award) {
  279. if (empty($orders[$award['order_id']])) {
  280. unset($order_awards['list'][$index]);
  281. continue;
  282. }
  283. $order = $orders[$award['order_id']];
  284. $user = $users[$order['user_id']] ?? [];
  285. $good = $goods[$award['goods_id']] ?? [];
  286. $award['total_pay_price'] = $order['pay_money'] ?? 0;
  287. $award['order_no'] = $order['order_no'] ?? '';
  288. $award['goods_name'] = $good['goods_name'];
  289. $award['nickname'] = $user['nickname'] ?: $user['mobile'];
  290. $award['mobile'] = $user['mobile'] ?? '';
  291. $award['avatar_url'] = $user['avatar_url'] ?: \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';;
  292. }
  293. $order_awards['list'] = array_values($order_awards['list']);
  294. return $this->success('success', $order_awards);
  295. }
  296. /**
  297. * @name:
  298. * @msg: 视频审核
  299. * @param {*}
  300. * @return {*}
  301. */
  302. public function actionVideoAudit()
  303. {
  304. if (!\Yii::$app->request->isPost) {
  305. return $this->error('请求方式错误');
  306. }
  307. $post = \Yii::$app->request->post();
  308. $rules = [
  309. [['id', 'check_status'], 'required'],
  310. [['id', 'check_status'], 'integer'],
  311. ['check_remark', 'required', 'when' => function ($model) {
  312. return $model->check_status == 2;
  313. }]
  314. ];
  315. $res = \Yii::$app->services->validate->validate($post, $rules);
  316. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  317. // dd($post);
  318. $post['check_time'] = time();
  319. if (1 == $post['check_status']) unset($post['check_remark']);
  320. try {
  321. $res = ShortVideo::setData($this->mall_id, $post);
  322. if (false === $res) {
  323. return $this->error('操作失败');
  324. } else {
  325. return $this->success('操作成功');
  326. }
  327. } catch (\Exception $e) {
  328. return $this->error($e->getMessage());
  329. }
  330. }
  331. /**
  332. * @name:
  333. * @msg: 取消视频黑名单
  334. * @param {*}
  335. * @return {*}
  336. */
  337. public function actionCancelBlacklist()
  338. {
  339. if (!\Yii::$app->request->isGet) {
  340. return $this->error('请求方式错误');
  341. }
  342. $id = \Yii::$app->request->get('id');
  343. if (empty($id)) return $this->error('参数错误');
  344. $video = ShortVideo::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $id, 'is_blacklist' => 1]);
  345. if (empty($video)) return $this->error('数据异常');
  346. $params = ['id' => $id, 'is_blacklist' => 0];
  347. try {
  348. $res = ShortVideo::setData($this->mall_id, $params);
  349. if (false === $res) {
  350. return $this->error('操作失败');
  351. } else {
  352. return $this->success('操作成功');
  353. }
  354. } catch (\Exception $e) {
  355. return $this->error($e->getMessage());
  356. }
  357. }
  358. /**
  359. * @name:
  360. * @msg: 删除视频
  361. * @param {*}
  362. * @return {*}
  363. */
  364. public function actionDelete()
  365. {
  366. if (!\Yii::$app->request->isGet) {
  367. return $this->error('请求方式错误');
  368. }
  369. $id = \Yii::$app->request->get('id');
  370. if (empty($id)) return $this->error('参数错误');
  371. $video = ShortVideo::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $id]);
  372. if (empty($video)) return $this->error('视频不能删除');
  373. $params = ['id' => $id, 'status' => -1];
  374. try {
  375. $res = ShortVideo::setData($this->mall_id, $params);
  376. if (false === $res) {
  377. return $this->error('删除失败');
  378. } else {
  379. return $this->success('删除成功');
  380. }
  381. } catch (\Exception $e) {
  382. return $this->error($e->getMessage());
  383. }
  384. }
  385. }