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.InRecord;
import com.ruoyi.warehouse.service.IInRecordService;

@RestController
@RequestMapping("/warehouse/in")
public class InRecordController extends BaseController
{
    @Autowired
    private IInRecordService inRecordService;

    @PreAuthorize("@ss.hasPermi('warehouse:in:list')")
    @GetMapping("/list")
    public TableDataInfo list(InRecord inRecord)
    {
        startPage();
        return getDataTable(inRecordService.selectInRecordList(inRecord));
    }

    @PreAuthorize("@ss.hasPermi('warehouse:in:export')")
    @Log(title = "入库记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InRecord inRecord)
    {
        List<InRecord> list = inRecordService.selectInRecordList(inRecord);
        ExcelUtil<InRecord> util = new ExcelUtil<InRecord>(InRecord.class);
        util.exportExcel(response, list, "入库记录");
    }

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

    @PreAuthorize("@ss.hasPermi('warehouse:in:add')")
    @Log(title = "入库记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody InRecord inRecord)
    {
        if (StringUtils.isNotBlank(inRecord.getInNo()) && !inRecordService.checkInNoUnique(inRecord))
        {
            return error("新增入库失败，入库单号已存在");
        }
        inRecord.setOperator(getUsername());
        return toAjax(inRecordService.insertInRecord(inRecord));
    }

    @PreAuthorize("@ss.hasPermi('warehouse:in:edit')")
    @Log(title = "入库记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody InRecord inRecord)
    {
        if (StringUtils.isNotBlank(inRecord.getInNo()) && !inRecordService.checkInNoUnique(inRecord))
        {
            return error("修改入库失败，入库单号已存在");
        }
        inRecord.setOperator(getUsername());
        return toAjax(inRecordService.updateInRecord(inRecord));
    }

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

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