package com.usky.website.controller.web;


import com.usky.common.core.bean.ApiResult;
import com.usky.common.log.annotation.Log;
import com.usky.common.log.enums.BusinessType;
import com.usky.website.domain.SiteCategory;
import com.usky.website.service.SiteCategoryService;
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.*;

import java.util.List;

/**
 * <p>
 * 官网_栏目管理 前端控制器
 * </p>
 *
 * @author ya
 * @since 2022-08-31
 */
@RestController
@RequestMapping("/siteCategory")
public class SiteCategoryController {

    @Autowired
    private SiteCategoryService siteCategoryService;

    /**
     * 官网-栏目查询
     *
     * @return
     */
    @Cacheable(cacheNames = "siteCategory",sync = true)
    @GetMapping("siteCategoryList")
    public ApiResult<List<SiteCategory>> siteCategoryList(@RequestParam(value = "categoryName", required = false) String categoryName) {
        return ApiResult.success(siteCategoryService.siteCategoryList(categoryName));
    }

    /**
     * 官网-子级栏目查询
     *
     * @param pid 父级ID
     * @return
     */
    @GetMapping("siteCategorySonList")
    public ApiResult<List<SiteCategory>> siteCategorySonList(@RequestParam(value = "pid") Integer pid) {
        return ApiResult.success(siteCategoryService.siteCategorySonList(pid));
    }

    /**
     * 官网-栏目新增
     *
     * @param siteCategory
     * @return
     */
    @Log(title = "新增官网栏目", businessType = BusinessType.INSERT)
    @CacheEvict(cacheNames="siteCategory", allEntries=true)
    @PostMapping("addSiteCategory")
    public ApiResult<Void> addSiteCategory(@RequestBody SiteCategory siteCategory) {
        siteCategoryService.addSiteCategory(siteCategory);
        return ApiResult.success();
    }

    /**
     * 官网-栏目修改
     *
     * @param siteCategory
     * @return
     */
    @Log(title = "修改官网栏目", businessType = BusinessType.UPDATE)
    @CacheEvict(cacheNames="siteCategory", allEntries=true)
    @PutMapping("updateSiteCategory")
    public ApiResult<Void> updateSiteCategory(@RequestBody SiteCategory siteCategory) {
        siteCategoryService.updateSiteCategory(siteCategory);
        return ApiResult.success();
    }

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


}

