<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\LhcLottery as LhcLotteryModel;
use Exception;
use App\Constants\HttpStatus;

class LhcLottery extends Controller
{
    /**
     * 开奖管理列表
     */
    public function list()
    {
        try {
            $params = request()->validate([
                'page' => ['nullable', 'integer', 'min:1'],
                'limit' => ['nullable', 'integer', 'min:1'],
                'type' => ['nullable', 'integer', 'min:1'],
                'status' => ['nullable', 'integer', 'min:1'],
                'is_settlement' => ['nullable', 'integer'],
                'issue' => ['nullable', 'string'],
                'open_code' => ['nullable', 'string'],
                'start_time' => ['nullable', 'string'],
                'end_time' => ['nullable', 'string'],
            ]);
            $page = request()->input('page', 1);
            $limit = request()->input('limit', 15);

            $query = new LhcLotteryModel();
            if (!empty($params['type'])) {
                $query = $query->where('type', $params['type']);
            }
            if (!empty($params['status'])) {
                $query = $query->where('status', $params['status']);
            }
            if (isset($params['is_settlement'])) {
                $query = $query->where('is_settlement', $params['is_settlement']);
            }
            if (!empty($params['issue'])) {
                $query = $query->where('issue', $params['issue']);
            }
            if (!empty($params['open_code'])) {
                $query = $query->where('open_code', $params['open_code']);
            }
            if (!empty($params['start_time'])) {
                $query = $query->where('open_time', '>=', strtotime($params['start_time'].' 00:00:00'));
            }
            if (!empty($params['end_time'])) {
                $query = $query->where('open_time', '<', strtotime($params['end_time'].' 23:59:59'));
            }
            $count = $query->count();
            $list = $query
                ->forPage($page, $limit)
                ->orderByDesc('open_time')
                ->get();
        } catch (Exception $e) {
            return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
        }
        return $this->success(['total' => $count, 'data' => $list]);

    }
    
    //设置预开奖号码
    public function setAdvanceCode()
    {
        try {
            $params = request()->validate([
                'id' => ['required','integer'],
                'advance_code' => ['required','array'],
            ]);
            $id = $params['id'];
            if (count($params['advance_code']) != 7) {
                throw new Exception('开奖号码必须是7个数');
            }
            $info = LhcLotteryModel::where('id', $id)->first();
            if (!$info) throw new Exception('数据不存在');
            if (!empty($info->open_code)) {
                throw new Exception('已开奖');
            }
            if (!empty($info->advance_code)) {
                throw new Exception('已设置');
            }
            $info->advance_code = implode(",",$params['advance_code']);
            $info->save();
            return $this->success();
        } catch (Exception $e) {
            return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
        }
    }

}