package com.atguigu.ssyx.product.controller;


import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.product.Attr;
import com.atguigu.ssyx.product.service.AttrService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品属性 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2023-09-15
 */
@Api(value = "Attr管理", tags = "平台属性管理")
@RestController
@RequestMapping(value = "/admin/product/attr")

public class AttrController {

    @Autowired
    private AttrService attrService;

    @ApiOperation(value = "获取平台属性列表")
    @GetMapping("{attrGroupId}")
    public Result index(
            @ApiParam(name = "attrGroupId", value = "分组id", required = true)
            @PathVariable Long attrGroupId) {
        try {
            return Result.ok(attrService.findByAttrGroupId(attrGroupId));
        } catch (Exception e) {
            throw new RuntimeException("获取平台属性列表异常", e);
        }
    }

    @ApiOperation(value = "获取平台属性")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        try {
            Attr attr = attrService.getById(id);
            return Result.ok(attr);
        } catch (Exception e) {
            throw new RuntimeException("获取平台属性异常", e);
        }
    }

    @ApiOperation(value = "新增平台属性")
    @PostMapping("save")
    public Result save(@RequestBody Attr attr) {
        try {
            attrService.save(attr);
            return Result.ok(null);
        } catch (Exception e) {
            throw new RuntimeException("新增平台属性异常", e);
        }
    }

    @ApiOperation(value = "修改平台属性")
    @PutMapping("update")
    public Result updateById(@RequestBody Attr attr) {
        try {
            attrService.updateById(attr);
            return Result.ok(null);
        } catch (Exception e) {
            throw new RuntimeException("修改平台属性异常", e);
        }
    }

    @ApiOperation(value = "删除平台属性")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        try {
            attrService.removeById(id);
            return Result.ok(null);
        } catch (Exception e) {
            throw new RuntimeException("删除平台属性异常", e);
        }
    }

    @ApiOperation(value = "根据id列表删除平台属性")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        try {
            attrService.removeByIds(idList);
            return Result.ok(null);
        } catch (Exception e) {
            throw new RuntimeException("根据id列表删除平台属性表异常", e);
        }
    }
}

