ShortVideo.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. <?php
  2. namespace addons\ShortVideo\common\models;
  3. use addons\ShortVideo\common\enums\AddonsShortVideoEnum;
  4. use common\enums\OrderStatusEnum;
  5. use common\enums\StatusEnum;
  6. use common\models\goods\Goods;
  7. use common\models\mall\MallAddons;
  8. use common\models\order\Order;
  9. use common\models\order\OrderDetail;
  10. use common\models\user\User;
  11. use Yii;
  12. /**
  13. * This is the model class for table "{{%addons_short_video}}".
  14. *
  15. * @property int $id
  16. * @property int $mall_id 商家id
  17. * @property int $user_id 用户id
  18. * @property string $title 标题
  19. * @property string $content 内容
  20. * @property string $video_url 视频路径
  21. * @property string $first_frame 首帧
  22. * @property int $video_wide 视频宽度
  23. * @property int $video_long 视频长度
  24. * @property string $duration_str 视频时长视频时长(时分秒字符串形式)
  25. * @property float $duration_seconds 视频时长(单位秒,浮点数形式)
  26. * @property int $related_type 关联类型1:关联商品 2:关联店铺 3:关联地址
  27. * @property int $related_goods_id 关联商品id
  28. * @property int $related_store_id 关联门店id
  29. * @property string $related_address 关联地址
  30. * @property string $related_detailed_address 关联详细地址
  31. * @property string $related_lon 关联经度
  32. * @property string $related_lat 关联维度
  33. * @property string $tag_id 标签id
  34. * @property int $open_integral_award 是否开启积分奖励 0:未开启 1:开启
  35. * @property int $open_money_award 是否开启现金奖励 0:未开启 1:开启
  36. * @property float $max_integral 积分最大值
  37. * @property float $surplus_integral 剩余的积分
  38. * @property float $spend_integral 花费积分
  39. * @property float $max_money 金额最大值
  40. * @property float $surplus_money 剩余金额
  41. * @property float $spend_money 花费金额
  42. * @property float $watch_integral_sum 观看奖励积分总和
  43. * @property float $watch_integral 观看奖励积分百分比
  44. * @property float $watch_integral_one 观看奖励积分一级百分比
  45. * @property float $watch_integral_two 观看奖励现金二级百分比
  46. * @property float $watch_money_sum 观看现金奖励总和
  47. * @property float $watch_money 观看奖励现金百分比
  48. * @property float $watch_money_one 观看奖励现金一级百分比
  49. * @property float $watch_money_two 观看奖励现金二级百分比
  50. * @property string $check_remark 审核备注
  51. * @property int $check_status 审核状态:0:未审核 1:审核通过 2:审核不通过
  52. * @property int $comment_num 评论量
  53. * @property int $like_num 点赞量
  54. * @property int $look_people_num 观看人数
  55. * @property int $look_complete_num 完整观看人数
  56. * @property int $look_num 浏览量
  57. * @property int $share_num 分享数量
  58. * @property int $share_click_num 分享点击数量
  59. * @property int $is_blacklist 是否拉黑0:否 1:是
  60. * @property int $is_top 是否置顶0:否 1:是
  61. * @property int $status 状态[-1:删除;0:禁用;1启用]
  62. * @property int $check_time 审核时间
  63. * @property int $top_time 置顶时间
  64. * @property int $blacklist_time 拉黑时间
  65. * @property int $created_at
  66. * @property int $updated_at
  67. */
  68. class ShortVideo extends \common\models\base\BaseActiveRecord
  69. {
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public static function tableName()
  74. {
  75. return '{{%addons_short_video}}';
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function rules()
  81. {
  82. return [
  83. [['mall_id', 'user_id'], 'required'],
  84. [['mall_id', 'user_id', 'video_wide', 'video_long', 'related_type', 'related_goods_id', 'related_store_id', 'open_integral_award', 'open_money_award', 'check_status', 'comment_num', 'like_num', 'look_people_num', 'look_complete_num', 'look_num', 'share_num', 'share_click_num', 'is_blacklist', 'is_top', 'status', 'check_time', 'top_time', 'blacklist_time', 'created_at', 'updated_at'], 'integer'],
  85. [['max_integral', 'surplus_integral', 'spend_integral', 'max_money', 'surplus_money', 'spend_money', 'watch_integral_sum', 'watch_integral', 'watch_integral_one', 'watch_integral_two', 'watch_money_sum', 'watch_money', 'watch_money_one', 'watch_money_two', 'duration_seconds'], 'number'],
  86. [['title', 'content', 'video_url', 'first_frame', 'related_address', 'related_detailed_address', 'tag_id', 'check_remark'], 'string', 'max' => 255],
  87. [['related_lon', 'related_lat', 'duration_str'], 'string', 'max' => 20],
  88. ];
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function attributeLabels()
  94. {
  95. return [
  96. 'id' => 'ID',
  97. 'mall_id' => '商城ID',
  98. 'user_id' => '会员ID',
  99. 'title' => '视频标题',
  100. 'content' => '描述内容',
  101. 'video_url' => '视频地址',
  102. 'first_frame' => '首帧',
  103. 'video_wide' => '视频宽度',
  104. 'video_long' => '视频长度',
  105. 'duration_str' => '视频时长视频时长(时分秒字符串形式)',
  106. 'duration_seconds' => '视频时长(单位秒,浮点数形式)',
  107. 'related_type' => '关联类型',
  108. 'related_goods_id' => '关联商品id',
  109. 'related_store_id' => '关联门店id',
  110. 'related_address' => '关联地址',
  111. 'related_detailed_address' => '关联详细地址',
  112. 'related_lon' => '关联经度',
  113. 'related_lat' => '关联维度',
  114. 'tag_id' => '标签id',
  115. 'open_integral_award' => '是否开启积分奖励',
  116. 'open_money_award' => '是否开启现金奖励',
  117. 'max_integral' => '积分最大值',
  118. 'surplus_integral' => '剩余的积分',
  119. 'spend_integral' => '花费积分',
  120. 'max_money' => '金额最大值',
  121. 'surplus_money' => '剩余金额',
  122. 'spend_money' => '花费金额',
  123. 'watch_integral_sum' => '观看奖励积分总和',
  124. 'watch_integral' => '观看奖励积分百分比',
  125. 'watch_integral_one' => '观看奖励积分一级百分比',
  126. 'watch_integral_two' => '观看奖励现金二级百分比',
  127. 'watch_money_sum' => '观看现金奖励总和',
  128. 'watch_money' => '观看奖励现金百分比',
  129. 'watch_money_one' => '观看奖励现金一级百分比',
  130. 'watch_money_two' => '观看奖励现金二级百分比',
  131. 'check_remark' => '审核备注',
  132. 'check_status' => '审核状态',
  133. 'comment_num' => '评论量',
  134. 'like_num' => '点赞量',
  135. 'look_people_num' => '观看人数',
  136. 'look_complete_num' => '完整观看人数',
  137. 'look_num' => '浏览量',
  138. 'share_num' => '分享数量',
  139. 'share_click_num' => '分享点击数量',
  140. 'is_blacklist' => '是否拉黑',
  141. 'is_top' => '是否置顶',
  142. 'status' => '状态[-1:删除;0:禁用;1启用]',
  143. 'check_time' => '审核时间',
  144. 'top_time' => '置顶时间',
  145. 'blacklist_time' => '拉黑时间',
  146. 'created_at' => 'Created At',
  147. 'updated_at' => 'Updated At',
  148. ];
  149. }
  150. /**
  151. * @name:
  152. * @msg: 获取视频列表,包括正常视频列表、待审核列表、黑名单列表。此方法主要是后台使用
  153. * @param {int} $mall_id
  154. * @param {int} $list_type 列表类型,1:正常,2:待审核,3:黑名单
  155. * @param {array} $conditions 获取列表条件
  156. * @return array
  157. */
  158. public static function getList(int $mall_id, int $list_type, array $conditions = [])
  159. {
  160. if (1 == $list_type) {
  161. // 正常列表
  162. $is_blacklist = 0;
  163. $check_status = AddonsShortVideoEnum::VIDEO_AUDIT_STATUS1;
  164. } elseif (2 == $list_type) {
  165. // 审核中列表
  166. $is_blacklist = 0;
  167. $check_status = AddonsShortVideoEnum::VIDEO_AUDIT_STATUS0;
  168. } elseif (3 == $list_type) {
  169. $is_blacklist = 1;
  170. $check_status = AddonsShortVideoEnum::VIDEO_AUDIT_STATUS1;
  171. } else {
  172. throw new \Exception('参数错误:list_type');
  173. }
  174. $wheres[] = ['status' => StatusEnum::ENABLED, 'check_status' => $check_status, 'is_blacklist' => $is_blacklist, 'mall_id' => $mall_id];
  175. $users = [];
  176. if (!empty($conditions['user_nickname'])) {
  177. $users = User::find()
  178. ->select('id,nickname,mobile,avatar_url')
  179. ->where(['status' => StatusEnum::ENABLED, 'mall_id' => $mall_id])
  180. ->andWhere(['like', 'nickname', $conditions['user_nickname']])
  181. ->asArray()
  182. ->indexBy('id')
  183. ->all();
  184. $wheres[] = ['user_id' => array_keys($users)];
  185. }
  186. if (!empty($conditions['related_type'])) $wheres[] = ['related_type' => $conditions['related_type']];
  187. if (!empty($conditions['video_title'])) $wheres[] = ['like', 'title', $conditions['video_title']];
  188. if (!empty($conditions['start_time'])) $wheres[] = ['>=', 'created_at', strtotime($conditions['start_time'] . ' 00:00:00')];
  189. if (!empty($conditions['end_time'])) $wheres[] = ['<=', 'created_at', strtotime($conditions['end_time'] . ' 23:59:59')];
  190. $params = [
  191. 'select' => 'id,user_id,title,first_frame,video_url,created_at,look_people_num,share_num,look_num,is_top,related_goods_id,related_type,spend_integral,spend_money,related_lon,related_lat,related_store_id,related_address,related_detailed_address,top_time',
  192. 'where' => $wheres,
  193. 'order' => 'is_top desc,created_at desc',
  194. 'page' => $conditions['page'],
  195. 'limit' => $conditions['limit'] ?? null,
  196. ];
  197. $video_list = ShortVideo::listPage($params);
  198. // $video_list = ShortVideo::getStaticError();
  199. // dd($video_list);
  200. if (empty($video_list['list'])) return $video_list;
  201. if (empty($users)) {
  202. $users = User::find()
  203. ->select('id,nickname,mobile,avatar_url')
  204. ->where(['status' => StatusEnum::ENABLED, 'mall_id' => $mall_id, 'id' => array_column($video_list['list'], 'user_id')])
  205. ->asArray()
  206. ->indexBy('id')
  207. ->all();
  208. }
  209. $relate_good_ids = $relate_store_ids = [];
  210. foreach ($video_list['list'] as $list) {
  211. if (AddonsShortVideoEnum::VIDEO_RELATE_TYPE1 == $list['related_type']) $relate_good_ids[] = $list['related_goods_id'];
  212. if (AddonsShortVideoEnum::VIDEO_RELATE_TYPE2 == $list['related_type']) $relate_store_ids[] = $list['related_store_id'];
  213. }
  214. // 关联的商品信息
  215. if (empty($relate_good_ids)) {
  216. $goods = [];
  217. } else {
  218. $goods = Goods::find()
  219. ->select('id,goods_name,cover_pic,price')
  220. ->where(['id' => $relate_good_ids, 'mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'is_on_sale' => 1])
  221. ->asArray()
  222. ->indexBy('id')
  223. ->all();
  224. }
  225. // 关联的店铺的信息
  226. if (empty($relate_store_ids)) {
  227. $stores = [];
  228. } else {
  229. // TODO:关联的店铺的信息,系统还没有店铺应用,暂时不做
  230. $stores = [];
  231. }
  232. // 分享观看信息
  233. $video_ids = array_column($video_list['list'], 'id');
  234. $browse_rewards = ShortVideoBrowseAward::find()
  235. ->select('video_id,sum(award_integral) as reward_score,sum(award_money) as reward_money')
  236. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'video_id' => $video_ids])
  237. ->groupBy('video_id')
  238. ->asArray()
  239. ->indexBy('video_id')
  240. ->all();
  241. // 转化金额
  242. $order_awards = ShortVideoOrderAward::find()
  243. ->select('video_id,order_id')
  244. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'video_id' => $video_ids, 'is_award' => 1])
  245. ->asArray()
  246. ->all();
  247. $video_order_map = [];
  248. if (!empty($order_awards)) {
  249. foreach ($order_awards as $award) {
  250. $video_order_map[$award['video_id']][] = $award['order_id'];
  251. }
  252. foreach ($video_order_map as $video_id => $order_ids) {
  253. $orders = Order::find()
  254. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $order_ids, 'pay_status' => OrderStatusEnum::PAY])
  255. ->asArray()
  256. ->all();
  257. $conversion[$video_id]['money'] = array_sum(array_column($orders, 'pay_money'));
  258. }
  259. } else {
  260. $conversion = [];
  261. }
  262. // 分红金额
  263. $dividends = ShortVideoOrderAward::find()
  264. ->select('sum(money) as money,video_id')
  265. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'video_id' => $video_ids, 'is_award' => 1])
  266. ->groupBy('video_id')
  267. ->asArray()
  268. ->indexBy('video_id')
  269. ->all();
  270. // dd($users);
  271. // 遍历 list
  272. foreach ($video_list['list'] as &$video) {
  273. $video['nickname'] = ($users[$video['user_id']]['nickname'] ?? $users[$video['user_id']]['mobile']) ?? '';
  274. $video['mobile'] = $users[$video['user_id']]['mobile'] ?? '';
  275. $video['avatar_url'] = !empty($users[$video['user_id']]['avatar_url']) ? $users[$video['user_id']]['avatar_url'] : \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
  276. $video['goods_list'] = $goods[$video['related_goods_id']] ?? [];
  277. $video['store_name'] = $stores[$video['related_store_id']] ?? '';
  278. $video['conversion'] = floatval($conversion[$video['id']]['money'] ?? 0);
  279. $video['dividend'] = floatval($dividends[$video['id']]['money'] ?? 0);
  280. $browse_reward = $browse_rewards[$video['id']] ?? [];
  281. $video['reward_money'] = $browse_reward['reward_money'] ?? 0;
  282. $video['reward_score'] = $browse_reward['reward_score'] ?? 0;
  283. }
  284. return $video_list;
  285. }
  286. public static function setData(int $mall_id, array $data)
  287. {
  288. $transaction = \Yii::$app->db->beginTransaction();
  289. if (!empty($data['id'])) {
  290. $query = self::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['id']]);
  291. if (!empty($data['user_id'])) $query->andWhere(['user_id' => $data['user_id']]);
  292. $model = $query->one();
  293. if (empty($model)) {
  294. $transaction->rollBack();
  295. self::$static_error = '视频不存在';
  296. return false;
  297. }
  298. } else {
  299. $model = new self();
  300. $model->loadDefaultValues();
  301. $model->mall_id = $mall_id;
  302. $model->check_time = time();
  303. // 更新标签使用记录
  304. if (!empty($data['tag_id'])) {
  305. $tag_ids = explode(',', $data['tag_id']);
  306. // dd($tag_ids);
  307. $tags = ShortVideoTag::findAll(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $tag_ids]);
  308. if (!empty($tags)) {
  309. foreach ($tags as $tag) {
  310. $tag->usage_num += 1;
  311. $tag->save();
  312. unset($tag);
  313. }
  314. }
  315. }
  316. }
  317. foreach ($data as $key => $val) {
  318. $model->$key = $val;
  319. }
  320. // dd($model);
  321. if (!$model->save()) {
  322. $transaction->rollBack();
  323. self::$static_error = '保存数据失败:' . $model->getErrorMessage();
  324. return false;
  325. }
  326. $transaction->commit();
  327. return $model;
  328. }
  329. /**
  330. * @name:
  331. * @msg: 判断会员是否满足发布视频的条件
  332. * @param {int} $mall_id
  333. * @param {int} $user_id
  334. * @return {*}
  335. */
  336. public static function userCanPush(int $mall_id, int $user_id)
  337. {
  338. $configs = ShortVideoConfig::getConfig($mall_id);
  339. if (empty($configs)) {
  340. $rst = [
  341. 'is_can_push' => false,
  342. 'index' => [],
  343. 'errinfo' => [],
  344. ];
  345. return $rst;
  346. }
  347. $is_can_push = false;
  348. // dd($configs['release_rule']);
  349. if (1 == $configs['release_rule']) {
  350. // 消费次数
  351. if (1 == $configs['purchase_times_status']) {
  352. $order_counts = Order::find()
  353. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'user_id' => $user_id, 'pay_status' => OrderStatusEnum::PAY])
  354. ->count();
  355. if ($order_counts >= $configs['purchase_times']) {
  356. $res_times = [
  357. 'consumed_times' => $order_counts,
  358. 'remainder_times' => 0,
  359. 'consume_times_res' => true,
  360. ];
  361. } else {
  362. $res_times = [
  363. 'consumed_times' => $order_counts,
  364. 'remainder_times' => $configs['purchase_times'] - $order_counts,
  365. 'consume_times_res' => false,
  366. ];
  367. }
  368. } else {
  369. $res_times = [
  370. 'consumed_times' => 0,
  371. 'remainder_times' => 0,
  372. 'consume_times_res' => true,
  373. ];
  374. }
  375. // 消费金额
  376. if (1 == $configs['purchase_cost_status']) {
  377. $order_amounts = Order::find()
  378. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'user_id' => $user_id, 'pay_status' => OrderStatusEnum::PAY])
  379. ->sum('pay_money');
  380. if ($order_amounts >= $configs['purchase_cost']) {
  381. $res_amount = [
  382. 'consumed_amount' => $order_amounts,
  383. 'remainder_amount' => 0,
  384. 'consume_amount_res' => true,
  385. ];
  386. } else {
  387. $res_amount = [
  388. 'consumed_amount' => $order_amounts ?? 0,
  389. 'remainder_amount' => $configs['purchase_cost'] - $order_amounts ?? 0,
  390. 'consume_amount_res' => false,
  391. ];
  392. }
  393. } else {
  394. $res_amount = [
  395. 'consumed_amount' => 0,
  396. 'remainder_amount' => 0,
  397. 'consume_amount_res' => true,
  398. ];
  399. }
  400. // 消费商品
  401. if (1 == $configs['purchase_goods_status']) {
  402. $order_detail = OrderDetail::find()
  403. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'user_id' => $user_id, 'goods_id' => $configs['purchase_goods_id']])
  404. ->andWhere(['>', 'order_status', OrderStatusEnum::PAY])
  405. ->one();
  406. $good = Goods::find()
  407. ->select('id,goods_name,cover_pic,price')
  408. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $configs['purchase_goods_id']])
  409. ->asArray()
  410. ->one();
  411. $res_good = [
  412. 'good' => $good,
  413. 'consume_good_res' => false,
  414. ];
  415. if (!empty($order_detail)) {
  416. $res_good['consume_good_res'] = true;
  417. }
  418. } else {
  419. $res_good = [
  420. 'good' => [],
  421. 'consume_good_res' => true,
  422. ];
  423. }
  424. } else {
  425. $res_times['consume_times_res'] = $res_amount['consume_amount_res'] = $res_good['consume_good_res'] = true;
  426. }
  427. if (true === $res_times['consume_times_res'] && true === $res_amount['consume_amount_res'] && true === $res_good['consume_good_res']) {
  428. $is_can_push = true;
  429. $rst = [
  430. 'is_can_push' => $is_can_push,
  431. 'index' => [],
  432. 'errinfo' => [],
  433. ];
  434. } else {
  435. $index = [];
  436. if (!$res_times['consume_times_res']) $index[] = 'times';
  437. if (!$res_amount['consume_amount_res']) $index[] = 'amount';
  438. if (!$res_good['consume_good_res']) $index[] = 'good';
  439. $rst = [
  440. 'is_can_push' => $is_can_push,
  441. 'index' => $index,
  442. 'errinfo' => ['times' => $res_times, 'amount' => $res_amount, 'good' => $res_good],
  443. ];
  444. }
  445. if (!$rst['is_can_push']) {
  446. if (empty($rst['index'])) {
  447. $res = [
  448. 'is_can_push' => $rst['is_can_push'],
  449. 'errmsg' => '您尚未满足达到视频的要求',
  450. ];
  451. } else {
  452. $errmsg = '您还需要再';
  453. foreach ($rst['index'] as $errindex) {
  454. if ('times' == $errindex) {
  455. $errmsg .= '消费 ' . $rst['errinfo'][$errindex]['remainder_times'] . ' 次 ';
  456. }
  457. if ('amount' == $errindex) {
  458. $errmsg .= '消费满 ' . $rst['errinfo'][$errindex]['remainder_amount'] . ' 元 ';
  459. }
  460. if ('good' == $errindex) {
  461. $errmsg .= '购买商品 【' . $rst['errinfo'][$errindex]['good']['goods_name'] . '】 ';
  462. }
  463. }
  464. $errmsg .= '后即可发布视频';
  465. $res = [
  466. 'is_can_push' => $rst['is_can_push'],
  467. 'errmsg' => $errmsg,
  468. 'good' => $rst['errinfo'][$errindex]['good'],
  469. ];
  470. }
  471. } else {
  472. $res = [
  473. 'is_can_push' => true,
  474. 'errmsg' => '您已达到发布视频的要求',
  475. ];
  476. }
  477. return $res;
  478. }
  479. /**
  480. * @name:
  481. * @msg: 会员浏览视频处理
  482. * @param {int} $mall_id
  483. * @param {array} $data video_id:视频id;user_id:当前会员id;share_log_id:该视频的分享记录id
  484. * @return {*}
  485. */
  486. public static function browse(int $mall_id, array $data)
  487. {
  488. $transaction = \Yii::$app->db->beginTransaction();
  489. $video = self::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['video_id'], 'check_status' => AddonsShortVideoEnum::VIDEO_AUDIT_STATUS1, 'is_blacklist' => 0]);
  490. if (empty($video)) {
  491. self::$static_error = '视频不存在';
  492. return false;
  493. }
  494. $video_params = [
  495. 'id' => $data['video_id'],
  496. 'look_num' => $video->look_num + 1,
  497. ];
  498. // 该会员之前是否看过改视频
  499. $video_browse = ShortVideoBrowse::findOne(['mall_id' => $mall_id, 'user_id' => $data['user_id'], 'video_id' => $data['video_id'], 'status' => StatusEnum::ENABLED]);
  500. if (empty($video_browse)) $video_params['look_people_num'] = $video->look_people_num + 1;
  501. // dd($video_params);
  502. $video_res = self::setData($mall_id, $video_params);
  503. if (false === $video_res) {
  504. $transaction->rollBack();
  505. return false;
  506. }
  507. $browse_data = [
  508. 'video_id' => $data['video_id'],
  509. 'user_id' => $data['user_id'],
  510. ];
  511. if (!empty($data['share_user_id'])) {
  512. $browse_data['is_share'] = 1;
  513. }
  514. $browse_res = ShortVideoBrowse::setData($mall_id, $browse_data);
  515. if (false === $browse_res) {
  516. $transaction->rollBack();
  517. self::$static_error = ShortVideoBrowse::getStaticError();
  518. return false;
  519. }
  520. $watch_data = [
  521. 'mall_id' => $mall_id,
  522. 'user_id' => $data['user_id'],
  523. 'video_id' => $data['video_id'],
  524. 'status' => StatusEnum::ENABLED,
  525. ];
  526. //记录观看记录
  527. $watch_res = ShortVideoUserWatch::setData($watch_data);
  528. if (false === $watch_res) {
  529. $transaction->rollBack();
  530. self::$static_error = ShortVideoUserWatch::getStaticError();
  531. return false;
  532. }
  533. $browse_num = ShortVideoBrowseNum::findOne(['mall_id' => $mall_id, 'user_id' => $data['user_id'], 'status' => StatusEnum::ENABLED]);
  534. $browse_times = $browse_num->look_num ?? 0;
  535. $browse_num_data = [
  536. 'user_id' => $data['user_id'],
  537. 'look_num' => $browse_times + 1,
  538. ];
  539. $browse_num_res = ShortVideoBrowseNum::setData($mall_id, $browse_num_data);
  540. if (false === $browse_num_res) {
  541. $transaction->rollBack();
  542. self::$static_error = ShortVideoBrowseNum::getStaticError();
  543. return false;
  544. }
  545. if (!empty($data['share_user_id'])) {
  546. $wheres = [
  547. 'mall_id' => $mall_id,
  548. 'status' => StatusEnum::ENABLED,
  549. 'user_id' => $data['share_user_id'],
  550. 'share_to_user_id' => 0,
  551. 'video_id' => $data['video_id'],
  552. ];
  553. $share_log = ShortVideoShare::find()
  554. ->where($wheres)
  555. ->orderBy('created_at desc')
  556. ->limit(1)
  557. ->one();
  558. if (!empty($share_log)) {
  559. $share_log->share_to_user_id = $data['user_id'];
  560. $sharelog_res = $share_log->save();
  561. if (false === $sharelog_res) {
  562. $transaction->rollBack();
  563. self::$static_error = $share_log->getErrors();
  564. return false;
  565. }
  566. }
  567. }
  568. $transaction->commit();
  569. return $browse_res;
  570. }
  571. /**
  572. * @desc:推广中心-调用
  573. * @Author: hua
  574. * @Date: 2021/11/12
  575. * @Time: 9:30
  576. * @Copyright: copyright (c) 2021 广东七件事集团
  577. * @param $params ['user_id' => 1, 'mall_id' => 5, 'addons_name' => 'Agent']
  578. * @return array
  579. */
  580. public static function getIncomeInfo(array $params)
  581. {
  582. $is_install = MallAddons::isInstall($params['mall_id'], $params['addons_name']);
  583. $data['total_commission'] = 0;
  584. $data['is_install'] = 0;
  585. $basic = \Yii::$app->services->setting->addons->get($params['mall_id'], $params['addons_name'], 'basic');
  586. $data['is_show_at_center'] = 1;
  587. if(isset($basic['is_show_at_center'])) $data['is_show_at_center'] = $basic['is_show_at_center'];
  588. if($is_install){
  589. $browse_reward = ShortVideoBrowseAward::find()
  590. ->where(['mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED])
  591. ->sum('award_money');
  592. $order_reward = ShortVideoOrderAward::find()
  593. ->where(['mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED, 'is_settlement' => 1, 'is_award' => 1])
  594. ->sum('award_money');
  595. $data['total_commission'] = $browse_reward * 1 + $order_reward * 1;
  596. $data['is_install'] = $is_install;
  597. }
  598. return $data;
  599. }
  600. }