导入xlsx

1-maintainWmsItemPage.wf.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<page id="maintainWmsItemPage">
    <main>
        <process id="boxTypeProcess">
        		<tablePopup id="importItems" title="importItems" 
				process="boxTypeProcess.import" enableType="none" 
				containId="false" pageId="editImportItemsPage">
                <enableExpression/>
            </tablePopup>
        </process>
    </main>
    <detail/>
</page

 

 

2-editImportItemsPage.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<pages>
    <editPage id="editImportItemsPage" title="editImportItemsPage">
        <workflow/>
        <initListeners/>
        <inputUIs>
            <file id="omsOrder.importFile" title="filePath" row="1" col="1" span="1" 
			readOnly="false" required="true" reserve="false" forceOverride="true" 
			focusUI="false" inVisible="false" showImage="false" fileSize="22577152">
                <visibleExpression/>
                <hql/>
                <eventListeners/>
            </file>
        </inputUIs>
    </editPage>
</pages>

 

 

3-editImportItemsPage.wf.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<page id="editImportItemsPage">
    <main>
        <process id="boxTypeProcess">
            <formCommit id="importItems" title="importItems" 
			process="boxTypeProcess.import" enableType="none" 
			multiMapping="false" download="false" closeTransactional="true" 
			confirmMessage="confirm" visibleType="none">
                <enableExpression/>
                <mappings>
                     <mapping id="omsOrder.importFile" className="file">
                        <entries/>
                    </mapping>
                </mappings>
                <actions>
                    <action managerName="noTransactionManager" 
					methodName="importItem" parameter="omsOrder.importFile"/>
                </actions>
                <forwards>
                    <forward name="refreshParent" newEnabled="true" editEnabled="true"/>
                </forwards>
            </formCommit>
        </process>
    </main>
    <detail/>
</page>

 

 

NoTransactionManagerImp.java

 

/**
 * @author yc min
 */
