package beilv.web.controller.competition;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import beilv.competition.domain.Competition;
import beilv.competition.service.ICompetitionService;
import beilv.site.domain.BeilvSite;
import beilv.site.service.IBeilvSiteService;
import beilv.stadium.domain.Stadium;
import beilv.stadium.service.IStadiumService;
import beilv.vipCardLog.domain.VipCardLog;
import beilv.vipCardLog.service.IVipCardLogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import beilv.common.annotation.Log;
import beilv.common.enums.BusinessType;
import beilv.common.core.controller.BaseController;
import beilv.common.core.domain.AjaxResult;
import beilv.common.utils.poi.ExcelUtil;
import beilv.common.core.page.TableDataInfo;

/**
 * 赛事发布Controller
 *
 * @author LG
 * @date 2024-12-31
 */
@Controller
@RequestMapping("/competition")
public class CompetitionController extends BaseController {
    private String prefix = "competition";

    @Autowired
    private ICompetitionService competitionService;

    @Autowired
    private IBeilvSiteService siteService;

//    @Autowired
//    private IStadiumService stadiumService;
    @Autowired
    private IVipCardLogService vipCardLogService;

    @RequiresPermissions("system:competition:view")
    @GetMapping()
    public String competition() {
        return prefix + "/competition";
    }

    /**
     * 查询赛事发布列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Competition competition) {
        startPage();
        List<Competition> list = competitionService.selectCompetitionList(competition);
        return getDataTable(list);
    }

    /**
     * 导出赛事发布列表
     */
    @RequiresPermissions("system:competition:export")
    @Log(title = "赛事发布", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Competition competition) {
        List<Competition> list = competitionService.selectCompetitionList(competition);
        ExcelUtil<Competition> util = new ExcelUtil<Competition>(Competition.class);
        return util.exportExcel(list, "赛事发布数据");
    }

    /**
     * 新增赛事发布
     */
    @RequiresPermissions("system:competition:add")
    @GetMapping("/add")
    public String add( ModelMap mmap) {
        List<BeilvSite> allToList = siteService.getAllToList();
        mmap.put("siteList",allToList);
        return prefix + "/add";
    }

    /**
     * 新增保存赛事发布
     */
    @RequiresPermissions("system:competition:add")
    @Log(title = "赛事发布", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Competition competition) {
        return toAjax(competitionService.insertCompetition(competition));
    }

    /**
     * 修改赛事发布
     */
    @RequiresPermissions("system:competition:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        mmap.put("competition", competitionService.selectCompetitionById(id));
        List<BeilvSite> allToList = siteService.getAllToList();
        mmap.put("siteList",allToList);
        return prefix + "/edit";
    }

    /**
     * 修改保存赛事发布
     */
    @RequiresPermissions("system:competition:edit")
    @Log(title = "赛事发布", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Competition competition) {
        return toAjax(competitionService.updateCompetition(competition));
    }

    /**
     * 删除赛事发布
     */
    @RequiresPermissions("system:competition:remove")
    @Log(title = "赛事发布", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(competitionService.deleteCompetitionByIds(ids));
    }

    /**
     * 赛事发布
     */
    @RequiresPermissions("system:competition:publish")
    @Log(title="赛事发布", businessType=BusinessType.UPDATE)
    @PostMapping("/publish")
    @ResponseBody
    public AjaxResult publish(Competition competition){
       return toAjax(competitionService.publishCompetitionById(competition));
    }

    /**
     * 关闭赛事
     * @param id
     * @return
     */
    @RequiresPermissions("system:competition:publish")
    @GetMapping("/close")
    @ResponseBody
    public AjaxResult close(String id){
        int i = competitionService.closeCompetitionById(id);
        return toAjax(i);
    }

    @GetMapping("/getCompetitionList")
    @ResponseBody
    public AjaxResult getCompetitionList(){
        return AjaxResult.success(competitionService.getCompetitionList());
    }


    /**
     * 查询购票记录
     */
    @PostMapping("/getTakePartList")
    @ResponseBody
    public TableDataInfo getTakePartList(VipCardLog vipCardLog) {
        startPage();
        //通过id获取参赛集合
        List<Map<String, Object>> maps = vipCardLogService.selectCanSaiList(vipCardLog);
        return getDataTable(maps);
    }


    @RequiresPermissions("system:competition:view")
    @GetMapping("/openDetails/{id}")
    public String openDetails(@PathVariable("id") Integer id, ModelMap mmap) {
        mmap.put("busId", id);
        return prefix + "/details";
    }

    /**
     * 赛事线下退款
     * @param orderId 购票记录id
     * @return
     */
    @GetMapping("/close/{orderId}")
    @ResponseBody
    public AjaxResult closeStadiumById(@PathVariable("orderId") String orderId) {
        return vipCardLogService.refundById(new VipCardLog(orderId), false, true);
    }

}
