
package com.tmerclub.cloud.combo.controller.app;

import com.tmerclub.cloud.combo.dto.ComboSpuDTO;
import com.tmerclub.cloud.combo.model.ComboSpu;
import com.tmerclub.cloud.combo.service.ComboSpuService;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.product.vo.ComboSpuVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 套装商品项
 *
 * @author tmerclub
 * @date 2022-08-16 09:18:06
 */
@RestController("appComboProdController")
@RequestMapping("/comboProd")
@Tag(name = "套装商品项")
public class ComboSpuController {

    @Autowired
    private ComboSpuService comboProdService;


    @GetMapping("/page")
    @Operation(summary = "获取套装商品项列表", description = "分页获取套装商品项列表")
    public ServerResponseEntity<PageVO<ComboSpuVO>> page(@Valid PageDTO pageDTO) {
        PageVO<ComboSpuVO> comboProdPage = comboProdService.page(pageDTO);
        return ServerResponseEntity.success(comboProdPage);
    }

    @GetMapping
    @Operation(summary = "获取套装商品项", description = "根据comboProdId获取套装商品项")
    public ServerResponseEntity<ComboSpuVO> getByComboProdId(@RequestParam Long comboProdId) {
        return ServerResponseEntity.success(comboProdService.getByComboSpuId(comboProdId));
    }

    @PostMapping
    @Operation(summary = "保存套装商品项", description = "保存套装商品项")
    public ServerResponseEntity<Void> save(@Valid @RequestBody ComboSpuDTO comboProdDTO) {
        ComboSpu comboProd = BeanUtil.map(comboProdDTO, ComboSpu.class);
        comboProdService.save(comboProd);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新套装商品项", description = "更新套装商品项")
    public ServerResponseEntity<Void> update(@Valid @RequestBody ComboSpuDTO comboProdDTO) {
        ComboSpu comboProd = BeanUtil.map(comboProdDTO, ComboSpu.class);
        comboProdService.update(comboProd);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除套装商品项", description = "根据套装商品项id删除套装商品项")
    public ServerResponseEntity<Void> delete(@RequestParam Long comboProdId) {
        comboProdService.deleteById(comboProdId);
        return ServerResponseEntity.success();
    }
}
