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