sprintf("编码[%s]已经存在", $data["code"])]; $items = array_filter(explode(";", $values)); $id = 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 update($params) { $data["id"] = $params["dictId"]; $data["code"] = $params["dictCode"]; $data["name"] = $params["dictName"]; $data["tips"] = $params["dictTips"]; $data["pid"] = 0; $values = $params["dictValues"]; if (self::isExistByCode($data["code"], $params["dictId"])) return ["msg" => sprintf("编码[%s]已经存在", $data["code"])]; $items = array_filter(explode(";", $values)); $id = Dict::table('sys_dict')->save($data); //删除原来的子项 Dict::where(["pid" => $params["dictId"]])->delete(); $childs = []; for ($i = 0; $i < count($items); $i++) { $fields = array_filter(explode(":", $items[$i])); $childs[] = ["pid" => $params["dictId"], "code" => $fields[0], "name" => $fields[1], "num" => $fields[2]]; } Dict::insertAll($childs); return ["code" => 200, "msg" => "成功"]; } public static function delete($id) { Dict::where(["id" => $id])->delete(); Dict::where(["pid" => $id])->delete(); 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; } /** * * @param type $code * @return type */ public static function findChildDictByCode($code) { $parent = Dict::where("code", $code)->findOrEmpty()->toArray(); $dictList = Dict::where("pid", $parent["id"])->select()->toArray(); return $dictList; } public static function findDictByCode($code) { $dict = Dict::where("code", $code)->findOrEmpty(); return $dict; } }