package com.atguigu.ssyx.product.controller;


import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.product.AttrGroup;
import com.atguigu.ssyx.product.service.AttrGroupService;
import com.atguigu.ssyx.vo.product.AttrGroupQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
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 = "AttrGroup管理", tags = "平台属性分组管理")
@RestController

@RequestMapping(value = "/admin/product/attrGroup")
public class AttrGroupController {

    @Autowired
    private AttrGroupService attrGroupService;

    @ApiOperation(value = "获取平台属性分组分页列表")
    @GetMapping("{page}/{limit}")
    public Result index(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,
                        @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit,
                        @ApiParam(name = "attrGroupQueryVo", value = "查询对象", required = false)
                        AttrGroupQueryVo attrGroupQueryVo) {
        try {
            Page<AttrGroup> pageParam = new Page<>(page, limit);
            IPage<AttrGroup> pageModel = attrGroupService.selectPage(pageParam, attrGroupQueryVo);
            return Result.ok(pageModel);
        } catch (Exception e) {
            throw new RuntimeException("获取平台属性分组分页列表异常", e);
        }
    }

    @ApiOperation(value = "获取平台属性分组")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        try {
            AttrGroup attrGroup = attrGroupService.getById(id);
            return StringUtils.isNotEmpty(attrGroup.toString()) ? Result.ok(attrGroup) : Result.fail(attrGroup);
        } catch (Exception e) {
            throw new RuntimeException("获取商品分类信息异常", e);
        }
    }

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

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

    @ApiOperation(value = "删除平台属性分组")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        try {
            attrGroupService.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 {
            attrGroupService.removeByIds(idList);
            return Result.ok(null);
        } catch (Exception e) {
            throw new RuntimeException("根据id列表删除平台属性分组异常", e);
        }
    }

    @ApiOperation(value = "获取全部平台属性分组")
    @GetMapping("findAllList")
    public Result findAllList() {
        try {
            return Result.ok(attrGroupService.findAllList());
        } catch (Exception e) {
            throw new RuntimeException("获取全部平台属性分组异常", e);
        }

    }
}

