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.IShopGoodsCategoryService;

/**
 * 商家端店铺商品分类（biz_goods_category，shop_id 非空，最多两级）
 */
@RestController
@RequestMapping("/agri/seller/shopCategory")
public class SellerShopGoodsCategoryController extends BaseController
{
    @Autowired
    private IShopGoodsCategoryService shopGoodsCategoryService;

    @PreAuthorize("@ss.hasPermi('agri:seller:shopCategory:list')")
    @GetMapping("/scope")
    public AjaxResult scope()
    {
        return success(shopGoodsCategoryService.selectScope());
    }

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

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

    @PreAuthorize("@ss.hasPermi('agri:seller:shopCategory:list')")
    @GetMapping("/level1Options")
    public AjaxResult level1Options()
    {
        return success(shopGoodsCategoryService.selectLevel1Options());
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:shopCategory:list')")
    @GetMapping("/level2Options")
    public AjaxResult level2Options()
    {
        return success(shopGoodsCategoryService.selectLevel2Options());
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:shopCategory:query')")
    @GetMapping("/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId)
    {
        return success(shopGoodsCategoryService.selectCategoryDetail(categoryId));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:shopCategory:add')")
    @Log(title = "店铺商品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody BizGoodsCategory category)
    {
        category.setCreateBy(getUsername());
        return toAjax(shopGoodsCategoryService.insertCategory(category));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:shopCategory:edit')")
    @Log(title = "店铺商品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BizGoodsCategory category)
    {
        category.setUpdateBy(getUsername());
        return toAjax(shopGoodsCategoryService.updateCategory(category));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:shopCategory:remove')")
    @Log(title = "店铺商品分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable("categoryIds") Long[] categoryIds)
    {
        try
        {
            shopGoodsCategoryService.deleteCategoryByIds(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;
        }
    }
}
