<?php
namespace app\model;

class Message extends Base 
{
    /**
     * 获取留言列表
     */
    public static function getList($per = 20, $startDate='', $endDate='')
    {
        $param = [];
        if(!empty($startDate)) {
            $param['startDate'] = $startDate;
        }
        if(!empty($endDate)) {
            $param['endDate'] = $endDate;
        }

        $paginate = [
            'list_rows' => $per,
            'query' => $param
        ];

        $items = self::when(!empty($startDate) && strtotime($startDate), function ($query) use($startDate) {
            $startTime = strtotime(date('Y-m-d 00:00:00', strtotime($startDate)));
            $query->where('create_time', '>=', $startTime);
        })
        ->when(!empty($endDate) && strtotime($endDate), function ($query) use($endDate) {
            $endTime = strtotime(date('Y-m-d 23:59:59', strtotime($endDate)));
            $query->where('create_time', '<=', $endTime);
        })
        ->order("create_time", 'desc')
        ->paginate($paginate);

        return $items;
    }

    public static function onAfterInsert($item)
    {
        $item->create_time = time();
        $item->save();
    }

    public static function getExportList($startDate='', $endDate='', $limit = 10000)
    {
        return self::when(!empty($startDate) && strtotime($startDate), function ($query) use($startDate) {
            $startTime = strtotime(date('Y-m-d 00:00:00', strtotime($startDate)));
            $query->where('create_time', '>=', $startTime);
        })
        ->when(!empty($endDate) && strtotime($endDate), function ($query) use($endDate) {
            $endTime = strtotime(date('Y-m-d 23:59:59', strtotime($endDate)));
            $query->where('create_time', '<=', $endTime);
        })
        ->order('create_time', 'desc')
        ->limit($limit)
        ->select()
        ->toArray();
    }

}