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 * * @author huangyq * @email admin@qhdswl.com * @date 2018-11-26 15:14:24 */ @RestController @RequestMapping("supplier") public class SupplierController { private Log logger = LogFactory.getLog(SupplierController.class); @Autowired private SupplierService supplierService; @Autowired private ExcelUtil excelUtil; /** * 查看列表 */ @RequestMapping("/list") @RequiresPermissions("supplier:list") @ResponseBody public R list(@RequestParam Map params) { ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", "thirdPartyMerchCode"); // ParamUtils.setName(params, "name"); //查询列表数据 Query query = new Query(params); List supplierList = supplierService.queryList(query); int total = supplierService.queryTotal(query); PageUtils pageUtil = new PageUtils(supplierList, total, query.getLimit(), query.getPage()); return R.ok().put("page", pageUtil); } /** * 查看信息 */ @RequestMapping("/info/{id}") @RequiresPermissions("supplier:info") @ResponseBody public R info(@PathVariable("id") Integer id) { SupplierEntity supplier = supplierService.queryObject(id); return R.ok().put("supplier", supplier); } /** * 保存 */ @RequestMapping("/save") @RequiresPermissions("supplier:save") @ResponseBody public R save(@RequestBody SupplierEntity supplier) { supplierService.save(supplier); return R.ok(); } /** * 修改 */ @RequestMapping("/update") @RequiresPermissions("supplier:update") @ResponseBody public R update(@RequestBody SupplierEntity supplier) { supplierService.update(supplier); return R.ok(); } /** * 删除 */ @RequestMapping("/delete") @RequiresPermissions("supplier:delete") @ResponseBody public R delete(@RequestBody Integer[]ids) { supplierService.deleteBatch(ids); return R.ok(); } /** * 查看所有列表 */ @RequestMapping("/queryAll") @ResponseBody public R queryAll(@RequestParam Map params) { ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", "thirdPartyMerchCode"); List list = supplierService.queryList(params); return R.ok().put("list", list); } /** * 导入 excel 数据 * @author zhuhh * @param file * @return */ @RequestMapping("/upload") public R upload(@RequestParam("file") MultipartFile file) { List supplierEntityList = new ArrayList<>(); // 读取 excel 数据 try { SupplierEntity supplierEntity = new SupplierEntity(); Map 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("导入成功!"); } }