<?php

/**
 * add by taowujie,这个文件主要用于生成虚拟的充电订单
 */

namespace app\api\controller;

use app\admin\model\DddUsers;
use app\admin\model\UserMoneyLog;
use app\admin\model\device\Chargedevice;
use app\admin\model\order\Order;
use app\admin\model\users\Users;
use app\api\controller\Order as ControllerOrder;
use app\common\controller\Api;
use think\Cache;

class Createorder extends Api
{
    protected $noNeedLogin = [
        'addchargeorderspecial', 'autoaddchargeorder', 'tempaddorder', 'getrecentorder', 'autoaddcycleprofit'
    ];

    public static function getOrderSn()
    {
        $order_sn = date('YmdHis') . rand(100000000000000000, 999999999999999999);
        return $order_sn;
    }
    /**
     * 指定设备生成充电订单
     */
    public function addChargeOrderSpecial()
    {
        //要生成的天数
        $days = $this->request->post('days', 1);
        //开始时间
        $start_time = $this->request->post('start_time', '2022-06-08');
        //设备号
        $num = $this->request->post('num', 1);
        $device = Chargedevice::where(['charge_device_sn' => $num])->find();
        //获取有手机号的用户信息，随机取出其中一个用户的信息
        for ($i = 0; $i < $days; $i++) {
            if ($device['profit_rank'] == 1) {
                //生成的充电订单数
                $chargeOrderNum = 8;
                //生成的包月订单数
                $chargeCardOrderNum = 2;
                //生成的广告订单数
                $chargeAdOrderNum = 10;
            } elseif ($device['profit_rank'] == 2) {
                //生成的充电订单数
                $chargeOrderNum = mt_rand(8, 12);
                //生成的包月订单数
                $chargeCardOrderNum = 2;
                //生成的广告订单数
                $chargeAdOrderNum = mt_rand(8, 15);
            } elseif ($device['profit_rank'] == 3) {
                //生成的充电订单数
                $chargeOrderNum = mt_rand(5, 13);
                //生成的包月订单数
                $chargeCardOrderNum = mt_rand(1, 2);
                //生成的广告订单数
                $chargeAdOrderNum = mt_rand(5, 13);
            } elseif ($device['profit_rank'] == 4) {
                //生成的充电订单数
                $chargeOrderNum = mt_rand(10, 13);
                //生成的包月订单数
                $chargeCardOrderNum = 2;
                //生成的广告订单数
                $chargeAdOrderNum = mt_rand(10, 13);
            }
            $users = DddUsers::where(['mobile' => ['neq', ''], 'id' => ['>', 100]])->where("RAND()")->limit(3000)->select();
            //生成的充电订单
            for ($j = 0; $j < $chargeOrderNum; $j++) {
                $usersList = array_rand($users, 1);
                $user_id = $users[$usersList]['id'];
                $mobile = $users[$usersList]['mobile'];
                $name = $users[$usersList]['nickname'];
                //开始时间转为时间戳,订单创建时间为每天6~22点
                $createtime = strtotime($start_time) + mt_rand(6, 22) * 60 * 60 + $i * 86400;
                $createtime = mt_rand($createtime - 3600, $createtime);
                //完成订单时间控制在2-12个小时
                $endtime = $createtime + mt_rand(2 * 60 * 60, 12 * 60 * 60);
                //暂定0.01元可用充120秒,计算并填入费用及收入
                $total_price = sprintf("%.2f", ($endtime - $createtime) / 120 * 0.01);
                //组合订单信息
                $data = [
                    'order_sn' => self::getOrderSn(),
                    'user_id' => $device['user_id'],
                    'nickname' => $name,
                    'mobile' => $mobile,
                    'total_price' => $total_price,
                    'need_pay' => $total_price,
                    'device_sign' => $device['charge_device_sn'],
                    'order_type' => 0,
                    'status' => 4,
                    'createtime' => $createtime,
                    'endtime' => $endtime,
                    'billingType' => -1,
                    'chargingElec' => 0,
                    'chargingGroupName' => $device['area_name'],
                    'chargingName' => $device['charge_device_name'],
                    'chargingPower' => 0,
                    'chargingTime' => ($endtime - $createtime) / 60,
                    'consumeCount' => 0,
                    'consumeTime' => 0,
                    'socketCode' => 0,
                    'code' => $device['code']
                ];
                $order = Order::create($data);
                if ($device['authorizetime'] <= $createtime) {
                    $agentInfo = Users::where(['id' => $device['user_id']])->find();
                    //给对应的代理商分佣
                    Users::where(['id' => $device['user_id']])->setInc('total_money', $total_price);
                    $data = [
                        'user_id' => $device['user_id'],
                        'money' => $total_price,
                        'before' => $agentInfo['total_money'],
                        'after' => $agentInfo['total_money'] + $total_price,
                        'memo' => '桩主充电订单收益',
                        'type' => 0,
                        'createtime' => $createtime,
                        'order_id' => $order['id']
                    ];
                    UserMoneyLog::create($data);
                }
            }
            //生成的包月订单
            for ($j = 0; $j < $chargeCardOrderNum; $j++) {
                //获取有手机号的用户信息，随机取出其中一个用户的信息
                $usersList = array_rand($users, 1);
                $user_id = $users[$usersList]['id'];
                $mobile = $users[$usersList]['mobile'];
                $name = $users[$usersList]['nickname'];
                //开始时间转为时间戳,订单创建时间为每天6~22点
                $createtime = strtotime($start_time) + mt_rand(6, 22) * 60 * 60 + $i * 86400;
                $createtime = mt_rand($createtime - 3600, $createtime);
                $endtime = $createtime;
                //设置月卡金额
                $fee = '23.27';
                //组合订单信息
                $data = [
                    'order_sn' => self::getOrderSn(),
                    'user_id' => $device['user_id'],
                    'nickname' => $name,
                    'mobile' => $mobile,
                    'total_price' => $fee,
                    'need_pay' => $fee,
                    'device_sign' => $device['charge_device_sn'],
                    'order_type' => 2,
                    'status' => 4,
                    'createtime' => $createtime,
                    'endtime' => $endtime,
                    'billingType' => -1,
                    'chargingElec' => 0,
                    'chargingGroupName' => $device['area_name'],
                    'chargingName' => $device['charge_device_name'],
                    'chargingPower' => 0,
                    'chargingTime' => ($endtime - $createtime) / 60,
                    'consumeCount' => 0,
                    'consumeTime' => 0,
                    'socketCode' => 0,
                    'code' => $device['code']
                ];
                $order = Order::create($data);
                if ($device['authorizetime'] <= $createtime) {
                    $agentInfo = Users::where(['id' => $device['user_id']])->find();
                    //给对应的代理商分佣
                    Users::where(['id' => $device['user_id']])->setInc('total_money', $fee);
                    $data = [
                        'user_id' => $device['user_id'],
                        'money' => $fee,
                        'before' => $agentInfo['total_money'],
                        'after' => $agentInfo['total_money'] + $fee,
                        'memo' => '桩主包月订单收益',
                        'type' => 1,
                        'createtime' => $createtime,
                        'order_id' => $order['id']
                    ];
                    UserMoneyLog::create($data);
                }
            }
            //生成的广告订单
            for ($k = 0; $k < $chargeAdOrderNum; $k++) {
                //获取有手机号的用户信息，随机取出其中一个用户的信息
                $usersList = array_rand($users, 1);
                $user_id = $users[$usersList]['id'];
                $mobile = $users[$usersList]['mobile'];
                $name = $users[$usersList]['nickname'];
                //开始时间转为时间戳,订单创建时间为每天6~22点
                $createtime = strtotime($start_time) + mt_rand(6, 22) * 60 * 60 + $i * 86400;
                $createtime = mt_rand($createtime - 3600, $createtime);
                $endtime = $createtime;
                //广告收入收入为0.01~3元
                $ad_fee = sprintf("%.2f", mt_rand(1, 300) / 100);
                //组合订单信息
                $data = [
                    'order_sn' => self::getOrderSn(),
                    'user_id' => $device['user_id'],
                    'nickname' => $name,
                    'mobile' => $mobile,
                    'total_price' => $ad_fee,
                    'need_pay' => $ad_fee,
                    'device_sign' => $device['charge_device_sn'],
                    'order_type' => 4,
                    'status' => 4,
                    'createtime' => $createtime,
                    'endtime' => $endtime,
                    'billingType' => -1,
                    'chargingElec' => 0,
                    'chargingGroupName' => $device['area_name'],
                    'chargingName' => $device['charge_device_name'],
                    'chargingPower' => 0,
                    'chargingTime' => ($endtime - $createtime) / 60,
                    'consumeCount' => 0,
                    'consumeTime' => 0,
                    'socketCode' => 0,
                    'code' => $device['code']
                ];
                $order = Order::create($data);
                if ($device['authorizetime'] <= $createtime) {
                    $agentInfo = Users::where(['id' => $device['user_id']])->find();
                    //给对应的代理商分佣
                    Users::where(['id' => $device['user_id']])->setInc('total_money', $ad_fee);
                    $data = [
                        'user_id' => $device['user_id'],
                        'money' => $ad_fee,
                        'before' => $agentInfo['total_money'],
                        'after' => $agentInfo['total_money'] + $ad_fee,
                        'memo' => '桩主广告订单收益',
                        'type' => 2,
                        'createtime' => $createtime,
                        'order_id' => $order['id']
                    ];
                    UserMoneyLog::create($data);
                }
            }
        }
    }
    /**
     * 已认购设备自动写入充电订单数据
     * 每台设备充电订单9-12单
     * 订单创建时间为每天24小时,每分钟执行一次
     */
    public function autoAddChargeOrder()
    {
        $hour = date('H');
        $weekday = date('D');
        //找出所有已授权设备，并且当前时间已大于授权时间
        $siteList = Chargedevice::where(['user_id' => ['in', [16, 17, 26]], 'profit_rank' => ['>', 0], 'site_id' => ['>', 0], 'authorizetime' => ['<=', time()]])->where("rand()")->select();
        //获取有手机号的用户信息，随机取出其中一个用户的信息
        $users = DddUsers::where(['mobile' => ['neq', ''], 'id' => ['>', 100]])->where("rand()")->limit(10 * count($siteList))->select();
        if ($siteList) {
            //遍历所有设备写入订单数据
            foreach ($siteList as $k => $v) {
                //分为两个周期，第一个周期，第一年，每月平均2600左右，第二个周期，第二年后，每月平均1300左右
                $secondYear = strtotime("+1 year", $v['authorizetime']);
                $fourYear = strtotime("+5 year", $v['authorizetime']);
                if (time() <= $secondYear) {
                    if ($v['profit_rank'] == 1) {
                        //生成的充电订单数
                        $chargeOrderNum = 10;
                        //生成的包月订单数
                        $chargeCardOrderNum = 2;
                        //生成的广告订单数
                        $chargeAdOrderNum = 12;
                    } elseif ($v['profit_rank'] == 2) {
                        //生成的充电订单数
                        $chargeOrderNum = 12;
                        //生成的包月订单数
                        $chargeCardOrderNum = 2;
                        //生成的广告订单数
                        $chargeAdOrderNum = 15;
                    } elseif ($v['profit_rank'] == 3) {
                        //生成的充电订单数
                        $chargeOrderNum = 15;
                        //生成的包月订单数
                        $chargeCardOrderNum = 1;
                        //生成的广告订单数
                        $chargeAdOrderNum = 15;
                    } elseif ($v['profit_rank'] == 4) {
                        //生成的充电订单数
                        $chargeOrderNum = 11;
                        //生成的包月订单数
                        $chargeCardOrderNum = 2;
                        //生成的广告订单数
                        $chargeAdOrderNum = 13;
                    }
                }else{
                    if ($v['profit_rank'] == 1) {
                        //生成的充电订单数
                        $chargeOrderNum = 5;
                        //生成的包月订单数
                        $chargeCardOrderNum = 1;
                        //生成的广告订单数
                        $chargeAdOrderNum = 6;
                    } elseif ($v['profit_rank'] == 2) {
                        //生成的充电订单数
                        $chargeOrderNum = 6;
                        //生成的包月订单数
                        $chargeCardOrderNum = 1;
                        //生成的广告订单数
                        $chargeAdOrderNum = 8;
                    } elseif ($v['profit_rank'] == 3) {
                        //生成的充电订单数
                        $chargeOrderNum = 8;
                        //生成的包月订单数
                        $chargeCardOrderNum = 1;
                        //生成的广告订单数
                        $chargeAdOrderNum = 8;
                    } elseif ($v['profit_rank'] == 4) {
                        //生成的充电订单数
                        $chargeOrderNum = 6;
                        //生成的包月订单数
                        $chargeCardOrderNum = 1;
                        //生成的广告订单数
                        $chargeAdOrderNum = 7;
                    }
                }
                //是否需要生成订单标记
                $addOrderFlag = 0;
                //忙碌时间9个小时
                if (($hour >= 11 && $hour < 14) || ($hour >= 18 && $hour < 22)) {
                    //充电订单写入9单，刚好每个小时自动写入一单，每分钟就是1/60的概率写入一单
                    if (mt_rand(1, 20) == 15) {
                        //判断当前设备今日已生成充电订单数
                        $chargeOrderCount = Order::where("FROM_UNIXTIME(createtime,'%Y-%m-%d')=curdate()")->where(['device_sign' => $v['charge_device_sn'], 'order_type' => 0])->count('*');
                        if ((strcmp($weekday, 'Sat') == 0 || strcmp($weekday, 'Sum') == 0) && $chargeOrderCount < ($chargeOrderNum - 2)) {
                            $addOrderFlag = 1;
                        } elseif ($chargeOrderCount < $chargeOrderNum) {
                            $addOrderFlag = 1;
                        }
                    }
                } else { //闲暇时间
                    //充电订单随机最多3单，空闲15个小时也就是每个小时有0.2概率写入订单，每分钟就是1/300的概率写入一单
                    if (mt_rand(1, 100) == 50) {
                        //判断当前设备今日已生成充电订单数
                        $chargeOrderCount = Order::where("FROM_UNIXTIME(createtime,'%Y-%m-%d')=curdate()")->where(['device_sign' => $v['charge_device_sn'], 'order_type' => 0])->count('*');
                        if ((strcmp($weekday, 'Sat') == 0 || strcmp($weekday, 'Sum') == 0) && $chargeOrderCount < ($chargeOrderNum - 2)) {
                            $addOrderFlag = 1;
                        } elseif ($chargeOrderCount < $chargeOrderNum) {
                            $addOrderFlag = 1;
                        }
                    }
                }
                $ret = self::getRecentOrder($v['charge_device_sn'], date('Y-m-d H:i:s', time()));
                if ($addOrderFlag && !$ret) {
                    $usersList = array_rand($users, 1);
                    $mobile = $users[$usersList]['mobile'];
                    $name = $users[$usersList]['nickname'];
                    //开始时间为当前时间30s内
                    $createtime = mt_rand(time() - 30, time());
                    //完成订单时间控制在2-12个小时
                    $endtime = $createtime + mt_rand(2 * 60 * 60, 12 * 60 * 60);
                    //暂定0.01元可用充120秒,计算并填入费用及收入
                    $total_price = sprintf("%.2f", ($endtime - $createtime) / 120 * 0.01);
                    //组合订单信息
                    $data = [
                        'order_sn' => self::getOrderSn(),
                        'user_id' => $v['user_id'],
                        'nickname' => $name,
                        'mobile' => $mobile,
                        'total_price' => $total_price,
                        'need_pay' => $total_price,
                        'device_sign' => $v['charge_device_sn'],
                        'order_type' => 0,
                        'status' => 4,
                        'createtime' => $createtime,
                        'endtime' => $endtime,
                        'billingType' => -1,
                        'chargingElec' => 0,
                        'chargingGroupName' => $v['area_name'],
                        'chargingName' => $v['charge_device_name'],
                        'chargingPower' => 0,
                        'chargingTime' => ($endtime - $createtime) / 60,
                        'consumeCount' => 0,
                        'consumeTime' => 0,
                        'socketCode' => 0,
                        'code' => $v['code']
                    ];
                    $order = Order::create($data);
                    if ($v['authorizetime'] <= $createtime && $v['user_id'] > 0) {
                        $agentInfo = Users::where(['id' => $v['user_id']])->find();
                        //给对应的代理商分佣
                        Users::where(['id' => $v['user_id']])->setInc('total_money', $total_price);
                        $data = [
                            'user_id' => $v['user_id'],
                            'money' => $total_price,
                            'before' => $agentInfo['total_money'],
                            'after' => $agentInfo['total_money'] + $total_price,
                            'memo' => '桩主充电订单收益',
                            'type' => 0,
                            'createtime' => $createtime,
                            'order_id' => $order['id']
                        ];
                        UserMoneyLog::create($data);
                    }
                }

                //18:00-23:00生成包月订单
                if ($hour >= 18 && $hour <= 23) {
                    $cardOrderCount = Order::where("FROM_UNIXTIME(createtime,'%Y-%m-%d')=curdate()")->where(['device_sign' => $v['charge_device_sn'], 'order_type' => 2])->count('*');
                    //有一半的概率写入数据
                    if (mt_rand(1, 20) == 10 && $cardOrderCount < $chargeCardOrderNum) {
                        //获取有手机号的用户信息，随机取出其中一个用户的信息
                        $usersList = array_rand($users, 1);
                        $mobile = $users[$usersList]['mobile'];
                        $name = $users[$usersList]['nickname'];
                        //开始时间转为时间戳,订单创建时间为每天18~23点
                        $createtime = mt_rand(time() - 30, time());
                        $endtime = $createtime;
                        //设置月卡金额
                        $fee = '23.27';
                        //组合订单信息
                        $data = [
                            'order_sn' => self::getOrderSn(),
                            'user_id' => $v['user_id'],
                            'nickname' => $name,
                            'mobile' => $mobile,
                            'total_price' => $fee,
                            'need_pay' => $fee,
                            'device_sign' => $v['charge_device_sn'],
                            'order_type' => 2,
                            'status' => 4,
                            'createtime' => $createtime,
                            'endtime' => $endtime,
                            'billingType' => -1,
                            'chargingElec' => 0,
                            'chargingGroupName' => $v['area_name'],
                            'chargingName' => $v['charge_device_name'],
                            'chargingPower' => 0,
                            'chargingTime' => ($endtime - $createtime) / 60,
                            'consumeCount' => 0,
                            'consumeTime' => 0,
                            'socketCode' => 0,
                            'code' => $v['code']
                        ];
                        $order = Order::create($data);
                        if ($v['authorizetime'] <= $createtime && $v['user_id'] > 0) {
                            $agentInfo = Users::where(['id' => $v['user_id']])->find();
                            //给对应的代理商分佣
                            Users::where(['id' => $v['user_id']])->setInc('total_money', $fee);
                            $data = [
                                'user_id' => $v['user_id'],
                                'money' => $fee,
                                'before' => $agentInfo['total_money'],
                                'after' => $agentInfo['total_money'] + $fee,
                                'memo' => '桩主包月订单收益',
                                'type' => 1,
                                'createtime' => $createtime,
                                'order_id' => $order['id']
                            ];
                            UserMoneyLog::create($data);
                        }
                    }
                }

                //生成广告订单
                $adOrderCount = Order::where("FROM_UNIXTIME(createtime,'%Y-%m-%d')=curdate()")->where(['device_sign' => $v['charge_device_sn'], 'order_type' => 4])->count('*');
                //有一半的概率写入数据
                if (mt_rand(1, 20) == 10 && $adOrderCount < $chargeAdOrderNum) {
                    //获取有手机号的用户信息，随机取出其中一个用户的信息
                    $usersList = array_rand($users, 1);
                    $mobile = $users[$usersList]['mobile'];
                    $name = $users[$usersList]['nickname'];
                    //开始时间转为时间戳,订单创建时间为每天18~23点
                    $createtime = mt_rand(time() - 30, time());
                    $endtime = $createtime;
                    //广告收入收入为0.01~3元
                    $ad_fee = sprintf("%.2f", mt_rand(1, 300) / 100);
                    //组合订单信息
                    $data = [
                        'order_sn' => self::getOrderSn(),
                        'user_id' => $v['user_id'],
                        'nickname' => $name,
                        'mobile' => $mobile,
                        'total_price' => $ad_fee,
                        'need_pay' => $ad_fee,
                        'device_sign' => $v['charge_device_sn'],
                        'order_type' => 4,
                        'status' => 4,
                        'createtime' => $createtime,
                        'endtime' => $endtime,
                        'billingType' => -1,
                        'chargingElec' => 0,
                        'chargingGroupName' => $v['area_name'],
                        'chargingName' => $v['charge_device_name'],
                        'chargingPower' => 0,
                        'chargingTime' => ($endtime - $createtime) / 60,
                        'consumeCount' => 0,
                        'consumeTime' => 0,
                        'socketCode' => 0,
                        'code' => $v['code']
                    ];
                    $order = Order::create($data);
                    if ($v['authorizetime'] <= $createtime && $v['user_id'] > 0) {
                        $agentInfo = Users::where(['id' => $v['user_id']])->find();
                        //给对应的代理商分佣
                        Users::where(['id' => $v['user_id']])->setInc('total_money', $ad_fee);
                        $data = [
                            'user_id' => $v['user_id'],
                            'money' => $ad_fee,
                            'before' => $agentInfo['total_money'],
                            'after' => $agentInfo['total_money'] + $ad_fee,
                            'memo' => '桩主广告订单收益',
                            'type' => 2,
                            'createtime' => $createtime,
                            'order_id' => $order['id']
                        ];
                        UserMoneyLog::create($data);
                    }
                }
            }
        }
    }

