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.WzMaterialsManagement;
import com.zhyc.module.stock.service.IWzMaterialsManagementService;
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/management")
public class WzMaterialsManagementController extends BaseController {
    private final IWzMaterialsManagementService wzMaterialsManagementService;

    public WzMaterialsManagementController(IWzMaterialsManagementService wzMaterialsManagementService) {
        this.wzMaterialsManagementService = wzMaterialsManagementService;
    }

    /**
     * 查询物资管理列表
     */
    @PreAuthorize("@ss.hasPermi('stock:management:list')")
    @GetMapping("/list")
    public TableDataInfo list(WzMaterialsManagement wzMaterialsManagement) {
        startPage();
        List<WzMaterialsManagement> list = wzMaterialsManagementService.selectWzMaterialsManagementList(wzMaterialsManagement);
        return getDataTable(list);
    }

    /**
     * 导出物资管理列表
     */
    @PreAuthorize("@ss.hasPermi('stock:management:export')")
    @Log(title = "物资管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WzMaterialsManagement wzMaterialsManagement) {
        List<WzMaterialsManagement> list = wzMaterialsManagementService.selectWzMaterialsManagementList(wzMaterialsManagement);
        ExcelUtil<WzMaterialsManagement> util = new ExcelUtil<>(WzMaterialsManagement.class);
        util.exportExcel(response, list, "物资管理数据");
    }

    /**
     * 获取物资管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('stock:management:query')")
    @GetMapping(value = "/{materialManagementCode}")
    public AjaxResult getInfo(@PathVariable("materialManagementCode") Long materialManagementCode) {
        return success(wzMaterialsManagementService.selectWzMaterialsManagementByMaterialManagementCode(materialManagementCode));
    }

    /**
     * 新增物资管理
     */
    @PreAuthorize("@ss.hasPermi('stock:management:add')")
    @Log(title = "物资管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WzMaterialsManagement wzMaterialsManagement) {
        return toAjax(wzMaterialsManagementService.insertWzMaterialsManagement(wzMaterialsManagement));
    }

    /**
     * 修改物资管理
     */
    @PreAuthorize("@ss.hasPermi('stock:management:edit')")
    @Log(title = "物资管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WzMaterialsManagement wzMaterialsManagement) {
        return toAjax(wzMaterialsManagementService.updateWzMaterialsManagement(wzMaterialsManagement));
    }

    /**
     * 删除物资管理
     */
    @PreAuthorize("@ss.hasPermi('stock:management:remove')")
    @Log(title = "物资管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{materialManagementCodes}")
    public AjaxResult remove(@PathVariable Long[] materialManagementCodes) {
        return toAjax(wzMaterialsManagementService.deleteWzMaterialsManagementByMaterialManagementCodes(materialManagementCodes));
    }

    @Log(title = "物资管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('stock:management:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<WzMaterialsManagement> util = new ExcelUtil<>(WzMaterialsManagement.class);
        List<WzMaterialsManagement> stockInList = util.importExcel(file.getInputStream());
        for (WzMaterialsManagement wzMaterialsManagement : stockInList) {
            System.out.println(wzMaterialsManagement);
        }
        String operName = getUsername();
        String message = wzMaterialsManagementService.importUser(stockInList, updateSupport, operName);
        // String message = "OK We are testing";
        return success(message);
    }
}
