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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
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.OutRecord;
import com.ruoyi.warehouse.service.IOutRecordService;

@RestController
@RequestMapping("/warehouse/out")
public class OutRecordController extends BaseController
{
    @Autowired
    private IOutRecordService outRecordService;

    @PreAuthorize("@ss.hasPermi('warehouse:out:list')")
    @GetMapping("/list")
    public TableDataInfo list(OutRecord outRecord)
    {
        startPage();
        return getDataTable(outRecordService.selectOutRecordList(outRecord));
    }

    @PreAuthorize("@ss.hasPermi('warehouse:out:export')")
    @Log(title = "出库记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OutRecord outRecord)
    {
        List<OutRecord> list = outRecordService.selectOutRecordList(outRecord);
        ExcelUtil<OutRecord> util = new ExcelUtil<OutRecord>(OutRecord.class);
        util.exportExcel(response, list, "出库记录");
    }

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

    @PreAuthorize("@ss.hasPermi('warehouse:out:add')")
    @Log(title = "出库记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody OutRecord outRecord)
    {
        if (StringUtils.isNotBlank(outRecord.getOutNo()) && !outRecordService.checkOutNoUnique(outRecord))
        {
            return error("新增出库失败，出库单号已存在");
        }
        outRecord.setOperator(getUsername());
        return toAjax(outRecordService.insertOutRecord(outRecord));
    }
}
