package com.younike.admin;

import java.util.List;

import com.younike.framework.config.AnonymousAccess;
import com.younike.system.domain.vo.CategoryVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.younike.common.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.younike.common.core.controller.BaseController;
import com.younike.common.core.domain.AjaxResult;
import com.younike.common.enums.BusinessType;
import com.younike.system.domain.Category;
import com.younike.system.service.ICategoryService;
import com.younike.common.utils.poi.ExcelUtil;
import com.younike.common.core.page.TableDataInfo;

import javax.annotation.security.PermitAll;

/**
 * 分类Controller
 * 
 * @author elev
 * @date 2022-01-04
 */
@Api("分类接口")
@RestController
@RequestMapping("/admin/category")
public class AdminCategoryController extends BaseController {
    @Autowired
    private ICategoryService categoryService;

    /**
     * 新增分类
     */
    @ApiOperation("新增/修改分类")
    @PreAuthorize("@ss.hasPermi('basic:category:add')")
//    @Log(title = "分类", businessType = BusinessType.INSERT)
//    @AnonymousAccess
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody CategoryVo category) {
        return categoryService.submit(category);
    }

    /**
     * 新增子分类
     */
    @ApiOperation("新增/修改子分类")
    @PreAuthorize("@ss.hasPermi('basic:category:add')")
//    @Log(title = "分类", businessType = BusinessType.INSERT)
//    @AnonymousAccess
    @PostMapping("/submitChild")
    public AjaxResult submitChild(@RequestBody Category category) {
        return categoryService.submitChild(category);
    }

    /**
     * 新增分类
     */
    @ApiOperation("获取分类列表")
    @PreAuthorize("@ss.hasPermi('basic:category:list')")
//    @Log(title = "分类", businessType = BusinessType.INSERT)
//    @AnonymousAccess
    @GetMapping("/categoryList")
    public AjaxResult categoryList(@RequestParam("bizType") Integer bizType) {
        return AjaxResult.success(categoryService.categoryList(bizType));
    }

    @ApiOperation("获取分类级联列表")
    @PermitAll
    @GetMapping("/categoryTree")
    public AjaxResult categoryTree() {
        return AjaxResult.success(categoryService.categoryTree());
    }

    /**
     * 开启/关闭
     */
    @ApiOperation("开启/关闭")
    @PreAuthorize("@ss.hasPermi('basic:category:edit')")
    @PutMapping("updateEnable")
    public AjaxResult updateEnable(@RequestBody Category category) {
        if (category == null || category.getCategoryId() == null || category.getIsEnable() == null) {
            return AjaxResult.error("参数错误");
        }
        return toAjax(categoryService.updateCategory(category));
    }

    /**
     * 编辑是否首页显示
     */
    @ApiOperation("首页显示/关闭")
    @PreAuthorize("@ss.hasPermi('basic:category:edit')")
    @PutMapping("updateIndexShow")
    public AjaxResult updateIndexShow(@RequestBody Category category) {
        if (category == null || category.getCategoryId() == null || category.getIndexShow() == null) {
            return AjaxResult.error("参数错误");
        }
        return toAjax(categoryService.updateCategory(category));
    }

    /**
     * 开启/关闭
     */
    @ApiOperation("修改排序")
    @PreAuthorize("@ss.hasPermi('basic:category:edit')")
    @PutMapping("updateSorting")
    public AjaxResult updateSorting(@RequestBody List<Category> categoryList) {
        if (categoryList == null || categoryList.size() == 0) {
            return AjaxResult.error("参数错误");
        }
        return toAjax(categoryService.updateSortingBatch(categoryList));
    }

    /**
     * 查询分类列表
     */
    @ApiOperation("查询分类列表")
    @PreAuthorize("@ss.hasPermi('basic:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(Category category) {
        startPage();
        List<Category> list = categoryService.selectCategoryList(category);
        return getDataTable(list);
    }

    /**
     * 导出分类列表
     */
    @ApiOperation("导出分类列表")
    @PreAuthorize("@ss.hasPermi('basic:category:export')")
    @Log(title = "分类", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Category category) {
        List<Category> list = categoryService.selectCategoryList(category);
        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
        return util.exportExcel(list, "分类数据");
    }

    /**
     * 获取分类详细信息
     */
    @ApiOperation("获取分类详细信息")
    @PreAuthorize("@ss.hasPermi('basic:category:query')")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId) {
        return AjaxResult.success(categoryService.selectCategoryByCategoryId(categoryId));
    }

    /**
     * 新增分类
     */
    @ApiOperation("新增分类")
//    @PreAuthorize("@ss.hasPermi('basic:category:add')")
//    @Log(title = "分类", businessType = BusinessType.INSERT)
    @AnonymousAccess
    @PostMapping
    public AjaxResult add(@RequestBody Category category) {
        return toAjax(categoryService.insertCategory(category));
    }



    /**
     * 修改分类
     */
    @ApiOperation("修改分类")
//    @PreAuthorize("@ss.hasPermi('basic:category:edit')")
//    @Log(title = "分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Category category) {
        return toAjax(categoryService.updateCategory(category));
    }

    /**
     * 删除分类
     */
    @ApiOperation("删除分类")
    @PreAuthorize("@ss.hasPermi('basic:category:remove')")
    @Log(title = "分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable Long[] categoryIds) {
        return toAjax(categoryService.deleteCategoryByCategoryIds(categoryIds));
    }
}
