<?php

namespace app\queue\redis;

use app\admin\logic\operation\OpurchaseclassLogic;
use app\common\enum\PayEnum;
use app\common\model\goods\Goods;
use app\common\model\goods\Unit;
use app\common\model\merchant\Merchant;
use app\common\model\opurchase\Opurchaseinfo;
use app\common\model\retail\Cashierclass;
use app\common\model\retail\Cashierinfo;
use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\model\store_order\StoreOrder;
use app\common\model\store_order_cart_info\StoreOrderCartInfo;
use app\common\model\store_product_unit\StoreProductUnit;
use app\common\model\system_store\SystemStore;
use app\common\model\system_store\SystemStoreStaff;
use app\common\model\user\User;
use app\common\service\PushService;
use Webman\RedisQueue\Consumer;
use Webman\Push\Api;
use support\exception\BusinessException;
use support\Log;
use think\facade\Db;

/**
 * 订单推送给收银台
 */
class PushPlatformPrintSend implements Consumer
{
    // 要消费的队列名
    public $queue = 'push-platform-print';

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

    // 消费
    public function consume($data)
    {
        $id=$data['id']??0;
        Log::info('打印推送开始'.$id);
        if(!$id)return false;
        $find = StoreOrder::where('id', $id)->find();
        if ($find) {
            $merchant = SystemStore::where('id', $find['store_id'])->field('name,phone')->find();
            $mer_user_info = SystemStoreStaff::where('store_id', $find['store_id'])->where('is_admin',1)->field('staff_name,phone')->find();
            $user = User::where('id', $find['uid'])->field('nickname,mobile')->find();
            $find['system_store_name'] = $merchant['name'];
            $find['pay_type_name'] = PayEnum::getPaySceneDesc($find['pay_type']);
            $find['system_store_phone'] = $merchant['phone'];
            $find['staff_name'] = $mer_user_info['staff_name'];
            $find['staff_phone'] = $mer_user_info['phone'];
            $find['nickname'] = $user['nickname']??'';
            $find['user_mobile'] = $user['mobile']??'';
            $find['info'] = StoreOrderCartInfo::where('oid', $find['id'])->field('store_id,product_id,cart_num,cart_info')->select()->each(function ($item) {
                $goods = StoreBranchProduct::where(['store_id'=>$item['store_id'],'product_id'=>$item['product_id']])->field('store_name,unit')->find();
                $item['unit_name'] = StoreProductUnit::where('id', $goods['unit'])->value('name');
                $item['store_name'] = $goods['store_name'];
                $item['total_price'] = $item['cart_info']['total_price'];
                $item['price'] = $item['cart_info']['price'];
                return $item;
            });
            PushService::push('store_merchant_'.$find['store_id'], $find['store_id'], ['type'=>'platform_print','msg'=>'打印队列','data'=>$find]);
            Log::info('打印推送结束'.$id);

        }
    }
    public function onConsumeFailure(\Throwable $exception, $package)
    {
        Log::error('打印队列推送失败。order_id:'. $package['data']['id'].',msg:'.$exception->getMessage());
        return $package;
    }
}