package com.kmall.admin.controller; import java.util.*; import com.kmall.admin.entity.SysCusUnitCodeEntity; import com.kmall.admin.service.SysCusUnitCodeService; import com.kmall.admin.fromcomm.entity.SysUserEntity; 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.admin.utils.ShiroUtils; 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-10 15:34:36 */ @RestController @RequestMapping("syscusunitcode") public class SysCusUnitCodeController { private Log logger = LogFactory.getLog(SysCusUnitCodeController.class); @Autowired private SysCusUnitCodeService sysCusUnitCodeService; @Autowired private ExcelUtil excelUtil; /** * 查看列表 */ @RequestMapping("/list") @RequiresPermissions("syscusunitcode:list") @ResponseBody public R list(@RequestParam Map params) { //查询列表数据 Query query = new Query(params); List sysCusUnitCodeList = sysCusUnitCodeService.queryList(query); int total = sysCusUnitCodeService.queryTotal(query); PageUtils pageUtil = new PageUtils(sysCusUnitCodeList, total, query.getLimit(), query.getPage()); return R.ok().put("page", pageUtil); } /** * 查看信息 */ @RequestMapping("/info/{sn}") @RequiresPermissions("syscusunitcode:info") @ResponseBody public R info(@PathVariable("sn") String sn) { SysCusUnitCodeEntity sysCusUnitCode = sysCusUnitCodeService.queryObject(sn); return R.ok().put("sysCusUnitCode", sysCusUnitCode); } /** * 保存 */ @RequestMapping("/save") @RequiresPermissions("syscusunitcode:save") @ResponseBody public R save(@RequestBody SysCusUnitCodeEntity sysCusUnitCode) { SysUserEntity user = ShiroUtils.getUserEntity(); sysCusUnitCode.setCreaterSn(user.getUserId().toString()); sysCusUnitCode.setCreateTime(new Date()); sysCusUnitCode.setModerSn(user.getUserId().toString()); sysCusUnitCode.setModTime(new Date()); sysCusUnitCodeService.save(sysCusUnitCode); return R.ok(); } /** * 修改 */ @RequestMapping("/update") @RequiresPermissions("syscusunitcode:update") @ResponseBody public R update(@RequestBody SysCusUnitCodeEntity sysCusUnitCode) { SysUserEntity user = ShiroUtils.getUserEntity(); sysCusUnitCode.setModerSn(user.getUserId().toString()); sysCusUnitCode.setModTime(new Date()); sysCusUnitCodeService.update(sysCusUnitCode); return R.ok(); } /** * 删除 */ @RequestMapping("/delete") @RequiresPermissions("syscusunitcode:delete") @ResponseBody public R delete(@RequestBody String[]sns) { sysCusUnitCodeService.deleteBatch(sns); return R.ok(); } /** * 查看所有列表 */ @RequestMapping("/queryAll") @ResponseBody public R queryAll(@RequestParam Map params) { List list = sysCusUnitCodeService.queryList(params); return R.ok().put("list", list); } /** * 导入 excel 数据 * @author zhuhh * @param file * @return */ @RequestMapping("/upload") public R upload(@RequestParam("file") MultipartFile file) { List sysCusUnitCodeEntityList = new ArrayList<>(); // 读取 excel 数据 try { SysCusUnitCodeEntity sysCusUnitCodeEntity = new SysCusUnitCodeEntity(); Map beans = new HashMap<>(); beans.put("sysCusUnitCodeEntity", sysCusUnitCodeEntity); beans.put("sysCusUnitCodeEntityList", sysCusUnitCodeEntityList); if (file.isEmpty()) { return R.error("文件不能为空!"); } excelUtil.readExcel(JxlsXmlTemplateName.SYS_CUS_UNIT_CODE_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 (sysCusUnitCodeEntityList == null || sysCusUnitCodeEntityList.size() == 0) { return R.error("读取excel失败!单元格内容不能为空!"); } sysCusUnitCodeService.uploadExcel(sysCusUnitCodeEntityList); } 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("导入成功!"); } }