<?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;
|
}
|
}
|
}
|