package cn.iocoder.yudao.module.product.controller.admin.storecategory;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.product.controller.admin.storecategory.vo.StoreProductCategoryListReqVO;
import cn.iocoder.yudao.module.product.controller.admin.storecategory.vo.StoreProductCategoryUpdateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.storecategory.vo.StoreProductCategoryCreateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.storecategory.vo.StoreProductCategoryRespVO;
import cn.iocoder.yudao.module.product.convert.storecategory.StoreProductCategoryConvert;
import cn.iocoder.yudao.module.product.dal.dataobject.storecategory.StoreProductCategoryDO;
import cn.iocoder.yudao.module.product.service.storecategory.StoreProductCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import javax.annotation.Resource;
import javax.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 商品分类")
@RestController
@RequestMapping("/product/store/product/category")
@Validated
public class StoreProductCategoryController {

    @Resource
    private StoreProductCategoryService storeCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建商品分类")
    @PreAuthorize("@ss.hasPermission('product:category:create')")
    public CommonResult<Long> createCategory(@Valid @RequestBody StoreProductCategoryCreateReqVO createReqVO) {
        return success(storeCategoryService.createCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品分类")
    @PreAuthorize("@ss.hasPermission('product:category:update')")
    public CommonResult<Boolean> updateCategory(@Valid @RequestBody StoreProductCategoryUpdateReqVO updateReqVO) {
        storeCategoryService.updateCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:category:delete')")
    public CommonResult<Boolean> deleteCategory(@RequestParam("id") Long id) {
        storeCategoryService.deleteCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:category:query')")
    public CommonResult<StoreProductCategoryRespVO> getCategory(@RequestParam("id") Long id) {
        StoreProductCategoryDO category = storeCategoryService.getCategory(id);
        return success(StoreProductCategoryConvert.INSTANCE.convert(category));
    }

    @GetMapping("/list")
    @Operation(summary = "获得商品分类列表")
    @PreAuthorize("@ss.hasPermission('product:category:query')")
    public CommonResult<List<StoreProductCategoryRespVO>> getCategoryList(@Valid StoreProductCategoryListReqVO treeListReqVO) {
        List<StoreProductCategoryDO> list = storeCategoryService.getEnableCategoryList(treeListReqVO);
        list.sort(Comparator.comparing(StoreProductCategoryDO::getSort));
        return success(StoreProductCategoryConvert.INSTANCE.convertList(list));
    }

}
