quanwei
2025-11-01 121b714d710cf3c865f4a1b5efe81abec11056d1
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
<?php
 
namespace app\branch\model\activity;
 
use app\common\model\branch\Activity as ActivityModel;
use app\common\model\branch\ActivityProduct as ActivityProductModel;
 
/**
 * 活动模型
 */
class Activity extends ActivityModel
{
    /**
     * 获取列表
     */
    public function getList($params, $branch_id)
    {
        return $this->with(['image', 'category'])
            ->where('branch_id', '=', $branch_id)
            ->where('is_delete', '=', 0)
            ->order(['sort' => 'desc', 'create_time' => 'desc'])
            ->paginate($params);
 
    }
 
    /**
     * 新增记录
     */
    public function add($data, $branch_id)
    {
        $model = new ActivityModel();
        $product = new ActivityProductModel();
        $data['app_id'] = self::$app_id;
        $data['branch_id'] = $branch_id;
        //报名时间
        $data['register_start_time'] = strtotime($data['reg_date'][0]);
        $data['register_end_time'] = strtotime($data['reg_date'][1]);
        //活动时间
        $data['activity_start_time'] = strtotime($data['act_date'][0]);
        $data['activity_end_time'] = strtotime($data['act_date'][1]);
        // 格式化坐标信息
        $coordinate = explode(',', $data['coordinate']);
        $data['latitude'] = $coordinate[0];
        $data['longitude'] = $coordinate[1];
        $product_list = $data['product_list']; // 促销商品
        $model->startTrans();
        try {
            $res = $model->save($data);
            if(!$res){
                $this->rollback();
                $this->error = '活动数据保存出错';
                return false;
            }
            if ($product_list) {
                $activity_id = $model->activity_id;
                foreach ($product_list as &$item){
                    $item['app_id'] = $data['app_id'];
                    $item['shop_supplier_id'] = $data['visit_supplier_id'];
                    $item['activity_id'] = $activity_id;
                }
                unset($item);
                $res1 = $product->saveAll($product_list);
                if(!$res1){
                    $this->rollback();
                    $this->error = '促销商品保存出错';
                    return false;
                }
            }
            // 提交事务
            $this->commit();
           return true;
        } catch (\Exception $e) {
            // 回滚事务
            $this->rollback();
            $this->error = $e->getMessage();
           return false;
        }
    }
 
    /**
     * 更新记录
     */
    public function edit($data)
    {
        $model = new ActivityModel();
        $product = new ActivityProductModel();
        //报名时间
        $data['register_start_time'] = strtotime($data['reg_date'][0]);
        $data['register_end_time'] = strtotime($data['reg_date'][1]);
        //活动时间
        $data['activity_start_time'] = strtotime($data['act_date'][0]);
        $data['activity_end_time'] = strtotime($data['act_date'][1]);
        // 格式化坐标信息
        $coordinate = explode(',', $data['coordinate']);
        $data['latitude'] = $coordinate[0];
        $data['longitude'] = $coordinate[1];
        $product_list = $data['product_list']; // 促销商品
        unset($data['create_time'], $data['update_time']);
        $model->startTrans();
        try {
            $res = $model->update($data, ['activity_id' => $data['activity_id']]);
            if(!$res){
                $this->rollback();
                $this->error = '活动数据保存出错';
                return false;
            }
            // 先软删除原有的促销商品
            $product->where('activity_id', '=', $data['activity_id'])->update(['is_delete' => 1]);
            if ($product_list) {
                foreach ($product_list as $key => &$item){
                    $item['app_id'] = $data['app_id'];
                    $item['shop_supplier_id'] = $data['visit_supplier_id'];
                    $item['activity_id'] = $data['activity_id'];
                    $item['is_delete'] = 0;
                }
                unset($item);
                $res1 = $product->saveAll($product_list);
                if(!$res1){
                    $this->rollback();
                    $this->error = '商品数据保存出错';
                    return false;
                }
            }
            // 提交事务
            $this->commit();
           return true;
        } catch (\Exception $e) {
            // 回滚事务
            $this->rollback();
            $this->error = $e->getMessage();
           return false;
        }
    }
 
    /**
     * 软删除
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }
 
    /**
     * 获取活动总数量
     */
    public static function getActivityTotal($where)
    {
        $model = new static;
        return $model->where($where)->where('is_delete', '=', 0)->count();
    }
 
    /**
     * 开启禁止
     */
    public function setStatus($status)
    {   
        // 开启事务
        $this->startTrans();
        try {
            //更改分会状态
            $this->save(['status' => $status]); 
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    public function activityGroupNum($date, $branch_id)
    {
        $field = 'count(*) as total,from_unixtime(create_time,\'%m-%d\') as `day`';
        if ($date == 'year') {
            $field = 'count(*) as total,from_unixtime(create_time,\'%m\') as `day`';
        }
        $model = $this->field($field)
            ->where('is_delete', '=', 0)
            ->when($date, function ($query, $date) {
                getModelTime($query, $date, 'create_time');
            })->when($branch_id, function ($query, $branch_id) {
                $query->where('branch_id', $branch_id);
            });
        return $model->order('create_time ASC')->group('day')->select();
    }
}