ShopRepository.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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\Hash;
  12. use League\Flysystem\Exception;
  13. class ShopRepository {
  14. public function __construct() {
  15. //$this->shop = $shop;
  16. }
  17. //新增
  18. public function addShopAccount($data = []){
  19. $accountData = ['account'=>$data['account'],'password'=>Hash::make($data['password'])];
  20. unset($data['account']);
  21. unset($data['password']);
  22. $shopData = $data;
  23. try{
  24. $shop = Shop::create($shopData);
  25. if($shop){
  26. $accountData['shop_id'] = $shop->shop_id;
  27. ShopAccount::create($accountData);
  28. }
  29. return true;
  30. }catch (Exception $exception){
  31. return false;
  32. }
  33. }
  34. //修改
  35. public function saveShopAccount($data = []){
  36. $shop_id = isset($data['shop_id'])?$data['shop_id']:0;
  37. $shop_account_id = isset($data['shop_account_id'])?$data['shop_account_id']:0;
  38. unset($data['shop_id']);
  39. unset($data['shop_account_id']);
  40. $shopData = $data;
  41. try{
  42. $shop = Shop::updateOrCreate(['shop_id'=>$shop_id],$shopData);
  43. $accountData = [];
  44. if($shop){
  45. $accountData['shop_id'] = $shop->shop_id;
  46. }
  47. if(isset($data['account']) && isset($data['password'])){
  48. $accountData['account'] = $data['account'];
  49. $accountData['password'] = Hash::make($data['password']);
  50. ShopAccount::updateOrCreate(['shop_id'=>$shop->shop_id],$accountData);
  51. }
  52. return true;
  53. }catch (Exception $exception){
  54. return false;
  55. }
  56. }
  57. public function shopList($request)
  58. {
  59. $where = [];
  60. if (isset($request['status'])) {
  61. $where[] = ['status', '=', $request['uid']];
  62. }
  63. if (isset($request['shop_name'])) {
  64. $where[] = ['shop_name', 'like', '%'.$request['username'].'%'];
  65. }
  66. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  67. // $where = $this->memberWhereList($request);
  68. return Shop::where($where)
  69. ->orderBy('shop_id', 'desc')
  70. ->paginate($perPage);
  71. }
  72. }