public class NoTransactionManagerImp extends DefaultBaseManager 
	implements NoTransactionManager{
	
	/**
	 * 导入物料信息
	 * @param file
	 */
	public void importItem(File file) {
		// 判断文件是否存在
		if (file == null) {
			throw new BusinessException("操作失败,未找对应文件!");
		}
		// 验证文件格式
		String name = file.getName();
		String suffix = name.substring(name.lastIndexOf(".") + 1,
				name.lastIndexOf(".") + 5);
		if (!suffix.equals("xlsx")) {
			throw new BusinessException("操作失败,导入文件格式错误!");
		}
		
		FileInputStream fileInput = null;
		 XSSFWorkbook xwb = null;
		 try {
			 fileInput = new FileInputStream(file);
			 xwb = new XSSFWorkbook(fileInput);
			 XSSFSheet sheet = xwb.getSheetAt(0);
			 createItem(sheet);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				if(fileInput != null){
					fileInput.close();
					fileInput = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			if(xwb != null){
				xwb = null;
			}
		}
	}
	/**
	 * 创建物料
	 * @param sheet
	 */
	@SuppressWarnings("unchecked")
	private void createItem(XSSFSheet sheet) {
		StringBuffer bufferLog = new StringBuffer();
		
		WmsLotRule wmsLotRule = null;
		//获取批次规则
		List<WmsLotRule> list = (List<WmsLotRule>)commonDao.
			findByQuery("FROM WmsLotRule rule"
				+ " WHERE rule.status = :status", "status", BaseStatus.ENABLED);
		if(list.isEmpty() || list.size() > 1){
			bufferLog.append("操作失败,未找到唯一的批次规则!\n");
		}else{
			wmsLotRule = list.get(0);
		}
		Map<String,WmsOrganization> sups = 
			new HashMap<String, WmsOrganization>();
		Map<String,WmsItemType> itemType = 
			new HashMap<String, WmsItemType>();
		Map<String,WmsSimilarCode> similars = 
			new HashMap<String, WmsSimilarCode>();
		List<Object[]> items = new ArrayList<Object[]>();
		String secondUnit = "箱";
		for(int i = 2; i <= sheet.getLastRowNum()+1; i ++){
			try {
				XSSFRow row = sheet.getRow(i-1);
				if(row == null){
					continue;
				}
				WmsItem item = null;
				String itemCode = getValue(row.getCell(0));
				if(StringUtils.isEmpty(itemCode)){
					bufferLog.append(
						"操作失败,["+i+"]行,物料编码不能为空!\n");
					continue;
				}else{
					if(StringUtils.isEmpty(itemCode)){
						bufferLog.append(
							"操作失败,["+i+"]行,物料编码不能为空!\n");
					}
					item = (WmsItem) commonDao.findByQueryUniqueResult
						("FROM WmsItem item WHERE item.code =:itemCode", 
							new String[]{"itemCode"}, new Object[]{itemCode});
					if(item == null){
						item = EntityFactory.getEntity(WmsItem.class);
					}
				}
				String name = getValue(row.getCell(1));
				String packageBoxType = getValue(row.getCell(20));
				if(StringUtils.isEmpty(name)){
					bufferLog.append(
						"操作失败,["+i+"]行,物料名称不能为空!\n");
				}
				String ename = getValue(row.getCell(2));
				String type = getValue(row.getCell(3));
				String typeValue = "";
				if(!StringUtils.isEmpty(type)){
					if("A".equals(type)){
						typeValue = WmsItemCategory.A;
					}else if("C".equals(type)){
						typeValue = WmsItemCategory.C;
					}else if("I".equals(type)){
						typeValue = WmsItemCategory.I;
					}else{
						typeValue = WmsItemCategory.HOMEMADE;
					}
				}
		        
				String itemTypeCode1 = getValue(row.getCell(4));
				String itemTypeCode2 = getValue(row.getCell(5));
				
				WmsItemType itemType1 = null;
				WmsItemType itemType2 = null;
				
				if(StringUtils.isEmpty(itemTypeCode1) 
					&& StringUtils.isEmpty(itemTypeCode2)){
					bufferLog.append(
						"操作失败,["+i+"]行套分类、散件分类不能同时为空!");
				}
				if(StringUtils.isNotEmpty(itemTypeCode1)){
					if(itemType.containsKey(itemTypeCode1)){
						itemType1 = itemType.get(itemTypeCode1);
					}else{
						itemType1 = (WmsItemType) commonDao.
							findByQueryUniqueResult("FROM WmsItemType itmeType"
								+ " WHERE itmeType.code=:code", 
								"code", itemTypeCode1);
						itemType.put(itemTypeCode1, itemType1);
					}
					if(itemType1 == null){
						bufferLog.append("操作失败,["+i+"]
							行散件物料分类["+itemTypeCode1+"]不存在!\n");
					}
				}

				if(StringUtils.isNotEmpty(itemTypeCode2)){
					if(itemType.containsKey(itemTypeCode2)){
						itemType2 = itemType.get(itemTypeCode2);
					}else{
						itemType2 = (WmsItemType) commonDao.
							findByQueryUniqueResult("FROM WmsItemType itmeType"
								+ " WHERE itmeType.code=:code", 
								"code", itemTypeCode2);
						itemType.put(itemTypeCode2, itemType2);
					}
					if(itemType2 == null){
						bufferLog.append("操作失败,["+i+"]
							行台套物料分类["+itemTypeCode2+"]不存在!\n");
					}
				}

				String beBom = getValue(row.getCell(6));
				boolean beBomFlag = false;
				if(StringUtils.isEmpty(beBom)){
					bufferLog.append(
						"操作失败,["+i+"]行是否套件不能为空!\n");
				}else{
					beBomFlag = beBom.equals("是")?Boolean.TRUE : Boolean.FALSE;
				}
				
				String leftRightPart = getValue(row.getCell(7));
				
				String similarCode = getValue(row.getCell(8));
				WmsSimilarCode similar = null;
				if(!StringUtils.isEmpty(similarCode)){
					if(similars.containsKey(similarCode)){
						similar = similars.get(similarCode);
					}else{
						similar = (WmsSimilarCode)commonDao.
						findByQueryUniqueResult(
						"FROM WmsSimilarCode similarCode"
								+ " WHERE similarCode.code=:code",
								"code", similarCode);
						similars.put(similarCode, similar);
					}
					if(similar == null){
						bufferLog.append("操作失败,["+i+"]
						行相似编码["+similarCode+"]不存在!\n");
					}
				}
				
				String highConcern = getValue(row.getCell(9));
				String highClaims = getValue(row.getCell(10));
				String highValue = getValue(row.getCell(11));
				
				String supplierCode = getValue(row.getCell(16));
				WmsOrganization supplier = null;
				
				if(StringUtils.isEmpty(supplierCode)){
					bufferLog.append("操作失败,
						["+i+"]行供应商编码不能为空!\n");
					System.out.println(row.getCell(0));
				}else{
					if(!sups.containsKey(supplierCode)){
						supplier = (WmsOrganization)commonDao.
							findByQueryUniqueResult(
							"FROM WmsOrganization supplier"
								+ " WHERE supplier.code=:code"
								+ " AND supplier.beSupplier = true", 
								"code", supplierCode);
						sups.put(supplierCode, supplier);
					}else{
						supplier = sups.get(supplierCode);
					}
					if(supplier == null){
						bufferLog.append("操作失败,["+i+"]
						行供应商编码["+supplierCode+"]不存在\n");
					}
				}
				
				boolean beLawCheck = false;
				if(row.getCell(17) != null){
					beLawCheck = getValue(row.getCell(17)).
						equals("是") ? Boolean.TRUE : Boolean.FALSE;
				}
				
				boolean beOutPackage = false;
				if(row.getCell(18) != null){
					beOutPackage = getValue(row.getCell(18)).
						equals("是") ? Boolean.TRUE : Boolean.FALSE;
				}
				
				String unit = getValue(row.getCell(12));
				if(StringUtils.isEmpty(unit)){
					bufferLog.append("操作失败,
						["+i+"]行单位名称不能为空!\n");
				}
				
				String weightStr = getValue(row.getCell(15));
				double weight = 0.0;
				if(StringUtils.isEmpty(weightStr)){
					bufferLog.append("操作失败,
						["+i+"]行物料单重不能为空!\n");
				}else{
					weight = getNumbericValue(row.getCell(15));
				}
				
				String convertFigureStr = getValue(row.getCell(14));
				int convertFigure = 0;
				if(StringUtils.isEmpty(convertFigureStr)){
					bufferLog.append("操作失败,
						["+i+"]行标装数量不能为空!\n");
				}else{
					convertFigure = 
						Integer.valueOf(getValue(row.getCell(14)));
				}
				
				String supplierOutStr =getValue(row.getCell(19));
				WmsOrganization supplierOut = null;
				if (!StringUtils.isEmpty(supplierOutStr)) {
					if(sups.containsKey(supplierOutStr)){
						supplierOut = sups.get(supplierOutStr);
					}else{
						String hql = "FROM WmsOrganization "+
							" supplier WHERE 1=1"+ 
							" AND supplier.beSupplier = true"
							+ " AND (supplier.code =:code"
							+" OR supplier.name =:name) ";
						supplierOut = (WmsOrganization) 
							this.commonDao.findByQueryUniqueResult(hql, 
								new String[]{"code","name"}, 
								new Object[]{supplierOutStr,supplierOutStr});
						sups.put(supplierOutStr, supplierOut);
					}
					if (supplierOut == null) {
						bufferLog.append("操作失败["+i+"]行委外供应商
							【"+ supplierOutStr +"】在系统中未维护!\n");
					}
				}
				Integer containTime = null;
				if(!StringUtils.isEmpty(getValue(row.getCell(21)))){
					try{
						containTime = 
						Integer.valueOf(getValue(row.getCell(21)));
					}catch(Exception e){
						bufferLog.append("操作失败,["+i+"]行物料容忍度
						【"+ row.getCell(21) +"】是数字!\n");
					}		
				}
				
				boolean beScreening = false;
				if(row.getCell(22) != null){
					beScreening = getValue(row.getCell(22)).
						equals("是") ? Boolean.TRUE : Boolean.FALSE;
				}
				
				boolean beAntirust = false;
				if(row.getCell(23) != null){
					beAntirust = getValue(row.getCell(23)).
						equals("是") ? Boolean.TRUE : Boolean.FALSE;
				}
				
				boolean beShockproof = false;
				if(row.getCell(24) != null){
					beShockproof = getValue(row.getCell(24)).
						equals("是") ? Boolean.TRUE : Boolean.FALSE;
				}
				
				int rustDay = 0;
				if(!StringUtils.isEmpty(getValue(row.getCell(25)))){
					try{
						rustDay = Integer.valueOf(getValue(row.getCell(25)));
					}catch(Exception e){
						bufferLog.append("操作失败,["+i+"]行防锈周期
						【"+ row.getCell(25) +"】是数字!\n");
					}		
				}
				
				WmsPackageUnit pu1 =null;
				if(item.isNew()){
					pu1 = EntityFactory.getEntity(WmsPackageUnit.class);
				}else{
					pu1 = (WmsPackageUnit) commonDao.
						findByQueryUniqueResult("FROM WmsPackageUnit p"
								+ " WHERE p.item.code =:code AND p.lineNo = 1", 
								new String[]{"code"}, new Object[]{itemCode});
					if(pu1==null){
						pu1 = EntityFactory.getEntity(WmsPackageUnit.class);
					}
				}
				WmsPackageUnit pu2 = null;
				if(!secondUnit.endsWith(unit)){
					if(item.isNew()){
						pu2 = EntityFactory.getEntity(WmsPackageUnit.class);
					}else{
						pu2 = (WmsPackageUnit) commonDao.
							findByQueryUniqueResult("FROM WmsPackageUnit p"
								+ " WHERE p.item.code =:code AND p.lineNo = 2 ", 
								new String[]{"code"}, new Object[]{itemCode});
						if(pu2==null){
							pu2 = EntityFactory.getEntity(WmsPackageUnit.class);
						}
					}
				}
				
				if(bufferLog.length() == 0){
					Object[] obj = new Object[]{item,
							itemCode,name,ename,typeValue,
							itemType1,itemType2,beBomFlag,leftRightPart,
							similar,highConcern,highClaims,highValue,
							supplier,beLawCheck,beOutPackage,wmsLotRule,
							supplierOut,containTime,beScreening,beAntirust,
							beShockproof,rustDay,packageBoxType,pu1,unit,weight,
							pu2,secondUnit,convertFigure
					};
					
					items.add(obj);
				}
			} catch (Exception e) {
				bufferLog.append("操作失败,["+i+"]行格式错误,请检查!\n");
			}
		}
		
		sups.clear();itemType.clear();similars.clear();
		if(bufferLog.length() > 0){
			ExceptionLog log = new ExceptionLog(
				UserHolder.getUser(), "maintainWmsItemPage.xml", 
					"importItem", bufferLog.toString());
			commonDao.store(log);
		}else{
			WmsItemManager wmsItemManager = (WmsItemManager) 
				applicationContext.getBean("wmsItemManager");
			int PAGE_NUMBER = 100;
			int size = items.size();
			int j = MyUtils.getSize(size, PAGE_NUMBER);
			for(int k=0 ; k<j ; k++){
				int toIndex = MyUtils.getIndex(k, size, PAGE_NUMBER);
				List<Object[]> ret = MyUtils.getListObj(
					items, k, toIndex, PAGE_NUMBER);
				Object[] obj = new Object[]{
						ret
				};
				wmsItemManager.createItem(obj);
				System.out.println("total:"+j+",going:"+(k+1));
			}
		}
	}
	private Double getNumbericValue(XSSFCell cell){
		Double value = 0D;
		if(cell == null){
			return value;
		}
		try {
			if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
				value = cell.getNumericCellValue();
			}
		} catch (Exception e) {
			value = 0D;
		}
		return value;
	}
	/**
	 *按照字符方式获取Excel表中的值
	 * @param cell
	 * @return
	 */
	private String getValue(XSSFCell cell){
		String value = "";
		if(cell == null){
			return value;
		}
		if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
			DecimalFormat df = new DecimalFormat("0"); 
			value = df.format(cell.getNumericCellValue());
		}else if(cell.getCellType() == Cell.CELL_TYPE_STRING){
			value = cell.getStringCellValue();
		}else if(cell.getCellType() == Cell.CELL_TYPE_BLANK){
			return value;
		}
		return value.trim();
	}
}

 

 

DefaultWmsItemManager.java

 

@SuppressWarnings("unchecked")
public class DefaultWmsItemManager extends 
	DefaultBaseManager implements WmsItemManager {
	
	public void createItem(Object[] obj){
		List<Object[]> items = (List<Object[]>) obj[0];
		for(Object[] excel : items){
			WmsItem item = (WmsItem) excel[0];
			 
			 if(item.isNew()){
				 item.setCode(excel[1].toString());
			 }
			 item.setName(excel[2].toString());
			 item.setEname(excel[3].toString());
			 item.setType(excel[4].toString());
			 item.setItemType1((WmsItemType)excel[5]);
			 item.setItemType2((WmsItemType)excel[6]);
			 item.setBeBOM(Boolean.valueOf(excel[7].toString()));
			 item.setLeftRightPart(excel[8].toString());
			 item.setSimilarCode((WmsSimilarCode)excel[9]);
			 item.setHighConcern(excel[10].toString());
			 
			 item.setHighClaims(excel[11].toString());
			 item.setHighValue(excel[12].toString());
			 item.setSupplier((WmsOrganization)excel[13]);
			 item.setBeLawCheck(Boolean.valueOf(excel[14].toString()));
			 item.setBeOutPackage(Boolean.valueOf(excel[15].toString()));
			 item.setLotRule((WmsLotRule)excel[16]);
			 item.setSupplierOut((WmsOrganization)excel[17]);
			 item.setContainTime(excel[18]==null?0:
				Integer.valueOf(excel[18].toString()));
			 item.setBeScreening(Boolean.valueOf(excel[19].toString()));
			 item.setBeAntirust(Boolean.valueOf(excel[20].toString()));
			 
			 item.setBeShockproof(Boolean.valueOf(excel[21].toString()));
			 item.setRustDay(excel[22]==null?0:
				Integer.valueOf(excel[22].toString()));
			 //设置箱型
			 item.setPackageBoxType(excel[23].toString());
			 item.setStatus(BaseStatus.ENABLED);
			 commonDao.store(item);
			 
			 WmsPackageUnit pu1 = (WmsPackageUnit) excel[24];
			 pu1.setLineNo(1);
			 pu1.setUnit(excel[25].toString());
			 pu1.setConvertFigure(1);
			 pu1.setWeight(Double.valueOf(excel[26].toString()));
			 pu1.setItem(item);
			 commonDao.store(pu1);
			 
			 WmsPackageUnit pu2 = (WmsPackageUnit) excel[27];
			 if(pu2!=null){
				 pu2.setLineNo(2);
				 pu2.setUnit(excel[28].toString());
				 pu2.setConvertFigure(Integer.valueOf(excel[29].toString()));
				 pu2.setItem(item);
				 commonDao.store(pu2);
			 }
		}
	}
}

 

 

MyUtils.java

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**yc min*/
public class MyUtils {
	private static Log log = LogFactory.getLog(MyUtils.class);
	public static SimpleDateFormat yy = 
	new SimpleDateFormat("yyyyMMdd");
	/**####*/
	public static String spilt4 = "####";
	 public static int getSize(int size,int PAGE_NUMBER){  
        int j = size / PAGE_NUMBER;  
        if((size % PAGE_NUMBER) > 0){  
            j += 1;  
        }  
        return j;  
    }  
    public static int getIndex(int k,int size,int PAGE_NUMBER){  
        int toIndex = ((k + 1) * PAGE_NUMBER);  
        if(toIndex > size){  
            toIndex = size;  
        }  
        return toIndex;  
    }  
    public static List<Object> getList(
		List<Object> list,int k,int toIdnex,int PAGE_NUMBER){  
        List<Object> ret = list.subList((k * PAGE_NUMBER), toIdnex);  
        return ret;  
    }  
    public static List<Object[]> getListObj(
		List<Object[]> list,int k,int toIndex,int PAGE_NUMBER){  
        List<Object[]> ret = list.subList((k * PAGE_NUMBER), toIndex);  
        return ret;  
    }
    /**return string(yyyyMMdd)*/
    public static String formatDateYYToStr(Date date) {
		try {
			return yy.format(date);
		} catch (Exception e) {
			log.debug("MyUtils.formatDateYYToStr():" + e.getMessage());
			return null;
		}

	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值