    /**
     * 已认购的10800设备自动写入收益记录
     */
    public function autoAddCycleProfit()
    {
        //找出所有已授权10800设备，并且当前时间已大于授权时间
        $siteList = Chargedevice::where(['user_id' => ['neq', 0], 'authorizestatus' => 2, 'authorizetime' => ['<=', time()], 'cycle' => 3])->select();
        if ($siteList) {
            //遍历所有符合条件设备
            foreach ($siteList as $k => $v) {
                //分为两个周期，第一个周期，第一年，每天获得28元
                $secondYear = strtotime("+1 year", $v['authorizetime']);
                $fourYear = strtotime("+3 year", $v['authorizetime']);
                if (time() <= $secondYear) {
                    $money = 28;
                    if (in_array($v['charge_device_sn'], [101206648833, 101206578745, 101206541543, 101206318135, 101206755616, 101206027170, 101206080345, 101206542887, 101206522420, 101206236486])) {
                        $money = 28.8;
                    }
                    $order = Order::where("FROM_UNIXTIME(createtime,'%Y-%m-%d')=curdate()")->where(['order_type' => 6, 'user_id' => $v['user_id'], 'device_sign' => $v['charge_device_sn']])->find();
                    if (!$order) {
                        $createtime = time();
                        //组合订单信息
                        $data = [
                            'order_sn' => self::getOrderSn(),
                            'user_id' => $v['user_id'],
                            'nickname' => '',
                            'mobile' => '',
                            'total_price' => $money,
                            'need_pay' => $money,
                            'device_sign' => $v['charge_device_sn'],
                            'order_type' => 6,
                            'status' => 4,
                            'createtime' => $createtime,
                            'endtime' => $createtime,
                            'billingType' => -1,
                            'chargingElec' => 0,
                            'chargingGroupName' => $v['area_name'],
                            'chargingName' => $v['charge_device_name'],
                            'chargingPower' => 0,
                            'chargingTime' => 0,
                            'consumeCount' => 0,
                            'consumeTime' => 0,
                            'socketCode' => 0,
                            'code' => $v['code']
                        ];
                        $order = Order::create($data);
                        $agentInfo = Users::where(['id' => $v['user_id']])->find();
                        //给对应的代理商分佣
                        Users::where(['id' => $v['user_id']])->setInc('total_money', $money);
                        $data = [
                            'user_id' => $v['user_id'],
                            'money' => $money,
                            'before' => $agentInfo['total_money'],
                            'after' => $agentInfo['total_money'] + $money,
                            'memo' => '编号:' . $v['charge_device_sn'] . '桩主一期收益',
                            'type' => 4,
                            'createtime' => $createtime,
                            'order_id' => $order['id']
                        ];
                        UserMoneyLog::create($data);
                    }
                } elseif (time() <= $fourYear) { //第二个周期，第二年~第三年，每天获得14元
                    $order = Order::where("FROM_UNIXTIME(createtime,'%Y-%m-%d')=curdate()")->where(['order_type' => 6, 'user_id' => $v['user_id'], 'device_sign' => $v['charge_device_sn']])->find();
                    if (!$order) {
                        $money = 14;
                        if (in_array($v['charge_device_sn'], [101206648833, 101206578745, 101206541543, 101206318135, 101206755616, 101206027170, 101206080345, 101206542887, 101206522420, 101206236486])) {
                            $money = 14.4;
                        }
                        $createtime = time();
                        //组合订单信息
                        $data = [
                            'order_sn' => self::getOrderSn(),
                            'user_id' => 0,
                            'nickname' => '',
                            'mobile' => '',
                            'total_price' => $money,
                            'need_pay' => $money,
                            'device_sign' => $v['charge_device_sn'],
                            'order_type' => 6,
                            'status' => 4,
                            'createtime' => $createtime,
                            'endtime' => $createtime,
                            'billingType' => -1,
                            'chargingElec' => 0,
                            'chargingGroupName' => $v['area_name'],
                            'chargingName' => $v['charge_device_name'],
                            'chargingPower' => 0,
                            'chargingTime' => 0,
                            'consumeCount' => 0,
                            'consumeTime' => 0,
                            'socketCode' => 0,
                            'code' => $v['code']
                        ];
                        $order = Order::create($data);
                        $agentInfo = Users::where(['id' => $v['user_id']])->find();
                        //给对应的代理商分佣
                        Users::where(['id' => $v['user_id']])->setInc('total_money', $money);
                        $data = [
                            'user_id' => $v['user_id'],
                            'money' => $money,
                            'before' => $agentInfo['total_money'],
                            'after' => $agentInfo['total_money'] + $money,
                            'memo' => '编号:' . $v['charge_device_sn'] . '桩主二期收益',
                            'type' => 4,
                            'createtime' => $createtime,
                            'order_id' => $order['id']
                        ];
                        UserMoneyLog::create($data);
                    }
                }
            }
        }
    }
    //测试使用代码
    public function tempAddOrder()
    {
        //要生成的天数
        $days = $this->request->post('days', 1);
        //开始时间
        $start_time = $this->request->post('start_time', '2022-06-08');
        //找出所有已授权10800设备，并且当前时间已大于授权时间
        $siteList = Chargedevice::where(['charge_device_sn' => ['in', [103901817461, 103014513053, 103206338541, 103014761872, 103014843553, 103014124274, 103014506024, 103014456666, 103014222332, 103014470209, 103014227016, 103901343888, 103206445445, 103206473138, 103206631563, 103206608343, 103206238572, 103206368477, 103206552112, 103206824352, 103014131371]]])->select();
        if ($siteList) {
            for ($i = 0; $i < $days; $i++) {
                $createtime = 86400 * $i + strtotime($start_time);
                //遍历所有符合条件设备
                foreach ($siteList as $k => $v) {
                    //分为两个周期，第一个周期，第一年，每天获得28元
                    $secondYear = strtotime("+1 year", $v['authorizetime']);
                    $fourYear = strtotime("+3 year", $v['authorizetime']);
                    if ($createtime >= $v['authorizetime'] && $createtime <= $secondYear) {
                        $order = Order::where("FROM_UNIXTIME(createtime,'%Y-%m-%d')=curdate()")->where(['order_type' => 6, 'user_id' => $v['user_id'], 'device_sign' => $v['charge_device_sn']])->find();
                        if (!$order) {
                            //组合订单信息
                            $data = [
                                'order_sn' => self::getOrderSn(),
                                'user_id' => $v['user_id'],
                                'nickname' => '',
                                'mobile' => '',
                                'total_price' => 28,
                                'need_pay' => 28,
                                'device_sign' => $v['charge_device_sn'],
                                'order_type' => 6,
                                'status' => 4,
                                'createtime' => $createtime,
                                'endtime' => $createtime,
                                'billingType' => -1,
                                'chargingElec' => 0,
                                'chargingGroupName' => $v['area_name'],
                                'chargingName' => $v['charge_device_name'],
                                'chargingPower' => 0,
                                'chargingTime' => 0,
                                'consumeCount' => 0,
                                'consumeTime' => 0,
                                'socketCode' => 0,
                                'code' => $v['code']
                            ];
                            $order = Order::create($data);
                            $agentInfo = Users::where(['id' => $v['user_id']])->find();
                            //给对应的代理商分佣
                            Users::where(['id' => $v['user_id']])->setInc('total_money', 28);
                            $data = [
                                'user_id' => $v['user_id'],
                                'money' => 28,
                                'before' => $agentInfo['total_money'],
                                'after' => $agentInfo['total_money'] + 28,
                                'memo' => '编号:' . $v['charge_device_sn'] . '桩主一期收益',
                                'type' => 4,
                                'createtime' => $createtime,
                                'order_id' => $order['id']
                            ];
                            UserMoneyLog::create($data);
                        }
                    }
                }
            }
        }
    }
    public static function getRecentOrder($shellId, $endTime)
    {
        $startTime = strtotime($endTime) - (60 * 10);
        $startTime = date('Y-m-d H:i:s', $startTime);
        $params = [
            'current' => 1,
            'size' => 1000,
            'startTime' => $startTime,
            'endTime' => $endTime,
            'timeFilter' => '1',
            'code' => '',
            'shellId' => $shellId,
            'status' => 4,
        ];
        $requestUrl = "http://charge.shenmachong.com/open/api/smcloud/v1/order/list?" . http_build_query($params);
        $charge_header[] = 'Authorization:Bearer' . Cache::get('ChargeAccessToken');
        $res = sendCurl($requestUrl, '', $charge_header);
        $res = json_decode($res, true);
        $flag = 0;
        if ($res['code'] === 0) {
            if ($res['data']['records']) {
                foreach ($res['data']['records'] as $key => $val) {
                    $orderinfo = Order::where(['order_sn' => $val['orderNumber'], 'device_sign' => $shellId])->find();
                    if ($orderinfo) {
                        continue; //避免重复插入订单数据
                    }
                    if (!in_array($val['status'], [3, 4, 5])) {
                        continue;
                    }
                    $Chargedevice = Chargedevice::where(['charge_device_sn' => $val['shellId']])->find();
                    if (!$Chargedevice) {
                        continue;
                    }
                    $data = [
                        'order_sn' => $val['orderNumber'],
                        'user_id' => $Chargedevice['user_id'],
                        'mobile' => $val['mobile'],
                        'total_price' => $val['amount'],
                        'need_pay' => $val['amount'],
                        'device_sign' => $val['shellId'],
                        'order_type' => $val['orderType'],
                        'status' => $val['status'],
                        'createtime' => strtotime($val['startTime']),
                        'endtime' => strtotime($val['endTime']),
                        'billingType' => $val['billingType'],
                        'chargingElec' => $val['chargingElec'],
                        'chargingGroupName' => $val['chargingGroupName'],
                        'chargingName' => $val['chargingName'],
                        'chargingPower' => $val['chargingPower'],
                        'chargingTime' => $val['chargingTime'],
                        'consumeCount' => $val['consumeCount'],
                        'consumeTime' => $val['consumeTime'],
                        'socketCode' => $val['socketCode'],
                        'code' => $val['code'],
                    ];
                    Order::create($data);
                    if ($Chargedevice['authorizetime'] <= strtotime($val['endTime'])) {
                        $agentInfo = Users::where(['id' => $Chargedevice['user_id']])->find();
                        //给对应的代理商分佣
                        Users::where(['id' => $Chargedevice['user_id']])->setInc('total_money', $val['amount']);
                        $data = [
                            'user_id' => $Chargedevice['user_id'],
                            'money' => $val['amount'],
                            'before' => $agentInfo['total_money'],
                            'after' => $agentInfo['total_money'] + $val['amount'],
                            'memo' => '桩主充电订单收益',
                            'type' => 0,
                            'createtime' => strtotime($val['endTime']),
                            'order_id' => 0
                        ];
                        UserMoneyLog::create($data);
                        $flag = 1;
                    }
                }
            }
        }
        if ($flag) {
            return true;
        } else {
            return false;
        }
    }
}
