<?php

namespace App\Models;

use App\Jobs\GrowingIO\SignFlowerReportJob;
use App\Models\User\UserModel;
use DB;
use Exception;
use Illuminate\Support\Facades\Redis;

/**
 * App\Models\FlowerLogModel
 *
 * @property int $id 主键
 * @property int $uid 用户id
 * @property int $type 记录类型[1:消费卡片;2:任务;3:充值;4:系统扣除;5:系统补偿;6:收获;7:签到;8:赠送;9:获赠;10:分享奖励;11:消费退回一张;12:红包]
 * @property float $gold_flower 金fa增减情况
 * @property float $red_flower 红fa增减情况
 * @property int $jsk 解锁卡
 * @property string $type_id 记录外链id[消费时：partner_id;任务：任务号；充值：充值金额；系统退还:partner_id;收获小fa:partner_id;赠送:接收者uid;获赠:赠送者uid]
 * @property string $remark 备注
 * @property int $fixed_at 记录处理时间
 * @property \Illuminate\Support\Carbon $create_at 操作时间
 * @property int $update_at 修改时间
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\FlowerLogModel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\FlowerLogModel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\FlowerLogModel query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\FlowerLogModel whereCreateAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\FlowerLogModel whereFixedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\FlowerLogModel whereGoldFlower($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\FlowerLogModel whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\FlowerLogModel whereJsk($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\FlowerLogModel whereRedFlower($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\FlowerLogModel whereRemark($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\FlowerLogModel whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\FlowerLogModel whereTypeId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\FlowerLogModel whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\FlowerLogModel whereUpdateAt($value)
 * @mixin \Eloquent
 */
class FlowerLogModel extends Model
{
    protected $table = "kdgx_partner_charge_pay_logs";

    protected $dateFormat = 'U';

    public const CREATED_AT = 'create_at';
    public const UPDATED_AT = null;

    protected $fillable = ['uid', 'type', 'gold_flower', 'red_flower', 'jsk', 'type_id', 'remark', 'fixed_at'];

    private $type = array(
        1 => [
            'type' => 1,
            'desc' => '消费卡片',
        ],
        2 => [
            'type' => 2,
            'desc' => '任务',
        ],
        3 => [
            'type' => 3,
            'desc' => '充值',
        ],
        4 => [
            'type' => 4,
            'desc' => '系统扣除',
        ],
        5 => [
            'type' => 5,
            'desc' => '系统补偿',
        ],
        6 => [
            'type' => 6,
            'desc' => '收获',
        ],
        7 => [
            'type' => 7,
            'desc' => '签到',
        ],
        8 => [
            'type' => 8,
            'desc' => '赠送',
        ],
        9 => [
            'type' => 9,
            'desc' => '获赠',
        ],
        10 => [
            'type' => 10,
            'desc' => '分享奖励',
        ],
        11 => [
            'type' => 11,
            'desc' => '消费退回一张',
        ],
        12 => [
            'type' => 12,
            'desc' => '红包',
        ],
        13 => [
            'type' => 13,
            'desc' => '72小时提升匹配成功率'
        ],
        14 => [
            'type' => 14,
            'desc' => '心动邀请'
        ],
        15 => [
            'type' => 15,
            'desc' => '会员每日小花礼包'
        ]
    );

    /**
     * 获取奖励红包 1.小花|2.小花红包|3.现金红包
     * @param  int  $uid
     * @return bool|string
     * @throws Exception
     */
    public function redpack(int $uid)
    {
        $feed_limit = Redis::hget("session_msy_{$uid}", "feed_limit") ?? 1;
        switch ($feed_limit) {
            case 5: // 小花红包
            case 15: // 小花
                $red_flower = sprintf("%.2f", 0.5 + mt_rand() / mt_getrandmax() * (0.8 - 0.5));
                break;
            case 30:
            case 50:
            case 70:
            case 90:
                $red_flower = sprintf("%.2f", 0.3 + mt_rand() / mt_getrandmax() * (0.5 - 0.3));
                break;
            default:
                return false;
        }
        DB::beginTransaction();
        try {
            DB::table('kdgx_partner_charge_user')->where('uid', $uid)->increment('red_flower', $red_flower);
            $data = array(
                'uid' => $uid,
                'create_at' => time(),
                'type' => 12,
                'gold_flower' => 0,
                'red_flower' => $red_flower,
                'jsk' => 0,
                'remark' => "划卡片红包"
            );
            $flowerLog = self::create($data);

            dispatch_now(new SignFlowerReportJob($flowerLog, UserModel::find($uid)));
            DB::commit();
            return array(
                'fee' => $red_flower,
                'type' => 1
            );
        } catch (Exception $e) {
            DB::rollBack();
            return false;
        }
    }
}
