<?php

namespace app\admin\controller\system;

use app\admin\controller\AuthController;
use crmeb\services\{UtilService, JsonService as Json};
use app\models\system\SystemAwardHistory as SystemAwardHistoryModel;


/**
 * 活动奖励记录
 */
class SystemAwardHistory extends AuthController
{
    public function index()
    {
        return $this->fetch();
    }

    public function getAwardHistory()
    {
        $where = UtilService::getMore([
            ['page', 1],
            ['limit', 20],
            ['activity', ''],
            ['period', ''],
        ]);
        $model = SystemAwardHistoryModel::page(intval($where['page']), intval($where['limit']));
        if ($where['activity']) {
            $model = $model->where('activity', $where['activity']);
        }
        if ($where['period']) {
            list($startTime, $endTime) = explode(' - ', $where['period']);
            $model = $model->where('ts', '>', strtotime($startTime));
            $model = $model->where('ts', '<', strtotime($endTime) + 24 * 3600);
        }
        $list = $model->field('*, FROM_UNIXTIME(ts, "%Y-%m-%d %H:%i:%s") as stime')->order('ts desc')->select();
        $count = $model->count();

        return Json::successlayui(['count'=>$count, 'data'=>$list]);
    }
}