package com.usky.website.controller.web;


import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.log.annotation.Log;
import com.usky.common.log.enums.BusinessType;
import com.usky.website.domain.SiteArticle;
import com.usky.website.service.SiteArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 官网_内容管理 前端控制器
 * </p>
 *
 * @author ya
 * @since 2022-08-31
 */
@RestController
@RequestMapping("/siteArticle")
public class SiteArticleController {

    @Autowired
    private SiteArticleService siteArticleService;

    /**
     * 官网-内容管理查询
     *
     * @param categoryid 栏目ID
     * @param id         主键ID
     * @param pageNum    页数
     * @param pageSize   每页条数
     * @return
     */
    @Cacheable(cacheNames = "siteArticle",sync = true)
    @GetMapping("siteArticleList")
    public ApiResult<CommonPage<SiteArticle>> siteArticleList(@RequestParam(value = "categoryid", required = false) Integer categoryid,
                                                              @RequestParam(value = "id", required = false) Integer id,
                                                              @RequestParam(value = "articleid", required = false) Integer articleid,
                                                              @RequestParam(value = "title", required = false) String title,
                                                              @RequestParam(value = "order", required = false) String order,
                                                              @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                              @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        return ApiResult.success(siteArticleService.siteArticleList(categoryid, id, articleid, title, order, pageNum, pageSize));
    }

    /**
     * 官网-内容新增
     *
     * @param siteArticle
     * @return
     */
    @Log(title = "新增-官网内容", businessType = BusinessType.INSERT)
    @CacheEvict(cacheNames = "siteArticle",allEntries = true)
    @PostMapping("addSiteArticle")
    public ApiResult<Void> addSiteArticle(@RequestBody SiteArticle siteArticle) {
        siteArticleService.addSiteArticle(siteArticle);
        return ApiResult.success();
    }

    /**
     * 官网-内容修改
     *
     * @param siteArticle
     * @return
     */
    @Log(title = "修改-官网内容", businessType = BusinessType.UPDATE)
    @CacheEvict(cacheNames = "siteArticle",allEntries = true)
    @PutMapping("updateSiteArticle")
    public ApiResult<Void> updateSiteArticle(@RequestBody SiteArticle siteArticle) {
        siteArticleService.updateSiteArticle(siteArticle);
        return ApiResult.success();
    }

    /**
     * 官网-内容删除
     *
     * @param id 主键ID
     * @return
     */
    @Log(title = "删除-官网内容", businessType = BusinessType.DELETE)
    @CacheEvict(cacheNames = "siteArticle",allEntries = true)
    @DeleteMapping("delSiteArticle")
    public ApiResult<Void> delSiteArticle(@RequestParam(value = "id") Integer id) {
        siteArticleService.delSiteArticle(id);
        return ApiResult.success();
    }

}

