DiaryLetterFacadeRepository.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace app\Repositories\Eloquent;
  3. use App\Repositories\Eloquent\BaseRepository;
  4. use App\Repositories\Contracts\DiaryLetterInterface;
  5. use App\Models\DiaryLetter;
  6. /**
  7. *
  8. * @author lilin
  9. *
  10. */
  11. class DiaryLetterFacadeRepository extends BaseRepository implements DiaryLetterInterface
  12. {
  13. public function getList(array $conditions, array $fields, string $sort, int $page, int $limit)
  14. {
  15. $result = DiaryLetter::where(function($query) use($conditions){
  16. if (isset($conditions['uid'])){
  17. $query->where('uid', $conditions['uid']);
  18. }
  19. if (isset($conditions['send_time'])){
  20. $query->where('send_time', '<=' , $conditions['send_time']);
  21. }
  22. })->orderByRaw($sort)->paginate($limit, $fields);
  23. return $this->response($result);
  24. }
  25. public function findBy(array $conditions, array $fields){
  26. $result = DiaryLetter::where(function($query) use($conditions){
  27. if (isset($conditions['id'])){
  28. $query->where('id', $conditions['id']);
  29. }
  30. if (isset($conditions['uid'])){
  31. $query->where('uid', $conditions['uid']);
  32. }
  33. })->first($fields);
  34. return $result ? $this->response($result) : $this->error()->dataDoesNotExist();
  35. }
  36. public function create(array $data)
  37. {
  38. $result = DiaryLetter::create($data);
  39. return $result ? $this->response($result) : $this->error()->fail();
  40. }
  41. public function updateBy(array $conditions, array $data)
  42. {
  43. $result = DiaryLetter::where(function($query) use($conditions){
  44. if (isset($conditions['id'])){
  45. $query->where('id', $conditions['id']);
  46. }
  47. if (isset($conditions['uid'])){
  48. $query->where('uid', $conditions['uid']);
  49. }
  50. if (isset($conditions['send_time'])){
  51. $query->where('send_time', '<=' , $conditions['send_time']);
  52. }
  53. })->update($data);
  54. return $result ? $this->response($result) : $this->error()->fail();
  55. }
  56. public function deleteBy(array $conditions)
  57. {
  58. $result = DiaryLetter::where(function($query) use($conditions){
  59. if (isset($conditions['id'])){
  60. $query->where('id', $conditions['id']);
  61. }
  62. if (isset($conditions['uid'])){
  63. $query->where('uid', $conditions['uid']);
  64. }
  65. })->delete();
  66. return $result ? $this->response($result) : $this->error()->fail();
  67. }
  68. }