| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- <?php
- /**
- * Created by PhpStorm.
- * User: 阿源
- * Date: 2020/8/31
- * Time: 11:21
- */
- namespace addons\ShortVideo\backend\controllers;
- use addons\ShortVideo\common\enums\AddonsShortVideoEnum;
- use addons\ShortVideo\common\models\ShortVideo;
- use addons\ShortVideo\common\models\ShortVideoBrowse;
- use addons\ShortVideo\common\models\ShortVideoBrowseAward;
- use addons\ShortVideo\common\models\ShortVideoCommissionItemSetting;
- use addons\ShortVideo\common\models\ShortVideoConfig;
- use addons\ShortVideo\common\models\ShortVideoGoods;
- use addons\ShortVideo\common\models\ShortVideoOrderAward;
- use addons\ShortVideo\common\models\ShortVideoTag;
- use common\enums\OrderStatusEnum;
- use common\enums\StatusEnum;
- use common\models\goods\Goods;
- use common\models\order\Order;
- use common\models\user\User;
- class VideoController extends BaseController
- {
- /**
- * @name:
- * @msg: 视频置顶/取消置顶操作
- * @param {*}
- * @return {*}
- */
- public function actionTop()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $id = \Yii::$app->request->get('id');
- $is_top = \Yii::$app->request->get('is_top');
- if (empty($id) || (empty($is_top) && !is_numeric($is_top))) {
- return $this->error('参数错误');
- }
- $params = ['id' => $id, 'is_top' => $is_top];
- if (1 == $is_top) {
- $params['top_time'] = time();
- } else {
- $params['top_time'] = 0;
- }
- try {
- $res = ShortVideo::setData($this->mall_id, $params);
- if (false === $res) {
- return $this->error('操作失败');
- } else {
- return $this->success('操作成功');
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- /**
- * @name:
- * @msg: 视频拉黑
- * @param {integer} $id 视频id
- * @return {*}
- */
- public function actionJoinBlacklist()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $id = \Yii::$app->request->get('id');
- if (empty($id)) return $this->error('参数错误');
- $params = ['id' => $id, 'is_blacklist' => 1, 'blacklist_time' => time()];
- try {
- $res = ShortVideo::setData($this->mall_id, $params);
- if (false === $res) {
- return $this->error('操作失败');
- } else {
- return $this->success('操作成功');
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- /**
- * @name:
- * @msg: 视频相关统计数据
- * @param {integer} $id 视频id
- * @return {*}
- */
- public function actionVideoData()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $id = \Yii::$app->request->get('id');
- $start_time = \Yii::$app->request->get('start_time');
- $end_time = \Yii::$app->request->get('end_time');
- if (!empty($start_time)) $start_time = strtotime($start_time . ' 00:00:00');
- if (!empty($end_time)) $end_time = strtotime($end_time . ' 23:59:59');
- if (empty($id)) return $this->error('参数错误');
- $video_info = ShortVideo::find()
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $id])
- ->asArray()
- ->one();
- if (empty($video_info)) return $this->error('视频不存在');
- $params[] = ['video_id' => $id];
- if (!empty($start_time)) $params[] = ['>=', 'created_at', $start_time];
- if (!empty($end_time)) $params[] = ['<=', 'created_at', $end_time];
- //累计订单金额
- $video_info['order_sum_money'] = ShortVideoOrderAward::getStatData($this->mall_id, 'order_sum_money', $params);
- //订单数量
- $video_info['order_num'] = ShortVideoOrderAward::getStatData($this->mall_id, 'order_num', $params);
- //订单金额
- // $video_info['order_money'] = $video_info['order_sum_money'];
- // 下单人数
- $video_info['order_people'] = ShortVideoOrderAward::getStatData($this->mall_id, 'order_people', $params);
- $query = ShortVideoBrowseAward::find()->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'video_id' => $id]);
- if (!empty($start_time)) $query->andWhere(['>=', 'created_at', $start_time]);
- if (!empty($end_time)) $query->andWhere(['<=', 'created_at', $end_time]);
- $awards = $query->all();
- $look_integral_own = $look_integral_one = $look_integral_two = $look_money_own = $look_money_one = $look_money_two = 0;
- if (!empty($awards)) {
- foreach ($awards as $award) {
- if ($award->award_type == AddonsShortVideoEnum::VIDEO_REWARD_TYPE0) {
- // 奖励积分
- if ($award->user_type == AddonsShortVideoEnum::VIDEO_REWARD_USER_TYPE0) $look_integral_own += $award->award_integral;
- if ($award->user_type == AddonsShortVideoEnum::VIDEO_REWARD_USER_TYPE1) $look_integral_one += $award->award_integral;
- // if ($award->user_type == AddonsShortVideoEnum::VIDEO_REWARD_USER_TYPE2) $look_integral_two += $award->award_integral;
- } elseif ($award->award_type == AddonsShortVideoEnum::VIDEO_REWARD_TYPE1) {
- // 奖励现金
- if ($award->user_type == AddonsShortVideoEnum::VIDEO_REWARD_USER_TYPE0) $look_money_own += $award->award_money;
- if ($award->user_type == AddonsShortVideoEnum::VIDEO_REWARD_USER_TYPE1) $look_money_one += $award->award_money;
- // if ($award->user_type == AddonsShortVideoEnum::VIDEO_REWARD_USER_TYPE2) $look_money_two += $award->award_money;
- }
- }
- }
- //观看积分
- $video_info['look_integral_own'] = floatval(round($look_integral_own, 2));
- $video_info['look_integral_one'] = floatval(round($look_integral_one, 2));
- $video_info['look_integral_two'] = floatval(round($look_integral_two, 2));
- //观看金额
- $video_info['look_money_own'] = floatval(round($look_money_own, 2));
- $video_info['look_money_one'] = floatval(round($look_money_one, 2));
- $video_info['look_money_two'] = floatval(round($look_money_two, 2));
- //分红奖励
- $query_vo = ShortVideoOrderAward::find()->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'video_id' => $id]);
- if (!empty($start_time)) $query_vo->andWhere(['>=', 'created_at', $start_time]);
- if (!empty($end_time)) $query_vo->andWhere(['<=', 'created_at', $end_time]);
- $video_info['dividend_money'] = $query_vo->sum('money');
- return $this->success('success', $video_info);
- }
- /**
- * @name:
- * @msg: 视频转化相关数据
- * @param {integer} $id 视频id
- * @return {*}
- */
- public function actionVideoTransaction()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $id = \Yii::$app->request->get('id');
- if (empty($id)) return $this->error('参数错误');
- $start_time = \Yii::$app->request->get('start_time');
- $end_time = \Yii::$app->request->get('end_time');
- if (!empty($start_time)) $start_time = strtotime($start_time);
- if (!empty($end_time)) $end_time = strtotime($end_time);
- $video_info = ShortVideo::find()
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $id])
- ->asArray()
- ->one();
- if (empty($video_info)) return $this->error('视频不存在');
- // dd($video_info);
- //完播比例
- $video_info['look_proportion'] = $video_info['look_complete_num'] == 0 ? 0 : round($video_info['look_complete_num'] / $video_info['look_people_num'] * 100, 2);
- $params[] = ['video_id' => $id];
- if (!empty($start_time)) $params[] = ['>=', 'created_at', $start_time];
- if (!empty($end_time)) $params[] = ['<=', 'created_at', $end_time];
- //订单金额
- $video_info['order_money'] = ShortVideoOrderAward::getStatData($this->mall_id, 'order_sum_money', $params);
- // 下单人数
- $video_info['order_people'] = ShortVideoOrderAward::getStatData($this->mall_id, 'order_people', $params);
- //下单百分比
- if (0 == $video_info['order_people'] || 0 == $video_info['look_people_num']) {
- $video_info['order_proportion'] = 0;
- } else {
- $video_info['order_proportion'] = round($video_info['order_people'] / $video_info['look_people_num'] * 100, 2);
- }
- // $params[] = ['is_settlement' => 1];
- //支付人数
- $video_info['pay_people'] = ShortVideoOrderAward::getStatData($this->mall_id, 'pay_people', $params);
- //支付金额
- $video_info['pay_money'] = ShortVideoOrderAward::getStatData($this->mall_id, 'pay_money', $params);
- //客单价
- if (0 == $video_info['pay_money'] || 0 == $video_info['order_people']) {
- $video_info['pay_average_money'] = 0;
- } else {
- $video_info['pay_average_money'] = round($video_info['pay_money'] / $video_info['pay_people'], 2);
- }
- //支付转化率
- if (0 == $video_info['pay_people'] || 0 == $video_info['look_people_num']) {
- $video_info['pay_proportion'] = 0;
- } else {
- $video_info['pay_proportion'] = round($video_info['pay_people'] / $video_info['look_people_num'] * 100, 2);
- }
- return $this->success('success', $video_info);
- }
- /**
- * @name:
- * @msg: 视频详情页订单列表
- * @param {*}
- * @return {*}
- */
- public function actionOrderList()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $id = \Yii::$app->request->get('video_id');
- if (empty($id)) return $this->error('参数错误');
- $start_time = \Yii::$app->request->get('start_time');
- $end_time = \Yii::$app->request->get('end_time');
- $nickname = \Yii::$app->request->get('nickname');
- $page = \Yii::$app->request->get('page', 1);
- $limit = \Yii::$app->request->get('limit', $this->pageSize);
- $wheres[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'video_id' => $id];
- if (!empty($start_time)) $wheres[] = ['>=', 'created_at', strtotime($start_time . ' 00:00:00')];
- if (!empty($end_time)) $wheres[] = ['<=', 'created_at', strtotime($end_time . ' 23:59:59')];
- if (!empty($nickname)) {
- $users = User::find()
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
- ->andWhere(['like', 'nickname', $nickname])
- ->asArray()
- ->indexBy('id')
- ->all();
- $order_ids = Order::find()
- ->select('id')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'user_id' => array_column($users, 'id')])
- ->asArray()
- ->column();
- $wheres[] = ['order_id' => $order_ids];
- }
- $params = [
- 'select' => 'id,user_id,created_at,video_id,goods_id,award_type,money,award_percentage,award_money,is_settlement,order_id',
- 'where' => $wheres,
- 'order' => 'created_at desc,id desc',
- 'page' => $page,
- 'limit' => $limit,
- ];
- $order_awards = ShortVideoOrderAward::listPage($params);
- if (empty($order_awards['list'])) {
- if (is_array($order_awards['list'])) {
- return $this->success('success', $order_awards);
- } else {
- return $this->error(ShortVideoOrderAward::getStaticError());
- }
- }
- $order_ids = array_column($order_awards['list'], 'order_id');
- $orders = Order::find()
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $order_ids])
- ->asArray()
- ->indexBy('id')
- ->all();
- if (empty($users)) {
- $user_ids = array_column($orders, 'user_id');
- $users = User::find()
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $user_ids])
- ->asArray()
- ->indexBy('id')
- ->all();
- }
- $good_ids = array_column($order_awards['list'], 'goods_id');
- $goods = Goods::find()
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $good_ids])
- ->asArray()
- ->indexBy('id')
- ->all();
- foreach ($order_awards['list'] as $index => &$award) {
- if (empty($orders[$award['order_id']])) {
- unset($order_awards['list'][$index]);
- continue;
- }
- $order = $orders[$award['order_id']];
- $user = $users[$order['user_id']] ?? [];
- $good = $goods[$award['goods_id']] ?? [];
- $award['total_pay_price'] = $order['pay_money'] ?? 0;
- $award['order_no'] = $order['order_no'] ?? '';
- $award['goods_name'] = $good['goods_name'];
- $award['nickname'] = $user['nickname'] ?: $user['mobile'];
- $award['mobile'] = $user['mobile'] ?? '';
- $award['avatar_url'] = $user['avatar_url'] ?: \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';;
- }
- $order_awards['list'] = array_values($order_awards['list']);
- return $this->success('success', $order_awards);
- }
- /**
- * @name:
- * @msg: 视频审核
- * @param {*}
- * @return {*}
- */
- public function actionVideoAudit()
- {
- if (!\Yii::$app->request->isPost) {
- return $this->error('请求方式错误');
- }
- $post = \Yii::$app->request->post();
- $rules = [
- [['id', 'check_status'], 'required'],
- [['id', 'check_status'], 'integer'],
- ['check_remark', 'required', 'when' => function ($model) {
- return $model->check_status == 2;
- }]
- ];
- $res = \Yii::$app->services->validate->validate($post, $rules);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- // dd($post);
- $post['check_time'] = time();
- if (1 == $post['check_status']) unset($post['check_remark']);
- try {
- $res = ShortVideo::setData($this->mall_id, $post);
- if (false === $res) {
- return $this->error('操作失败');
- } else {
- return $this->success('操作成功');
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- /**
- * @name:
- * @msg: 取消视频黑名单
- * @param {*}
- * @return {*}
- */
- public function actionCancelBlacklist()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $id = \Yii::$app->request->get('id');
- if (empty($id)) return $this->error('参数错误');
- $video = ShortVideo::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $id, 'is_blacklist' => 1]);
- if (empty($video)) return $this->error('数据异常');
- $params = ['id' => $id, 'is_blacklist' => 0];
- try {
- $res = ShortVideo::setData($this->mall_id, $params);
- if (false === $res) {
- return $this->error('操作失败');
- } else {
- return $this->success('操作成功');
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- /**
- * @name:
- * @msg: 删除视频
- * @param {*}
- * @return {*}
- */
- public function actionDelete()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $id = \Yii::$app->request->get('id');
- if (empty($id)) return $this->error('参数错误');
- $video = ShortVideo::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $id]);
- if (empty($video)) return $this->error('视频不能删除');
- $params = ['id' => $id, 'status' => -1];
- try {
- $res = ShortVideo::setData($this->mall_id, $params);
- if (false === $res) {
- return $this->error('删除失败');
- } else {
- return $this->success('删除成功');
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- }
|