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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
 
namespace app\common\model\branch;
 
use app\common\model\BaseModel;
 
/**
 * 活动模型
 */
class Activity extends BaseModel
{
    protected $name = 'branch_activity';
    protected $pk = 'activity_id';
 
    /**
     * 追加字段
     * @var string[]
     */
    protected $append = [
        'register_time_text',
        'activity_time_text',
        'status_text',
        'audit_status_text',
    ];
 
    /**
     * 关联封面图
     * @return \think\model\relation\HasOne
     */
    public function image()
    {
        return $this->hasOne('app\\common\\model\\file\\UploadFile', 'file_id', 'image_id');
    }
 
    /**
     * 关联分享海报
     * @return \think\model\relation\HasOne
     */
    public function pic()
    {
        return $this->hasOne('app\\common\\model\\file\\UploadFile', 'file_id', 'pic_id');
    }
 
    /**
     * 关联活动分类表
     * @return \think\model\relation\BelongsTo
     */
    public function category()
    {
        return $this->BelongsTo("app\\common\\model\\branch\\Category", 'category_id', 'category_id');
    }
 
    /**
     * 关联连盟
     * @return \think\model\relation\BelongsTo
     */
    public function branch()
    {
        return $this->BelongsTo("app\\common\\model\\branch\\Branch", 'branch_id', 'branch_id');
    }
 
    /**
     * 关联走访企业
     * @return \think\model\relation\BelongsTo
     */
    public function visitSupplier()
    {
        return $this->BelongsTo("app\\common\\model\\supplier\\Supplier", 'visit_supplier_id', 'shop_supplier_id');
    }
 
    /**
     * 关联提供场地企业
     * @return \think\model\relation\BelongsTo
     */
    public function spaceSupplier()
    {
        return $this->BelongsTo("app\\common\\model\\supplier\\Supplier", 'space_supplier_id', 'shop_supplier_id');
    }
 
    /**
     *报名时间
     * @param $value
     * @param $data
     * @return string
     */
    public function getRegisterTimeTextAttr($value, $data)
    {
        if(isset($data['register_start_time'])) {
            return $data['register_start_time'] ? date('Y-m-d H:i', $data['register_start_time']).' - ' . date('Y-m-d H:i', $data['register_end_time']) : '';
        }
        return '';
    }
    /**
     * 活动时间
     * @param $value
     * @param $data
     * @return string
     */
    public function getActivityTimeTextAttr($value, $data)
    {
        if(isset($data['activity_start_time'])) {
            return $data['activity_start_time'] ? date('Y-m-d H:i', $data['activity_start_time']).' - ' . date('Y-m-d H:i', $data['activity_end_time']) : '';
        }
        return '';
    }
    /**
     * 活动状态
     * @param $value
     * @param $data
     * @return string
     */
    public function getStatusTextAttr($value, $data)
    {
        if (!isset($data['activity_start_time'])) {
            return '';
        }
        if (time() >= $data['activity_start_time'] && ($data['activity_end_time']) >= time()) {
            $status_text = "进行中";
            $status = 1;
        } elseif (time() > ($data['activity_end_time'])){
            $status_text = "已结束";
            $status = 2;
        } else {
            $status_text = "未开始";
            $status = 0;
        }
        if (time() >= $data['register_start_time'] && ($data['register_end_time']) >= time()) {
            $reg_status_text = "进行中";
            $reg_status = 1;
        } elseif (time() > ($data['register_end_time'])) {
            $reg_status_text = "已结束";
            $reg_status = 2;
        } else {
            $reg_status_text = "未开始";
            $reg_status = 0;
        }
        $result = [
            "status" => $status,
            "status_text" => $status_text,
            "reg_status" => $reg_status,
            "reg_status_text" => $reg_status_text,
            "act_start_time" => date("Y-m-d H:i", $data['activity_start_time']),
            "act_end_time" => date("Y-m-d H:i", $data['activity_end_time']),
            "reg_end_time" => date("Y-m-d H:i", $data['register_end_time'])
        ];
        return $result;
    }
 
