package com.fs.store.controller.store;

import com.fs.his.param.FsStoreProductListSParam;
import com.fs.his.vo.FsStoreProductExcelVO;
import com.fs.his.vo.FsStoreProductListSVO;
import com.fs.store.utils.UserUtil;
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.his.domain.FsStoreProduct;
import com.fs.his.domain.FsStoreProductAttr;
import com.fs.his.domain.FsStoreProductRule;
import com.fs.his.param.FsStoreProductAddEditParam;
import com.fs.his.service.IFsStoreProductAttrService;
import com.fs.his.service.IFsStoreProductService;
import com.fs.his.vo.FsStoreProductVO;
import com.fs.his.vo.OptionsVO;
import com.fs.store.vo.FsStoreProductStoreExcelVO;
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.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 商品Controller
 *
 * @author fs
 * @date 2023-06-08
 */
@RestController
@RequestMapping("/store/storeProduct")
public class FsStoreProductController extends BaseController
{
    @Autowired
    private IFsStoreProductService fsStoreProductService;
    @Autowired
    private IFsStoreProductAttrService attrService;
    /**
 * 查询商品列表
 */

@GetMapping("/list")
public TableDataInfo list(FsStoreProductListSParam fsStoreProduct)
{
    startPage();
    fsStoreProduct.setStoreId(UserUtil.getLoginUserStoreID());
    List<FsStoreProductListSVO> list = fsStoreProductService.selectFsStoreProductListSVO(fsStoreProduct);
    return getDataTable(list);
}




    /**
     * 导出商品列表
     */

    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsStoreProduct fsStoreProduct)
    {
        fsStoreProduct.setStoreId(UserUtil.getLoginUserStoreID());
        List<FsStoreProduct> list = fsStoreProductService.selectFsStoreProductList(fsStoreProduct);
        ExcelUtil<FsStoreProduct> util = new ExcelUtil<FsStoreProduct>(FsStoreProduct.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());
        for (FsStoreProductExcelVO fsStoreProductExcelVO : list) {
            fsStoreProductExcelVO.setStoreId(UserUtil.getLoginUserStoreID());
        }
        String message = fsStoreProductService.importSrorePeoduct(list, updateSupport);
        return AjaxResult.success(message);
    }

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

    @PostMapping("/price/{price}")
    public AjaxResult editPrice(@RequestBody FsStoreProduct fsStoreProduct, @PathVariable("price")Integer price)
    {
        fsStoreProduct.setStoreId(UserUtil.getLoginUserStoreID());
        return toAjax(fsStoreProductService.updateFsStoreProductPrice(fsStoreProduct,price));
    }
    /**
     * 新增商品
     */

    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsStoreProduct fsStoreProduct)
    {
        fsStoreProduct.setStoreId(UserUtil.getLoginUserStoreID());
        return toAjax(fsStoreProductService.insertFsStoreProduct(fsStoreProduct));
    }

    /**
     * 修改商品
     */

    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsStoreProduct fsStoreProduct)
    {

        return toAjax(fsStoreProductService.updateFsStoreProduct(fsStoreProduct));
    }

    /**
     * 删除商品
     */

    @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);
    }



    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addOrEdit")
    public R addOrEdit(@RequestBody FsStoreProductAddEditParam fsStoreProduct)
    {
        fsStoreProduct.setStoreId(UserUtil.getLoginUserStoreID());

        return fsStoreProductService.addOrEdit(fsStoreProduct);
    }

}
