12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace app\common\api;
- use app\common\model\Dict;
- use think\Facade\Db;
- /**
- * Description of DictApi
- *
- * @author sgq
- */
- class DictApi {
- public static function create($params) {
- $data["code"] = $params["dictCode"];
- $data["name"] = $params["dictName"];
- $data["tips"] = $params["dictTips"];
- $data["pid"] = 0;
- $values = $params["dictValues"];
- if (self::isExistByCode($data["code"]))
- return ["msg" => 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;
- }
- }
|