package com.ruoyi.web.controller.school;

import java.util.List;
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 com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TbSchoolFacilities;
import com.ruoyi.system.service.ITbSchoolFacilitiesService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学校设施Controller
 * 
 * @author ruoyi
 * @date 2022-10-12
 */
@Controller
@RequestMapping("/system/facilities")
public class TbSchoolFacilitiesController extends BaseController
{
    private String prefix = "system/facilities";

    @Autowired
    private ITbSchoolFacilitiesService tbSchoolFacilitiesService;

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

    /**
     * 查询学校设施列表
     */
    @RequiresPermissions("system:facilities:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TbSchoolFacilities tbSchoolFacilities)
    {
        startPage();
        List<TbSchoolFacilities> list = tbSchoolFacilitiesService.selectTbSchoolFacilitiesList(tbSchoolFacilities);
        return getDataTable(list);
    }

    /**
     * 导出学校设施列表
     */
    @RequiresPermissions("system:facilities:export")
    @Log(title = "学校设施", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TbSchoolFacilities tbSchoolFacilities)
    {
        List<TbSchoolFacilities> list = tbSchoolFacilitiesService.selectTbSchoolFacilitiesList(tbSchoolFacilities);
        ExcelUtil<TbSchoolFacilities> util = new ExcelUtil<TbSchoolFacilities>(TbSchoolFacilities.class);
        return util.exportExcel(list, "学校设施数据");
    }

    /**
     * 新增学校设施
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存学校设施
     */
    @RequiresPermissions("system:facilities:add")
    @Log(title = "学校设施", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TbSchoolFacilities tbSchoolFacilities)
    {
        tbSchoolFacilities.setCreator(getUserId());
        return toAjax(tbSchoolFacilitiesService.insertTbSchoolFacilities(tbSchoolFacilities));
    }

    /**
     * 修改学校设施
     */
    @RequiresPermissions("system:facilities:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TbSchoolFacilities tbSchoolFacilities = tbSchoolFacilitiesService.selectTbSchoolFacilitiesById(id);
        mmap.put("tbSchoolFacilities", tbSchoolFacilities);
        return prefix + "/edit";
    }

    /**
     * 修改保存学校设施
     */
    @RequiresPermissions("system:facilities:edit")
    @Log(title = "学校设施", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TbSchoolFacilities tbSchoolFacilities)
    {
        if(tbSchoolFacilities.getFacilities().startsWith(",")){
            tbSchoolFacilities.setFacilities(tbSchoolFacilities.getFacilities().substring(1));
        }
        tbSchoolFacilities.setModifiedBy(getUserId());
        return toAjax(tbSchoolFacilitiesService.updateTbSchoolFacilities(tbSchoolFacilities));
    }

    /**
     * 删除学校设施
     */
    @RequiresPermissions("system:facilities:remove")
    @Log(title = "学校设施", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tbSchoolFacilitiesService.deleteTbSchoolFacilitiesByIds(ids));
    }
}
