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.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
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.MallAttributeValueBo;
import org.dromara.mall.product.domain.vo.MallAttributeValueVo;
import org.dromara.mall.product.service.MallAttributeValueService;
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/value")
public class AdminMallAttributeValueController extends BaseController {

    private final MallAttributeValueService mallAttributeValueService;

    /**
     * 查询属性值列表
     */
    @Operation(summary = "查询属性值列表", description = "分页查询属性值列表")
    @SaCheckPermission("mall:product:attribute:value:list")
    @GetMapping("/list")
    public TableDataInfo<MallAttributeValueVo> list(@Parameter(description = "属性值查询条件") MallAttributeValueBo bo,
                                                     @Parameter(description = "分页参数") PageQuery pageQuery) {
        return mallAttributeValueService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出属性值列表
     */
    @Operation(summary = "导出属性值列表", description = "导出属性值到Excel")
    @SaCheckPermission("mall:product:attribute:value:export")
    @Log(title = "属性值", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Parameter(description = "属性值查询条件") MallAttributeValueBo bo,
                       @Parameter(description = "响应对象") HttpServletResponse response) {
        List<MallAttributeValueVo> list = mallAttributeValueService.queryList(bo);
        ExcelUtil.exportExcel(list, "属性值", MallAttributeValueVo.class, response);
    }

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

    /**
     * 根据属性ID查询属性值列表
     */
    @Operation(summary = "根据属性ID查询属性值列表", description = "根据属性ID查询属性值列表")
    @SaCheckPermission("mall:product:attribute:value:list")
    @GetMapping("/attribute/{attributeId}")
    public R<List<MallAttributeValueVo>> listByAttributeId(@Parameter(description = "属性ID", required = true)
                                                             @NotNull(message = "属性ID不能为空")
                                                             @PathVariable Long attributeId) {
        return R.ok(mallAttributeValueService.queryListByAttributeId(attributeId));
    }

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

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

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