LzcController.php 9.71 KB
<?php

namespace App\Http\Controllers\Backend\Lzc;

use App\Modules\Models\CouponOrder\CouponOrder;
use App\Modules\Models\GuideRecord\GuideRecord;
use App\Modules\Models\Machine\Machine;
use App\Modules\Models\Production\Production;
use App\Modules\Models\RedPack\RedPack;
use App\Modules\Models\Settlement\Finance;
use App\Modules\Models\Spot\Spot;
use App\Modules\Models\WxGuide\WxGuide;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use EasyWeChat\Factory;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;


class LzcController extends Controller
{
    //
    public function query()
    {

        $spot = Spot::with('guideRule')->where('code', '5ad9c8319a0ac')->first();
//        var_dump($spot->guide_rule);
        echo $spot->guide_rule;
        if ($spot->guide_rule == null) {
            echo '1111';
        }
        return $spot->toArray();
        exit();
        $spot = Spot::with('guideRule')->where('code', '5add38acc6760')->first()->toArray();
        var_dump($spot);
        if (!isset($spot['guide_rule'])) {
            echo '11111';
        }
        exit();
//        echo "1111";
//        $url1 = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx3185fb4a3633beb0&secret=d0fee84366bfb19c95505f4ddfa47b63';

        $url1 = 'https://api.weixin.qq.com/wxa/getwxacode?access_token=8__XJrRyuobNOp07-WCgpyiZ3G3DPZrOY9Sq6VDZMSHraXP4NK1oC0x52HZ8zFYGbHWsIE37qfn0O2YbWmeTgxLnuKk4iQM0FL72oXCT3KoQ1uBoh0vRn_ADIun1lgyTWZgy0wDbrqBCWj4L1EKOHgAEAXBU';
//        echo $url1;
        $fields = [
            "path"=>"pages/homepages/homepages?id=xiao3"
        ];

        $ch = curl_init();

        $options[CURLOPT_RETURNTRANSFER] = true;
        $options[CURLOPT_TIMEOUT] = 10;
        $options[CURLOPT_POST] = true;
        $options[CURLOPT_URL] = $url1;
        $options[CURLOPT_POSTFIELDS] = $fields;

        curl_setopt_array($ch, $options);

        $str = curl_exec($ch);

        curl_close($ch);

//        $arr = json_decode($str, true);

        echo '<img src="'.$str.'" />';


//        var_dump($arr);

    }

    public function monthLy(Request $request)
    {
        if(!$time = $request->input('date')){
            $time = date('Y-m');
        }


        $deal_time = date("Ym", strtotime("$time -1 month"));


        $start_time = date("Y-m-01 00:00:00", strtotime("$time -1 month"));
        $end_time = date("Y-m-01 00:00:00", strtotime("$time"));
        $orders = Production::select('rent.spot_id','rent.business_id','rent.real_total','production.return_time')
            ->leftJoin('rent', 'production.rent_id', '=', 'rent.id')
            ->where('production.return_time', '>=', $start_time)
            ->where('production.return_time','<=',$end_time)
            ->where('production.is_return',1)
            ->get()
            ->toArray();


        $res=GuideRecord::select('guide_record.spot_id','guide_record.business_id','guide_record.real_total')
            ->where('guide_record.pay_time', '>=', $start_time)
            ->where('guide_record.pay_time','<=',$end_time)
            ->where('guide_record.is_pay',1)
            ->get()
            ->toArray();



        $income_data = [];
        $date = [ ];


        if (!empty($orders)) {
            for ($i = 0; $i < count($orders); $i++) {
                if (!isset($income_data[$orders[$i]['spot_id']]['real_total'])) {
                    $income_data[$orders[$i]['spot_id']]['real_total'] = 0;
                }
                $income_data[$orders[$i]['spot_id']]['real_total'] += $orders[$i]['real_total'];
            }
        }

        if(!empty($res)){
            for($i=0;$i<count($res);$i++){
                if(!isset($date[$res[$i]['spot_id']]['real_total'])){
                    $date[$res[$i]['spot_id']]['real_total'] = 0;
                }
                $date[$res[$i]['spot_id']]['real_total'] += $res[$i]['real_total'];
            }

        }

        $insert_data = [];
        $spots = Spot::select('*')->get()->toArray();
        foreach ($spots as $k => $v) {
            $insert_data[$k]['business_id'] = (int)$v['business_id'];
            $insert_data[$k]['spot_id'] = (int)$v['id'];
            $insert_data[$k]['month'] = (int)$deal_time;
            $insert_data[$k]['total'] = empty($income_data[$v['id']]['real_total']) ? 0 : $income_data[$v['id']]['real_total'];
            $insert_data[$k]['guide_total'] = empty($date[$v['id']]['real_total']) ? 0 : $date[$v['id']]['real_total'];
            $insert_data[$k]['created_at'] =date("Y-m-d H:i:s");
            $insert_data[$k]['updated_at'] =date("Y-m-d H:i:s");
        }


        $res = Finance::insert($insert_data);
    }

    /**hongbao
     * @param Request $request
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function doRedPack(Request $request)
    {
        $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();
//        return $datas;
        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);
        }

    }

    /**测试 orwhere
     * @return mixed
     */
    function getAvailableFullDiscountCoupon()
    {
        $spot_id = 3;
        $customer_id=1;
        $total = 500;
        $where = [
            ['coupon_order.customer_id', '=', $customer_id],
            ['coupon_order.status', '=', 1],
            ['coupon_order.expire_time', '>=', date('Y-m-d')],
            ['coupon_type.discount', '<=', $total],
            ['coupon_type.type', '=', 2],
        ];

        $coupon = CouponOrder::where($where)
            ->where(function ($query) use($spot_id) {
                $query->where('coupon_order.spot_id', '=', $spot_id)
                    ->orWhere('coupon_type.auto_send', '=', 1);
            })
            ->rightjoin('coupon_type', 'coupon_order.coupon_type_id', '=', 'coupon_type.id')
            ->orderBy('discount_money', 'desc')
            ->select('coupon_order.*', 'coupon_type.type', 'coupon_type.discount', 'coupon_type.auto_send', 'coupon_type.schedule_time', 'coupon_type.discount_money')
            ->first();

        return $coupon;
    }

    public function availablePowerNumber($has_power = 75)
    {
        $machine = Machine::where('mac_no', '1708080101091147')->first();
        $where = [
            ['power.has_power', '>', $has_power],
            ['power.status', '=', 2],

        ];
        $res =  $machine
            ->power()
            ->where($where)
            ->groupBy('power.power_type')
            ->select('power.power_type', DB::raw('COUNT(power.power_type) as num'))
            ->get()
            ->toArray();

        $re = array_column($res, 'num');
        $total = array_sum($re);
        $res['total'] = $total;
        return $res;
    }
}