ConfigCityManagementRepository.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\ConfigCityManagement;
  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. class ConfigCityManagementRepository {
  9. public function __construct(ConfigCityManagement $configCityManagement) {
  10. $this->configCityManagement = $configCityManagement;
  11. }
  12. //列表
  13. public function index($request)
  14. {
  15. $perPage = isset($request['per_page']) ? $request['per_page'] : env('PER_PAGE');
  16. $where = [];
  17. if(isset($request['id'])){
  18. $where[] = ['id', '=', $request['id']];
  19. }
  20. return $this->configCityManagement->where($where)->orderBy('id', 'asc')->paginate($perPage);
  21. }
  22. public function create($request)
  23. {
  24. if($this->configCityManagement->where('city_id', $request['city_id'])->exists()){
  25. throw new HttpException(500, '该城市已经存在');
  26. }
  27. $data = [
  28. 'province_id' => $request['province_id'],
  29. 'province_name' => $request['province_name'],
  30. 'city_id' => $request['city_id'],
  31. 'city_name' => $request['city_name'],
  32. 'express_type' => $request['express_type'],
  33. 'status' => $request['status']?$request['status']:0,
  34. ];
  35. if (!$this->configCityManagement->create($data)) {
  36. throw new HttpException(500, '添加失败');
  37. }
  38. }
  39. public function edit($request)
  40. {
  41. $configCity = $this->configCityManagement->where('id', $request['id'])->first();
  42. $configCity->province_id = $request['province_id'];
  43. $configCity->province_name = $request['province_name'];
  44. $configCity->city_id = $request['city_id'];
  45. $configCity->city_name = $request['city_name'];
  46. $configCity->express_type = $request['express_type'];
  47. $configCity->status = $request['status']?$request['status']:0;
  48. DB::beginTransaction();
  49. try {
  50. $res = $configCity->save();
  51. if ($res) {
  52. DB::commit();
  53. return Response::create();
  54. }
  55. DB::rollBack();
  56. return Response::create([
  57. 'message' => '编辑失败,请重试',
  58. 'status_code' => 500
  59. ]);
  60. } catch (QueryException $exception) {
  61. DB::rollBack();
  62. return Response::create([
  63. 'message' => '编辑失败,请重试',
  64. 'error' => $exception->getMessage(),
  65. 'status_code' => 500
  66. ]);
  67. }
  68. }
  69. public function delete($request)
  70. {
  71. $configCity = $this->configCityManagement->where('id', $request['id'])->first();
  72. DB::beginTransaction();
  73. try{
  74. $res = $configCity->delete();
  75. if($res){
  76. DB::commit();
  77. return Response::create();
  78. }
  79. DB::rollBack();
  80. return Response::create([
  81. 'message' => '删除失败,请重试',
  82. 'status_code' => 500
  83. ]);
  84. }catch (QueryException $exception){
  85. DB::rollBack();
  86. return Response::create([
  87. 'message' => '删除失败,请重试',
  88. 'error' => $exception->getMessage(),
  89. 'status_code' => 500
  90. ]);
  91. }
  92. }
  93. }