package com.fs.store.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.fs.common.core.domain.R;
import com.fs.store.domain.FsStoreProductAttr;
import com.fs.store.param.FsProductAttrValueParam;
import com.fs.store.param.FsStoreProductAddEditParam;
import com.fs.store.param.FsStoreTuiProductAttrValueParam;
import com.fs.store.service.IFsStoreProductAttrService;
import com.fs.store.service.IFsStoreProductAttrValueService;
import com.fs.store.vo.*;
import com.mysql.cj.util.StringUtils;
import io.swagger.annotations.ApiOperation;
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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
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.store.domain.FsStoreProduct;
import com.fs.store.service.IFsStoreProductService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

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

    @Autowired
    private IFsStoreProductAttrService attrService;

    @Autowired
    private IFsStoreProductAttrValueService attrValueService;


    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('store:storeProduct:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsStoreProduct fsStoreProduct)
    {
        startPage();
        List<FsStoreProductListVO> list;
        if(StringUtils.isNullOrEmpty(fsStoreProduct.getBarCode())){
            list = fsStoreProductService.selectFsStoreProductListVO(fsStoreProduct);
        }else{
            list = fsStoreProductService.selectFsStoreProductBarCodeListVO(fsStoreProduct);
        }
        return getDataTable(list);
    }

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


    @Log(title = "商品导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('store:storeProduct:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<FsStoreProductExportVO> util = new ExcelUtil<>(FsStoreProductExportVO.class);
        List<FsStoreProductExportVO> list = util.importExcel(file.getInputStream());
        String message = fsStoreProductService.importStoreProduct(list, updateSupport);
        return AjaxResult.success(message);
    }


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

    /**
     * 获取商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('store:storeProduct:query')")
    @GetMapping(value = "/{productId}")
    public R getInfo(@PathVariable("productId") Long productId)
    {
        FsStoreProduct product=fsStoreProductService.selectFsStoreProductById(productId);
        List<FsStoreProductAttr> attrs=attrService.selectFsStoreProductAttrByProductId(productId);
        return R.ok().put("data",product).put("attrs", attrs);
    }

    @PreAuthorize("@ss.hasPermi('store:storeProduct:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addOrEdit")
    public R addOrEdit(@RequestBody FsStoreProductAddEditParam fsStoreProduct)
    {
        if (fsStoreProduct.getIsShow() ==1){
            logger.info("商品上架：{}",fsStoreProduct.getProductName()+new Date());
        }

        if (fsStoreProduct.getIsDisplay() ==1){
            logger.info("商品前端展示：{}",fsStoreProduct.getProductName()+new Date());
        }
        return fsStoreProductService.addOrEdit(fsStoreProduct);
    }




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

    @PreAuthorize("@ss.hasPermi('store:storeProduct:putOn')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @PostMapping("/putOn/{productIds}")
    public AjaxResult putOn(@PathVariable Long[] productIds)
    {
        return toAjax(fsStoreProductService.updateFsStoreProductIsShow(productIds,1));
    }
    @PreAuthorize("@ss.hasPermi('store:storeProduct:pullOff')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @PostMapping("/pullOff/{productIds}")
    public AjaxResult pullOff(@PathVariable Long[] productIds)
    {
        return toAjax(fsStoreProductService.updateFsStoreProductIsShow(productIds,0));
    }
    @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("/getStoreProductAttrValueList")
    public TableDataInfo getStoreProductAttrValueList(FsProductAttrValueParam param)
    {
        startPage();
        List<FsStoreProductAttrValueVO> list=attrValueService.selectFsStoreProductAttrValueListVO(param);
        return getDataTable(list);
    }

    @ApiOperation("获取推广商品列表")
    @GetMapping("/getStoreTuiProductAttrValueList")
    public TableDataInfo getStoreTuiProductAttrValueList(FsStoreTuiProductAttrValueParam param, HttpServletRequest request){
        startPage();
        List<FsStoreTuiProductAttrValueVO> list=attrValueService.selectStoreTuiProductAttrValueVOList(param);
        return getDataTable(list);
    }

}
