package com.seer.teach.mall.good.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.mall.entity.CategoryEntity;
import com.seer.teach.mall.response.CategoryResp;
import com.seer.teach.mall.convert.CategoryConvert;
import com.seer.teach.mall.service.ICategoryService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  商品类型前端控制器
 * </p>
 *
 * @author Captain
 * @since 2025-06-10
 */
@RestController
@RequestMapping("/mall/category")
@Tag(name = "管理端 - 商品类型管理")
@Validated
public class AdminCategoryController {

    @Autowired
    private ICategoryService categoryService;


    /**
     * 分頁查詢
     */
    @Operation(summary = "分頁查詢")
    @GetMapping("/page/{pageSize}/{pageNum}")
    @SaCheckPermission("admin:category:page")
    public ResultBean<PageListBean<CategoryResp>> list(
            @Parameter(description = "每页数量") @PathVariable("pageNum") Integer pageNum,
            @Parameter(description = "当前页") @PathVariable("pageSize") Integer pageSize) {
        return ResultBean.success(categoryService.queryPageList(pageNum, pageSize,null,null));
    }

    /**
     * 查看详情
     */
    @Operation(summary = "查看详情")
    @GetMapping("/info/{id}")
    @SaCheckPermission("admin:category:detail")
    public ResultBean<CategoryResp> info(@Parameter(description = "主键") @PathVariable("id") Integer id) {
        CategoryEntity category = categoryService.getById(id);
        return ResultBean.success(CategoryConvert.INSTANCE.one(category));
    }

    /**
     * 保存
     */
    @Operation(summary = "保存")
    @PostMapping("/save")
    @SaCheckPermission("admin:category:save")
    public ResultBean<Boolean> save(@RequestBody CategoryEntity category) {
        return ResultBean.success(categoryService.save(category));
    }

    /**
     * 修改显示状态
     */
    @Operation(summary = "修改显示状态")
    @PutMapping("/{id}/updateIsShowStatus")
    @SaCheckPermission("admin:category:updateIsShowStatus")
    public ResultBean<Boolean> updateIsShowStatus( @PathVariable("id") Integer id ,@RequestParam("isShow")Integer isShow) {
        return ResultBean.success(categoryService.updateIsShowStatus(id,isShow));
    }

    /**
     * 修改是否首页显示状态
     */
    @Operation(summary = "修改是否首页显示状态")
    @PutMapping("/{id}/updateShowIndexStatus")
    @SaCheckPermission("admin:category:updateShowIndexStatus")
    public ResultBean<Boolean> updateShowIndexStatus( @PathVariable("id") Integer id ,@RequestParam("showIndex")Integer showIndex) {
        return ResultBean.success(categoryService.updateShowIndexStatus(id,showIndex));
    }

    /**
     * 修改
     */
    @Operation(summary = "修改")
    @PutMapping("/update")
    @SaCheckPermission("admin:category:update")
    public ResultBean<Boolean> update(@RequestBody CategoryEntity category) {
        return ResultBean.success(categoryService.updateById(category));
    }

    /**
     * 删除
     */
    @Operation(summary = "删除")
    @DeleteMapping("/delete")
    @SaCheckPermission("admin:category:delete")
    public ResultBean<Boolean> delete(@Parameter(description = "主键")@RequestBody List<Integer> ids) {
        return ResultBean.success(categoryService.removeBatchByIds(ids));
    }

    /**
     * 查看所有列表
     */
    @Operation(summary = "查看所有列表")
    @GetMapping("/queryAll")
    @SaCheckPermission("admin:category:queryAll")
    public ResultBean<List<CategoryResp>> queryAll() {
        List<CategoryEntity> list = categoryService.query().list();
        //添加顶级菜单
        CategoryEntity root = new CategoryEntity();
        root.setId(0);
        root.setName("一级分类");
        root.setParentId(-1);
        list.add(0,root);
        return ResultBean.success(CategoryConvert.INSTANCE.list(list));
    }

}
