FloorRepository.php 3.2 KB

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