package com.gxwebsoft.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.shop.entity.SpecValue;
import com.gxwebsoft.shop.param.SpecValueParam;
import com.gxwebsoft.shop.service.SpecValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 规格值控制器
 *
 * @author 科技小王子
 * @since 2024-04-01 09:49:15
 */
@Api(tags = "规格值管理")
@RestController
@RequestMapping("/api/shop/spec-value")
public class SpecValueController extends BaseController {
    @Resource
    private SpecValueService specValueService;

    @ApiOperation("分页查询规格值")
    @GetMapping("/page")
    public ApiResult<PageResult<SpecValue>> page(SpecValueParam param) {
        // 使用关联查询
        return success(specValueService.pageRel(param));
    }

    @ApiOperation("查询全部规格值")
    @GetMapping()
    public ApiResult<List<SpecValue>> list(SpecValueParam param) {
        // 使用关联查询
        return success(specValueService.listRel(param));
    }

    @ApiOperation("根据id查询规格值")
    @GetMapping("/{id}")
    public ApiResult<SpecValue> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(specValueService.getByIdRel(id));
    }

    @ApiOperation("添加规格值")
    @PostMapping()
    public ApiResult<?> save(@RequestBody SpecValue specValue) {
        if (specValueService.count(new LambdaQueryWrapper<SpecValue>().eq(SpecValue::getSpecId,specValue.getSpecId()).eq(SpecValue::getSpecValue,specValue.getSpecValue())) > 0) {
          return fail("规格值已存在");
        }
        if (specValueService.save(specValue)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("修改规格值")
    @PutMapping()
    public ApiResult<?> update(@RequestBody SpecValue specValue) {
        if (specValueService.updateById(specValue)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("删除规格值")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (specValueService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @ApiOperation("批量添加规格值")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<SpecValue> list) {
        if (specValueService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("批量修改规格值")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<SpecValue> batchParam) {
        if (batchParam.update(specValueService, "spec_value_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("批量删除规格值")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (specValueService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
