package com.kxs.product.biz.controller.kxsapp;

import com.kxs.common.core.constant.CacheConstants;
import com.kxs.common.core.util.R;
import com.kxs.common.security.annotation.Inner;
import com.kxs.product.biz.constant.ProductConstants;
import com.kxs.product.biz.service.KxsBrandService;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 产品模块(品牌管理)控制层
 *
 * @author 系统
 * @since 2023-12-20 10:09:39
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("brand")
public class BrandController {

    /**
     * 服务对象
     */
    private final KxsBrandService kxsBrandService;



    /**
     * 品牌列表
     *
     * @return 品牌列表
     */
    @GetMapping("/list")
    public R list() {

        return R.ok(kxsBrandService.getSortByList());
    }


    /**
     * 按品牌id
     *
     * @param id id
     * @return {@link R}
     */
    @Inner
    @GetMapping("/loadByBrand")
    @Cacheable(value = ProductConstants.BRAND_DETAILS, key = "#id", unless = "#result == null")
    public R loadByBrand(@RequestParam("id") Integer id) {

        return R.ok(kxsBrandService.getById(id));
    }


    /**
     * 品牌排序
     *
     * @param param 实体对象
     * @return 品牌排序
     */
    @PostMapping("/sort")
    public R sort(@RequestBody Map<String, List<Integer>> param) {
        return R.ok(kxsBrandService.sortByList(param.get("sorts")));
    }

}

