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.WzStockIn;
import com.zhyc.module.stock.service.IWzStockInService;
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/in")
public class WzStockInController extends BaseController
{
    private final IWzStockInService wzStockInService;

    public WzStockInController(IWzStockInService wzStockInService) {
        this.wzStockInService = wzStockInService;
    }

    /**
     * 查询入库记录列表
     */
    @PreAuthorize("@ss.hasPermi('stock:in:list')")
    @GetMapping("/list")
    public TableDataInfo list(WzStockIn wzStockIn)
    {
        startPage();
        List<WzStockIn> list = wzStockInService.selectWzStockInList(wzStockIn);
        logger.debug(wzStockIn.toString());
        return getDataTable(list);
    }

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

    /**
     * 获取入库记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('stock:in:query')")
    @GetMapping(value = "/{stockInCode}")
    public AjaxResult getInfo(@PathVariable("stockInCode") Long stockInCode)
    {
        return success(wzStockInService.selectWzStockInByStockInCode(stockInCode));
    }

    /**
     * 新增入库记录
     */
    @PreAuthorize("@ss.hasPermi('stock:in:add')")
    @Log(title = "入库记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WzStockIn wzStockIn)
    {
        return toAjax(wzStockInService.insertWzStockIn(wzStockIn));
    }

    /**
     * 修改入库记录
     */
    @PreAuthorize("@ss.hasPermi('stock:in:edit')")
    @Log(title = "入库记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WzStockIn wzStockIn)
    {
        return toAjax(wzStockInService.updateWzStockIn(wzStockIn));
    }

    /**
     * 删除入库记录
     */
    @PreAuthorize("@ss.hasPermi('stock:in:remove')")
    @Log(title = "入库记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{stockInCodes}")
    public AjaxResult remove(@PathVariable Long[] stockInCodes)
    {
        return toAjax(wzStockInService.deleteWzStockInByStockInCodes(stockInCodes));
    }

    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('stock:in:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<WzStockIn> util = new ExcelUtil<>(WzStockIn.class);
        List<WzStockIn> stockInList = util.importExcel(file.getInputStream());
        stockInList.removeIf(wzStockIn -> wzStockIn.getDocDate() == null);
        String operName = getUsername();
        String message = wzStockInService.importUser(stockInList, updateSupport, operName);
//        String message = "OK We are testing";
        return success(message);
    }
}
