package com.fs.hisStore.controller.store;

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.param.FsStoreProductListSParam;
import com.fs.his.vo.FsStoreProductExcelVO;
import com.fs.his.vo.FsStoreProductListSVO;
import com.fs.his.vo.OptionsVO;
import com.fs.hisStore.domain.FsStoreProductAttrScrm;
import com.fs.hisStore.domain.FsStoreProductRuleScrm;
import com.fs.hisStore.domain.FsStoreProductScrm;
import com.fs.hisStore.param.FsStoreProductAddEditParam;
import com.fs.hisStore.service.IFsStoreProductAttrScrmService;
import com.fs.hisStore.service.IFsStoreProductScrmService;
import com.fs.hisStore.utils.UserUtil;
import com.fs.hisStore.vo.FsStoreProductListVO;
import com.fs.store.vo.FsStoreProductStoreExcelVO;
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.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

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

@GetMapping("/list")
public TableDataInfo list(FsStoreProductScrm fsStoreProduct)
{
    startPage();
    List<FsStoreProductListVO> list;
    if(StringUtils.isNullOrEmpty(fsStoreProduct.getBarCode())){
        list = fsStoreProductService.selectFsStoreProductListVO(fsStoreProduct);
    }else{
        list = fsStoreProductService.selectFsStoreProductBarCodeListVO(fsStoreProduct);
    }
    return getDataTable(list);
}




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

    @Log(title = "商品", businessType = BusinessType.EXPORT,isStoreLog = true,logParam = {"商品","导出商品信息"})
    @GetMapping("/export")
    public AjaxResult export(FsStoreProductScrm fsStoreProduct)
    {
        fsStoreProduct.setStoreId(UserUtil.getLoginUserStoreID());
        List<FsStoreProductScrm> list = fsStoreProductService.selectFsStoreProductList(fsStoreProduct);
        ExcelUtil<FsStoreProductScrm> util = new ExcelUtil<FsStoreProductScrm>(FsStoreProductScrm.class);
        return util.exportExcel(list, "商品数据");
    }

    @Log(title = "商品导入", businessType = BusinessType.IMPORT,isStoreLog = true,logParam = {"商品","导入商品信息"})
    @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 FsStoreProductScrm fsStoreProduct, @PathVariable("price")Integer price)
    {
        fsStoreProduct.setStoreId(UserUtil.getLoginUserStoreID());
        return toAjax(fsStoreProductService.updateFsStoreProductPrice(fsStoreProduct,price));
    }
    /**
     * 新增商品
     */

    @Log(title = "商品", businessType = BusinessType.INSERT,isStoreLog = true,logParam = {"商品","新增商品信息"})
    @PostMapping
    public AjaxResult add(@RequestBody FsStoreProductScrm fsStoreProduct)
    {
        fsStoreProduct.setStoreId(UserUtil.getLoginUserStoreID());
        return toAjax(fsStoreProductService.insertFsStoreProduct(fsStoreProduct));
    }

    /**
     * 修改商品
     */

    @Log(title = "商品", businessType = BusinessType.UPDATE,isStoreLog = true,logParam = {"商品","修改商品信息"})
    @PutMapping
    public AjaxResult edit(@RequestBody FsStoreProductScrm fsStoreProduct)
    {
        return toAjax(fsStoreProductService.updateFsStoreProduct(fsStoreProduct));
    }

    /**
     * 删除商品
     */

    @Log(title = "商品", businessType = BusinessType.DELETE,isStoreLog = true,logParam = {"商品","批量删除商品信息"})
	@DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds)
    {

        return toAjax(fsStoreProductService.deleteFsStoreProductByIds(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,Long[] stores){

        return new ResponseEntity<>(fsStoreProductService.getFormatAttr(productId,jsonStr, stores), HttpStatus.OK);
    }
    /**
     * 获取商品详细信息
     */

    @GetMapping(value = "/{productId}")
    public R getInfo(@PathVariable("productId") Long productId)
    {

        FsStoreProductScrm product = fsStoreProductService.selectFsStoreProductByProductId(productId);
        List<FsStoreProductAttrScrm> attrs=attrService.selectFsStoreProductAttrByProductId(productId);
        return R.ok().put("data",product).put("attrs", attrs);
    }


    @GetMapping("/getAllStoreProductRule")
    public R getAllList()
    {

        List<FsStoreProductRuleScrm> list = fsStoreProductService.selectFsStoreProductRuleList();
        return R.ok().put("data",list);
    }



    @Log(title = "商品管理", businessType = BusinessType.INSERT, businessTypeExpression = "#p0.getProductId()>0? T(com.fs.common.enums.BusinessType).UPDATE: T(com.fs.common.enums.BusinessType).INSERT"
            , isStoreLog = true, logParamExpression = "#p0.getProductId()>0? new String[]{'商品','修改商品'}: new String[]{'商品','新增商品'}")
    @PostMapping(value = "/addOrEdit")
    public R addOrEdit(@RequestBody FsStoreProductAddEditParam fsStoreProduct)
    {
        fsStoreProduct.setStoreId(UserUtil.getLoginUserStoreID());

        return fsStoreProductService.addOrEdit(fsStoreProduct);
    }

}
