quanwei
18 hours ago c441dea81bd86bdfb12dff35821fed51f4cc91c2
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
<?php
namespace app\operations\model\plus\business;
 
use app\common\model\plus\business\Industry as CommonIndustry;
use think\facade\Cache;
 
class Industry extends CommonIndustry
{
    /**
     * 添加行业
     */
    public function add($data)
    {
        // 开启事务
        $this->startTrans();
        try {
            // 写入数据
            $this->save([
                'name' => $data['name'],
                'parent_id' => isset($data['parent_id']) ? $data['parent_id'] : 0,
                'sort' => isset($data['sort']) ? $data['sort'] : 0,
                'app_id' => self::$app_id,
                'create_time' => time(),
                'update_time' => time(),
                'status' => 1
            ]);
            // 提交事务
            $this->commit();
            // 清理缓存
            Cache::tag('cache')->clear();
            return true;
        } catch (\Exception $e) {
            // 回滚事务
            $this->rollback();
            $this->error = $e->getMessage();
            return false;
        }
    }
 
    /**
     * 编辑行业
     */
    public function edit($data)
    {
        // 检查是否将自己或子行业设为上级
        if (isset($data['parent_id']) && $data['parent_id'] > 0) {
            $subIds = $this->getSubIndustryId($this['industry_id']);
            if (in_array($data['parent_id'], $subIds)) {
                $this->error = '不能将自己或子行业设为上级';
                return false;
            }
        }
        // 开启事务
        $this->startTrans();
        try {
            // 更新数据
            $this->allowField(['name', 'parent_id', 'sort', 'update_time'])->save([
                'name' => $data['name'],
                'parent_id' => isset($data['parent_id']) ? $data['parent_id'] : 0,
                'sort' => isset($data['sort']) ? $data['sort'] : 0,
                'update_time' => time(),
            ]);
            // 提交事务
            $this->commit();
            // 清理缓存
            Cache::tag('cache')->clear();
            return true;
        } catch (\Exception $e) {
            // 回滚事务
            $this->rollback();
            $this->error = $e->getMessage();
            return false;
        }
    }
}