quanwei
2 days ago 04102f7237efefa744090ed7c25f7b5d0807b679
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
<?php
 
namespace app\shop\model\supplier;
 
use app\common\model\supplier\member\Plan as MemberPlanModel;
 
/**
 * 供应商年卡套餐模型
 */
class Plan extends MemberPlanModel
{
    /**
     * 获取列表记录
     */
    public function getList($data)
    {
        $model = $this;
        if (!empty($data['name'])) {
            $model = $model->where('name', 'like', '%' . $data['name'] . '%');
        }
        if ($data['status'] >= 0) {
            $model = $model->where('status', '=', $data['status']);
        }
        $list = $model->with('plan_access')
            ->where(['is_delete'=>0])
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->paginate($data);
        if (empty($list)) {
            $addDate=[
                'name' => '默认套餐',
                'price' => 0,
                'sort' => 0,
                'status' => 1,
                'is_default' => 1,
                'app_id' => self::$app_id,
            ];
            $this->save($addDate);
            $list = $model->with('plan_access')
                ->order(['sort' => 'asc', 'create_time' => 'desc'])
                ->paginate($data);
        }
 
        return $list;
    }
 
    /**
     * 新增记录
     */
    public function add($data)
    {
        $data['app_id'] = self::$app_id;
        $this->transaction(function () use ($data) {
           $this->save($data);
           $this->addPlanAccess($data['access_ids']);
        });
        return $this;
    }
 
    /**
     * 编辑记录
     */
    public function edit($data)
    {
        $this->transaction(function () use ($data) {
            $this->save($data);
            $this->addPlanAccess($data['access_ids']);
        });
        return $this;
    }
    private function addPlanAccess($list)
    {
        $this->planAccess()->where('plan_id', $this->plan_id)->delete();
        $list = json_decode($list);
        $data=array_map(function($item){
            return [
                'access_id' => $item,
                'app_id' => self::$app_id,
            ];
        },$list);
        return $this->planAccess()->saveAll($data);
    }
    /**
     * 软删除
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }
}