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

use plugin\saiadmin\basic\think\BaseLogic;
use plugin\saiadmin\exception\ApiException;
use plugin\saiadmin\utils\Helper;
use app\dice\model\player_coin_record\DicePlayerCoinRecord;

/**
 * 玩家购买抽奖记录逻辑层
 */
class DicePlayerCoinRecordLogic extends BaseLogic
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new DicePlayerCoinRecord();
    }

    /**
     * 添加前：总抽奖次数 = 购买抽奖次数 + 赠送抽奖次数
     */
    public function add(array $data): mixed
    {
        $data = $this->fillTotalDrawCount($data);
        return parent::add($data);
    }

    /**
     * 修改前：总抽奖次数 = 购买抽奖次数 + 赠送抽奖次数
     */
    public function edit($id, array $data): mixed
    {
        $data = $this->fillTotalDrawCount($data);
        return parent::edit($id, $data);
    }

    private function fillTotalDrawCount(array $data): array
    {
        $paid = isset($data['paid_draw_count']) ? (int) $data['paid_draw_count'] : 0;
        $free = isset($data['free_draw_count']) ? (int) $data['free_draw_count'] : 0;
        $data['total_draw_count'] = $paid + $free;
        return $data;
    }
}
