package com.sckw.system.controller.forkliftapp;

import com.sckw.core.utils.BeanUtils;
import com.sckw.core.web.response.BaseResult;
import com.sckw.system.model.Materials;
import com.sckw.system.model.vo.res.MaterialsResVo;
import com.sckw.system.repository.MaterialsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 物料信息Controller
 * @author chenxiaofei
 * @date 2026-01-07
 */
@RestController
@RequestMapping("/materials")
@Tag(name = "物料信息相关接口")
public class MaterialsController {

    @Autowired
    private MaterialsService materialsService;

    /**
     * 获取物料列表
     * @return BaseResult
     */
    @GetMapping("/list")
    @Operation(summary = "获取物料列表", description = "获取所有物料信息列表")
    public BaseResult<List<MaterialsResVo>> getMaterialsList() {
        List<Materials> list = materialsService.list();
        List<MaterialsResVo> resVoList = list.stream()
                .map(material -> {
                    MaterialsResVo resVo = new MaterialsResVo();
                    BeanUtils.copyProperties(material, resVo);
                    return resVo;
                })
                .collect(Collectors.toList());
        return BaseResult.success(resVoList);
    }

    /**
     * 根据ID获取物料信息
     * @param id 物料ID
     * @return BaseResult
     */
    @GetMapping("/detail")
    @Operation(summary = "获取物料详情", description = "根据ID获取物料详细信息")
    public BaseResult<MaterialsResVo> getMaterialDetail(@RequestParam Long id) {
        Materials material = materialsService.getById(id);
        if (material == null) {
            return BaseResult.success(null);
        }
        MaterialsResVo resVo = new MaterialsResVo();
        BeanUtils.copyProperties(material, resVo);
        return BaseResult.success(resVo);
    }

}

