sprintf("编码[%s]已经存在", $data["code"])]; $items = array_filter(explode(";", $values)); $id = Dict::table('sys_dict')->insertGetId($data); $childs = []; for ($i = 0; $i < count($items); $i++) { $fields = array_filter(explode(":", $items[$i])); $childs[] = ["pid" => $id, "code" => $fields[0], "name" => $fields[1], "num" => $fields[2]]; } Dict::insertAll($childs); return ["code" => 200, "msg" => "成功"]; } public static function isExistByCode($code, $id = 0) { $where = []; if ($id > 0) { $where[] = ["id", "<>", $id]; } $where[] = ["code", "=", $code]; $dict = Dict::where($where)->findOrEmpty()->toArray(); return $dict; } /** * 获取一条 * @param type $id * @return type */ public static function getOne($id) { $info = Dict::where("id", $id)->findOrEmpty()->toArray(); if ($info) { $items = Dict::where("pid", "=", $id)->select()->toArray(); $info["children"] = $items; } return $info; } /** * 获取列表 * @param type $request * @return type */ public static function getList($request) { $order = trim($request->param("order")) ?: "desc"; $offset = trim($request->param("offset")) ?: 0; $limit = trim($request->param("limit")) ?: 10; $name = trim($request->param("condition")); $where = []; $where[] = ["pid", "=", "0"]; if ($name) { $where[] = ["name", "like", "%" . $name . "%"]; } $count = Dict::where($where)->count(); $list = Dict::where($where)->limit($offset, $limit)->order("id " . $order)->select()->toArray(); foreach ($list as &$item) { $items = Dict::where("pid", "=", $item["id"])->column("name"); $item["detail"] = implode(",", $items); } return ["total" => $count, "rows" => $list]; } /** * * @param type $code * @return type */ public static function selectByParentCode($code) { $parent = Dict::where("code", $code)->findOrEmpty()->toArray(); $dictList = Dict::where("pid", $parent["id"])->select()->toArray(); $tmp = []; foreach ($dictList as $dict) { $tmp[$dict["code"]] = $dict["name"]; } return $tmp; } }