AboutService.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Services;
  3. use App\Services\BaseService;
  4. use App\Models\About;
  5. class AboutService extends BaseService
  6. {
  7. public static $MODEL= About::class;
  8. /**
  9. * @description: 获取查询条件
  10. * @param {array} $search 查询内容
  11. * @return {array}
  12. */
  13. public static function getWhere(array $search = []): array
  14. {
  15. $where = [];
  16. if (isset($search['id']) && $search['id'] !== '') {
  17. $where[] = ['id', '=', $search['id']];
  18. }
  19. if (isset($search['language']) && $search['language'] !== '') {
  20. $where[] = ['language', '=', $search['language']];
  21. }
  22. if (isset($search['status']) && $search['status'] !== '') {
  23. $where[] = ['status', '=', $search['status']];
  24. }
  25. if (isset($search['title']) && $search['title'] !== '') {
  26. $where[] = ['title', 'like', '%' . $search['title'] . '%'];
  27. }
  28. return $where;
  29. }
  30. /**
  31. * @description: 查询单条数据
  32. * @param array $search
  33. * @return
  34. */
  35. public static function findOne(array $search)
  36. {
  37. return static::model()::where(static::getWhere($search))->first();
  38. }
  39. /**
  40. * @description: 查询所有数据
  41. * @param array $search
  42. * @return \Illuminate\Database\Eloquent\Collection
  43. */
  44. public static function findAll(array $search = [])
  45. {
  46. return static::model()::where(static::getWhere($search))->get();
  47. }
  48. /**
  49. * @description: 分页查询
  50. * @param array $search
  51. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  52. */
  53. public static function paginate(array $search = [])
  54. {
  55. $limit = isset($search['limit']) ? $search['limit'] : 15;
  56. $paginator = static::model()::where(static::getWhere($search))
  57. ->orderBy('updated_at', 'desc')
  58. ->paginate($limit);
  59. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  60. }
  61. /**
  62. * @description:
  63. * @param {*} $params
  64. * @return {*}
  65. */
  66. public static function submit($params = [])
  67. {
  68. $result = false;
  69. $msg['code'] = self::NOT;
  70. $msg['msg'] = '';
  71. // 2. 判断是否是更新
  72. if (!empty($params['id'])) {
  73. // 更新
  74. $info = self::findOne(['id'=>$params['id']] );
  75. if (!$info) {
  76. $msg['msg'] = '数据不存在!';
  77. }else{
  78. $result = $info->update($params);
  79. $id = $params['id'];
  80. }
  81. } else {
  82. // 创建
  83. $result = $info = self::model()::create($params);
  84. $id = $result->id;
  85. }
  86. if($result){
  87. $msg['code'] = self::YES;
  88. $msg['msg'] = '设置成功';
  89. }else{
  90. $msg['msg'] = empty($msg['msg']) ?'操作失败':$msg['msg'];
  91. }
  92. return $msg;
  93. }
  94. }