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

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
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.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.goodsservice.domain.BizGoodsService;
import com.ruoyi.web.modules.goodsservice.exception.GoodsServiceBatchDeleteException;
import com.ruoyi.web.modules.goodsservice.service.IGoodsServiceCatalogService;
import com.ruoyi.web.modules.goodsservice.vo.GoodsServiceListVO;

/**
 * 平台端商品服务管理
 */
@RestController
@RequestMapping("/agri/goodsService")
public class GoodsServiceController extends BaseController
{
    @Autowired
    private IGoodsServiceCatalogService goodsServiceCatalogService;

    @PreAuthorize("@ss.hasPermi('agri:goodsService:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizGoodsService query)
    {
        startPage();
        List<GoodsServiceListVO> list = goodsServiceCatalogService.selectGoodsServiceList(query);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('agri:goodsService:list')")
    @GetMapping("/{serviceId}")
    public AjaxResult getInfo(@PathVariable("serviceId") Long serviceId)
    {
        return success(goodsServiceCatalogService.selectGoodsServiceById(serviceId));
    }

    @PreAuthorize("@ss.hasPermi('agri:goodsService:add')")
    @Log(title = "商品服务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizGoodsService row)
    {
        row.setCreateBy(getUsername());
        return toAjax(goodsServiceCatalogService.insertGoodsService(row));
    }

    @PreAuthorize("@ss.hasPermi('agri:goodsService:edit')")
    @Log(title = "商品服务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizGoodsService row)
    {
        row.setUpdateBy(getUsername());
        return toAjax(goodsServiceCatalogService.updateGoodsService(row));
    }

    @PreAuthorize("@ss.hasPermi('agri:goodsService:remove')")
    @Log(title = "商品服务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{serviceIds}")
    public AjaxResult remove(@PathVariable("serviceIds") Long[] serviceIds)
    {
        try
        {
            goodsServiceCatalogService.deleteGoodsServiceByIds(serviceIds, getUsername());
            return success();
        }
        catch (GoodsServiceBatchDeleteException 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;
        }
    }
}
