<?php

namespace app\queue\redis;

use app\common\enum\PayEnum;
use app\common\logic\PayNotifyLogic;
use app\common\model\retail\Cashierclass;
use app\common\model\store_order\StoreOrder;
use app\common\model\store_order_cart_info\StoreOrderCartInfo;
use app\common\model\system_store\SystemStore;
use app\common\model\user_recharge\UserRecharge;
use app\common\service\Curl;
use app\common\service\pay\PayService;
use app\common\service\PushService;
use Webman\RedisQueue\Consumer;
use support\exception\BusinessException;
use support\Log;

/**
 * 企业微信订单推送队列消费
 */
class OrderWetchaPushSend implements Consumer
{
    // 要消费的队列名
    public $queue = 'order_wetcha_push_send';

    // 连接名,对应 plugin/webman/redis-queue/redis.php 里的连接`
    public $connection = 'default';

    // 消费
    public function consume($data)
    {
        $url = getenv('WETCHA_PUSH');
        if ($url) {
            $order=$data['order'];
            $order_id = $order['order_id'];
            $total_price = $order['total_price'];
            $pay_price = $order['pay_price'];
            $pay_time = date('Y-m-d H:i:s', $order['pay_time']);
            $phone = $order['user_phone'];
            $pay_type = PayEnum::getPaySceneDesc($order['pay_type']);
            $store_name = SystemStore::where('id', $order['store_id'])->value('name');
            $cart_info=StoreOrderCartInfo::where('oid',$order['id'])->field('cart_info')->select();
            $product_info="\r\n>商品信息:<font color=\"comment\">------------------</font>";
            foreach($cart_info as $k=>$v['cart_info']){
                $a=$v['cart_info']['cart_info']['name'].'  数量:'.$v['cart_info']['cart_info']['cart_num'].'  价格:'.$v['cart_info']['cart_info']['pay_price'].'元';
                $product_info.="\r\n><font color=\"comment\">$a</font>";
            }
            $arr = ["msgtype" => "markdown", "markdown" => ["content" => "有新的订单请及时跟踪
                >订单号:<font color=\"comment\">$order_id</font>
                >门店信息:<font color=\"comment\">$store_name</font>"
                .$product_info
                ."\r\n>下单时间:<font color=\"comment\">$pay_time</font>
                >订单金额:<font color=\"comment\">$total_price</font>
                >支付金额:<font color=\"comment\">$pay_price</font>
                >支付方式:<font color=\"comment\">$pay_type</font>
                >下单用户手机号:<font color=\"comment\">$phone</font>"]];
            (new Curl())->postJson($url, json_encode($arr));
        }
    }
    // 消费失败时
    public function onConsumeFailure(\Throwable $exception, $package)
    {
        $package['max_attempts'] = 0;
        Log::error('推送订单失败', ['order_id' => $package['data'], 'error' => $package['error']]);
        return true;
    }
}