package com.jeesharp.business.modules.mall.controller;

import com.jeesharp.business.modules.mall.application.MallSpuSpecApplicationService;
import com.jeesharp.business.modules.mall.dto.MallSpuSpecSaveDTO;
import com.jeesharp.business.modules.mall.entity.MallSpuSpec;
import com.jeesharp.business.modules.mall.vo.MallSpuSpecInfoVO;
import com.jeesharp.business.modules.mall.vo.MallSpuSpecListVO;
import com.jeesharp.business.modules.mall.vo.MallSpuSpecQueryVO;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import com.jeesharp.framework.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * SPU规格关联管理 Controller
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/manager/business/mall/spuSpec/v1")
@Tag(name = "SPU规格关联管理")
@AllArgsConstructor
public class MallSpuSpecController extends BaseController {

    private final MallSpuSpecApplicationService mallSpuSpecApplicationService;

    @Operation(summary = "获取详情")
    @GetMapping(value = "info")
    public AjaxResult<MallSpuSpecInfoVO> info(@RequestParam("id") String id) {
        MallSpuSpecInfoVO vo = mallSpuSpecApplicationService.get(id);
        return AjaxResult.okData(vo);
    }

    @Operation(summary = "获取列表（分页）")
    @PostMapping(value = "list")
    public AjaxResult<PageRespVO<MallSpuSpecListVO>> list(@RequestBody MallSpuSpecQueryVO query) {
        PageRespVO<MallSpuSpecListVO> page = mallSpuSpecApplicationService.findPage(new Page<>(query), query);
        return AjaxResult.okData(page);
    }

    @Operation(summary = "保存")
    @PostMapping(value = "save")
    public AjaxResult<Void> save(@RequestBody @Valid MallSpuSpecSaveDTO dto) {
        mallSpuSpecApplicationService.save(dto);
        return AjaxResult.ok("保存成功");
    }

    @Operation(summary = "删除")
    @GetMapping(value = "delete")
    public AjaxResult<Void> delete(@RequestParam("ids") String ids) {
        mallSpuSpecApplicationService.delete(ids);
        return AjaxResult.ok("删除成功");
    }

    @Operation(summary = "获取SPU规格列表")
    @GetMapping(value = "getSpuSpecs")
    public AjaxResult<List<MallSpuSpec>> getSpuSpecs(@RequestParam("spuId") String spuId) {
        List<MallSpuSpec> specs = mallSpuSpecApplicationService.getSpuSpecs(spuId);
        return AjaxResult.okData(specs);
    }

    @Operation(summary = "批量保存SPU规格")
    @PostMapping(value = "batchSave")
    public AjaxResult<Void> batchSave(@RequestParam("spuId") String spuId,
                               @RequestParam("specIds") List<String> specIds) {
        mallSpuSpecApplicationService.batchSaveSpuSpecs(spuId, specIds);
        return AjaxResult.ok("保存成功");
    }
}
