<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\dice\model\player_coin_record;

use app\dice\model\player\DicePlayer;
use plugin\saiadmin\basic\think\BaseModel;
use think\model\relation\BelongsTo;

/**
 * 玩家购买抽奖记录模型
 *
 * dice_player_coin_record 玩家购买抽奖记录
 *
 * @property  $id ID
 * @property  $player_id 玩家id
 * @property  $use_coins 消耗硬币
 * @property  $total_draw_count 总抽奖次数
 * @property  $paid_draw_count 购买抽奖次数
 * @property  $free_draw_count 赠送抽奖次数
 * @property  $remark 备注
 * @property  $create_time 创建时间
 * @property  $update_time 修改时间
 */
class DicePlayerCoinRecord extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 数据库表名称
     * @var string
     */
    protected $table = 'dice_player_coin_record';

    /**
     * 关联模型 dicePlayer
     */
    public function dicePlayer(): BelongsTo
    {
        return $this->belongsTo(DicePlayer::class, 'player_id', 'id');
    }

    /**
     * 按关联玩家用户名搜索（dicePlayer.username）
     */
    public function searchUsernameAttr($query, $value)
    {
        if ($value === '' || $value === null) {
            return;
        }
        $playerIds = DicePlayer::where('username', 'like', '%' . $value . '%')->column('id');
        if (!empty($playerIds)) {
            $query->whereIn('player_id', $playerIds);
        } else {
            $query->whereRaw('1=0');
        }
    }

    /** 消耗硬币下限 */
    public function searchUseCoinsMinAttr($query, $value)
    {
        if ($value !== '' && $value !== null) {
            $query->where('use_coins', '>=', $value);
        }
    }

    /** 消耗硬币上限 */
    public function searchUseCoinsMaxAttr($query, $value)
    {
        if ($value !== '' && $value !== null) {
            $query->where('use_coins', '<=', $value);
        }
    }

    /** 总抽奖次数下限 */
    public function searchTotalDrawCountMinAttr($query, $value)
    {
        if ($value !== '' && $value !== null) {
            $query->where('total_draw_count', '>=', $value);
        }
    }

    /** 总抽奖次数上限 */
    public function searchTotalDrawCountMaxAttr($query, $value)
    {
        if ($value !== '' && $value !== null) {
            $query->where('total_draw_count', '<=', $value);
        }
    }

    /** 购买抽奖次数下限 */
    public function searchPaidDrawCountMinAttr($query, $value)
    {
        if ($value !== '' && $value !== null) {
            $query->where('paid_draw_count', '>=', $value);
        }
    }

    /** 购买抽奖次数上限 */
    public function searchPaidDrawCountMaxAttr($query, $value)
    {
        if ($value !== '' && $value !== null) {
            $query->where('paid_draw_count', '<=', $value);
        }
    }

    /** 赠送抽奖次数下限 */
    public function searchFreeDrawCountMinAttr($query, $value)
    {
        if ($value !== '' && $value !== null) {
            $query->where('free_draw_count', '>=', $value);
        }
    }

    /** 赠送抽奖次数上限 */
    public function searchFreeDrawCountMaxAttr($query, $value)
    {
        if ($value !== '' && $value !== null) {
            $query->where('free_draw_count', '<=', $value);
        }
    }

    /** 创建时间起始 */
    public function searchCreateTimeMinAttr($query, $value)
    {
        if ($value !== '' && $value !== null) {
            $query->where('create_time', '>=', $value);
        }
    }

    /** 创建时间结束 */
    public function searchCreateTimeMaxAttr($query, $value)
    {
        if ($value !== '' && $value !== null) {
            $query->where('create_time', '<=', $value);
        }
    }
}
