package com.fs.hisStore.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
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.hisStore.vo.FsStoreProductAttrValueVO;
import com.fs.hisStore.vo.FsStoreProductExportVO;
import com.fs.hisStore.vo.FsStoreProductListVO;
import com.fs.hisStore.vo.FsStoreTuiProductAttrValueVO;
import com.fs.statis.dto.ModifyMoreDTO;
import com.fs.hisStore.domain.FsStoreProductScrm;
import com.fs.hisStore.domain.FsStoreProductAttrScrm;
import com.fs.hisStore.param.FsProductAttrValueParam;
import com.fs.hisStore.param.FsStoreProductAddEditParam;
import com.fs.hisStore.param.FsStoreTuiProductAttrValueParam;
import com.fs.hisStore.service.IFsStoreProductAttrScrmService;
import com.fs.hisStore.service.IFsStoreProductAttrValueScrmService;
import com.fs.hisStore.service.IFsStoreProductScrmService;
import com.fs.statis.dto.ProductAuditDTO;
import com.mysql.cj.util.StringUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
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 org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

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

    @Autowired
    private IFsStoreProductAttrScrmService attrService;

    @Autowired
    private IFsStoreProductAttrValueScrmService attrValueService;


    /**
     * 批量修改商品
     * @param modifyMoreDTO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('store:storeProduct:list')")
    @PostMapping("/batchModify")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE,isStoreLog = true,logParam = {"商品","批量修改商品信息"})
    public R batchModify(@RequestBody ModifyMoreDTO modifyMoreDTO){
        fsStoreProductService.batchModify(modifyMoreDTO);
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('store:storeProduct:list')")
    @PostMapping("/batchAudit")
    @Log(title = "商品审核", businessType = BusinessType.AUDIT,isStoreLog = true,logParam = {"商品","批量审核商品信息"},
    logParamExpression = "#p0.getProductIds().size()>1?"
            +"(#p0.isAudit==1?new String[]{'商品','商品批量审核通过'}: new String[]{'商品','商品批量审核退回'}):" +
            "(#p0.isAudit==1?new String[]{'商品','商品审核通过'}: new String[]{'商品','商品审核退回'})")
    public R batchAudit(@RequestBody ProductAuditDTO auditDTO){
        fsStoreProductService.batchAudit(auditDTO);
        return R.ok();
    }

    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('store:storeProduct:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsStoreProductScrm fsStoreProduct)
    {
        log.info("查询商品列表 参数: {}", 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, isStoreLog = true,logParam = {"商品","导出商品信息"})
    @GetMapping("/export")
    public AjaxResult export(FsStoreProductScrm 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,isStoreLog = true,logParam = {"商品","商品导入"})
    @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)
    {
        FsStoreProductScrm product=fsStoreProductService.selectFsStoreProductById(productId);
        List<FsStoreProductAttrScrm> attrs=attrService.selectFsStoreProductAttrByProductId(productId);
        return R.ok().put("data",product).put("attrs", attrs);
    }

    @PreAuthorize("@ss.hasPermi('store:storeProduct:add')")
    @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)
    {
        if(ObjectUtils.isNotNull(fsStoreProduct.getIsShow())) {
            if (fsStoreProduct.getIsShow() ==1){
                logger.info("商品上架：{}",fsStoreProduct.getProductName()+new Date());
            }
        }

        if(ObjectUtils.isNotNull(fsStoreProduct.getIsDisplay())) {
            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,isStoreLog = true,
            logParamExpression ="#p0.length>1?new String[]{'商品','批量删除商品信息'}: new String[]{'商品','删除商品信息'}" )
	@DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds)
    {
        return toAjax(fsStoreProductService.deleteFsStoreProductByIds(productIds));
    }

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

    @GetMapping("/listBySearch")
    public R listBySearCh(FsStoreProductScrm product)
    {
        List<FsStoreProductScrm> list = fsStoreProductService.selectFsStoreProductList(product);
        return R.ok().put("data",list);
    }

}
