1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- namespace app\Repositories\Eloquent;
- use App\Repositories\Eloquent\BaseRepository;
- use App\Repositories\Contracts\DiarySleepInterface;
- use App\Models\DiarySleep;
- /**
- *
- * @author lilin
- *
- */
- class DiarySleepFacadeRepository extends BaseRepository implements DiarySleepInterface
- {
- public function getList(array $conditions, array $fields, string $sort, int $page, int $limit)
- {
- $result = DiarySleep::with(['level'])->where(function($query) use($conditions){
- if (isset($conditions['uid'])){
- $query->where('uid', $conditions['uid']);
- }
- })->orderByRaw($sort)->paginate($limit, $fields);
-
- $result->each(function($item){
- $item->day = date('m月d日', strtotime($item->day));
- $item->start_time = date('H:i', strtotime($item->start_time));
- $item->end_time = date('H:i', strtotime($item->end_time));
- });
-
- return $this->response($result);
- }
-
- public function findBy(array $conditions, array $fields){
- $result = DiarySleep::where(function($query) use($conditions){
- if (isset($conditions['id'])){
- $query->where('id', $conditions['id']);
- }
- if (isset($conditions['uid'])){
- $query->where('uid', $conditions['uid']);
- }
- })->first($fields);
- return $result ? $this->response($result) : $this->error()->dataDoesNotExist();
- }
-
- public function create(array $data)
- {
- try {
- $result = DiarySleep::create($data);
- return $result ? $this->response($result) : $this->error()->fail();
- } catch (\Exception $e) {
- $this->error()->dataDoesExist('今日睡眠记录已存在!');
- }
- }
-
- public function updateBy(array $conditions, array $data)
- {
- $result = DiarySleep::where(function($query) use($conditions){
- if (isset($conditions['id'])){
- $query->where('id', $conditions['id']);
- }
- if (isset($conditions['uid'])){
- $query->where('uid', $conditions['uid']);
- }
- })->update($data);
- return $result ? $this->response($result) : $this->error()->fail();
- }
-
- public function deleteBy(array $conditions)
- {
- $result = DiarySleep::where(function($query) use($conditions){
- if (isset($conditions['id'])){
- $query->where('id', $conditions['id']);
- }
- if (isset($conditions['uid'])){
- $query->where('uid', $conditions['uid']);
- }
- })->delete();
- return $result ? $this->response($result) : $this->error()->fail();
- }
- }
|