package com.tld.service.impl; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.tld.excel.ExcelUtils; import com.tld.mapper.StorageLocationMapper; import com.tld.model.StorageLocation; import com.tld.service.StorageLocationService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.text.SimpleDateFormat; import java.util.*; @Service public class StorageLocationServiceImpl implements StorageLocationService { @Autowired private StorageLocationMapper storageLocationMapper; @Override public Map getStorage(StorageLocation storageLocation) { Map map = new HashMap<>(); try{ PageHelper.startPage(storageLocation.getPage(), storageLocation.getLimit()); PageInfo list = new PageInfo<>(storageLocationMapper.getStorage(storageLocation)); map.put("data", list); map.put("msg", "200"); }catch (Exception e){ e.printStackTrace(); map.put("msg", "500"); map.put("errMsg", "服务器请求异常,请稍后再试"); } return map; } @Override @Transactional(rollbackFor = Exception.class) public Map addStorage(StorageLocation storageLocation, HttpServletRequest request) { Map map = new HashMap<>(); try{ int count = storageLocationMapper.getStorageCount(storageLocation); if(count != 0){ map.put("msg", "500"); map.put("errMsg", "编号存在!"); return map; } storageLocation.setModifyUser(request.getHeader("userId")); storageLocationMapper.addStorage(storageLocation); map.put("msg", "200"); }catch (Exception e){ e.printStackTrace(); map.put("msg", "500"); map.put("errMsg", "服务器请求异常,请稍后再试"); } return map; } @Override @Transactional(rollbackFor = Exception.class) public Map delStorage(String id) { Map map = new HashMap<>(); try{ storageLocationMapper.delStorage(id); map.put("msg", "200"); }catch (Exception e){ e.printStackTrace(); map.put("msg", "500"); map.put("errMsg", "服务器请求异常,请稍后再试"); } return map; } @Override @Transactional(rollbackFor = Exception.class) public Map updateStorage(StorageLocation storageLocation, HttpServletRequest request) { Map map = new HashMap<>(); try{ storageLocation.setModifyUser(request.getHeader("userId")); storageLocationMapper.updateStorage(storageLocation); map.put("msg", "200"); }catch (Exception e){ e.printStackTrace(); map.put("msg", "500"); map.put("errMsg", "服务器请求异常,请稍后再试"); } return map; } @Override public void export(StorageLocation storageLocation, HttpServletResponse response) { try{ //导出数据汇总 List> sheetDataList = new ArrayList<>(); //表头数据 List head = Arrays.asList("库位编号", "库位名称", "所在仓库","库位类型","库位容量","是否混合库位","创建时间"); //查询数据 List> list = storageLocationMapper.export(storageLocation); sheetDataList.add(head); for(Map userMap : list){ List listSheet = new ArrayList<>(); for(String key: userMap.keySet()){ listSheet.add(userMap.get(key)); } sheetDataList.add(listSheet); } //当前时间 Date time = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMddHHmmss"); ExcelUtils.export(response, "库位数据导出" + sdf.format(time), sheetDataList); }catch (Exception e){ e.printStackTrace(); } } @Override public Map getStorageAll(StorageLocation storageLocation) { Map map = new HashMap<>(); try{ List list = storageLocationMapper.getStorage(storageLocation); map.put("data", list); map.put("msg", "200"); }catch (Exception e){ e.printStackTrace(); map.put("msg", "500"); map.put("errMsg", "服务器请求异常,请稍后再试"); } return map; } }