DiarySleepFacadeRepository.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace app\Repositories\Eloquent;
  3. use App\Repositories\Eloquent\BaseRepository;
  4. use App\Repositories\Contracts\DiarySleepInterface;
  5. use App\Models\DiarySleep;
  6. /**
  7. *
  8. * @author lilin
  9. *
  10. */
  11. class DiarySleepFacadeRepository extends BaseRepository implements DiarySleepInterface
  12. {
  13. public function getList(array $conditions, array $fields, string $sort, int $page, int $limit)
  14. {
  15. $result = DiarySleep::with(['level'])->where(function($query) use($conditions){
  16. if (isset($conditions['uid'])){
  17. $query->where('uid', $conditions['uid']);
  18. }
  19. })->orderByRaw($sort)->paginate($limit, $fields);
  20. $result->each(function($item){
  21. $item->day = date('m月d日', strtotime($item->day));
  22. $item->start_time = date('H:i', strtotime($item->start_time));
  23. $item->end_time = date('H:i', strtotime($item->end_time));
  24. });
  25. return $this->response($result);
  26. }
  27. public function findBy(array $conditions, array $fields){
  28. $result = DiarySleep::where(function($query) use($conditions){
  29. if (isset($conditions['id'])){
  30. $query->where('id', $conditions['id']);
  31. }
  32. if (isset($conditions['uid'])){
  33. $query->where('uid', $conditions['uid']);
  34. }
  35. })->first($fields);
  36. return $result ? $this->response($result) : $this->error()->dataDoesNotExist();
  37. }
  38. public function create(array $data)
  39. {
  40. try {
  41. $result = DiarySleep::create($data);
  42. return $result ? $this->response($result) : $this->error()->fail();
  43. } catch (\Exception $e) {
  44. $this->error()->dataDoesExist('今日睡眠记录已存在!');
  45. }
  46. }
  47. public function updateBy(array $conditions, array $data)
  48. {
  49. $result = DiarySleep::where(function($query) use($conditions){
  50. if (isset($conditions['id'])){
  51. $query->where('id', $conditions['id']);
  52. }
  53. if (isset($conditions['uid'])){
  54. $query->where('uid', $conditions['uid']);
  55. }
  56. })->update($data);
  57. return $result ? $this->response($result) : $this->error()->fail();
  58. }
  59. public function deleteBy(array $conditions)
  60. {
  61. $result = DiarySleep::where(function($query) use($conditions){
  62. if (isset($conditions['id'])){
  63. $query->where('id', $conditions['id']);
  64. }
  65. if (isset($conditions['uid'])){
  66. $query->where('uid', $conditions['uid']);
  67. }
  68. })->delete();
  69. return $result ? $this->response($result) : $this->error()->fail();
  70. }
  71. }