package com.ruoyi.web.controller.warehouse;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
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.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.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.warehouse.domain.ProductInfo;
import com.ruoyi.warehouse.service.IProductInfoService;

@RestController
@RequestMapping("/warehouse/product")
public class ProductInfoController extends BaseController
{
    @Autowired
    private IProductInfoService productInfoService;

    @PreAuthorize("@ss.hasPermi('warehouse:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductInfo productInfo)
    {
        startPage();
        return getDataTable(productInfoService.selectProductInfoList(productInfo));
    }

    @PreAuthorize("@ss.hasPermi('warehouse:product:export')")
    @Log(title = "产品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductInfo productInfo)
    {
        List<ProductInfo> list = productInfoService.selectProductInfoList(productInfo);
        ExcelUtil<ProductInfo> util = new ExcelUtil<ProductInfo>(ProductInfo.class);
        util.exportExcel(response, list, "产品数据");
    }

    @PreAuthorize("@ss.hasPermi('warehouse:product:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return success(productInfoService.selectProductInfoById(id));
    }

    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        return success(productInfoService.selectProductInfoAll());
    }

    @PreAuthorize("@ss.hasPermi('warehouse:product:add')")
    @Log(title = "产品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ProductInfo productInfo)
    {
        if (StringUtils.isNotBlank(productInfo.getProductCode()) && !productInfoService.checkProductCodeUnique(productInfo))
        {
            return error("新增产品失败，产品编码已存在");
        }
        return toAjax(productInfoService.insertProductInfo(productInfo));
    }

    @PreAuthorize("@ss.hasPermi('warehouse:product:edit')")
    @Log(title = "产品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ProductInfo productInfo)
    {
        if (StringUtils.isNotBlank(productInfo.getProductCode()) && !productInfoService.checkProductCodeUnique(productInfo))
        {
            return error("修改产品失败，产品编码已存在");
        }
        return toAjax(productInfoService.updateProductInfo(productInfo));
    }

    @PreAuthorize("@ss.hasPermi('warehouse:product:remove')")
    @Log(title = "产品信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productInfoService.deleteProductInfoByIds(ids));
    }
}
