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.StockCheckRecord;
import com.ruoyi.warehouse.service.IStockCheckRecordService;

@RestController
@RequestMapping("/warehouse/check")
public class StockCheckRecordController extends BaseController
{
    @Autowired
    private IStockCheckRecordService stockCheckRecordService;

    @PreAuthorize("@ss.hasPermi('warehouse:check:list')")
    @GetMapping("/list")
    public TableDataInfo list(StockCheckRecord stockCheckRecord)
    {
        startPage();
        return getDataTable(stockCheckRecordService.selectStockCheckRecordList(stockCheckRecord));
    }

    @PreAuthorize("@ss.hasPermi('warehouse:check:export')")
    @Log(title = "盘库记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StockCheckRecord stockCheckRecord)
    {
        List<StockCheckRecord> list = stockCheckRecordService.selectStockCheckRecordList(stockCheckRecord);
        ExcelUtil<StockCheckRecord> util = new ExcelUtil<StockCheckRecord>(StockCheckRecord.class);
        util.exportExcel(response, list, "盘库记录");
    }

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

    @PreAuthorize("@ss.hasPermi('warehouse:check:add')")
    @Log(title = "盘库记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody StockCheckRecord stockCheckRecord)
    {
        if (StringUtils.isNotBlank(stockCheckRecord.getCheckNo()) && !stockCheckRecordService.checkCheckNoUnique(stockCheckRecord))
        {
            return error("新增盘库失败，盘点单号已存在");
        }
        stockCheckRecord.setCheckUser(getUsername());
        return toAjax(stockCheckRecordService.insertStockCheckRecord(stockCheckRecord));
    }

    @PreAuthorize("@ss.hasPermi('warehouse:check:edit')")
    @Log(title = "盘库记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody StockCheckRecord stockCheckRecord)
    {
        return toAjax(stockCheckRecordService.updateStockCheckRecord(stockCheckRecord));
    }

    @PreAuthorize("@ss.hasPermi('warehouse:check:confirm')")
    @Log(title = "盘库确认", businessType = BusinessType.UPDATE)
    @PutMapping("/confirm/{id}")
    public AjaxResult confirm(@PathVariable Long id)
    {
        return toAjax(stockCheckRecordService.confirmStockCheckRecord(id, getUsername()));
    }

    @PreAuthorize("@ss.hasPermi('warehouse:check:remove')")
    @Log(title = "盘库记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(stockCheckRecordService.deleteStockCheckRecordByIds(ids));
    }
}
