<?php
namespace app\index\controller;
use think\facade\Db;
use app\index\controller\Common;
use app\service\SeoService;
use app\service\ApiService;
use app\service\ArticleService;
use app\service\ArticleCategoryService;
use app\service\BreadcrumbService;
use app\service\MultilingualService;
use app\service\LinkService;

/**
 * 历史大奖
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
class History extends Common {
	/**
     * 构造方法
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-11-30
     * @desc    description
     */
    public function __construct() {
        parent::__construct();
    }

	/**
     * 历史大奖
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2021-11-08
     * @desc    description
     */
	public function Index() {

        $params = $this->data_request;

        // 获取历史列表
        $ret = self::HistoryList($params);

        // 分页
        $page_params = [
            'number'    => $ret['data']['page_size'],
            'total'     => $ret['data']['total'],
            'where'     => $this->data_request,
            'page'      => $ret['data']['page'],
            'url'       => MyUrl('index/History/index'),
            'bt_number' => IsMobile() ? 2 : 4,
        ];
        $page = new \base\Page($page_params);
        $page_html = $page->GetPageHtml();

        // 模板数据
        $assign = [
            // 列表数据
            'news_title'       => Mylang('common.historical_grand_prize'),
            'page_html'        => $page_html,
            'data_list'        => $ret['data']['data'],
            'data_total'       => $ret['data']['total'],
            'params'           => $params,
            'article_footer_list'      => ArticleService::RecommendedArticleList(111,1),
            // 友情链接
            'link_list'   => LinkService::HomeLinkList(),
        ];

        $assign['home_seo_site_title'] = Mylang('common.historical_grand_prize');
        $assign['home_seo_site_keywords'] = MyLang('lottery.home_seo_site_keywords');
        $assign['home_seo_site_description'] = MyLang('lottery.home_seo_site_description');

        // 数据赋值
        MyViewAssign($assign);
        return MyView();
	}
    /**
     * 获取数据
     */
    public static function HistoryList($params = []) {
        $lang = MultilingualService::GetUserMultilingualValue();
        // 返回格式
        $result = [
            'page_start'    => 0,
            'page_size'     => 0,
            'page'          => 1,
            'page_total'    => 0,
            'total'         => 0,
            'data'          => [],
        ];
        $where = [];
        $order_by = 'lottery_date desc';
        $field = 'a.*,r.area_name,r.area_nameen,r.area_namehi,r.area_namees';
        $result['page'] = max(1, isset($params['page']) ? intval($params['page']) : 1);
        $result['page_size'] = empty($params['page_size']) ? MyC('home_search_limit_number', 20, true) : intval($params['page_size']);
        // 数量不能超过500
        if($result['page_size'] > 500) {
            $result['page_size'] = 500;
        }
        $result['page_start'] = intval(($result['page']-1)*$result['page_size']);

        $data_query = Db::name('aatopprize')->alias('a')->join('areas r', 'a.areaid=r.areaid');

        $result['total'] = (int) $data_query->where($where)->count();
        
        $result['data'] = $data_query->where($where)->field($field)->order($order_by)->limit($result['page_start'], $result['page_size'])->select()->toArray();
        foreach($result['data'] as $k=>&$v) {
            switch($lang){
                case 'zh': $result['data'][$k]['areaid'] = $v['area_name']; break;
                case 'hi': $result['data'][$k]['areaid'] = $v['area_namehi']; break;
                case 'es': $result['data'][$k]['areaid'] = $v['area_namees']; break;
                default: $result['data'][$k]['areaid'] = $v['area_nameen']; break;
            }
            $type = $v['lottery_type'];
            switch($type){
                case 1: $result['data'][$k]['lottery_type'] = Mylang('common.menu_zodiac'); break;
                case 2: $result['data'][$k]['lottery_type'] = Mylang('common.menu_hermes'); break;
                case 3: $result['data'][$k]['lottery_type'] = Mylang('common.menu_demeter'); break;
                case 4: $result['data'][$k]['lottery_type'] = Mylang('common.menu_ploutos'); break;
                case 5: $result['data'][$k]['lottery_type'] = Mylang('common.menu_fates'); break;
                case 6: $result['data'][$k]['lottery_type'] = Mylang('common.menu_immortality'); break;
            }
        }
        $result['page_total'] = ceil($result['total']/$result['page_size']);

        return DataReturn(MyLang('handle_success'), 0, $result);

    }


}
?>