DictApi.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace app\common\api;
  3. use app\common\model\Dict;
  4. use think\Facade\Db;
  5. /**
  6. * Description of DictApi
  7. *
  8. * @author sgq
  9. */
  10. class DictApi {
  11. public static function create($params) {
  12. $data["code"] = $params["dictCode"];
  13. $data["name"] = $params["dictName"];
  14. $data["tips"] = $params["dictTips"];
  15. $data["pid"] = 0;
  16. $values = $params["dictValues"];
  17. if (self::isExistByCode($data["code"]))
  18. return ["msg" => sprintf("编码[%s]已经存在", $data["code"])];
  19. $items = array_filter(explode(";", $values));
  20. $id = Dict::table('sys_dict')->insertGetId($data);
  21. $childs = [];
  22. for ($i = 0; $i < count($items); $i++) {
  23. $fields = array_filter(explode(":", $items[$i]));
  24. $childs[] = ["pid" => $id, "code" => $fields[0], "name" => $fields[1], "num" => $fields[2]];
  25. }
  26. Dict::insertAll($childs);
  27. return ["code" => 200, "msg" => "成功"];
  28. }
  29. public static function isExistByCode($code, $id = 0) {
  30. $where = [];
  31. if ($id > 0) {
  32. $where[] = ["id", "<>", $id];
  33. }
  34. $where[] = ["code", "=", $code];
  35. $dict = Dict::where($where)->findOrEmpty()->toArray();
  36. return $dict;
  37. }
  38. /**
  39. * 获取一条
  40. * @param type $id
  41. * @return type
  42. */
  43. public static function getOne($id) {
  44. $info = Dict::where("id", $id)->findOrEmpty()->toArray();
  45. if ($info) {
  46. $items = Dict::where("pid", "=", $id)->select()->toArray();
  47. $info["children"] = $items;
  48. }
  49. return $info;
  50. }
  51. /**
  52. * 获取列表
  53. * @param type $request
  54. * @return type
  55. */
  56. public static function getList($request) {
  57. $order = trim($request->param("order")) ?: "desc";
  58. $offset = trim($request->param("offset")) ?: 0;
  59. $limit = trim($request->param("limit")) ?: 10;
  60. $name = trim($request->param("condition"));
  61. $where = [];
  62. $where[] = ["pid", "=", "0"];
  63. if ($name) {
  64. $where[] = ["name", "like", "%" . $name . "%"];
  65. }
  66. $count = Dict::where($where)->count();
  67. $list = Dict::where($where)->limit($offset, $limit)->order("id " . $order)->select()->toArray();
  68. foreach ($list as &$item) {
  69. $items = Dict::where("pid", "=", $item["id"])->column("name");
  70. $item["detail"] = implode(",", $items);
  71. }
  72. return ["total" => $count, "rows" => $list];
  73. }
  74. /**
  75. *
  76. * @param type $code
  77. * @return type
  78. */
  79. public static function selectByParentCode($code) {
  80. $parent = Dict::where("code", $code)->findOrEmpty()->toArray();
  81. $dictList = Dict::where("pid", $parent["id"])->select()->toArray();
  82. $tmp = [];
  83. foreach ($dictList as $dict) {
  84. $tmp[$dict["code"]] = $dict["name"];
  85. }
  86. return $tmp;
  87. }
  88. }