package com.atguigu.ssyx.product.controller;


import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.product.SkuInfo;
import com.atguigu.ssyx.product.service.SkuInfoService;
import com.atguigu.ssyx.vo.product.SkuInfoQueryVo;
import com.atguigu.ssyx.vo.product.SkuInfoVo;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * sku信息 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2023-09-15
 */
@Api(value = "SkuInfo管理", tags = "商品Sku管理")
@RestController

@RequestMapping(value = "/admin/product/skuInfo")
public class SkuInfoController {

    @Autowired
    private SkuInfoService skuInfoService;

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

    @ApiOperation(value = "新增商品sku信息")
    @PostMapping("save")
    public Result save(@RequestBody SkuInfoVo skuInfoVo) {
        try {
            skuInfoService.saveSkuInfo(skuInfoVo);
            return Result.ok(null);
        } catch (Exception e) {
            throw new RuntimeException("新增商品sku信息异常", e);
        }
    }

    @ApiOperation(value = "获取商品sku信息")
    @GetMapping("get/{id}")
    public Result<SkuInfoVo> get(@PathVariable Long id) {
        try {
            SkuInfoVo skuInfoVo = skuInfoService.getSkuInfoVo(id);
            return Result.ok(skuInfoVo);
        } catch (Exception e) {
            throw new RuntimeException("新增商品sku信息异常", e);
        }
    }

    @ApiOperation(value = "修改商品sku信息")
    @PutMapping("update")
    public Result updateById(@RequestBody SkuInfoVo skuInfoVo) {
        try {
            skuInfoService.updateSkuInfo(skuInfoVo);
            return Result.ok(null);
        } catch (Exception e) {
            throw new RuntimeException("修改商品sku信息异常", e);
        }
    }

    @ApiOperation(value = "删除商品sku信息")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        try {
            skuInfoService.deleteById(id);
            return Result.ok(null);
        } catch (Exception e) {
            throw new RuntimeException("删除商品sku信息异常", e);
        }
    }

    @ApiOperation(value = "根据id列表删除商品sku信息")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        try {
            skuInfoService.deleteByIds(idList);
            return Result.ok(null);
        } catch (Exception e) {
            throw new RuntimeException("根据id列表删除商品sku信息异常", e);
        }
    }

    @ApiOperation(value = "商品审核")
    @GetMapping("check/{skuId}/{status}")
    public Result check(@PathVariable("skuId") Long skuId, @PathVariable("status") Integer status) {
        try {
            skuInfoService.check(skuId, status);
            return Result.ok(null);
        } catch (Exception e) {
            throw new RuntimeException("商品审核异常", e);
        }
    }

    @ApiOperation(value = "商品上/下架")
    @GetMapping("publish/{skuId}/{status}")
    public Result publish(@PathVariable("skuId") Long skuId,
                          @PathVariable("status") Integer status) {
        try {
            skuInfoService.publish(skuId, status);
            return Result.ok(null);
        } catch (Exception e) {
            throw new RuntimeException("商品上/下架异常", e);
        }
    }

    @ApiOperation(value = "新人专享")
    @GetMapping("isNewPerson/{skuId}/{status}")
    public Result isNewPerson(@PathVariable("skuId") Long skuId,
                              @PathVariable("status") Integer status) {
        try {
            skuInfoService.isNewPerson(skuId, status);
            return Result.ok(null);
        } catch (Exception e) {
            throw new RuntimeException("新人专享异常", e);
        }
    }
}

