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.WarehouseArea;
import com.ruoyi.warehouse.service.IWarehouseAreaService;

@RestController
@RequestMapping("/warehouse/area")
public class WarehouseAreaController extends BaseController
{
    @Autowired
    private IWarehouseAreaService warehouseAreaService;

    @PreAuthorize("@ss.hasPermi('warehouse:area:list')")
    @GetMapping("/list")
    public TableDataInfo list(WarehouseArea warehouseArea)
    {
        startPage();
        return getDataTable(warehouseAreaService.selectWarehouseAreaList(warehouseArea));
    }

    @PreAuthorize("@ss.hasPermi('warehouse:area:export')")
    @Log(title = "库区定义", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WarehouseArea warehouseArea)
    {
        List<WarehouseArea> list = warehouseAreaService.selectWarehouseAreaList(warehouseArea);
        ExcelUtil<WarehouseArea> util = new ExcelUtil<WarehouseArea>(WarehouseArea.class);
        util.exportExcel(response, list, "库区数据");
    }

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

    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        return success(warehouseAreaService.selectWarehouseAreaAll());
    }

    @PreAuthorize("@ss.hasPermi('warehouse:area:add')")
    @Log(title = "库区定义", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody WarehouseArea warehouseArea)
    {
        if (StringUtils.isNotBlank(warehouseArea.getAreaCode()) && !warehouseAreaService.checkAreaCodeUnique(warehouseArea))
        {
            return error("新增库区失败，库区编码已存在");
        }
        return toAjax(warehouseAreaService.insertWarehouseArea(warehouseArea));
    }

    @PreAuthorize("@ss.hasPermi('warehouse:area:edit')")
    @Log(title = "库区定义", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody WarehouseArea warehouseArea)
    {
        if (StringUtils.isNotBlank(warehouseArea.getAreaCode()) && !warehouseAreaService.checkAreaCodeUnique(warehouseArea))
        {
            return error("修改库区失败，库区编码已存在");
        }
        return toAjax(warehouseAreaService.updateWarehouseArea(warehouseArea));
    }

    @PreAuthorize("@ss.hasPermi('warehouse:area:remove')")
    @Log(title = "库区定义", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(warehouseAreaService.deleteWarehouseAreaByIds(ids));
    }
}
