UserHonor.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model\api;
  4. use think\Model;
  5. /**
  6. * 用户荣誉定义表模型
  7. * @mixin \think\Model
  8. */
  9. class UserHonor extends Model
  10. {
  11. /**
  12. * 获取荣誉列表
  13. * @param array $post
  14. * @param string $field
  15. * @return array
  16. */
  17. public function getList($post, $field = "*")
  18. {
  19. $post["pageSize"] = $post["pageSize"] > 50 ? 50 : (int)$post["pageSize"];
  20. $post["page"] = $post["page"] <= 0 ? 1 : (int)$post["page"];
  21. $where = [];
  22. if (isset($post["status"]) && in_array((string)$post["status"], ["0", "1"])) {
  23. $where[] = ["status", "=", (int)$post["status"]];
  24. }
  25. if (!empty($post["name"])) {
  26. $where[] = ["name", "like", "%{$post["name"]}%"];
  27. }
  28. $totalCount = $this->where($where)->count();
  29. $data = null;
  30. if ($totalCount > 0) {
  31. $data = $this
  32. ->field($field)
  33. ->where($where)
  34. ->order("sort", "desc")
  35. ->order("id", "desc")
  36. ->page($post["page"], $post["pageSize"])
  37. ->select();
  38. if (!empty($data)) {
  39. $data = $data->toArray();
  40. }
  41. }
  42. $data = empty($data) ? [] : $data;
  43. return ["list" => $data, "pageSize" => $post["pageSize"], "page" => $post["page"], "totalCount" => $totalCount];
  44. }
  45. /**
  46. * 获取所有启用的荣誉(用于下拉选择)
  47. * @return array
  48. */
  49. public function getAllEnabled()
  50. {
  51. $data = $this
  52. ->field("id,name,icon")
  53. ->where("status", 1)
  54. ->order("sort", "desc")
  55. ->order("id", "desc")
  56. ->select();
  57. if (!empty($data)) {
  58. $data = $data->toArray();
  59. }
  60. return empty($data) ? [] : $data;
  61. }
  62. }