<?php namespace App\Jobs\Company\LiveLuckyBag;


use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

use App\Facades\Servers\Logs\Log;

use App\Models\Manager\CustomAmount;
use App\Models\Manager\CustomCoupon;
use App\Models\Manager\CustomScore;
use App\Models\Manager\LiveLuckyBagWinning;

use App\Servers\DB\DbService;


/**
 * 奖品发放
 */
class LuckyBagRewardJobs implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /** 奖品数据 **/
    protected $winData;

    /**
     * 任务可尝试的次数
     *
     * @var int
     */
    public $tries = 3;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->winData    = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // 获取数据
        $winData                                = $this->winData;
        try {
            // 如果不是1,2,3 类型，不处理
            if( !in_array($winData['prize_type'],[1,2,3,4]) )  return 0;
            // 切换数据库
            (new DbService())->getConnectionNameByCompanyId($winData['company_id']);
            // 中奖记录
            $LiveLuckyBagWinning                = (new LiveLuckyBagWinning);
            // 先查询是不是有未处理的
            $oldId                              = $LiveLuckyBagWinning->query()->where([['custom_uid','=',$winData['custom_uid']],['lucky_bag_id','=',$winData['lucky_bag_id']],['status','=',0],['participate_id','=',$winData['participate_id']]])->value('id');
            // 如果有没有的话，不处理
            if( !$oldId )                       return 0;
            // 如果是积分
            if( $winData['prize_type'] == 1 ){
                // 积分大于0
                if( $winData['prize'] > 0 ){
                    // 积分发放
                    $result 		      = (new CustomScore)->trade($winData['custom_uid'],$winData['shop_id'],$winData['company_id'],$winData['lucky_bag_id'],$winData['prize'],11,1,'直播福袋');
                    // 发放失败，改为未中奖
                    if( isset($result['error']) ){
                        // 修改状态
                        $result                             = $LiveLuckyBagWinning->edit($oldId,['status'=>1]);
                        // 发放失败，改为未中奖
                        if( !$result )                      Log::info('job_error/lucky_bag_reward_jobs', '状态修改失败', $winData);
                        Log::info('job_error/lucky_bag_reward_jobs', $result['error'], $winData);
                    }
                }
            }
            // 优惠券
            if( $winData['prize_type'] == 2 ){
                // 优惠券存在ID
                if( $winData['prize'] ) {
                    // 优惠卷给与
                    $result 		      = (new CustomCoupon)->giveCoupon($winData['prize'],$winData['custom_uid'],$winData['shop_id'],$winData['company_id']);
                    // 发放失败，改为未中奖
                    if( isset($result['error']) ){
                        // 修改状态
                        $result                             = $LiveLuckyBagWinning->edit($oldId,['status'=>1]);
                        // 发放失败，改为未中奖
                        if( !$result )                      Log::info('job_error/lucky_bag_reward_jobs', '状态修改失败', $winData);
                        Log::info('job_error/lucky_bag_reward_jobs', $result['error'], $winData);
                    }
                }
            }
            // 红包
            if( $winData['prize_type'] == 3 || $winData['prize_type'] == 4){
                // 积分大于0
                if( $winData['prize'] > 0 ){
                    // 积分发放
                    $result 		      = (new CustomAmount)->trade($winData['custom_uid'],$winData['shop_id'],$winData['company_id'],$winData['lucky_bag_id'],$winData['prize'],9,1,'直播福袋');
                    // 发放失败，改为未中奖
                    if( isset($result['error']) ){
                        // 修改状态
                        $result                             = $LiveLuckyBagWinning->edit($oldId,['status'=>1]);
                        // 发放失败，改为未中奖
                        if( !$result )                      Log::info('job_error/lucky_bag_reward_jobs', '状态修改失败', $winData);
                        Log::info('job_error/lucky_bag_reward_jobs', $result['error'], $winData);
                    }
                }
            }
        } catch (Throwable $th) {
            Log::info('job_error/lucky_bag_reward_jobs', $th->getMessage(), $winData);
        }
    }
}
