package com.kmall.admin.fromcomm.controller;

import com.kmall.admin.dto.CateStoreDto;
import com.kmall.admin.dto.CopyBrandDto;
import com.kmall.admin.dto.CopyCategoryDto;
import com.kmall.admin.dto.StoreIdDto;
import com.kmall.admin.service.BrandService;
import com.kmall.admin.service.CategoryService;
import com.kmall.admin.service.StoreService;
import com.kmall.common.constant.JxlsXmlTemplateName;
import com.kmall.common.utils.R;
import com.kmall.common.utils.RRException;
import com.kmall.common.utils.excel.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 批量上传公共类
 * @author huangyq
 * @version 1.0
 * 2019-10-25 10:40
 */
@RestController
@RequestMapping("/uploadCommon")
public class UploadStoreIdController extends AbstractController {
    @Autowired
    private StoreService storeService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private ExcelUtil excelUtil;

    /**
     * 上传文件
     */
    @RequestMapping("/storeIdUpload")
    public R storeIdUpload(@RequestParam("file") MultipartFile file) {
        List<StoreIdDto> storeIdDtoList = new ArrayList<>();//信息
        try {
            Map<String, Object> beans = new HashMap<String, Object>();
            beans.put("StoreIdDtoList", storeIdDtoList);
            if (file.isEmpty()) {
                return R.error("文件不能为空！");
            }
            excelUtil.readExcel(JxlsXmlTemplateName.STORE_ID_DTO_LIST, beans, file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("导入失败！");
        }
//        throw new RRException("导入成功");
        String storeIds = storeService.checkStoreIdUpload(storeIdDtoList);
        //上传文件
        return R.ok("导入成功").put("storeIds", storeIds);
    }

    /**
     * 上传文件
     */
    @RequestMapping("/cateStoreUpload")
    public R cateStoreUpload(@RequestParam("file") MultipartFile file) {
        List<CateStoreDto> cateStoreDtoList = new ArrayList<>();//信息
        try {
            Map<String, Object> beans = new HashMap<String, Object>();
            beans.put("CateStoreDtoList", cateStoreDtoList);
            if (file.isEmpty()) {
                return R.error("文件不能为空！");
            }
            excelUtil.readExcel(JxlsXmlTemplateName.CATE_STORE_DTO_LIST, beans, file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("导入失败！");
        }
//        throw new RRException("导入成功");
        CopyCategoryDto copyCategoryDto = categoryService.checkCateStoreUpload(cateStoreDtoList);
        //上传文件
        return R.ok("导入成功").put("copyCategoryDto", copyCategoryDto);
    }

    /**
     * 上传文件
     */
    @RequestMapping("/brandStoreUpload")
    public R brandStoreUpload(@RequestParam("file") MultipartFile file) {
        List<CateStoreDto> cateStoreDtoList = new ArrayList<>();//信息
        try {
            Map<String, Object> beans = new HashMap<String, Object>();
            beans.put("CateStoreDtoList", cateStoreDtoList);
            if (file.isEmpty()) {
                return R.error("文件不能为空！");
            }
            excelUtil.readExcel(JxlsXmlTemplateName.CATE_STORE_DTO_LIST, beans, file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("导入失败！");
        }
//        throw new RRException("导入成功");
        CopyBrandDto copyBrandDto = brandService.checkBrandStoreUpload(cateStoreDtoList);
        //上传文件
        return R.ok("导入成功").put("copyBrandDto", copyBrandDto);
    }
}
