package com.fs.oms.controller;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.oms.domain.OmsProduct;
import com.fs.oms.domain.OmsProductAttr;
import com.fs.oms.param.OmsProductAddEditParam;
import com.fs.oms.param.OmsProductAttrValueParam;
import com.fs.oms.service.IOmsProductAttrService;
import com.fs.oms.service.IOmsProductAttrValueService;
import com.fs.oms.service.IOmsProductService;
import com.fs.oms.vo.OmsProductAttrValueVO;
import com.fs.oms.vo.OmsProductExportVO;
import com.fs.oms.vo.OmsProductListVO;
import com.fs.store.param.FsProductAttrValueParam;
import com.fs.store.service.IFsStoreProductAttrValueService;
import com.fs.store.vo.FsStoreProductAttrValueVO;
import com.mysql.cj.util.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品Controller
 *
 * @author oms
 * @date 2023-06-07
 */
@RestController
@RequestMapping("/oms/product")
public class OmsProductController extends BaseController
{
    @Autowired
    private IOmsProductService omsProductService;

    @Autowired
    private IOmsProductAttrService attrService;

    @Autowired
    private IOmsProductAttrValueService attrValueService;

    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('oms:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(OmsProduct omsProduct)
    {
        startPage();
        List<OmsProductListVO> list;
        if(StringUtils.isNullOrEmpty(omsProduct.getBarCode())){
            list = omsProductService.selectOmsProductListVO(omsProduct);
        }else{
            list = omsProductService.selectOmsProductBarCodeListVO(omsProduct);
        }
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('oms:product:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OmsProduct omsProduct)
    {
        List<OmsProductExportVO> list = omsProductService.selectOmsProductExportList(omsProduct);
        ExcelUtil<OmsProductExportVO> util = new ExcelUtil<OmsProductExportVO>(OmsProductExportVO.class);
        return util.exportExcel(list, "product");
    }

    /**
     * 获取商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('oms:product:query')")
    @GetMapping(value = "/{productId}")
    public R getInfo(@PathVariable("productId") Long productId)
    {
        OmsProduct product = omsProductService.selectOmsProductById(productId);
        List<OmsProductAttr> attrs = attrService.selectOmsProductAttrByProductId(productId);
        return R.ok().put("data",product).put("attrs", attrs);
    }

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('oms:product:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addOrEdit")
    public R add(@RequestBody OmsProductAddEditParam omsProduct)
    {
        return omsProductService.addOrEdit(omsProduct);
    }

//    /**
//     * 修改商品
//     */
//    @PreAuthorize("@ss.hasPermi('system:product:edit')")
//    @Log(title = "商品", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody OmsProduct omsProduct)
//    {
//        return toAjax(omsProductService.updateOmsProduct(omsProduct));
//    }

    /**
     * 删除商品
     */
    @PreAuthorize("@ss.hasPermi('oms:product:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds)
    {
        return toAjax(omsProductService.deleteOmsProductByIds(productIds));
    }

    @ApiOperation(value = "生成属性")
    @PostMapping(value = "/genFormatAttr/{productId}")
    public ResponseEntity genFormatAttr(@PathVariable Long productId, @RequestBody String jsonStr){
        return new ResponseEntity<>(omsProductService.getFormatAttr(productId,jsonStr), HttpStatus.OK);
    }

    @GetMapping("/getOmsProductAttrValueList")
    public TableDataInfo getOmsProductAttrValueList(OmsProductAttrValueParam param)
    {
        startPage();
        List<OmsProductAttrValueVO> list=attrValueService.selectOmsProductAttrValueListVO(param);
        return getDataTable(list);
    }

}
