DeviceRepository.php 4.73 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/26
 * Time: 16:39
 */

namespace App\Repositories\Backend\Device;

use EasyWeChat\Factory;
use Illuminate\Support\Facades\DB;
use App\Exceptions\GeneralException;
use App\Modules\Repositories\Device\BaseDeviceRepository;
use App\Events\Backend\Access\Device\DeviceCreated;

class DeviceRepository extends BaseDeviceRepository
{

    private $apply_id;

    public function getForDataTable()
    {
        return $this->query()->get();
    }


    public function select($device)
    {
        $wx_config = config('wechat.official_account.default');
        $app = Factory::officialAccount($wx_config);
        $shakearound = $app->shake_around;
        $apply_id = $device->apply_id;
        $result = $shakearound->device->status((int)$apply_id);
        if (!$result) {
            throw new GeneralException(trans('exceptions.backend.device.wx_beacon_info'));
            die();
        }

        return $result;
    }

    public function create($input)
    {

        $info = $input['data'];
        //调用shake_around 类
        $wx_config = config('wechat.official_account.default');
        $app = Factory::officialAccount($wx_config);
        $shakearound = $app->shake_around;
        //获取access_token
        $accessToken = $app->access_token;
        $token = $accessToken->getToken(); // token 字符串

        $data = [
            'access_token' => $token,
            'quantity' => (int)$info['num'],
            'apply_reason' => $info['remark'],
        ];
        //发起页面申请
        $result = $shakearound->device->apply($data);

        if (!$result) {
            throw new GeneralException(trans('exceptions.backend.device.wx_beacon_error'));
        }
        $apply_id = $result['data']['apply_id'];
        $this->apply_id = (int)$apply_id;
        $device = $this->createUserStub($info);

        DB::transaction(function () use ($device, $info) {
            if ($device->save()) {
                //Attach new roles
                event(new DeviceCreated($device));

                return true;
            }

            throw new GeneralException(trans('exceptions.backend.device.create_error'));
        });

        //这里是微信查beacon插入数据库
        $b = 0;
        for ($i = 0; $i < 8; $i++) {
            $res = $shakearound->device->listByApplyId( $this->apply_id, $b, 50);
            $rows = [];
            if ($res) {
                foreach ($res['data']['devices'] as $dev) {
                    $row = [];
                    $row['apply_id'] = (int)$this->apply_id;
                    $row['device_id'] = $dev['device_id'];
                    $row['major'] = $dev['major'];
                    $row['minor'] = $dev['minor'];
                    $row['rssi'] = (int)75;
                    $row['sd'] = (int)5;
                    $row['status'] = (int)0;
                    $rows[] = $row;
                    $b = (int)$dev['device_id'];
                }
                $this->CreateBeacon($rows);
            }

        }
    }

    public function CreateBeacon($data)
    {
        $data = DB::table('beacon')->insert($data);

        if (!$data) {

            throw new GeneralException(trans('exceptions.backend.device.wx_beacon_insert'));
            return false;
        }
        return  true;

    }
    public function update(Business $business, $input)
    {


        DB::transaction(function () use ($business, $input) {
            if ($business->update($input)) {
                event(new BusinessUpdated($business));

                return true;
            }

            throw new GeneralException(trans('exceptions.backend.business.update_error'));
        });
    }


    public function delete(Spot $spot, $input)
    {

        DB::transaction(function () use ($spot, $input) {
            if ($spot->delete($input)) {
                event(new SpotDeleted($spot));

                return true;
            }

            throw new GeneralException(trans('exceptions.backend.business.update_error'));
        });

    }

    protected function createUserStub($input)
    {
        $device = self::MODEL;
        $device = new $device;
        $device->remark = $input['remark'];
        $device->num = $input['num'];
        $device->apply_id = $this->apply_id;
        return $device;
    }


    /**
     * @param  $input
     * @param  $user
     *
     * @throws GeneralException
     */
    protected function checkUserByUsername($input, $business)
    {

        if ($business->business_name != $input['business_name']) {
            if ($this->query()->where('business_name', '=', $input['business_name'])->first()) {
                throw new GeneralException(trans('exceptions.backend.business.business_name_error'));
            }
        }
    }
}