package org.dromara.mall.product.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.mall.product.domain.bo.MallProductBo;
import org.dromara.mall.product.domain.vo.MallProductVo;
import org.dromara.mall.product.service.MallProductService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 商品管理Controller
 *
 * @author pengles
 */
@Tag(name = "管理端-商品管理", description = "商品管理接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mall/product")
public class AdminMallProductController extends BaseController {

    private final MallProductService mallProductService;

    /**
     * 查询商品列表
     */
    @Operation(summary = "查询商品列表", description = "分页查询商品列表")
    @SaCheckPermission("mall:product:list")
    @GetMapping("/list")
    public TableDataInfo<MallProductVo> list(@Parameter(description = "商品查询条件") MallProductBo bo,
                                             @Parameter(description = "分页参数") PageQuery pageQuery) {
        return mallProductService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取商品详细信息
     */
    @Operation(summary = "获取商品详细信息", description = "根据商品ID获取商品详细信息")
    @SaCheckPermission("mall:product:query")
    @GetMapping("/{id}")
    public R<MallProductVo> getInfo(@Parameter(description = "商品ID", required = true)
                                    @NotNull(message = "主键不能为空")
                                    @PathVariable Long id) {
        return R.ok(mallProductService.queryById(id));
    }

    /**
     * 新增商品
     */
    @Operation(summary = "新增商品", description = "新增商品")
    @SaCheckPermission("mall:product:add")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Parameter(description = "商品信息") @Validated @RequestBody MallProductBo bo) {
        return toAjax(mallProductService.insertByBo(bo));
    }

    /**
     * 修改商品
     */
    @Operation(summary = "修改商品", description = "修改商品")
    @SaCheckPermission("mall:product:edit")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Parameter(description = "商品信息") @Validated @RequestBody MallProductBo bo) {
        return toAjax(mallProductService.updateByBo(bo));
    }

    /**
     * 删除商品
     */
    @Operation(summary = "删除商品", description = "批量删除商品")
    @SaCheckPermission("mall:product:remove")
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@Parameter(description = "商品ID数组", required = true)
                          @NotNull(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(mallProductService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
