Passed
Push — master ( da3a82...34c540 )
by Jianhua
03:39
created

EntityRepository::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 16
rs 9.8666
1
<?php
2
/**
3
 * @author  Eddy <[email protected]>
4
 */
5
6
namespace App\Repository\Admin;
7
8
use App\Model\Admin\Category;
9
use App\Model\Admin\Comment;
10
use App\Model\Admin\CommentOperateLog;
11
use App\Model\Admin\ContentTag;
12
use App\Model\Admin\Entity;
13
use App\Model\Admin\EntityField;
14
use App\Repository\Searchable;
15
use Illuminate\Database\Schema\Blueprint;
16
use Illuminate\Support\Facades\DB;
17
use Illuminate\Support\Facades\Schema;
18
use App\Exceptions\CreateTableException;
19
20
class EntityRepository
21
{
22
    use Searchable;
23
24
    public static function list($perPage, $condition = [])
25
    {
26
        $data = Entity::query()
27
            ->where(function ($query) use ($condition) {
28
                Searchable::buildQuery($query, $condition);
29
            })
30
            ->orderBy('id', 'desc')
31
            ->paginate($perPage);
32
        $data->transform(function ($item) {
33
            xssFilter($item);
34
            $item->editUrl = route('admin::entity.edit', ['id' => $item->id]);
35
            $item->deleteUrl = route('admin::entity.delete', ['id' => $item->id]);
36
            $item->fieldUrl = route('admin::entityField.index') . '?entity_id=' . $item->id;
37
            $item->contentUrl = route('admin::content.index', ['entity' => $item->id]);
38
            $item->commentListUrl = route('admin::comment.index', ['entity_id' => $item->id]);
39
            return $item;
40
        });
41
42
        return [
43
            'code' => 0,
44
            'msg' => '',
45
            'count' => $data->total(),
46
            'data' => $data->items(),
47
        ];
48
    }
49
50
    /**
51
     * 新增模型
52
     *
53
     * @param array $data
54
     * @param mixed $createDB
55
     * @throws CreateTableException|\Exception
56
     * @return Entity
57
     */
58
    public static function add($data, $createDB = true)
59
    {
60
        $entity = Entity::query()->create($data);
61
        try {
62
            if (!$createDB) {
63
                return $entity;
64
            }
65
66
            if (Schema::hasTable($data['table_name'])) {
67
                throw new \RuntimeException("数据库表已存在");
68
            }
69
70
            Schema::create($data['table_name'], function (Blueprint $table) {
71
                $table->increments('id');
72
                $table->timestamps();
73
                $table->engine = 'InnoDB';
74
            });
75
76
            return $entity;
77
        } catch (\Exception $e) {
78
            $entity->delete();
79
            throw new CreateTableException("创建数据库表异常");
80
        }
81
    }
82
83
    public static function update($id, $data)
84
    {
85
        return Entity::query()->where('id', $id)->update($data);
86
    }
87
88
    public static function find($id)
89
    {
90
        return Entity::query()->find($id);
91
    }
92
93
    public static function external($id)
94
    {
95
        return Entity::query()->External()->find($id);
96
    }
97
98
    public static function all()
99
    {
100
        return Entity::query()->get();
101
    }
102
103
    public static function systemMenu()
104
    {
105
        $entities = Entity::query()->where('is_show_content_manage', Entity::CONTENT_MANAGE_YES)
106
            ->pluck('name', 'id')->all();
107
        $autoMenu = [];
108
        foreach ($entities as $k => $v) {
109
            $autoMenu[] = [
110
                'url' => route('admin::content.index', ['entity' => $k]),
111
                'name' => $v,
112
                'id' => $k,
113
            ];
114
        }
115
116
        return $autoMenu;
117
    }
118
119
    public static function delete($id)
120
    {
121
        $table = Entity::query()->findOrFail($id);
122
        DB::beginTransaction();
123
124
        Schema::dropIfExists($table->table_name);
125
        Entity::destroy($id);
126
        EntityField::query()->where('entity_id', $id)->delete();
127
        Category::query()->where('model_id', $id)->delete();
128
        ContentTag::query()->where('entity_id', $id)->delete();
129
        CommentOperateLog::query()->join('comments', 'comment_operate_logs.comment_id', '=', 'comments.id')
130
            ->where('entity_id', $id)
131
            ->delete();
132
        Comment::query()->where('entity_id', $id)->delete();
133
134
        DB::commit();
135
    }
136
}
137