package com.shzg.project.worn.controller;

import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.shzg.common.utils.SecurityUtils;
import com.shzg.project.worn.domain.dto.WornInboundPartialFinishDTO;
import com.shzg.project.worn.domain.dto.WornInboundUpdateDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.shzg.framework.aspectj.lang.annotation.Log;
import com.shzg.framework.aspectj.lang.enums.BusinessType;
import com.shzg.project.worn.domain.WornInboundBill;
import com.shzg.project.worn.service.IWornInboundBillService;
import com.shzg.framework.web.controller.BaseController;
import com.shzg.framework.web.domain.AjaxResult;
import com.shzg.common.utils.poi.ExcelUtil;
import com.shzg.framework.web.page.TableDataInfo;

/**
 * 入库库存Controller
 * 
 * @author shzg
 * @date 2026-03-26
 */
@RestController
@RequestMapping("/worn/inboundBill")
public class WornInboundBillController extends BaseController
{
    @Autowired
    private IWornInboundBillService wornInboundBillService;

    /**
     * 查询入库库存列表
     */
    @PreAuthorize("@ss.hasPermi('worn:inbound:list')")
    @GetMapping("/list")
    public TableDataInfo list(WornInboundBill wornInboundBill)
    {
        startPage();
        if (!SecurityUtils.isAdmin())
        {
            wornInboundBill.setProjectId(SecurityUtils.getDeptId());
        }
        List<WornInboundBill> list = wornInboundBillService.selectWornInboundBillList(wornInboundBill);
        return getDataTable(list);
    }

    /**
     * 导出入库库存列表
     */
    @PreAuthorize("@ss.hasPermi('worn:inbound:export')")
    @Log(title = "入库库存", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WornInboundBill wornInboundBill)
    {
        List<WornInboundBill> list = wornInboundBillService.selectWornInboundBillList(wornInboundBill);
        ExcelUtil<WornInboundBill> util = new ExcelUtil<WornInboundBill>(WornInboundBill.class);
        util.exportExcel(response, list, "入库库存数据");
    }

    /**
     * 获取入库库存详细信息
     */
    @PreAuthorize("@ss.hasPermi('worn:inbound:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wornInboundBillService.selectWornInboundBillById(id));
    }

    /**
     * 新增入库库存
     */
    @PreAuthorize("@ss.hasPermi('worn:inbound:add')")
    @Log(title = "入库库存", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody WornInboundBill wornInboundBill)
    {
        return toAjax(wornInboundBillService.insertWornInboundBill(wornInboundBill));
    }

    /**
     * 修改入库库存
     */
    @PreAuthorize("@ss.hasPermi('worn:inbound:edit')")
    @Log(title = "入库库存", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WornInboundBill wornInboundBill)
    {
        return toAjax(wornInboundBillService.updateWornInboundBill(wornInboundBill));
    }

    /**
     * 删除入库库存
     */
    @PreAuthorize("@ss.hasPermi('worn:inbound:remove')")
    @Log(title = "入库库存", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wornInboundBillService.deleteWornInboundBillByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('worn:inbound:edit')")
    @Log(title = "入库单入库", businessType = BusinessType.UPDATE)
    @PostMapping("/inboundFinish")
    public AjaxResult inboundFinish(@RequestBody WornInboundPartialFinishDTO dto)
    {
        return toAjax(wornInboundBillService.inboundFinish(dto));
    }
    /**
     * 入库单作废（整单）
     */
    @PostMapping("/void")
    public AjaxResult voidBill(@RequestBody WornInboundBill wornInboundBill)
    {
        return toAjax(wornInboundBillService.voidBill(wornInboundBill.getBillNo()));
    }

    /**
     * 打印入库单
     */
    @GetMapping("/print/{id}")
    public void print(@PathVariable Long id, HttpServletResponse response) throws Exception
    {
        byte[] pdf = wornInboundBillService.generatePdf(id);

        response.reset();
        response.setContentType("application/pdf");
        response.setCharacterEncoding("UTF-8");
        response.setContentLength(pdf.length);
        response.setHeader("Content-Disposition", "inline; filename=inbound.pdf");
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0");

        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(pdf);
        outputStream.flush();
        outputStream.close();
    }

}
