ConfigCityManagementRepository.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. if(isset($request['express_type'])){
  21. if ($request['express_type'] == 1){
  22. $where[] = ['express_type', '!=', 2];//返回含有自提的
  23. }elseif ($request['express_type'] == 2){
  24. $where[] = ['express_type', '!=', 1];//返回含有快递的
  25. }
  26. }
  27. if(isset($request['status'])){
  28. $where[] = ['status', '=', $request['status']];
  29. }
  30. return $this->configCityManagement->where($where)->orderBy('id', 'asc')->paginate($perPage);
  31. }
  32. public function create($request)
  33. {
  34. if($this->configCityManagement->where('city_id', $request['city_id'])->exists()){
  35. throw new HttpException(500, '该城市已经存在');
  36. }
  37. $data = [
  38. 'province_id' => $request['province_id'],
  39. 'province_name' => $request['province_name'],
  40. 'city_id' => $request['city_id'],
  41. 'city_name' => $request['city_name'],
  42. 'express_type' => $request['express_type'],
  43. 'status' => $request['status']?$request['status']:0,
  44. ];
  45. if (!$this->configCityManagement->create($data)) {
  46. throw new HttpException(500, '添加失败');
  47. }
  48. }
  49. public function edit($request)
  50. {
  51. $configCity = $this->configCityManagement->where('id', $request['id'])->first();
  52. $configCity->province_id = $request['province_id'];
  53. $configCity->province_name = $request['province_name'];
  54. $configCity->city_id = $request['city_id'];
  55. $configCity->city_name = $request['city_name'];
  56. $configCity->express_type = $request['express_type'];
  57. $configCity->status = $request['status']?$request['status']:0;
  58. DB::beginTransaction();
  59. try {
  60. $res = $configCity->save();
  61. if ($res) {
  62. DB::commit();
  63. return Response::create();
  64. }
  65. DB::rollBack();
  66. return Response::create([
  67. 'message' => '编辑失败,请重试',
  68. 'status_code' => 500
  69. ]);
  70. } catch (QueryException $exception) {
  71. DB::rollBack();
  72. return Response::create([
  73. 'message' => '编辑失败,请重试',
  74. 'error' => $exception->getMessage(),
  75. 'status_code' => 500
  76. ]);
  77. }
  78. }
  79. public function delete($request)
  80. {
  81. $configCity = $this->configCityManagement->where('id', $request['id'])->first();
  82. DB::beginTransaction();
  83. try{
  84. $res = $configCity->delete();
  85. if($res){
  86. DB::commit();
  87. return Response::create();
  88. }
  89. DB::rollBack();
  90. return Response::create([
  91. 'message' => '删除失败,请重试',
  92. 'status_code' => 500
  93. ]);
  94. }catch (QueryException $exception){
  95. DB::rollBack();
  96. return Response::create([
  97. 'message' => '删除失败,请重试',
  98. 'error' => $exception->getMessage(),
  99. 'status_code' => 500
  100. ]);
  101. }
  102. }
  103. }