package com.ruoyi.web.modules.goods.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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.facade.ICategoryFacade;
import com.ruoyi.web.modules.goods.domain.BizGoods;
import com.ruoyi.web.modules.goods.dto.GoodsAuditDTO;
import com.ruoyi.web.modules.goods.dto.GoodsOffShelfDTO;
import com.ruoyi.web.modules.goods.exception.GoodsBatchOperationException;
import com.ruoyi.web.modules.goods.service.IGoodsService;
import com.ruoyi.web.modules.goods.support.GoodsBatchResponseSupport;

/**
 * 平台端商品管理
 */
@RestController
@RequestMapping("/agri/goods")
public class GoodsController extends BaseController
{
    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private ICategoryFacade categoryFacade;

    @PreAuthorize("@ss.hasPermi('agri:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizGoods query)
    {
        startPage();
        return getDataTable(goodsService.selectPlatformList(query));
    }

    @PreAuthorize("@ss.hasPermi('agri:goods:list')")
    @GetMapping("/pendingCount")
    public AjaxResult pendingCount()
    {
        Map<String, Object> data = new HashMap<>();
        data.put("pendingCount", goodsService.countPlatformPending());
        return success(data);
    }

    @PreAuthorize("@ss.hasPermi('agri:goods:query')")
    @GetMapping("/{goodsId}")
    public AjaxResult getInfo(@PathVariable("goodsId") Long goodsId)
    {
        return success(goodsService.selectPlatformDetail(goodsId));
    }

    @PreAuthorize("@ss.hasPermi('agri:goods:query')")
    @GetMapping("/categoryOptions")
    public AjaxResult categoryOptions(@RequestParam("shopId") Long shopId)
    {
        return success(categoryFacade.listPlatformLevel2Options());
    }

    @PreAuthorize("@ss.hasPermi('agri:goods:audit')")
    @Log(title = "商品审核", businessType = BusinessType.UPDATE)
    @PutMapping("/audit")
    public AjaxResult audit(@RequestBody GoodsAuditDTO dto)
    {
        try
        {
            goodsService.auditGoods(dto, getUsername());
            return success();
        }
        catch (GoodsBatchOperationException e)
        {
            return GoodsBatchResponseSupport.toAjaxResult(e);
        }
    }

    @PreAuthorize("@ss.hasPermi('agri:goods:offshelf')")
    @Log(title = "商品下架", businessType = BusinessType.UPDATE)
    @PutMapping("/offShelf")
    public AjaxResult offShelf(@RequestBody GoodsOffShelfDTO dto)
    {
        try
        {
            goodsService.offShelfPlatform(dto, getUsername());
            return success();
        }
        catch (GoodsBatchOperationException e)
        {
            return GoodsBatchResponseSupport.toAjaxResult(e);
        }
    }
}
