package com.ruoyi.web.modules.category.controller;

import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.modules.category.domain.BizGoodsCategory;
import com.ruoyi.web.modules.category.exception.CategoryBatchDeleteException;
import com.ruoyi.web.modules.category.service.IPlatformCategoryService;

/**
 * 平台端商品分类（shop_id 为 NULL，最多两级）
 */
@RestController
@RequestMapping("/agri/category")
public class PlatformCategoryController extends BaseController
{
    @Autowired
    private IPlatformCategoryService platformCategoryService;

    @PreAuthorize("@ss.hasPermi('agri:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam(value = "keyword", required = false) String keyword)
    {
        startPage();
        return getDataTable(platformCategoryService.selectPlatformCategoryRowList(keyword));
    }

    @PreAuthorize("@ss.hasPermi('agri:category:list')")
    @GetMapping("/tree")
    public AjaxResult tree(@RequestParam(value = "keyword", required = false) String keyword)
    {
        return success(platformCategoryService.selectPlatformCategoryTree(keyword));
    }

    @PreAuthorize("@ss.hasPermi('agri:category:list')")
    @GetMapping("/level1Options")
    public AjaxResult level1Options()
    {
        return success(platformCategoryService.selectPlatformLevel1Options());
    }

    @PreAuthorize("@ss.hasPermi('agri:category:list')")
    @GetMapping("/level2Options")
    public AjaxResult level2Options()
    {
        return success(platformCategoryService.selectPlatformLevel2Options());
    }

    @PreAuthorize("@ss.hasPermi('agri:category:add')")
    @Log(title = "平台商品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody BizGoodsCategory category)
    {
        category.setCreateBy(getUsername());
        return toAjax(platformCategoryService.insertPlatformCategory(category));
    }

    @PreAuthorize("@ss.hasPermi('agri:category:edit')")
    @Log(title = "平台商品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BizGoodsCategory category)
    {
        category.setUpdateBy(getUsername());
        return toAjax(platformCategoryService.updatePlatformCategory(category));
    }

    @PreAuthorize("@ss.hasPermi('agri:category:remove')")
    @Log(title = "平台商品分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable("categoryIds") Long[] categoryIds)
    {
        try
        {
            platformCategoryService.deletePlatformCategoryByIds(categoryIds, getUsername());
            return success();
        }
        catch (CategoryBatchDeleteException e)
        {
            Map<String, Object> data = new HashMap<>();
            data.put("reasons", e.getReasons());
            AjaxResult result = AjaxResult.error(e.getMessage());
            result.put(AjaxResult.DATA_TAG, data);
            return result;
        }
    }
}
