package com.ruoyi.biemo.business.controller;

import com.ruoyi.biemo.business.domain.Category;
import com.ruoyi.biemo.business.service.CategoryService;
import com.ruoyi.biemo.core.page.Page;
import com.ruoyi.biemo.core.page.PageFactory;
import com.ruoyi.biemo.nlp.TextClassificationUtils;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping(value = "/makesoft/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
    /**
     * 查询分类管理列表
     */
    //@PreAuthorize("@ss.hasPermi('biemo:category:list')")
    @GetMapping("/list")
    public Page<Category> list(Category category)
    {
        Page<Category> page =  categoryService.selectCategoryPage(category,PageFactory.defaultPage());
        return page;
    }

    /**
     * 导出分类管理列表
     */
    //@PreAuthorize("@ss.hasPermi('biemo:category:export')")
    @Log(title = "分类管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Category category)
    {
        List<Category> list = categoryService.selectCategoryList(category);
        ExcelUtil<Category> util = new ExcelUtil<>(Category.class);
        util.exportExcel(response, list, "分类管理数据");
    }

    /**
     * 获取分类管理详细信息
     */
    //@PreAuthorize("@ss.hasPermi('biemo:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(categoryService.selectCategoryById(id));
    }

    /**
     * 新增分类管理或修改文章
     */
    //@PreAuthorize("@ss.hasPermi('biemo:category:add')")
    @Log(title = "分类管理", businessType = BusinessType.INSERT)
    @RequestMapping
    public AjaxResult add(MultipartFile[] files , String id,String name,String parentId,Integer orderNum)
    {
        Category category = new Category();
        category.setId(id);
        category.setName(name);
        category.setParentId(parentId);
        category.setOrderNum(orderNum);
        TextClassificationUtils.myTrainOrLoadModel(files,name);
        categoryService.insertOrUpdateCategory(category);
        return AjaxResult.success();
    }


    /**
     * 删除分类管理
     */
    //@PreAuthorize("@ss.hasPermi('biemo:category:remove')")
    @Log(title = "分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        categoryService.deleteCategoryByIds(ids);
        return AjaxResult.success();
    }


}
