<?php
declare(strict_types=1);

namespace app\manager\controller;

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

/**
 * 推送记录管理
 * @auth true
 * @menu true
 */
class PackagePushRecords extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    protected $table = 'offer_package_push_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}%"];
            }
            
            // 来源筛选
            if ($source_type = input('source_type/s', '')) {
                $where[] = ['source_type', '=', $source_type];
            }

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

            // 查询数据
            $query = $this->app->db->name($this->table);
            
            // 获取总数
            $total = $query->where($where)->count();
            
            // 分页查询数据
            $list = $query->where($where)
                ->field('*')
                ->order('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();
    }
} 