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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
 
namespace app\common\model\branch;
 
use app\common\model\BaseModel;
use app\common\service\order\OrderService;
 
/**
 * 用户模型
 */
 
class ActivityUser extends BaseModel
{
    protected $name = 'branch_activity_user';
    protected $pk = 'order_id';
 
    /**
     * 追加字段
     * @var string[]
     */
    protected $append = [
        'pay_status_text',
        'pay_type_text',
        'status_text',
    ];
 
    /**
     * 关联会员记录表
     * @return \think\model\relation\BelongsTo
     */
 
    public function user()
    {
        return $this->belongsTo('app\\common\\model\\user\\User');
    }
 
    /**
     * 关联活动记录表
     * @return \think\model\relation\BelongsTo
     */
 
    public function activity()
    {
        return $this->belongsTo('app\\common\\model\\branch\\Activity', 'activity_id', 'activity_id');
    }
    public function branch()
    {
        return $this->belongsTo('app\\common\\model\\branch\\Branch', 'branch_id', 'branch_id');
    }
 
    /**
     * 支付状态
     */
    public function getPayStatusTextAttr($value, $data)
    {
        if($data["pay_status"] == 20){
            $pay_status = "已支付";
        }else{
            $pay_status = "未支付";
        }
        return $pay_status;
    }
    /**
     * 支付方式
     */
    public function getPayTypeTextAttr($value, $data)
    {
        $pay_type="";
        if($data["pay_type"] == 20){
            $pay_type = "微信支付";
        }elseif($data["pay_type"] == 10){
            $pay_type = "余额支付";
        }
        return $pay_type;
    }
    /**
     * 报名状态
     */
    public function getStatusTextAttr($value, $data)
    {
        $status = '报名失败';
        if ($data["status"] == 1) {
            $status = "报名成功";
        } else {
            if ($data['pay_price'] && $data['pay_status'] == 10) {
                $status = "待支付活动费用";
            }
        }
        return $status;
    }
 
    /**
     * 详情
     */
    public static function detail($where, $with = ['user'])
    {
        !is_array($where) && $where = ['order_id' => (int)$where];
        return (new static())->where(array_merge(['is_delete' => 0, 'status' => 1], $where))->with($with)->find();
    }
 
    /**
     * 待支付详情
     */
    public static function getPayDetail($orderNo)
    {
        $model = new static();
        return $model->where(['order_no' => $orderNo, 'pay_status' => 10])->with(['user'])->find();
    }
 
    /**
     * 新增用户记录
     * @param $user_id
     * @param $data
     * @return bool
     */
    public static function add($data)
    {
        $model = new static;
        $model->save(array_merge([
            'is_delete' => 0,
            'app_id' => $model::$app_id
        ], $data));
        return $model['order_id'];
    }
    /**
     * 获取报名数量
     */
    public static function getRegNum($activity_id)
    {
        return (new static())->where('activity_id', '=', $activity_id)
            ->where('status', '=', 1)
            ->where('is_delete', '=', 0)
            ->count();
    }
 
    /**
     * 获取签到数量
     */
    public static function getVerifyNum($activity_id)
    {
        return (new static())->where('activity_id', '=', $activity_id)
            ->where('is_verify', '=', 1)
            ->where('is_delete', '=', 0)
            ->count();
    }
 
    /**
     * 生成订单号
     */
    public function orderNo()
    {
        return OrderService::createOrderNo();
    }
 
    /**
     * 获取报名总数 (可指定某天)
     */
    public function getUserData($startDate = null, $endDate = null, $type, $branch_id = 0)
    {
        $model = $this->alias('u')
                ->join('branch_member', 'u.user_id = branch_member.user_id', 'left');
 
        !is_null($startDate) && $model = $model->where('pay_time', '>=', strtotime($startDate));
 
        if (is_null($endDate)) {
            !is_null($startDate) && $model = $model->where('pay_time', '<', strtotime($startDate) + 86400);
        } else {
            $model = $model->where('pay_time', '<', strtotime($endDate) + 86400);
        }
 
        if ($branch_id > 0) {
            $model = $model->where('branch_member.branch_id', '=', $branch_id);
        }
 
        $model = $model->where('u.is_delete', '=', 0)
            ->where('pay_status', '=', 20);
 
        if ($type == 'user_total') {
            // 订单数量
            return $model->count();
        } else if ($type == 'total_price') {
            // 订单总金额
            return $model->sum('pay_price');
        }
        return 0;
    }
 
    //判断用户是否报名
    public static function isReg($user_id, $activity_id)
    {
        return !!(new static())->where('user_id', '=', $user_id)
            ->where('status', '=', 1)
            ->where('user_id <> reg_user_id')
            ->where('activity_id', '=', $activity_id)
            ->count();
    }
 
    //判断帮报的用户是否报名
    public static function isRegByFriend($mobile, $activity_id)
    {
        return !!(new static())->where('mobile', '=', $mobile)
            ->where('status', '=', 1)
            ->where('is_friend', '=', 1)
            ->where('activity_id', '=', $activity_id)
            ->count();
    }
 
    //判断用户是否签到
    public static function isVerify($user_id, $activity_id)
    {
        return !!(new static())->where('user_id', '=', $user_id)
            ->where('is_verify', '=', 1)
            ->where('activity_id', '=', $activity_id)
            ->count();
    }
 
    /**
     * 获取某天的总报名费
     * 结束时间不传则查一天
     */
    public function getTotalPrice($startDate = null, $endDate = null, $branch_id)
    {
        $model = $this->alias('u')
                ->join('branch_member', 'u.user_id = branch_member.user_id', 'left');
 
        !is_null($startDate) && $model = $model->where('pay_time', '>=', strtotime($startDate));
 
        if (is_null($endDate)) {
            !is_null($startDate) && $model = $model->where('pay_time', '<', strtotime($startDate) + 86400);
        } else {
            $model = $model->where('pay_time', '<', strtotime($endDate) + 86400);
        }
 
        return $model->where('pay_status', '=', 20)
            ->where('branch_member.branch_id', '=', $branch_id)
            ->where('u.is_delete', '=', 0)
            ->sum('pay_price');
    }
 
    /**
     * 会员排行榜(报名活跃度)
     */
    public function getUserRankList($date, $branch_id, $limit = 7)
    {
        return $this->alias('A')
            ->join('branch_member B', 'A.user_id=B.user_id')
            ->join('user C', 'C.user_id=A.user_id')
            ->field('count(A.order_id) as total,C.nickName,C.avatarUrl,A.*')
            ->when($date, function ($query, $date) {
                getModelTime($query, $date, 'A.create_time');
            })
            ->where('B.branch_id', '=', $branch_id)
            ->where('A.is_delete', '=', 0)
            ->group('A.user_id')
            ->order('total DESC')
            ->limit($limit)
            ->select();
    }
}