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.MallBrandBo;
import org.dromara.mall.product.domain.vo.MallBrandVo;
import org.dromara.mall.product.service.MallBrandService;
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/brand")
public class AdminMallBrandController extends BaseController {

    private final MallBrandService mallBrandService;

    /**
     * 查询品牌列表
     */
    @Operation(summary = "查询品牌列表", description = "分页查询品牌列表")
    @SaCheckPermission("mall:product:brand:list")
    @GetMapping("/list")
    public TableDataInfo<MallBrandVo> list(@Parameter(description = "品牌查询条件") MallBrandBo bo,
                                           @Parameter(description = "分页参数") PageQuery pageQuery) {
        return mallBrandService.queryPageList(bo, pageQuery);
    }

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

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

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

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

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