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

use app\dice\basic\DiceBaseLogic;
use plugin\saiadmin\exception\ApiException;
use plugin\saiadmin\utils\Helper;
use app\dice\model\player_ticket_record\DicePlayerTicketRecord;

/**
 * 抽奖券获取记录逻辑层
 */
class DicePlayerTicketRecordLogic extends DiceBaseLogic
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new DicePlayerTicketRecord();
        // 默认按主键倒序：最新数据优先展示
        $this->setOrderType('DESC');
    }

    /**
     * 添加前：total_ticket_count = paid_ticket_count + free_ticket_count
     */
    public function add(array $data): mixed
    {
        $data = $this->fillTotalTicketCount($data);
        return parent::add($data);
    }

    /**
     * 修改前：total_ticket_count = paid_ticket_count + free_ticket_count
     */
    public function edit($id, array $data): mixed
    {
        $data = $this->fillTotalTicketCount($data);
        return parent::edit($id, $data);
    }

    private function fillTotalTicketCount(array $data): array
    {
        $paid = isset($data['paid_ticket_count']) ? (int) $data['paid_ticket_count'] : 0;
        $free = isset($data['free_ticket_count']) ? (int) $data['free_ticket_count'] : 0;
        $data['total_ticket_count'] = $paid + $free;
        return $data;
    }
}
