package com.fs.store.controller;

import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.core.security.SecurityUtils;
import com.fs.store.domain.*;
import com.fs.store.param.FsProductAttrValueParam;
import com.fs.store.service.IFsStoreProductAttrValueService;
import com.fs.store.service.IFsStoreProductService;
import com.fs.store.vo.FsStoreProductAttrValueVO;
import com.fs.store.vo.FsStoreProductListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品Controller
 *
 * @author fs
 * @date 2022-03-15
 */
@RestController
@RequestMapping("/store/storeProduct")
public class FsStoreProductController extends BaseController
{
    @Autowired
    private IFsStoreProductService fsStoreProductService;


    @Autowired
    private IFsStoreProductAttrValueService attrValueService;

    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('store:storeProduct:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsStoreProduct fsStoreProduct)
    {
        startPage();
        fsStoreProduct.setIsDel(0);
        // 只能展示属于本公司的产品
        fsStoreProduct.setCompanyIds(String.valueOf(SecurityUtils.getLoginUser().getCompany().getCompanyId()));
        List<FsStoreProductListVO> list = fsStoreProductService.selectFsStoreProductListVO(fsStoreProduct);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('store:storeProduct:query')")
    @GetMapping(value = "/{productId}")
    public R getInfo(@PathVariable("productId") Long productId)
    {
        FsStoreProduct product=fsStoreProductService.selectFsStoreProductById(productId);
        FsStoreProductAttrValue map=new FsStoreProductAttrValue();
        map.setProductId(productId);
        List<FsStoreProductAttrValue> values=attrValueService.selectFsStoreProductAttrValueList(map);
        return R.ok().put("product", product).put("values",values);
    }

    @GetMapping("/getStoreProductAttrValueList")
    public TableDataInfo getStoreProductAttrValueList(FsProductAttrValueParam param)
    {
        startPage();
        List<FsStoreProductAttrValueVO> list=attrValueService.selectFsStoreProductAttrValueListVO(param);
        return getDataTable(list);
    }

}
