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) {

        // 严格按 cgldb.sql：查询 std_alloy(GRADEID, NAME)
        LambdaQueryWrapper<StdAlloy> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(StdAlloy::getGradeid, StdAlloy::getName);
        if (StringUtils.isNotBlank(keyword)) {
            queryWrapper.like(StdAlloy::getName, keyword)
                    .or()
                    .like(StdAlloy::getGradeid, keyword);
        }
        queryWrapper.orderByAsc(StdAlloy::getName);

        List<StdAlloy> stdAlloyList = steelGradeInfoService.list(queryWrapper);

        List<StdAlloyVO> stdAlloyVOList = new ArrayList<>();
        for (StdAlloy stdAlloy : stdAlloyList) {
            StdAlloyVO stdAlloyVO = new StdAlloyVO();
            BeanUtils.copyProperties(stdAlloy, stdAlloyVO);
            stdAlloyVOList.add(stdAlloyVO);
        }

        return R.ok(stdAlloyVOList);
    }

    @GetMapping("/info")
    @Operation(summary ="询单个钢种详情")
    public R<StdAlloy> getSteelGradeInfo(@RequestParam Integer gradeid) {
        return R.ok(steelGradeInfoService.getById(gradeid));
    }


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

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

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