package com.zbkj.admin.controller;

import com.alibaba.fastjson.JSON;
import com.zbkj.common.request.StoreProductAddRequest;
import com.zbkj.common.response.CommonResult;
import com.zbkj.common.utils.excel.ExcelImportUtil;
import com.zbkj.common.utils.excel.ExcelValidUtil;
import com.zbkj.common.utils.excel.ImportParams;
import com.zbkj.service.service.ExcelService;
import com.zbkj.service.service.StoreProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("api/admin/import/excel")
@Api(tags = "导入 -- Excel")
public class ExcelImportController {

    @Autowired
    private ExcelService excelService;

    @Autowired
    private StoreProductService storeProductService;

    /**
     * 商品导入
     */
    @ApiOperation(value = "产品导入")
    @RequestMapping(value = "/product", method = RequestMethod.POST)
    public CommonResult<String> export(@RequestParam("multipart") MultipartFile multipart) {
        try {
            ExcelValidUtil.canImport(multipart);
            List<StoreProductAddRequest> list = (List<StoreProductAddRequest>) ExcelImportUtil.importExcelByIs(multipart.getInputStream(), StoreProductAddRequest.class, new ImportParams());
            CommonResult<String> result = storeProductService.saveBatch(list);
            return result;
        } catch (Exception e) { e.printStackTrace(); }
        return CommonResult.failed();
    }
}



