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.MallAttributeTemplateBo;
import org.dromara.mall.product.domain.vo.MallAttributeTemplateVo;
import org.dromara.mall.product.service.MallAttributeTemplateService;
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/template")
public class AdminMallAttributeTemplateController extends BaseController {

    private final MallAttributeTemplateService mallAttributeTemplateService;

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

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

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

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

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

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