    /**
     * 审核状态文本
     * @param $value
     * @param $data
     * @return string
     */
    public function getAuditStatusTextAttr($value, $data)
    {
        $status = isset($data['audit_status']) ? $data['audit_status'] : 0;
        $text = [0=>'待审核',10=> '已通过', 20=> '已驳回'];
        return [
            'status' => $status,
            'text' => $text[$status] ?? '待审核'
        ];
    }
 
    /**
     * 活动详情
     */
    public static function detail($activity_id)
    {
        return (new static())->with(['image', 'category', 'branch', 'pic', 'visitSupplier', 'spaceSupplier'])->find($activity_id);
    }
 
    /**
     * 更新报名人数
     */
    public static function incTotal($activity_id, $inc = 1)
    {
        $res = self::detail($activity_id);
        if ($res) {
            $total = $res['total'] + $inc;
            // if ($res['limit_num'] && $res['limit_num'] < $total) {
            //     $total = $res['limit_num'];
            // }
            $res->save(['total' => $total]);
        }
    }
 
    /**
     * 批量更新活动
     * @param $activityIds
     * @param $data
     * @return bool
     */
    public static function onBatchUpdate($activityIds, $data)
    {
        $model = new static();
        return $model->where('activity_id', 'in', $activityIds)->save($data);
    }
 
    /**
     * 获取活动数据 (可指定某天)
     */
    public function getActivityData($startDate = null, $endDate = null, $type, $branch_id = 0)
    {
        $model = $this;
        !is_null($startDate) && $model = $model->where('create_time', '>=', strtotime($startDate));
        if (is_null($endDate)) {
            !is_null($startDate) && $model = $model->where('create_time', '<', strtotime($startDate) + 86400);
        } else {
            $model = $model->where('create_time', '<', strtotime($endDate) + 86400);
        }
        if ($branch_id > 0) {
            $model = $model->where('branch_id', '=', $branch_id);
        }
        $model = $model->where('is_delete', '=', 0);
        if ($type == 'activity_total') {
            return $model->count();
        }
        return 0;
    }
 
    /**
     * 分会排行榜
     */
    public function getBranchRankList($date, $parent_branch_id, $limit = 7)
    {
        return $this->alias('A')
            ->join('branch B', 'A.branch_id=B.branch_id')
            ->field('count(A.activity_id) as total,B.name as branch_name')
            ->when($date, function ($query, $date) {
                getModelTime($query, $date, 'A.create_time');
            })
            ->where('B.parent_branch_id', '=', $parent_branch_id)
            ->where('A.is_delete', '=', 0)
            ->group('A.branch_id')
            ->order('total DESC')
            ->limit($limit)
            ->select();
    }
 
    /**
     * 检查走访企业30天内是否已被走访
     * @param int $visit_supplier_id 被走访企业ID
     * @param int $visit_supplier_name 被走访企业名称
     * @param int $exclude_activity_id 排除的活动ID(编辑时使用)
     * @return array|bool 返回剩余天数或false
     */
    public function checkVisitLimit($visit_supplier_id, $visit_supplier_name, $exclude_activity_id = 0)
    {
        // 查询30天内该分会是否走访过该企业
        $thirtyDaysAgo = time() - (30 * 24 * 3600);
        
        $query = $this
            ->where('is_delete', '=', 0)
            ->where('create_time', '>', $thirtyDaysAgo);
        if ($visit_supplier_id!=0){
            $query = $query->where('visit_supplier_id', '=', $visit_supplier_id);
        }elseif ($visit_supplier_name!=''){
            $query = $query->where('visit_supplier_name', '=', $visit_supplier_name);
        }
        // 如果是编辑操作,排除当前活动
        if ($exclude_activity_id > 0) {
            $query =  $query->where('activity_id', '<>', $exclude_activity_id);
        }
        
        $recentVisit = $query->order('create_time', 'desc')->find();
        
        if ($recentVisit) {
            // 计算剩余天数
            $daysSinceVisit = floor((time() - strtotime($recentVisit['create_time'])) / (24 * 3600));
            $remainingDays = 30 - $daysSinceVisit;
            return [
                'visited' => true,
                'remaining_days' => max(0, $remainingDays),
                'last_visit_time' => $recentVisit['create_time'],
                'activity_id' => $recentVisit['activity_id']
            ];
        }
        
        return false;
    }
}