FloorRepository.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Repositories\V2;
  3. use App\Models\Floor;
  4. use Symfony\Component\HttpKernel\Exception\HttpException;
  5. use Dingo\Api\Http\Response;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Database\QueryException;
  8. /**
  9. * Created by PhpStorm.
  10. * User: durong
  11. * Date: 2019/6/3
  12. * Time: 下午2:36
  13. */
  14. class FloorRepository
  15. {
  16. public function __construct(Floor $floor)
  17. {
  18. $this->floor = $floor;
  19. }
  20. public function index()
  21. {
  22. return $this->floor->paginate(20);
  23. }
  24. public function create($request)
  25. {
  26. $data = [
  27. 'name' => $request['name'],
  28. 'floor_location' => $request['floor_location'],
  29. 'group_ids' => $request['group_ids'],//多个banner用英文,隔开
  30. 'floor_type' => $request['floor_type'],
  31. 'is_open' => $request['is_open']
  32. ];
  33. if (!$this->floor->create($data)) {
  34. throw new HttpException(500, '添加失败');
  35. }
  36. }
  37. public function edit($request)
  38. {
  39. $floor = $this->floor->where('id', $request['id'])->first();
  40. $floor->name = $request['name'];
  41. $floor->floor_location = $request['floor_location'];
  42. $floor->group_ids = $request['group_ids'];
  43. $floor->floor_type = $request['floor_type'];
  44. $floor->is_open = $request['is_open'] ?? 0;
  45. DB::beginTransaction();
  46. try{
  47. $res = $floor->save();
  48. if($res){
  49. DB::commit();
  50. return Response::create();
  51. }
  52. DB::rollBack();
  53. return Response::create([
  54. 'message' => '编辑失败,请重试',
  55. 'status_code' => 500
  56. ]);
  57. }catch (QueryException $exception){
  58. DB::rollBack();
  59. return Response::create([
  60. 'message' => '编辑失败,请重试',
  61. 'error' => $exception->getMessage(),
  62. 'status_code' => 500
  63. ]);
  64. }
  65. }
  66. public function editStatus($request)
  67. {
  68. $floor = $this->floor->find($request['id']);
  69. $floor->is_open = $request['is_open'];
  70. $floor->updated_at = date('Y-m-d H:i:s');
  71. $res = $floor->save();
  72. if (!$res) {
  73. throw new HttpException(500, '修改状态失败');
  74. }
  75. }
  76. public function floorBind($request)
  77. {
  78. $banner_id = $request['banner_id'];
  79. $floor_array = $this->floor->select('id','group_ids')->where('floor_type',0)->get();
  80. $floor_ids = [];
  81. foreach ($floor_array->toArray() as $value){
  82. if ($banner_id == $value['group_ids']){
  83. $floor_ids[] = $value['id'];
  84. }
  85. }
  86. if (count($floor_ids)>0){
  87. $edit_floor = $this->floor->whereIn('id',$floor_ids)->update(['is_open'=>0]);
  88. if (!$edit_floor){
  89. throw new HttpException(500, '该楼层关闭失败');
  90. }
  91. }
  92. }
  93. }