DictApi.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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::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 update($params) {
  30. $data["id"] = $params["dictId"];
  31. $data["code"] = $params["dictCode"];
  32. $data["name"] = $params["dictName"];
  33. $data["tips"] = $params["dictTips"];
  34. $data["pid"] = 0;
  35. $values = $params["dictValues"];
  36. if (self::isExistByCode($data["code"], $params["dictId"]))
  37. return ["msg" => sprintf("编码[%s]已经存在", $data["code"])];
  38. $items = array_filter(explode(";", $values));
  39. $id = Dict::table('sys_dict')->save($data);
  40. //删除原来的子项
  41. Dict::where(["pid" => $params["dictId"]])->delete();
  42. $childs = [];
  43. for ($i = 0; $i < count($items); $i++) {
  44. $fields = array_filter(explode(":", $items[$i]));
  45. $childs[] = ["pid" => $params["dictId"], "code" => $fields[0], "name" => $fields[1], "num" => $fields[2]];
  46. }
  47. Dict::insertAll($childs);
  48. return ["code" => 200, "msg" => "成功"];
  49. }
  50. public static function delete($id) {
  51. Dict::where(["id" => $id])->delete();
  52. Dict::where(["pid" => $id])->delete();
  53. return ["code" => 200, "msg" => "成功"];
  54. }
  55. public static function isExistByCode($code, $id = 0) {
  56. $where = [];
  57. if ($id > 0) {
  58. $where[] = ["id", "<>", $id];
  59. }
  60. $where[] = ["code", "=", $code];
  61. $dict = Dict::where($where)->findOrEmpty()->toArray();
  62. return $dict;
  63. }
  64. /**
  65. * 获取一条
  66. * @param type $id
  67. * @return type
  68. */
  69. public static function getOne($id) {
  70. $info = Dict::where("id", $id)->findOrEmpty()->toArray();
  71. if ($info) {
  72. $items = Dict::where("pid", "=", $id)->select()->toArray();
  73. $info["children"] = $items;
  74. }
  75. return $info;
  76. }
  77. /**
  78. * 获取列表
  79. * @param type $request
  80. * @return type
  81. */
  82. public static function getList($request) {
  83. $order = trim($request->param("order")) ?: "desc";
  84. $offset = trim($request->param("offset")) ?: 0;
  85. $limit = trim($request->param("limit")) ?: 10;
  86. $name = trim($request->param("condition"));
  87. $where = [];
  88. $where[] = ["pid", "=", "0"];
  89. if ($name) {
  90. $where[] = ["name", "like", "%" . $name . "%"];
  91. }
  92. $count = Dict::where($where)->count();
  93. $list = Dict::where($where)->limit($offset, $limit)->order("id " . $order)->select()->toArray();
  94. foreach ($list as &$item) {
  95. $items = Dict::where("pid", "=", $item["id"])->column("name");
  96. $item["detail"] = implode(",", $items);
  97. }
  98. return ["total" => $count, "rows" => $list];
  99. }
  100. /**
  101. *
  102. * @param type $code
  103. * @return type
  104. */
  105. public static function selectByParentCode($code) {
  106. $parent = Dict::where("code", $code)->findOrEmpty()->toArray();
  107. $dictList = Dict::where("pid", $parent["id"])->select()->toArray();
  108. $tmp = [];
  109. foreach ($dictList as $dict) {
  110. $tmp[$dict["code"]] = $dict["name"];
  111. }
  112. return $tmp;
  113. }
  114. /**
  115. *
  116. * @param type $code
  117. * @return type
  118. */
  119. public static function findChildDictByCode($code) {
  120. $parent = Dict::where("code", $code)->findOrEmpty()->toArray();
  121. $dictList = Dict::where("pid", $parent["id"])->select()->toArray();
  122. return $dictList;
  123. }
  124. }