<?php
declare(strict_types=1);

namespace app\manager\controller;

use think\admin\Controller;
use think\admin\service\AdminService;

/**
 * 回传记录管理
 * @auth true
 * @menu true
 */
class PackageCallbackRecords extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    protected $table = 'offer_package_callback_records';

    /**
     * 回传记录列表
     * @auth true
     * @menu true
     */
    public function index()
    {
        $this->title = '回传记录列表';
        $this->fetch();
    }

    /**
     * 获取回传记录列表
     * @auth true
     */
    public function get_list()
    {
        try {
            $page = input('page/d', 1);
            $limit = input('limit/d', 15);
            $where = [];
            
            // 包名搜索
            if ($package_name = input('package_name/s', '')) {
                $where[] = ['package_name', 'like', "%{$package_name}%"];
            }
            
            // 事件名称搜索
            if ($event_name = input('event_name/s', '')) {
                $where[] = ['event_name', 'like', "%{$event_name}%"];
            }
            
            // 推送ID搜索
            if ($push_record_id = input('push_record_id/d', '')) {
                $where[] = ['push_record_id', '=', $push_record_id];
            }
            
            // 状态筛选
            if ($status = input('status/s', '')) {
                $where[] = ['status', '=', $status];
            }

            // 时间范围筛选
            if ($start_time = input('start_time/s', '')) {
                $where[] = ['callback_time', '>=', $start_time];
            }
            if ($end_time = input('end_time/s', '')) {
                $where[] = ['callback_time', '<=', $end_time];
            }

            // 查询数据
            $query = $this->app->db->name($this->table)
                ->alias('a')
                ->join('offer_package_push_records b', 'a.push_record_id = b.id', 'left')
                ->field('a.*, b.source_type');
            
            // 获取总数
            $total = $query->where($where)->count();
            
            // 分页查询数据
            $list = $query->where($where)
                ->order('a.id desc')
                ->limit(($page - 1) * $limit, $limit)
                ->select()
                ->toArray();

            return json([
                'code' => 0,
                'msg' => '',
                'count' => $total,
                'data' => $list
            ]);
            
        } catch (\Exception $e) {
            trace("获取回传记录列表异常：" . $e->getMessage());
            return json([
                'code' => 1,
                'msg' => '获取数据失败，请稍后重试',
                'count' => 0,
                'data' => []
            ]);
        }
    }

    /**
     * 查看详情
     * @auth true
     */
    public function detail()
    {
        $id = input('id/d', 0);
        if (empty($id)) {
            $this->error('参数错误');
        }
        
        $vo = $this->app->db->name($this->table)->where('id', $id)->find();
        if (empty($vo)) {
            $this->error('记录不存在');
        }
        
        $this->vo = $vo;
        $this->fetch();
    }
} 