package beilv.session.controller;

import java.util.List;

import beilv.session.domain.BeilvSession;
import beilv.session.service.IBeilvSessionService;
import beilv.venue.domain.BeilvVenue;
import beilv.venue.service.IBeilvVenueService;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
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 lc
 * @date 2025-01-06
 */
@Controller
@RequestMapping("/system/session")
public class BeilvSessionController extends BaseController {
    private String prefix = "system/session";

    @Autowired
    private IBeilvSessionService beilvSessionService;

    @Autowired
    private IBeilvVenueService beilvVenueService;

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

    /**
     * 查询时段管理列表
     */
    @RequiresPermissions("system:session:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BeilvSession beilvSession) {
        startPage();
        List<BeilvSession> list = beilvSessionService.selectBeilvSessionList(beilvSession);
        return getDataTable(list);
    }

    /**
     * 导出时段管理列表
     */
    @RequiresPermissions("system:session:export")
    @Log(title = "时段管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BeilvSession beilvSession) {
        List<BeilvSession> list = beilvSessionService.selectBeilvSessionList(beilvSession);
        ExcelUtil<BeilvSession> util = new ExcelUtil<BeilvSession>(BeilvSession.class);
        return util.exportExcel(list, "时段管理数据");
    }

    /**
     * 新增时段管理
     */
    @RequiresPermissions("system:session:add")
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        List<BeilvVenue> beilvSitesList = beilvVenueService.selectBeilvVenueList(new BeilvVenue());
        mmap.put("beilvSitesList", beilvSitesList);
        return prefix + "/add";
    }

    /**
     * 新增保存时段管理
     */
    @RequiresPermissions("system:session:add")
    @Log(title = "时段管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BeilvSession beilvSession) {
        return judgment(beilvSessionService.insertBeilvSession(beilvSession));
    }

    /**
     * 修改时段管理
     */
    @RequiresPermissions("system:session:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        BeilvSession beilvSession = beilvSessionService.selectBeilvSessionById(id);
        List<BeilvVenue> beilvSitesList = beilvVenueService.selectBeilvVenueList(new BeilvVenue());
        mmap.put("beilvSitesList", beilvSitesList);
        mmap.put("beilvSession", beilvSession);
        return prefix + "/edit";
    }

    /**
     * 修改保存时段管理
     */
    @RequiresPermissions("system:session:edit")
    @Log(title = "时段管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BeilvSession beilvSession) {
        return judgment(beilvSessionService.updateBeilvSession(beilvSession));
    }

    private AjaxResult judgment(int i) {
        if (i > 0) {
            return toAjax(i);
        } else {
            return AjaxResult.error("当前数据已存在");
        }
    }

    /**
     * 删除时段管理
     */
    @RequiresPermissions("system:session:remove")
    @Log(title = "时段管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(beilvSessionService.deleteBeilvSessionByIds(ids));
    }
}
