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.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.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.category.support.SellerShopContext;
import com.ruoyi.web.modules.goodsservice.facade.IGoodsServiceFacade;
import com.ruoyi.web.modules.goods.domain.BizGoods;
import com.ruoyi.web.modules.goods.dto.GoodsOffShelfDTO;
import com.ruoyi.web.modules.goods.dto.GoodsSaveDTO;
import com.ruoyi.web.modules.goods.exception.GoodsBatchOperationException;
import com.ruoyi.web.modules.goods.service.IGoodsService;

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

    @Autowired
    private ICategoryFacade categoryFacade;

    @Autowired
    private IGoodsServiceFacade goodsServiceFacade;

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

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

    @PreAuthorize("@ss.hasPermi('agri:seller:goods:add')")
    @Log(title = "商家商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GoodsSaveDTO dto)
    {
        Long goodsId = goodsService.insertSellerGoods(SellerShopContext.getShopId(), dto, getUsername());
        Map<String, Object> data = new HashMap<>();
        data.put("goodsId", goodsId);
        AjaxResult result = AjaxResult.success();
        result.put(AjaxResult.DATA_TAG, data);
        return result;
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:goods:edit')")
    @Log(title = "商家商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GoodsSaveDTO dto)
    {
        goodsService.updateSellerGoods(SellerShopContext.getShopId(), dto, getUsername());
        return success();
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:goods:edit')")
    @Log(title = "商品提交上架", businessType = BusinessType.UPDATE)
    @PutMapping("/{goodsId}/submit")
    public AjaxResult submit(@PathVariable("goodsId") Long goodsId)
    {
        Map<String, Object> data = goodsService.submitGoods(SellerShopContext.getShopId(), goodsId, getUsername());
        AjaxResult result = AjaxResult.success("提交成功");
        result.put(AjaxResult.DATA_TAG, data);
        return result;
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:goods:offshelf')")
    @Log(title = "商家商品下架", businessType = BusinessType.UPDATE)
    @PutMapping("/{goodsId}/offShelf")
    public AjaxResult offShelf(@PathVariable("goodsId") Long goodsId)
    {
        try
        {
            goodsService.offShelfSeller(SellerShopContext.getShopId(), goodsId, getUsername());
            return success();
        }
        catch (GoodsBatchOperationException 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;
        }
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:goods:list')")
    @GetMapping("/categoryOptions")
    public AjaxResult categoryOptions()
    {
        return success(categoryFacade.listLevel2ByShopId(SellerShopContext.getShopId()));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:goods:list')")
    @GetMapping("/serviceOptions")
    public AjaxResult serviceOptions()
    {
        Map<String, Object> data = new HashMap<>();
        data.put("all", goodsServiceFacade.listAllEnabled());
        data.put("defaultShow", goodsServiceFacade.listDefaultShow());
        return success(data);
    }
}
