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.MallCategoryBo;
import org.dromara.mall.product.domain.vo.MallCategoryVo;
import org.dromara.mall.product.service.MallCategoryService;
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/category")
public class AdminMallCategoryController extends BaseController {

    private final MallCategoryService mallCategoryService;

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

    /**
     * 查询商品分类树形列表
     */
    @Operation(summary = "查询商品分类树形列表", description = "查询商品分类树形结构")
    @SaCheckPermission("mall:product:category:list")
    @GetMapping("/tree")
    public R<List<MallCategoryVo>> tree() {
        List<MallCategoryVo> list = mallCategoryService.queryTreeList();
        return R.ok(list);
    }

    /**
     * 导出商品分类列表
     */
    @Operation(summary = "导出商品分类列表", description = "导出商品分类到Excel")
    @SaCheckPermission("mall:product:category:export")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Parameter(description = "分类查询条件") MallCategoryBo bo,
                       @Parameter(description = "响应对象") HttpServletResponse response) {
        List<MallCategoryVo> list = mallCategoryService.queryList(bo);
        ExcelUtil.exportExcel(list, "商品分类", MallCategoryVo.class, response);
    }

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

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

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

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