package com.fs.his.controller;

import java.util.Collection;
import java.util.List;

import com.fs.common.core.redis.RedisCache;
import com.fs.his.vo.FsArticleListVO;
import com.fs.his.vo.FsArticleVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
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.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.his.domain.FsArticle;
import com.fs.his.service.IFsArticleService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 文章Controller
 *
 * @author fs
 * @date 2023-07-04
 */
@RestController
@RequestMapping("/his/article")
public class FsArticleController extends BaseController
{
    @Autowired
    private IFsArticleService fsArticleService;
    @Autowired
    RedisCache redisCache;
    /**
     * 查询文章列表
     */
    @PreAuthorize("@ss.hasPermi('his:healthArticle:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsArticle fsArticle)
    {
        startPage();
        List<FsArticleListVO> list = fsArticleService.selectFsArticleListVO(fsArticle);
        return getDataTable(list);
    }

    /**
     * 导出文章列表
     */
    @PreAuthorize("@ss.hasPermi('his:healthArticle:export')")
    @Log(title = "文章", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsArticle fsArticle)
    {
        List<FsArticle> list = fsArticleService.selectFsArticleList(fsArticle);
        ExcelUtil<FsArticle> util = new ExcelUtil<FsArticle>(FsArticle.class);
        return util.exportExcel(list, "文章数据");
    }

    /**
     * 获取文章详细信息
     */
    @PreAuthorize("@ss.hasPermi('his:healthArticle:query')")
    @GetMapping(value = "/{articleId}")
    public AjaxResult getInfo(@PathVariable("articleId") Long articleId)
    {
        return AjaxResult.success(fsArticleService.selectFsArticleByArticleId(articleId));
    }

    /**
     * 新增文章
     */
    @PreAuthorize("@ss.hasPermi('his:healthArticle:add')")
    @Log(title = "文章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsArticle fsArticle)
    {
        fsArticleService.insertFsArticle(fsArticle);
        Collection<String> keys = redisCache.keys("getArticleList*");
        for (String key : keys) {
            redisCache.deleteObject(key);
        }

        return toAjax(1);
    }

    /**
     * 修改文章
     */
    @PreAuthorize("@ss.hasPermi('his:healthArticle:edit')")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsArticle fsArticle)
    {
        fsArticleService.updateFsArticle(fsArticle);
        Collection<String> keys = redisCache.keys("getArticleList*");
        for (String key : keys) {
            redisCache.deleteObject(key);
        }
        return toAjax(1);
    }

    /**
     * 删除文章
     */
    @PreAuthorize("@ss.hasPermi('his:healthArticle:remove')")
    @Log(title = "文章", businessType = BusinessType.DELETE)
	@DeleteMapping("/{articleIds}")
    public AjaxResult remove(@PathVariable Long[] articleIds)
    {
        fsArticleService.deleteFsArticleByArticleIds(articleIds);
        Collection<String> keys = redisCache.keys("getArticleList*");
        for (String key : keys) {
            redisCache.deleteObject(key);
        }
        return toAjax(1);
    }
}
