quanwei
2025-10-28 36cacbaf78e510713002fcd5e3d61cece2e01421
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
 
namespace app\api\model\plus\live;
 
use app\common\enum\user\GiftLogSceneEnum;
use app\common\model\plus\live\Gift as GiftModel;
use app\api\model\user\User as UserModel;
use app\common\model\plus\live\RoomGift as RoomGiftModel;
use app\common\model\user\GiftLog as GiftLogModel;
use app\common\model\plus\live\Room as RoomModel;
use app\common\model\plus\live\UserGift as UserGiftModel;
use app\api\model\supplier\Supplier as SupplierModel;
/**
 * 礼物模型
 */
class Gift extends GiftModel
{
 
    /**
     * 获取礼物列表
     */
    public function getList()
    {
        return $this->with(['image'])
            ->where('is_delete', '=', 0)
            ->order(['sort' => 'asc', 'create_time' => 'asc'])
            ->select();
    }
 
    public function sendGift($user, $room_id, $gift_id){
        $gift = self::detail($gift_id);
        $room = RoomModel::detail($room_id);
        if(!$gift || $gift['is_delete'] == 1){
            $this->error = '礼物不存在';
            return false;
        }
        if($user['gift_money'] < $gift['price']){
            $this->error = '余额不足';
            return false;
        }
        $this->startTrans();
        try {
            // 扣除
            (new UserModel())->where('user_id', '=', $user['user_id'])
                ->dec('gift_money', $gift['price'])
                ->update();
            // 主播增加
            (new SupplierModel())->where('shop_supplier_id', '=', $room['shop_supplier_id'])
                ->inc('total_gift', $gift['price'])
                ->inc('gift_money', $gift['price'])
                ->update();
            // 房间增加
            (new RoomModel())->where('room_id', '=', $room_id)
                ->inc('gift_num', $gift['price'])
                ->update();
            // 房间用户礼物数增加
            $user_gift_model = UserGiftModel::detail($room_id, $user['user_id']);
            $user_gift_model->where('room_id', '=', $room_id)->where('user_id', '=', $user['user_id'])
                ->inc('gift_num', $gift['price'])->update();
            // 保存记录
            (new RoomGiftModel())->save([
                'room_id' => $room_id,
                'user_id' => $user['user_id'],
                'shop_supplier_id' => $room['shop_supplier_id'],
                'price' => $gift['price'],
                'gift_name' =>  $gift['gift_name'],
                'app_id' => self::$app_id
            ]);
            GiftLogModel::add(GiftLogSceneEnum::CONSUME, [
                'user_id' => $user['user_id'],
                'money' => -$gift['price'],
            ], ['gift_name' => $gift['gift_name']]);
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
}