<?php
declare (strict_types = 1);

namespace app\command\controller;

use app\command\CmdLog;
use think\Exception;
use think\facade\Config;
use think\facade\Db;
use think\facade\Log;
use WpOrg\Requests\Requests;

class Send
{

    // 订单数 => 分钟数
    private $mintue = [
        0 => 2,
        1 => 5,
        2 => 15,
        3 => 30,
        4 => 60,
        5 => 120,
        6 => 240,
    ];

    // 发货入口
    public function runs(){
        CmdLog::debug("正在执行补单...");
        // 查询发放失败的订单
        $time = time();                                     // 当前时间
        $where = "status =1 and gstatus !=1 and  dateline >= ".( $time - 1 * 24 * 60 * 60 );
        // 回调失败的订单
        $orderList       = Db::table('app_order')->where($where)->limit(50)->select()->toArray();
        // 游戏未发货订单处理
        if (!empty($orderList))
        {
            foreach ($orderList as $k => $v) {
                // 查询订单 自动补单次数
                $fixOrderCount = $v['wap_number'];

                foreach ($this->mintue as $minK => $minV) {
                    // 补单次数 && 订单间隔时间 大于 当前次数时间
                    if ($minK === $fixOrderCount && $time - $v['dateline'] >= $minV * 60) {
                        $res = $this->fixOrder($v);
                        if(!$res){
                            continue;
                        }
                    }
                }

            }
        }else {
            CmdLog::debug("无订单补发！");
        }
    }

    private function fixOrder($orderInfo)
    {
        // 通过下单的 appid 找到游戏配置
        $gamesInfo = Db::table('app_games_config')->where(['app_id'=>$orderInfo['appid']])->find();
        if(!$gamesInfo){
            CmdLog::info("order_id:{$orderInfo['appid']}游戏配置没找到！");
            return false;
        }
        // 组装post参数，并签名
        $params = [
            'order_id' => $orderInfo['order_id'],
            'good_name' => $orderInfo['subject'],
            'cp_order_id' => $orderInfo['cp_order_id'],
            'uid' => $orderInfo['uid'],
            'money' => $orderInfo['money'],
            'app_id' => $orderInfo['appid'],
            'service_id' => $orderInfo['server_id'],
            'service_name' => $orderInfo['server_name'],
            'role_id' => $orderInfo['role_id'],
            'role_name' => $orderInfo['role_name'],
            'time' => time(),
            'pay_status' => $orderInfo['status'],
            'extend' => $orderInfo['extra_info'],
        ];
        ksort($params);
        $signStr = http_build_query($params)."&paykey=".$gamesInfo['paykey'];
        $params['sign'] = md5($signStr);
        $response = Requests::post($gamesInfo['send_url'],['Accept' => 'application/json'],$params)->body;
        Db::table('app_order')->where(['order_id'=>$orderInfo['order_id']])->update(['wap_number'=>Db::raw('wap_number+1')]);
        // 补单记录
        Db::table("app_callback_log")->insert([
            'order_id' => $orderInfo['order_id'],
            'requrl' => $gamesInfo['send_url'],
            'reqparams' => json_encode($params),
            'reqresult' => $response,
            'reqtime' => time(),
        ]);
        $response_arr = json_decode($response,true);
        if($response_arr['code'] === 200){
            Db::table('app_order')->where(['order_id'=>$orderInfo['order_id']])->update(['gstatus'=>1]);
            CmdLog::info("order_id:{$orderInfo['appid']}补单成功！");
            return true;
        }else{
            Db::table('app_order')->where(['order_id'=>$orderInfo['order_id']])->update(['gstatus'=>2]);
            CmdLog::info("order_id:{$orderInfo['appid']}补单失败！返回：{$response}");
        }
        return false;
    }


}
