Send_RedPack.php 4.12 KB
<?php

namespace App\Console\Commands;

use App\Modules\Models\CouponOrder\CouponOrder;
use App\Modules\Models\RedPack\RedPack;
use Illuminate\Console\Command;
use EasyWeChat\Factory;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class Send_RedPack extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'Send_RedPack {--day=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '发送红包';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        Log:info('Send_RedPack----start');
        $config = config('wechat.payment.redpack');

        $app = Factory::payment($config);

        $time = date('Y-m-d');
        $day = date( "Y-m-d", strtotime( "$time -1 day" ) );
        $start_time = $day.' 00:00:00';
        $end_time = $day.' 23:59:59';

        $datas = CouponOrder::select('customers.id as uid','customers.openid as oid', 'coupon_order.wx_guide_id', 'wx_guide.bonus',DB::raw('COUNT(coupon_order.wx_guide_id) as num'))
            ->leftJoin('wx_guide', 'coupon_order.wx_guide_id', '=', 'wx_guide.id')
            ->leftJoin('customers', 'wx_guide.user_id', '=', 'customers.id')
            ->where('coupon_order.status', '=', 2)
            ->where('coupon_order.updated_at','<=',$end_time)
            ->where('coupon_order.updated_at', '>=', $start_time)
            ->groupBy('coupon_order.wx_guide_id', 'customers.openid', 'customers.id', 'wx_guide.bonus')
            ->get()
            ->toArray();

        foreach ($datas as $data){
            if(!$data['uid'] || !$data['oid']){
                continue;
            }
            $money = $data['bonus'] * $data['num'];
            $order_id = 'RP' . date('Ymd') . str_pad(mt_rand(1, 9999999999), 10, '0', STR_PAD_LEFT);//R201807238820290612
            $redpackData = [
                'mch_billno'   => $order_id,
                'send_name'    => '灰兔智能',
                're_openid'    => $data['oid'],
                'total_num'    => 1,  //固定为1,可不传
                'total_amount' => $money,  //单位为分,不小于100//
                'wishing'      => '晓兔祝您万事如意, 心想事成',
                //'client_ip'    => '192.168.0.1',  //可不传,不传则由 SDK 取当前客户端 IP
                'act_name'     => '优惠券',
                'remark'       => '景区导游',
                // ...
            ];
            $result = $app->redpack->sendNormal($redpackData);
            $js_result = json_encode($result, JSON_UNESCAPED_UNICODE);
            Log::info('[redpack params]: '.$js_result);
            //{"return_code":"SUCCESS","return_msg":"发放成功","result_code":"SUCCESS","err_code":"SUCCESS","err_code_des":"发放成功","mch_billno":"RP201810169185965624","mch_id":"1487106062","wxappid":"wxc2696bc3c9241968","re_openid":"oVIM80iK-7631xOrWcuhOuXjnpoY","total_amount":"100","send_listid":"1000041701201810163000174641430"}
            if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){
                $da = [
                    'send_list_id'=>$result['send_listid'],
                    'money'=>$result['total_amount'],
                    'user_id'=>$data['uid'],
                    'order_no'=>$result['mch_billno'],
                    'wx_guide_id'=>$data['wx_guide_id'],
                    'status'=>1,
                    'comment'=>$js_result
                ];
            }else{
                $da = [
                    //'send_list_id'=>$result['send_listid'],
                    'money'=>$money,
                    'user_id'=>$data['uid'],
                    'order_no'=>$order_id,
                    'wx_guide_id'=>$data['wx_guide_id'],
                    'status'=>2,
                    'comment'=>$js_result
                ];
            }
            RedPack::create($da);
        }
    }
}