package com.xinelu.manage.controller.infocategory;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.xinelu.common.custominterface.Insert;
import com.xinelu.manage.dto.infocategory.InfoCategoryDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xinelu.common.annotation.Log;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.enums.BusinessType;
import com.xinelu.manage.domain.infocategory.InfoCategory;
import com.xinelu.manage.service.infocategory.IInfoCategoryService;
import com.xinelu.common.utils.poi.ExcelUtil;
import com.xinelu.common.core.page.TableDataInfo;

/**
 * 资讯分类Controller
 *
 * @author xinelu
 * @date 2024-04-11
 */
@RestController
@RequestMapping("/manage/category")
public class InfoCategoryController extends BaseController {
    @Resource
    private IInfoCategoryService infoCategoryService;

    /**
     * 查询资讯分类列表
     */
    @PreAuthorize("@ss.hasPermi('manage:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(InfoCategory infoCategory) {
        startPage();
        List<InfoCategory> list = infoCategoryService.selectInfoCategoryList(infoCategory);
        return getDataTable(list);
    }

    /**
     * 导出资讯分类列表
     */
    @PreAuthorize("@ss.hasPermi('manage:category:export')")
    @Log(title = "资讯分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InfoCategory infoCategory) {
        List<InfoCategory> list = infoCategoryService.selectInfoCategoryList(infoCategory);
        ExcelUtil<InfoCategory> util = new ExcelUtil<>(InfoCategory.class);
        util.exportExcel(response, list, "资讯分类数据");
    }

    /**
     * 获取资讯分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(infoCategoryService.selectInfoCategoryById(id));
    }

    /**
     * 新增资讯分类
     */
    @PreAuthorize("@ss.hasPermi('manage:category:add')")
    @Log(title = "资讯分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody @Validated(Insert.class) InfoCategoryDTO infoCategoryDTO) {

        return infoCategoryService.insertInfoCategory(infoCategoryDTO);
    }


    /**
     * 修改资讯分类
     */
    @PreAuthorize("@ss.hasPermi('manage:category:edit')")
    @Log(title = "资讯分类", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody InfoCategory infoCategory) {
        return infoCategoryService.updateInfoCategory(infoCategory);
    }

    /**
     * 删除资讯分类
     */
    @PreAuthorize("@ss.hasPermi('manage:category:remove')")
    @Log(title = "资讯分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(infoCategoryService.deleteInfoCategoryByIds(ids));
    }
}
