WxGuideRepository.php 2.38 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/2/7
 * Time: 18:12
 */

namespace App\Repositories\Backend\WxGuide;

use App\Modules\Models\Machine\Machine;
use Illuminate\Support\Facades\DB;
use App\Modules\Repositories\WxGuide\BaseWxGuideRepository;

class WxGuideRepository extends BaseWxGuideRepository
{

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


    public function getinfo()
    {
        return $this->query()->select('wx_guide.id', 'wx_guide.expire_time', 'wx_guide.bonus', 'wx_guide.number', 'wx_guide.qrcode', 'wx_guide.created_at as ctime', 'spot.spotname', 'business.business_name', 'coupon_type.type', 'coupon_type.discount', 'coupon_type.discount_money', 'coupon_type.reason', 'customers.nick_name')
            ->leftjoin('spot', 'wx_guide.spot_id', '=', 'spot.id')
            ->leftjoin('business', 'wx_guide.business_id', '=', 'business.id')
            ->leftjoin('coupon_type', 'wx_guide.coupon_type_id', '=', 'coupon_type.id')
            ->leftjoin('customers', 'wx_guide.user_id', '=', 'customers.id')
            ->get();

    }

    protected function createUserStub($info)
    {


        $wxguide = self::MODEL;
        $wxguide = new $wxguide;
        $wxguide->business_id = $info['business_id'];
        $wxguide->spot_id = $info['spot_id'];
//        $wxguide->number = $info['number'];
        $wxguide->expire_time = $info['expire_time'];
        $wxguide->qrcode = $this->createqrcode();
        $wxguide->old = 0;
        $wxguide->coupon_type_id = $info['coupon_type_id'];
        $wxguide->bonus = $info['bonus'] * 100;

        return $wxguide;
    }


    //生成唯一标签
    public function createqrcode()
    {
        $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
        return $orderSn = $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));

    }

    public function create($input)
    {

        $info = $input['data'];

        $wxguide = $this->createUserStub($info);

        DB::transaction(function () use ($wxguide, $info) {
            if ($wxguide->save()) {

                //event(new Explain_infoCreated($wxguide));

                return true;
            }

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


    }
}