LinkService.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\Link;
  4. use Hyperf\RpcServer\Annotation\RpcService;
  5. use App\Tools\Result;
  6. #[RpcService(name: "LinkService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  7. class LinkService implements LinkServiceInterface
  8. {
  9. /**
  10. * @param array $data
  11. * @return array
  12. */
  13. public function getLinkList(array $data): array
  14. {
  15. $where = [];
  16. if(isset($data['keyWord'])){
  17. $where = [
  18. ['link.title','like','%'.$data['keyWord'].'%']
  19. ];
  20. }
  21. $result = [];
  22. if(isset($data['pageSize'])){
  23. $rep = Link::where($where)
  24. ->leftJoin("website","website.id","link.website_id")
  25. ->select("link.*","website.website_name")
  26. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])->orderBy("sort","asc")->get();
  27. $count = Link::where($where)->count();
  28. $result = [
  29. 'rows'=>$rep,
  30. 'count'=>$count
  31. ];
  32. }else{
  33. $result = Link::where($data)->orderBy("sort","asc")->get();
  34. }
  35. return $result?Result::success($result):Result::error("没有查到数据");
  36. }
  37. /**
  38. * @param array $data
  39. * @return array
  40. */
  41. public function createLink(array $data): array
  42. {
  43. $result = Link::insertGetId($data);
  44. if (empty($result)) {
  45. return Result::error("创建失败", 0);
  46. } else {
  47. return Result::success(["id" => $result]);
  48. }
  49. }
  50. /**
  51. * @param array $data
  52. * @return array
  53. */
  54. public function updateLink(array $data): array
  55. {
  56. //website_name` = '三农市场网', `status_name` =
  57. unset($data['website_name']);
  58. unset($data['status_name']);
  59. $result = Link::where('id', $data['id'])->update($data);
  60. if (empty($result)) {
  61. return Result::error("更新失败", 0);
  62. } else {
  63. return Result::success();
  64. }
  65. }
  66. /**
  67. * @param int $id
  68. * @return array
  69. */
  70. public function delLink(int $id): array
  71. {
  72. $result = Link::where('id', $id)->delete();
  73. if (empty($result)) {
  74. return Result::error("删除失败", 0);
  75. } else {
  76. return Result::success();
  77. }
  78. }
  79. }