// +---------------------------------------------------------------------- namespace app\services\system; use app\model\system\Dictionary; use app\model\system\DictionaryType; use Xin\Support\Arr; class DictionaryService{ /** * @title 获取字典 * * @return void */ public function getDictionary($request){ $param = $request->param(); $map = []; if(isset($param['code']) && $param['code']){ $map[] = ['dic_type', '=', $param['code']]; } if(isset($param['name']) && $param['name']){ $map[] = ['dic_type', '=', $param['name']]; } $list = Dictionary::where($map)->order('id desc')->paginate($request->pageConfig); return $list; } /** * @title 获取字典明细 * * @return void */ public function getDictionaryDetail($request){ $param = $request->param(); $map = []; if(isset($param['name']) && $param['name']){ $map[] = ['dic_type', '=', $param['name']]; } $list = Dictionary::where($map)->order('id desc')->select(); return $list; } /** * @title 获取字典分类 * * @param [type] $request * @return void */ public function getTree($request){ $list = DictionaryType::select()->toArray(); $tree = Arr::tree($list, null, 0, ['id'=>'id', 'parent' => 'parent_id', 'child' => 'children']); return $tree; } /** * @title 添加字段分类 * * @param [type] $request * @return void */ public function addcate($request){ $data = $request->param(); return DictionaryType::create($data); } /** * @title 修改字段分类 * * @param [type] $request * @return void */ public function editcate($request){ $data = $request->param(); $dicType = DictionaryType::find($data['id']); if($data['parent_id'] == $data['id']){ throw new \think\Exception('上级不能为自己!', 100); } //更新树下字段明细 $dic = Dictionary::where('dic_type', '=', $dicType['code'])->select(); $dicSave = []; if($dic){ foreach ($dic as $key => $value) { $dicSave[] = ['id' => $value['id'], 'dic_type' => $data['code']]; } } if($dicSave){ (new Dictionary())->saveAll($dicSave); } return $dicType->save($data); } /** * @title 删除字段分类 * * @param [type] $request * @return void */ public function deleteCategory($request){ $map = []; $id = $request->param('id'); if(is_array($id)){ $map[] = ['id', 'IN', $id]; }else{ $map[] = ['id', '=', $id]; } DictionaryType::destroy(function($q) use($map){ $q->where($map); }); } /** * @title 创建字段明细 * * @return void */ public function createDic($request){ $data = $request->param(); return Dictionary::create($data); } /** * @title 更新字典明细 * * @param [type] $request * @return void */ public function updateDic($request){ $data = $request->param(); $dic = Dictionary::find($data['id']); $dic->save($data); return $dic; } /** * @title 删除字典明细 * * @param [type] $request * @return void */ public function deleteDic($request){ $map = []; $id = $request->param('id'); if(is_array($id)){ $map[] = ['id', 'IN', $id]; }else{ $map[] = ['id', '=', $id]; } Dictionary::destroy(function($q) use($map){ $q->where($map); }); } }