package com.zhyc.module.stock.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhyc.common.annotation.Log;
import com.zhyc.common.core.controller.BaseController;
import com.zhyc.common.core.domain.AjaxResult;
import com.zhyc.common.enums.BusinessType;
import com.zhyc.module.stock.domain.WzStockOut;
import com.zhyc.module.stock.service.IWzStockOutService;
import com.zhyc.common.utils.poi.ExcelUtil;
import com.zhyc.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 出库记录Controller
 * 
 * @author HashMap
 * @date 2025-08-05
 */
@RestController
@RequestMapping("/stock/out")
public class WzStockOutController extends BaseController
{
    private final IWzStockOutService wzStockOutService;

    public WzStockOutController(IWzStockOutService wzStockOutService) {
        this.wzStockOutService = wzStockOutService;
    }

    /**
     * 查询出库记录列表
     */
    @PreAuthorize("@ss.hasPermi('stock:out:list')")
    @GetMapping("/list")
    public TableDataInfo list(WzStockOut wzStockOut)
    {
        startPage();
        List<WzStockOut> list = wzStockOutService.selectWzStockOutList(wzStockOut);
        return getDataTable(list);
    }

    /**
     * 导出出库记录列表
     */
    @PreAuthorize("@ss.hasPermi('stock:out:export')")
    @Log(title = "出库记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WzStockOut wzStockOut)
    {
        List<WzStockOut> list = wzStockOutService.selectWzStockOutList(wzStockOut);
        ExcelUtil<WzStockOut> util = new ExcelUtil<>(WzStockOut.class);
        util.exportExcel(response, list, "出库记录数据");
    }

    /**
     * 获取出库记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('stock:out:query')")
    @GetMapping(value = "/{stockOutCode}")
    public AjaxResult getInfo(@PathVariable("stockOutCode") Long stockOutCode)
    {
        return success(wzStockOutService.selectWzStockOutByStockOutCode(stockOutCode));
    }

    /**
     * 新增出库记录
     */
    @PreAuthorize("@ss.hasPermi('stock:out:add')")
    @Log(title = "出库记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WzStockOut wzStockOut)
    {
        return toAjax(wzStockOutService.insertWzStockOut(wzStockOut));
    }

    /**
     * 修改出库记录
     */
    @PreAuthorize("@ss.hasPermi('stock:out:edit')")
    @Log(title = "出库记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WzStockOut wzStockOut)
    {
        return toAjax(wzStockOutService.updateWzStockOut(wzStockOut));
    }

    /**
     * 删除出库记录
     */
    @PreAuthorize("@ss.hasPermi('stock:out:remove')")
    @Log(title = "出库记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{stockOutCodes}")
    public AjaxResult remove(@PathVariable Long[] stockOutCodes)
    {
        return toAjax(wzStockOutService.deleteWzStockOutByStockOutCodes(stockOutCodes));
    }

    /**
     * 导入出库记录
     */
    @Log(title = "出库记录", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('stock:out:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<WzStockOut> util = new ExcelUtil<>(WzStockOut.class);
        List<WzStockOut> WzStockOutList = util.importExcel(file.getInputStream());
        WzStockOutList.removeIf(wzStockOut -> wzStockOut.getDocId() == null);
        String operName = getUsername();
        String message;
        try {
            message = wzStockOutService.importUser(WzStockOutList, updateSupport, operName);
        }catch (Exception e) {
            message = e.getMessage();
            logger.error(e.getMessage());
        }
       // String message = "OK We are testing";
        return success(message);
    }
}
