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.web.core.BaseController;
import org.dromara.mall.product.domain.bo.MallProductAttributeBo;
import org.dromara.mall.product.domain.vo.MallProductAttributeVo;
import org.dromara.mall.product.service.MallProductAttributeService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

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

    private final MallProductAttributeService mallProductAttributeService;

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

    /**
     * 根据商品ID查询商品属性关联列表
     */
    @Operation(summary = "根据商品ID查询商品属性关联列表", description = "根据商品ID查询商品属性关联列表")
    @SaCheckPermission("mall:product:attribute:product:list")
    @GetMapping("/product/{productId}")
    public R<List<MallProductAttributeVo>> listByProductId(@Parameter(description = "商品ID", required = true)
                                                             @NotNull(message = "商品ID不能为空")
                                                             @PathVariable Long productId) {
        return R.ok(mallProductAttributeService.queryListByProductId(productId));
    }

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

    /**
     * 批量新增商品属性关联
     */
    @Operation(summary = "批量新增商品属性关联", description = "批量新增商品属性关联")
    @SaCheckPermission("mall:product:attribute:product:add")
    @Log(title = "商品属性关联", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public R<Void> addBatch(@Parameter(description = "商品属性关联信息列表") @Validated @RequestBody List<MallProductAttributeBo> list) {
        return toAjax(mallProductAttributeService.insertBatch(list));
    }

    /**
     * 保存商品属性关联（先删除旧的，再插入新的）
     */
    @Operation(summary = "保存商品属性关联", description = "保存商品属性关联，先删除旧的，再插入新的")
    @SaCheckPermission("mall:product:attribute:product:edit")
    @Log(title = "商品属性关联", businessType = BusinessType.UPDATE)
    @PostMapping("/product/{productId}")
    public R<Void> saveProductAttributes(@Parameter(description = "商品ID", required = true)
                                          @NotNull(message = "商品ID不能为空")
                                          @PathVariable Long productId,
                                          @Parameter(description = "商品属性关联信息列表") @RequestBody List<MallProductAttributeBo> list) {
        return toAjax(mallProductAttributeService.saveProductAttributes(productId, list));
    }

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

    /**
     * 删除商品属性关联
     */
    @Operation(summary = "删除商品属性关联", description = "批量删除商品属性关联")
    @SaCheckPermission("mall:product:attribute: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(mallProductAttributeService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 根据商品ID删除商品属性关联
     */
    @Operation(summary = "根据商品ID删除商品属性关联", description = "根据商品ID删除商品属性关联")
    @SaCheckPermission("mall:product:attribute:product:remove")
    @Log(title = "商品属性关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/product/{productId}")
    public R<Void> removeByProductId(@Parameter(description = "商品ID", required = true)
                                      @NotNull(message = "商品ID不能为空")
                                      @PathVariable Long productId) {
        return toAjax(mallProductAttributeService.deleteByProductId(productId));
    }
}
