package com.fizz.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fizz.business.domain.StdAlloy;
import com.fizz.business.service.SteelGradeInfoService;
import com.fizz.business.vo.StdAlloyVO;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@Tag(name ="钢种管理")
@RestController
@RequestMapping("/api/steelGrade")
@Anonymous
public class SteelGradeInfoController {

    @Autowired
    private SteelGradeInfoService steelGradeInfoService;

    @GetMapping("/list")
    @Operation(summary = "查询钢种列表")
    public R<List<StdAlloyVO>> list(@RequestParam(value = "keyword", required = false) String keyword) {

        // 使用 LambdaQueryWrapper 查询 StdAlloy 表中的数据，支持按名称/编号模糊查询
        LambdaQueryWrapper<StdAlloy> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(StdAlloy::getGradeid, StdAlloy::getName); // 只查询 gradeId 和 name 字段
        if (StringUtils.isNotBlank(keyword)) {
            queryWrapper.like(StdAlloy::getName, keyword)
                    .or()
                    .like(StdAlloy::getGradeid, keyword);
        }
        queryWrapper.orderByAsc(StdAlloy::getName); // 按 name 排序

        // 查询 StdAlloy 数据
        List<StdAlloy> stdAlloyList = steelGradeInfoService.list(queryWrapper);

        // 使用 BeanUtils 将 StdAlloy 对象的字段映射到 StdAlloyVO
        List<StdAlloyVO> stdAlloyVOList = new ArrayList<>();
        for (StdAlloy stdAlloy : stdAlloyList) {
            StdAlloyVO stdAlloyVO = new StdAlloyVO();
            BeanUtils.copyProperties(stdAlloy, stdAlloyVO); // 将 StdAlloy 属性复制到 StdAlloyVO
            stdAlloyVOList.add(stdAlloyVO);
        }

        // 返回结果
        return R.ok(stdAlloyVOList);
    }

    @GetMapping("/info")
    @Operation(summary ="询单个钢种详情")
    public R<StdAlloy> getSteelGradeInfo(@RequestParam Integer gradeid) {

        // 使用 LambdaQueryWrapper 查询 StdAlloy 表中的数据
        LambdaQueryWrapper<StdAlloy> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StdAlloy::getGradeid, gradeid); // 只查询 gradeId 和 name 字段

        // 查询 StdAlloy 数据
        StdAlloy stdAlloyList = steelGradeInfoService.getById(gradeid);

        // 返回结果
        return R.ok(stdAlloyList);
    }


    @PostMapping("/add")
    @Operation(summary ="新增")
    public R<Boolean> add(@RequestBody StdAlloy steelGradeInfo) {
        return R.ok(steelGradeInfoService.save(steelGradeInfo));
    }

    @PutMapping("/update")
    @Operation(summary ="更新")
    public R<Boolean> update(@RequestBody StdAlloy steelGradeInfo) {
        return R.ok(steelGradeInfoService.updateById(steelGradeInfo));
    }

    @Operation(summary ="删除")
    @DeleteMapping("/delete/{id}")
    public R<Boolean> delete(@PathVariable Integer id) {
        return R.ok(steelGradeInfoService.removeById(id));
    }
}