package com.fs.his.controller;

import java.util.List;

import com.fs.common.core.domain.R;
import com.fs.his.domain.FsStoreProductAttr;
import com.fs.his.domain.FsStoreProductRule;
import com.fs.his.param.FsProductAttrValueParam;
import com.fs.his.param.FsStoreProductAddEditParam;
import com.fs.his.service.IFsStoreProductAttrService;
import com.fs.his.vo.FsStoreProductAttrValueVO;
import com.fs.his.vo.FsStoreProductExcelVO;
import com.fs.his.vo.FsStoreProductVO;
import com.fs.his.vo.OptionsVO;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.his.domain.FsStoreProduct;
import com.fs.his.service.IFsStoreProductService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商品Controller
 *
 * @author fs
 * @date 2023-06-08
 */
@RestController
@RequestMapping("/his/storeProduct")
public class FsStoreProductController extends BaseController
{
    @Autowired
    private IFsStoreProductService fsStoreProductService;
    @Autowired
    private IFsStoreProductAttrService attrService;
    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('his:storeProduct:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsStoreProduct fsStoreProduct)
    {
        startPage();
        List<FsStoreProductVO> list = fsStoreProductService.selectFsStoreProductListVO(fsStoreProduct);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('his:storeProduct:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsStoreProduct fsStoreProduct)
    {
        List<FsStoreProductExcelVO> list = fsStoreProductService.selectFsStoreProductExcelVO(fsStoreProduct);
        ExcelUtil<FsStoreProductExcelVO> util = new ExcelUtil<FsStoreProductExcelVO>(FsStoreProductExcelVO.class);
        return util.exportExcel(list, "商品数据");
    }

    @Log(title = "商品导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<FsStoreProductExcelVO> util = new ExcelUtil<>(FsStoreProductExcelVO.class);
        List<FsStoreProductExcelVO> list = util.importExcel(file.getInputStream());
        String message = fsStoreProductService.importSrorePeoduct(list, updateSupport);
        return AjaxResult.success(message);
    }




    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<FsStoreProductExcelVO> util = new ExcelUtil<>(FsStoreProductExcelVO.class);
        return util.importTemplateExcel("商品数据");
    }

//    /**
//     * 获取商品详细信息
//     */
//    @PreAuthorize("@ss.hasPermi('his:product:query')")
//    @GetMapping(value = "/{productId}")
//    public AjaxResult getspInfo(@PathVariable("productId") Long productId)
//    {
//        return AjaxResult.success(fsStoreProductService.selectFsStoreProductByProductId(productId));
//    }
    @GetMapping("/getStoreProductAttrValueList")
    public TableDataInfo getStoreProductAttrValueList(FsProductAttrValueParam param)
    {
    startPage();
    List<FsStoreProductAttrValueVO> list=fsStoreProductService.selectFsStoreProductAttrValueListVO(param);
    return getDataTable(list);
}

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('his:storeProduct:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsStoreProduct fsStoreProduct)
    {
        return toAjax(fsStoreProductService.insertFsStoreProduct(fsStoreProduct));
    }



    @PreAuthorize("@ss.hasPermi('his:storeProduct:editPrice')")
    @PostMapping("/price/{price}")
    public AjaxResult editPrice(@RequestBody FsStoreProduct fsStoreProduct, @PathVariable("price")Integer price)
    {

        return toAjax(fsStoreProductService.updateFsStoreProductPrice(fsStoreProduct,price));
    }

    /**
     * 修改商品
     */
    @PreAuthorize("@ss.hasPermi('his:storeProduct:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsStoreProduct fsStoreProduct)
    {
        return toAjax(fsStoreProductService.updateFsStoreProduct(fsStoreProduct));
    }

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


    /**
     * 获取商品商家列表
     */
    @GetMapping("/storeList")
    public TableDataInfo storeList()
    {
        List<OptionsVO> list = fsStoreProductService.selectFsStoreProductStoreList();
        return getDataTable(list);
    }

    @ApiOperation(value = "生成属性")
    @PostMapping(value = "/genFormatAttr/{productId}")
    public ResponseEntity genFormatAttr(@PathVariable Long productId, @RequestBody String jsonStr){

        return new ResponseEntity<>(fsStoreProductService.getFormatAttr(productId,jsonStr), HttpStatus.OK);
    }
    /**
     * 获取商品详细信息
     */
    @GetMapping(value = "/{productId}")
    public R getInfo(@PathVariable("productId") Long productId)
    {
        FsStoreProduct product = fsStoreProductService.selectFsStoreProductByProductId(productId);
        List<FsStoreProductAttr> attrs=attrService.selectFsStoreProductAttrByProductId(productId);
        return R.ok().put("data",product).put("attrs", attrs);
    }


    @GetMapping("/getAllStoreProductRule")
    public R getAllList()
    {
        List<FsStoreProductRule> list = fsStoreProductService.selectFsStoreProductRuleList();
        return R.ok().put("data",list);
    }


    @PreAuthorize("@ss.hasPermi('his:storeProduct:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addOrEdit")
    public R addOrEdit(@RequestBody FsStoreProductAddEditParam fsStoreProduct)
    {
        return fsStoreProductService.addOrEdit(fsStoreProduct);
    }


}
