package com.ruoyi.web.controller.school;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
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.TbNews;
import com.ruoyi.system.service.ITbNewsService;
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;
import java.net.URLDecoder;

/**
 * 新闻与活动Controller
 * 
 * @author ruoyi
 * @date 2022-10-25
 */
@Controller
@RequestMapping("/system/news")
public class TbNewsController extends BaseController
{
    private String prefix = "business/news";

    @Autowired
    private ITbNewsService tbNewsService;

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

    /**
     * 查询新闻与活动列表
     */
    @RequiresPermissions("system:news:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TbNews tbNews)
    {
        startPage();
        List<TbNews> list = tbNewsService.selectTbNewsList(tbNews);
        return getDataTable(list);
    }

    /**
     * 导出新闻与活动列表
     */
    @RequiresPermissions("system:news:export")
    @Log(title = "新闻与活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TbNews tbNews)
    {
        List<TbNews> list = tbNewsService.selectTbNewsList(tbNews);
        ExcelUtil<TbNews> util = new ExcelUtil<TbNews>(TbNews.class);
        return util.exportExcel(list, "新闻与活动数据");
    }

    /**
     * 新增新闻与活动
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存新闻与活动
     */
    @RequiresPermissions("system:news:add")
    @Log(title = "新闻与活动", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TbNews tbNews)
    {
        tbNews.setCreator(getUserId());
        return toAjax(tbNewsService.insertTbNews(tbNews));
    }

    /**
     * 修改新闻与活动
     */
    @RequiresPermissions("system:news:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TbNews tbNews = tbNewsService.selectTbNewsById(id);
        mmap.put("tbNews", tbNews);
        return prefix + "/edit";
    }

    /**
     * 修改保存新闻与活动
     */
    @RequiresPermissions("system:news:edit")
    @Log(title = "新闻与活动", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TbNews tbNews)
    {
        tbNews.setModifiedBy(getUserId());
        return toAjax(tbNewsService.updateTbNews(tbNews));
    }

    /**
     * 删除新闻与活动
     */
    @RequiresPermissions("system:news:remove")
    @Log(title = "新闻与活动", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tbNewsService.deleteTbNewsByIds(ids));
    }
}
