ShopRepository.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019-04-28
  6. * Time: 15:49
  7. */
  8. namespace App\Repositories;
  9. use App\Shop;
  10. use App\ShopAccount;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Hash;
  13. use League\Flysystem\Exception;
  14. class ShopRepository {
  15. public function __construct() {
  16. //$this->shop = $shop;
  17. }
  18. //新增
  19. public function addShopAccount($data = []){
  20. $accountData = ['account'=>$data['account'],'password'=>Hash::make($data['password'])];
  21. unset($data['account']);
  22. unset($data['password']);
  23. $shopData = $data;
  24. try{
  25. $shop = Shop::create($shopData);
  26. if($shop){
  27. $accountData['shop_id'] = $shop->shop_id;
  28. ShopAccount::create($accountData);
  29. }
  30. return true;
  31. }catch (Exception $exception){
  32. return false;
  33. }
  34. }
  35. //修改
  36. public function saveShopAccount($data = []){
  37. $shop_id = isset($data['shop_id'])?$data['shop_id']:0;
  38. $shop_account_id = isset($data['shop_account_id'])?$data['shop_account_id']:0;
  39. unset($data['shop_id']);
  40. unset($data['shop_account_id']);
  41. $shopData = $data;
  42. try{
  43. $shop = Shop::create($shopData);
  44. $accountData = [];
  45. if($shop){
  46. $accountData['shop_id'] = $shop->shop_id;
  47. }
  48. if(isset($data['account']) && isset($data['password'])){
  49. $accountData['account'] = $data['account'];
  50. $accountData['password'] = Hash::make($data['password']);
  51. ShopAccount::create($accountData);
  52. }
  53. return true;
  54. }catch (Exception $exception){
  55. return false;
  56. }
  57. }
  58. //修改
  59. public function editShopAccount($data = []){
  60. $shop_id = $data['shop_id'];
  61. $shop_account_id = $data['shop_account_id'];
  62. $account = $data['account'];
  63. $password = $data['password'];
  64. unset($data['password']);
  65. unset($data['shop_id']);
  66. unset($data['shop_account_id']);
  67. unset($data['account']);
  68. DB::beginTransaction();
  69. try{
  70. $shop = Shop::where(['shop_id'=>$shop_id])->update($data);
  71. $accountData = [];
  72. if(!empty($account) && !empty($password)){
  73. $accountData['shop_id'] = $shop_id;
  74. $accountData['account'] = $account;
  75. $accountData['password'] = Hash::make($password);
  76. ShopAccount::where(['id'=>$shop_account_id])->update($accountData);
  77. }
  78. DB::commit();
  79. return true;
  80. }catch (Exception $exception){
  81. DB::rollBack();
  82. return false;
  83. }
  84. }
  85. public function shopList($request)
  86. {
  87. $where = [];
  88. if (isset($request['status'])) {
  89. $where[] = ['status', '=', $request['status']];
  90. }
  91. if (isset($request['shop_name'])) {
  92. $where[] = ['shop_name', 'like', '%'.$request['shop_name'].'%'];
  93. }
  94. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  95. return Shop::where($where)
  96. ->orderBy('shop_id', 'desc')
  97. ->paginate($perPage);
  98. }
  99. }