123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019-04-28
- * Time: 15:49
- */
- namespace App\Repositories;
- use App\Shop;
- use App\ShopAccount;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Hash;
- use League\Flysystem\Exception;
- class ShopRepository {
- public function __construct() {
- //$this->shop = $shop;
- }
- //新增
- public function addShopAccount($data = []){
- $accountData = ['account'=>$data['account'],'password'=>Hash::make($data['password'])];
- unset($data['account']);
- unset($data['password']);
- $shopData = $data;
- try{
- $shop = Shop::create($shopData);
- if($shop){
- $accountData['shop_id'] = $shop->shop_id;
- ShopAccount::create($accountData);
- }
- return true;
- }catch (Exception $exception){
- return false;
- }
- }
- //修改
- public function saveShopAccount($data = []){
- $shop_id = isset($data['shop_id'])?$data['shop_id']:0;
- $shop_account_id = isset($data['shop_account_id'])?$data['shop_account_id']:0;
- unset($data['shop_id']);
- unset($data['shop_account_id']);
- $shopData = $data;
- try{
- $shop = Shop::create($shopData);
- $accountData = [];
- if($shop){
- $accountData['shop_id'] = $shop->shop_id;
- }
- if(isset($data['account']) && isset($data['password'])){
- $accountData['account'] = $data['account'];
- $accountData['password'] = Hash::make($data['password']);
- ShopAccount::create($accountData);
- }
- return true;
- }catch (Exception $exception){
- return false;
- }
- }
- //修改
- public function editShopAccount($data = []){
- $shop_id = $data['shop_id'];
- $shop_account_id = $data['shop_account_id'];
- $account = $data['account'];
- $password = $data['password'];
- unset($data['password']);
- unset($data['shop_id']);
- unset($data['shop_account_id']);
- unset($data['account']);
- DB::beginTransaction();
- try{
- $shop = Shop::where(['shop_id'=>$shop_id])->update($data);
- $accountData = [];
- if(!empty($account) && !empty($password)){
- $accountData['shop_id'] = $shop_id;
- $accountData['account'] = $account;
- $accountData['password'] = Hash::make($password);
- ShopAccount::where(['id'=>$shop_account_id])->update($accountData);
- }
- DB::commit();
- return true;
- }catch (Exception $exception){
- DB::rollBack();
- return false;
- }
- }
- public function shopList($request)
- {
- $where = [];
- if (isset($request['status'])) {
- $where[] = ['status', '=', $request['status']];
- }
- if (isset($request['shop_name'])) {
- $where[] = ['shop_name', 'like', '%'.$request['shop_name'].'%'];
- }
- $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
- return Shop::where($where)
- ->orderBy('shop_id', 'desc')
- ->paginate($perPage);
- }
- }
|