|  | @@ -1,18 +1,28 @@
 | 
	
		
			
				|  |  |  package com.kmall.admin.controller;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +import java.util.ArrayList;
 | 
	
		
			
				|  |  | +import java.util.HashMap;
 | 
	
		
			
				|  |  |  import java.util.List;
 | 
	
		
			
				|  |  |  import java.util.Map;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import com.kmall.admin.entity.SupplierEntity;
 | 
	
		
			
				|  |  |  import com.kmall.admin.service.SupplierService;
 | 
	
		
			
				|  |  |  import com.kmall.admin.utils.ParamUtils;
 | 
	
		
			
				|  |  | +import com.kmall.common.constant.JxlsXmlTemplateName;
 | 
	
		
			
				|  |  |  import com.kmall.common.utils.PageUtils;
 | 
	
		
			
				|  |  |  import com.kmall.common.utils.Query;
 | 
	
		
			
				|  |  |  import com.kmall.common.utils.R;
 | 
	
		
			
				|  |  | +import com.kmall.common.utils.RRException;
 | 
	
		
			
				|  |  | +import com.kmall.common.utils.excel.ExcelUtil;
 | 
	
		
			
				|  |  | +import com.kmall.manager.manager.express.sf.ServiceException;
 | 
	
		
			
				|  |  | +import org.apache.commons.logging.Log;
 | 
	
		
			
				|  |  | +import org.apache.commons.logging.LogFactory;
 | 
	
		
			
				|  |  |  import org.apache.shiro.authz.annotation.RequiresPermissions;
 | 
	
		
			
				|  |  | +import org.jxls.reader.XLSDataReadException;
 | 
	
		
			
				|  |  |  import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  |  import org.springframework.stereotype.Controller;
 | 
	
		
			
				|  |  |  import org.springframework.web.bind.annotation.*;
 | 
	
		
			
				|  |  | +import org.springframework.web.multipart.MultipartFile;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  /**
 | 
	
		
			
				|  |  |   * Controller
 | 
	
	
		
			
				|  | @@ -21,12 +31,18 @@ import org.springframework.web.bind.annotation.*;
 | 
	
		
			
				|  |  |   * @email admin@qhdswl.com
 | 
	
		
			
				|  |  |   * @date 2018-11-26 15:14:24
 | 
	
		
			
				|  |  |   */
 | 
	
		
			
				|  |  | -@Controller
 | 
	
		
			
				|  |  | +@RestController
 | 
	
		
			
				|  |  |  @RequestMapping("supplier")
 | 
	
		
			
				|  |  |  public class SupplierController {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private Log logger = LogFactory.getLog(SupplierController.class);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      private SupplierService supplierService;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private ExcelUtil excelUtil;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * 查看列表
 | 
	
		
			
				|  |  |       */
 | 
	
	
		
			
				|  | @@ -105,4 +121,56 @@ public class SupplierController {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          return R.ok().put("list", list);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 导入 excel 数据
 | 
	
		
			
				|  |  | +     * @author zhuhh
 | 
	
		
			
				|  |  | +     * @param file
 | 
	
		
			
				|  |  | +     * @return
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    @RequestMapping("/upload")
 | 
	
		
			
				|  |  | +    public R upload(@RequestParam("file") MultipartFile file) {
 | 
	
		
			
				|  |  | +        List<SupplierEntity> supplierEntityList = new ArrayList<>();
 | 
	
		
			
				|  |  | +        // 读取 excel 数据
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            SupplierEntity supplierEntity = new SupplierEntity();
 | 
	
		
			
				|  |  | +            Map<String, Object> beans = new HashMap<>();
 | 
	
		
			
				|  |  | +            beans.put("SupplierEntity", supplierEntity);
 | 
	
		
			
				|  |  | +            beans.put("SupplierEntityList", supplierEntityList);
 | 
	
		
			
				|  |  | +            if (file.isEmpty()) {
 | 
	
		
			
				|  |  | +                return R.error("文件不能为空!");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            excelUtil.readExcel(JxlsXmlTemplateName.SUPPLIER_DTO_List, beans, file.getInputStream());
 | 
	
		
			
				|  |  | +        } catch (XLSDataReadException e) {
 | 
	
		
			
				|  |  | +            e.printStackTrace();
 | 
	
		
			
				|  |  | +            logger.error("商品供货商读取excel失败:" + e.getMessage());
 | 
	
		
			
				|  |  | +            return R.error("导入失败!请在单元格输入正确的数据格式");
 | 
	
		
			
				|  |  | +        } catch (Exception e) {
 | 
	
		
			
				|  |  | +            e.printStackTrace();
 | 
	
		
			
				|  |  | +            logger.error("商品供货商读取excel失败:" + e.getMessage());
 | 
	
		
			
				|  |  | +            return R.error("读取excel数据失败!");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        // 新增数据
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            if (supplierEntityList == null || supplierEntityList.size() == 0) {
 | 
	
		
			
				|  |  | +                return R.error("读取excel失败!单元格内容不能为空!");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            supplierService.uploadExcel(supplierEntityList);
 | 
	
		
			
				|  |  | +        } catch (ServiceException e) {
 | 
	
		
			
				|  |  | +            e.printStackTrace();
 | 
	
		
			
				|  |  | +            logger.error("商品供货商数据格式校验失败:" + e.getMessage());
 | 
	
		
			
				|  |  | +            return R.error(e.getMessage());
 | 
	
		
			
				|  |  | +        } catch (RRException e) {
 | 
	
		
			
				|  |  | +            e.printStackTrace();
 | 
	
		
			
				|  |  | +            logger.error("商品供货商添加数据失败:" + e.getMsg());
 | 
	
		
			
				|  |  | +            return R.error(e.getMsg());
 | 
	
		
			
				|  |  | +        } catch (Exception e) {
 | 
	
		
			
				|  |  | +            e.printStackTrace();
 | 
	
		
			
				|  |  | +            logger.error("商品供货商添加数据失败:" + e.getMessage());
 | 
	
		
			
				|  |  | +            return R.error("数据保存数据库失败!");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return R.ok("导入成功!");
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  }
 |