package com.goldenwater.slgc.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.goldenwater.common.annotation.Log;
import com.goldenwater.common.core.controller.BaseController;
import com.goldenwater.common.core.domain.AjaxResult;
import com.goldenwater.common.core.page.TableDataInfo;
import com.goldenwater.common.enums.BusinessType;
import com.goldenwater.common.utils.poi.ExcelUtil;
import com.goldenwater.slgc.domain.CmsArticle;
import com.goldenwater.slgc.service.CmsArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;

/**
 * 内容文章管理
 *
 * @author goldenwater
 */
@Tag(name = "13. 内容文章管理")
@RestController
@RequestMapping("/slgc/cms-article")
public class CmsArticleController extends BaseController {
    @Autowired
    private CmsArticleService cmsArticleService;

    /**
     * 查询内容文章列表
     *
     * @param cmsArticle 内容文章查询条件
     * @return 分页列表数据
     */
    @Operation(summary = "13.01 查询内容文章列表")
    @GetMapping("/list")
    public TableDataInfo list(CmsArticle cmsArticle) {
        startPage();
        List<CmsArticle> list = cmsArticleService.selectCmsArticleList(cmsArticle);
        return getDataTable(list);
    }

    /**
     * 获取内容文章详细信息
     *
     * @param aid 文章ID
     * @return 内容文章信息
     */
    @Operation(summary = "13.02 获取内容文章详细信息")
    @GetMapping(value = "/{aid}")
    public AjaxResult getInfo(@PathVariable String aid) {
        return success(cmsArticleService.selectCmsArticleById(aid));
    }

    /**
     * 新增内容文章
     *
     * @param cmsArticle 内容文章信息
     * @return 操作结果
     */
    @Operation(summary = "13.03 新增内容文章")
    @Log(title = "内容文章管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmsArticle cmsArticle) {
        return toAjax(cmsArticleService.insertCmsArticle(cmsArticle));
    }

    /**
     * 修改内容文章
     *
     * @param cmsArticle 内容文章信息
     * @return 操作结果
     */
    @Operation(summary = "13.04 修改内容文章")
    @Log(title = "内容文章管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmsArticle cmsArticle) {
        return toAjax(cmsArticleService.updateCmsArticle(cmsArticle));
    }

    /**
     * 删除内容文章
     *
     * @param aids 待删除的文章ID数组
     * @return 操作结果
     */
    @Operation(summary = "13.05 删除内容文章")
    @Log(title = "内容文章管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{aids}")
    public AjaxResult remove(@PathVariable String[] aids) {
        return toAjax(cmsArticleService.deleteCmsArticleByIds(aids));
    }

    /**
     * 批量新增内容文章
     *
     * @param list 内容文章列表
     * @return 操作结果
     */
    @Operation(summary = "13.06 批量新增内容文章")
    @Log(title = "内容文章管理", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public AjaxResult batchAdd(@RequestBody List<CmsArticle> list) {
        return toAjax(cmsArticleService.batchInsert(list));
    }

    /**
     * 批量修改内容文章
     *
     * @param list 内容文章列表
     * @return 操作结果
     */
    @Operation(summary = "13.07 批量修改内容文章")
    @Log(title = "内容文章管理", businessType = BusinessType.UPDATE)
    @PutMapping("/batch")
    public AjaxResult batchEdit(@RequestBody List<CmsArticle> list) {
        return toAjax(cmsArticleService.batchUpdate(list));
    }

    /**
     * 批量删除内容文章
     *
     * @param aids 待删除的文章ID数组
     * @return 操作结果
     */
    @Operation(summary = "13.08 批量删除内容文章")
    @Log(title = "内容文章管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/batch/{aids}")
    public AjaxResult batchRemove(@PathVariable String[] aids) {
        return toAjax(cmsArticleService.deleteCmsArticleByIds(aids));
    }

    /**
     * 获取今日发布文章数
     *
     * @return 今日发布文章数量
     */
    @Operation(summary = "13.09 获取今日发布文章数")
    @GetMapping("/published/today")
    public AjaxResult getArticlesPublicNum() {
        return success(cmsArticleService.selectTodayPublishedCount());
    }

    /**
     * 导出内容文章列表
     *
     * @param cmsArticle 查询条件
     */
    @Operation(summary = "13.10 导出内容文章")
    @Log(title = "内容文章管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(CmsArticle cmsArticle, HttpServletResponse response) {
        List<CmsArticle> list = cmsArticleService.selectCmsArticleList(cmsArticle);
        ExcelUtil<CmsArticle> util = new ExcelUtil<CmsArticle>(CmsArticle.class);
        util.exportExcel(response, list, "内容文章数据");
    }

    /**
     * 导入内容文章列表
     *
     * @param file 上传的Excel文件   
     * @param cateId 目标分类ID（可选，导入时统一归类）
     * @return 导入结果
     */
    @Operation(summary = "13.11 导入内容文章")
    @Log(title = "内容文章管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file,
                                 @RequestParam(value = "cateId", required = false) String cateId) throws Exception {
        ExcelUtil<CmsArticle> util = new ExcelUtil<CmsArticle>(CmsArticle.class);
        List<CmsArticle> articleList = util.importExcel(file.getInputStream());
        String message = cmsArticleService.importCmsArticle(articleList, cateId);
        return success(message);
    }

    /**
     * 下载导入模板
     */
    @Operation(summary = "13.12 下载内容文章导入模板")
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<CmsArticle> util = new ExcelUtil<CmsArticle>(CmsArticle.class);
        util.importTemplateExcel(response, "内容文章数据");
    }
}
