package com.fs.delivery.controller;

import com.fs.common.annotation.Log;
import com.fs.common.config.LoginContextManager;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.delivery.domain.CtArticle;
import com.fs.delivery.service.ICtArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章Controller
 * 
 * @author fs
 * @date 2026-01-20
 */
@RestController
@RequestMapping("/delivery/article")
public class CtArticleController extends BaseController
{
    @Autowired
    private ICtArticleService ctArticleService;

    /**
     * 查询文章列表
     */
    @PreAuthorize("@ss.hasPermi('delivery:article:list')")
    @GetMapping("/list")
    public TableDataInfo list(CtArticle ctArticle)
    {
        startPage();
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        ctArticle.setCompanyId(currentCompanyId);
        List<CtArticle> list = ctArticleService.selectCtArticleList(ctArticle);
        return getDataTable(list);
    }

    /**
     * 导出文章列表
     */
    @PreAuthorize("@ss.hasPermi('delivery:article:export')")
    @Log(title = "文章", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CtArticle ctArticle)
    {
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        ctArticle.setCompanyId(currentCompanyId);
        List<CtArticle> list = ctArticleService.selectCtArticleList(ctArticle);
        ExcelUtil<CtArticle> util = new ExcelUtil<CtArticle>(CtArticle.class);
        return util.exportExcel(list, "文章数据");
    }

    /**
     * 获取文章详细信息
     */
    @PreAuthorize("@ss.hasPermi('delivery:article:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ctArticleService.selectCtArticleById(id));
    }

    /**
     * 新增文章
     */
    @PreAuthorize("@ss.hasPermi('delivery:article:add')")
    @Log(title = "文章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CtArticle ctArticle)
    {
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        ctArticle.setCompanyId(currentCompanyId);
        return toAjax(ctArticleService.insertCtArticle(ctArticle));
    }

    /**
     * 修改文章
     */
    @PreAuthorize("@ss.hasPermi('delivery:article:edit')")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CtArticle ctArticle)
    {
        Long currentCompanyId = LoginContextManager.getCurrentCompanyId();
        ctArticle.setCompanyId(currentCompanyId);
        return toAjax(ctArticleService.updateCtArticle(ctArticle));
    }

    /**
     * 删除文章
     */
    @PreAuthorize("@ss.hasPermi('delivery:article:remove')")
    @Log(title = "文章", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ctArticleService.deleteCtArticleByIds(ids));
    }
}
