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.MallAttributeBo;
import org.dromara.mall.product.domain.vo.MallAttributeVo;
import org.dromara.mall.product.service.MallAttributeService;
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")
public class AdminMallAttributeController extends BaseController {

    private final MallAttributeService mallAttributeService;

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

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

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

    /**
     * 根据模板ID查询属性列表
     */
    @Operation(summary = "根据模板ID查询属性列表", description = "根据属性模板ID查询属性列表")
    @SaCheckPermission("mall:product:attribute:list")
    @GetMapping("/template/{templateId}")
    public R<List<MallAttributeVo>> listByTemplateId(@Parameter(description = "模板ID", required = true)
                                                       @NotNull(message = "模板ID不能为空")
                                                       @PathVariable Long templateId) {
        return R.ok(mallAttributeService.queryListByTemplateId(templateId));
    }

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

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

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