java批量导入excel到Mysql数据库

package com.google.i_sales.service.data; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.WorkbookSettings; import jxl.read.biff.BiffException; import org.extremecomponents.table.limit.Limit; import org.hibernate.Criteria; import org.hibernate.JDBCException; import org.hibernate.criterion.CriteriaSpecification; import org.hibernate.criterion.Order; import org.hibernate.criterion.Projection; import org.hibernate.criterion.Projections; import org.hibernate.criterion.Restrictions; import org.hibernate.impl.CriteriaImpl; import org.springframework.util.Assert; import org.springside.core.dao.HibernateEntityDao; import org.springside.core.dao.support.Page; import org.springside.core.utils.BeanUtils; import com.google.i_sales.components.acegi.domain.User; import com.google.i_sales.domain.Business; import com.google.i_sales.domain.Dcompany; import com.google.i_sales.domain.MsalesCompany; import com.google.i_sales.domain.Reject; import com.google.i_sales.domain.SalesCompany; import com.google.i_sales.domain.Service; import com.google.i_sales.domain.TelephoneCompany; import com.google.i_sales.domain.TradeType; import com.google.i_sales.domain.UnitInfo; import com.google.i_sales.domain.Vcompany; import com.google.i_sales.service.common.BusinessManager; import com.google.i_sales.service.common.RejectManager; import com.google.i_sales.service.common.ServiceManager; import com.google.i_sales.service.common.TradeTypeManager; import com.google.i_sales.service.msales.MsalesCompanyManager; import com.google.i_sales.service.sales.DcompanyManager; import com.google.i_sales.service.sales.SalesCompanyManager; import com.google.i_sales.service.security.UserManager; import com.google.i_sales.service.telephone.TelephoneCompanyManager; /** * @author wxl2012 */ public class VcompanyManager extends HibernateEntityDao { Integer[] states = {new Integer(0),new Integer(2),new Integer(5),new Integer(6)}; private ServiceManager serviceManager; public void setServiceManager(ServiceManager serviceManager) { this.serviceManager = serviceManager; } public Vcompany getVcompany(Integer id) { return get(Vcompany.class, id); } /** * 批量倒入客户信息 */ public List saveImportCompany(File file, BusinessManager businessManager, User user, TradeTypeManager tradeTypeManager, RejectManager rejectManager, ServiceManager serviceManager) { List log = new ArrayList(); int count = 0; if (!file.exists()) { System.err.println("Directory or file not exists."); log.add("系统错误"); return log; } if (user == null) { log.add("用户超时,请重新登录"); return log; } try { //fis is the input file InputStream fis = new FileInputStream(file); WorkbookSettings ws = new WorkbookSettings(); ws.setLocale(Locale.SIMPLIFIED_CHINESE); //get the excel workbook Workbook rwb = Workbook.getWorkbook(fis, ws); //return how many sheets the workbook have Sheet[] sheets = rwb.getSheets(); for (int i = 0; i < sheets.length; ++i) { System.out.println("number" + sheets[i].getRows()); //operate every row in the sheet for (int row = 1; row < sheets[i].getRows(); ++row) { //get all the cells of a certain column Cell[] cells = sheets[i].getRow(row); Vcompany vcompany = new Vcompany(); String company_name = ""; String company_fax = ""; String company_add = ""; //地区 String region = ""; String company_website = ""; String company_tel=""; Integer i_tradetype = 1; Integer i_business =1; Integer i_service = 4; String company_email = ""; String company_mainfunction = ""; String company_remarks = ""; String company_person1 = ""; String company_person1_tel = ""; String company_person1_title = ""; String company_person2 = ""; String company_person2_tel = ""; String company_person2_title = ""; String company_person3 = ""; String company_person3_tel = ""; String company_person3_title = ""; String company_source = ""; //exception caused by invalidate company name try { //if company name is null company_name = cells[0].getContents(); System.out.println(count+":"+company_name); if (company_name.equals("") || company_name == null) { log.add("工作表" + i + " 第" + (row + 1) + "行:没有公司名称,批量导入停止"); break; } //if company name does exist int i_com_name = 0; try{ i_com_name = findBy("comName", company_name).size(); }catch(Exception e){ System.out.println("query com name fail"); e.printStackTrace(); } if (i_com_name != 0) { log.add("工作表" + i + " 第" + (row + 1) + "行:公司名称" + company_name + "已存在"); continue; } else { if (company_name.length() > 40) { log.add("工作表" + i + " 第" + (row + 1) + "行:公司名称超长"); continue; } else { vcompany.setComName(company_name); } } } catch (java.lang.ArrayIndexOutOfBoundsException e) { log.add("工作表" + i + " 第" + (row + 1) + "行:没有公司名称,批量导入停止"); break; } //exception caused by invalidate trade type try { i_tradetype = Integer.valueOf(cells[1].getContents()); if (i_tradetype == null) { log .add("工作表" + i + " 第" + (row + 1) + "行:企业性质不能为空"); continue; } else if (i_tradetype > 6 || i_tradetype < 1) { log .add("工作表" + i + " 第" + (row + 1) + "行:企业性质代号错误"); continue; } else { TradeType tempTradeType = tradeTypeManager .getTradeType(i_tradetype); vcompany.setTradetype(tempTradeType); } } catch (NumberFormatException e) { // e.printStackTrace(); log.add("工作表" + i + " 第" + (row + 1) + "行:企业性质格式不对"); continue; } try { i_business = Integer.valueOf(cells[2] .getContents()); // System.out.println("工作表" + i + " 第" + (row + 1) // + "行:i_business" + i_business); if (i_business == null) { log.add("工作表" + i + " 第" + (row + 1) + "行:行业不能为空"); continue; } else if (i_business > 23 || i_business < 1) { log.add("工作表" + i + " 第" + (row + 1) + "行:行业代号错误"); continue; } else { Business tempBusiness = businessManager .getBusiness(i_business); vcompany.setBusiness(tempBusiness); } } catch (NumberFormatException e) { // e.printStackTrace(); log.add("工作表" + i + " 第" + (row + 1) + "行:行业格式不对"); continue; } catch (java.lang.ArrayIndexOutOfBoundsException e) { } try { region = cells[3].getContents(); if (region != null) { if (region.length() <= 20) { vcompany.setRegion(region); } else { log.add("工作表" + i + " 第" + (row + 1) + "行:区域名称超长"); continue; } } } catch (java.lang.ArrayIndexOutOfBoundsException e) { } try{ company_tel = cells[4].getContents(); if (company_tel != null) { if (company_tel.length() <= 20) { vcompany.setComTel(company_tel); } else { log.add("工作表" + i + " 第" + (row + 1) + "行:公司电话超长"); continue; } } else { log.add("工作表" + i + " 第" + (row + 1) + "行:公司电话不能为空"); continue; }} catch (java.lang.ArrayIndexOutOfBoundsException e) { } try { company_fax = cells[5].getContents(); // System.out.println("工作表" + i + " 第" + (row + 1) // + "行:company_fax" + company_fax); if (company_fax != null) { if (company_fax.length() <= 20) { vcompany.setComFax(company_fax); } else { log.add("工作表" + i + " 第" + (row + 1) + "行:公司传真超长"); continue; } } } catch (java.lang.ArrayIndexOutOfBoundsException e) { } try { company_add = cells[6].getContents(); if (company_add != null) { if (company_add.length() <= 255) { vcompany.setComAddress(company_add); } else { log.add("工作表" + i + " 第" + (row + 1) + "行:公司地址超长"); continue; } } } catch (java.lang.ArrayIndexOutOfBoundsException e) { } try { company_website = cells[7].getContents(); if (company_website != null) { if (company_website.length() <= 255) { vcompany.setComWebsite(company_website); } else { log.add("工作表" + i + " 第" + (row + 1) + "行:公司网址超长"); continue; } } } catch (java.lang.ArrayIndexOutOfBoundsException e) { } try { company_email = cells[8].getContents(); if (company_email != null) { if (company_email.length() <= 255) { vcompany.setComEmail(company_email); } else { log.add("工作表" + i + " 第" + (row + 1) + "行:公司Email超长"); continue; } } } catch (java.lang.ArrayIndexOutOfBoundsException e) { } try { company_mainfunction = cells[9].getContents(); if (company_mainfunction != null) { if (company_mainfunction.length() <= 200) { vcompany.setMainfunction(company_mainfunction); } else { log.add("工作表" + i + " 第" + (row + 1) + "行:公司主营业务超长"); continue; } } } catch (java.lang.ArrayIndexOutOfBoundsException e) { } try { company_remarks = cells[10].getContents(); // System.out.println("工作表" + i + " 第" + (row + 1) // + "行:company_remarks" + company_remarks); if (company_remarks != null) { if (company_remarks.length() <= 100) { vcompany.setRemarks(company_remarks); } else { log .add("工作表" + i + " 第" + (row + 1) + "行:备注超长"); continue; } } } catch (java.lang.ArrayIndexOutOfBoundsException e) { } vcompany.setState(new Integer(0)); try { company_person1 = cells[11].getContents(); if (company_person1 != null) { if (company_person1.length() <= 30) { vcompany.setContactPerson1(company_person1); } else { log.add("工作表" + i + " 第" + (row + 1) + "行:联系人1姓名超长"); continue; } } } catch (java.lang.ArrayIndexOutOfBoundsException e) { } try { company_person1_tel = cells[12].getContents(); if (company_person1_tel != null) { if (company_person1_tel.length() <= 30) { vcompany .setContactPerson1Phone(company_person1_tel); } else { log.add("工作表" + i + " 第" + (row + 1) + "行:联系人1电话超长"); continue; } } } catch (java.lang.ArrayIndexOutOfBoundsException e) { } try { company_person1_title = cells[13].getContents(); if (company_person1_title != null) { if (company_person1_title.length() <= 30) { vcompany .setContactPerson1Title(company_person1_title); } else { log.add("工作表" + i + " 第" + (row + 1) + "行:联系人1职位超长"); continue; } } } catch (java.lang.ArrayIndexOutOfBoundsException e) { } try { company_person2 = cells[14].getContents(); if (company_person2 != null) { if (company_person2.length() <= 30) { vcompany.setContactPerson2(company_person2); } else { log.add("工作表" + i + " 第" + (row + 1) + "行:联系人2姓名超长"); continue; } } } catch (java.lang.ArrayIndexOutOfBoundsException e) { } try { company_person2_tel = cells[15].getContents(); if (company_person2_tel != null) { if (company_person2_tel.length() <= 30) { vcompany .setContactPerson2Phone(company_person2_tel); } else { log.add("工作表" + i + " 第" + (row + 1) + "行:联系人2电话超长"); continue; } } } catch (java.lang.ArrayIndexOutOfBoundsException e) { } try { company_person2_title = cells[16].getContents(); if (company_person2_title != null) { if (company_person2_title.length() <= 30) { vcompany .setContactPerson2Title(company_person2_title); } else { log.add("工作表" + i + " 第" + (row + 1) + "行:联系人2职位超长"); continue; } } } catch (java.lang.ArrayIndexOutOfBoundsException e) { } try { company_person3 = cells[17].getContents(); // System.out.println("工作表" + i + " 第" + (row + 1) // + "行:company_person3" + company_person3); if (company_person3 != null) { if (company_person3.length() <= 30) { vcompany.setContactPerson3(company_person3); } else { log.add("工作表" + i + " 第" + (row + 1) + "行:联系人3姓名超长"); continue; } } } catch (java.lang.ArrayIndexOutOfBoundsException e) { } try { company_person3_tel = cells[18].getContents(); if (company_person3_tel != null) { if (company_person3_tel.length() <= 30) { vcompany .setContactPerson3Phone(company_person3_tel); } else { log.add("工作表" + i + " 第" + (row + 1) + "行:联系人3电话超长"); continue; } } } catch (java.lang.ArrayIndexOutOfBoundsException e) { } try { company_person3_title = cells[19].getContents(); if (company_person3_title != null) { if (company_person3_title.length() <= 30) { vcompany .setContactPerson3Title(company_person3_title); } else { log.add("工作表" + i + " 第" + (row + 1) + "行:联系人3职位超长"); continue; } } } catch (java.lang.ArrayIndexOutOfBoundsException e) { } try { company_source = cells[20].getContents(); // System.out.println("工作表" + i + " 第" + (row + 1) // + "行:company_source" + company_source); if (company_source != null) { // System.out.println("company_source.length()" // + company_source.length()); if (company_source.length() <= 20) { vcompany.setSource(company_source); } else { log.add("工作表" + i + " 第" + (row + 1) + "行:公司来源超长"); continue; } } } catch (java.lang.ArrayIndexOutOfBoundsException e) { } try { i_service = Integer.valueOf(cells[21].getContents()); if (i_service == null) { log .add("工作表" + i + " 第" + (row + 1) + "行:企业业务形态不能为空,批量导入停止"); break; } else if (i_service > 5 || i_service < 1) { log .add("工作表" + i + " 第" + (row + 1) + "行:企业业务形态代号错误,批量导入停止"); break; } else { Service tempService = serviceManager.get(i_service); vcompany.setService(tempService); } } catch (NumberFormatException e) { // e.printStackTrace(); log.add("工作表" + i + " 第" + (row + 1) + "行:企业业务形态错误,批量导入停止"); break; } vcompany.setUser(user); Date date = new Date(); vcompany.setTime(date); super.save(vcompany); ++count; } } log.add("成功导入" + count + "条数据"); rwb.close(); fis.close(); } catch (BiffException e) { System.err.println("Open file ERROR! - BiffException"); e.printStackTrace(); log.add("系统错误"); return log; } catch (IOException e) { System.err.println("Open file ERROR! - IOException"); e.printStackTrace(); log.add("系统错误"); return log; } catch (NumberFormatException e) { System.err.println("Open file ERROR! - IOException"); e.printStackTrace(); log.add("系统错误,但成功导入" + count + "条数据"); return log; } catch (java.lang.OutOfMemoryError e) { log.add("系统错误,xls文件出错"); return log; } catch (Exception e) { System.err.println("Some Error! - Exception"); e.printStackTrace(); log.add("发生错误,但成功导入" + count + "条数据"); return log; } return log; } /*save information for the stated company * * pjj * */ public List saveStateImportCompany(File file, TelephoneCompanyManager telephoneCompanyManager, User user, SalesCompanyManager salesCompanyManager, MsalesCompanyManager msalesCompanyManager, DcompanyManager dcompanyManager, UserManager userManager,RejectManager rejectManager){ //initialize the log file System.out.println("begin saveStateImportCompany"); List log = new ArrayList(); Set states = new HashSet(){{add("0");add("1");add("2"); add("3");add("4");add("5");add("6");add("7"); add("8");add("9");add("10");add("11");}}; Set rejects = new HashSet(){{add("0");add("1");add("2"); add("3");add("4");add("5");add("6");add("7"); add("8");add("9"); add("10");add("11");add("12");add("13");add("14");add("15"); add("16");add("17");add("18");add("19");add("20");add("21");}}; Set scores = new HashSet(){{add("0");add("1");add("3");add("5");}}; int count = 0; if (!file.exists()) { System.err.println("Directory or file not exists."); log.add("系统错误"); return log; } if (user == null) { log.add("用户超时,请重新登录"); return log; } try{ //fis is the input file InputStream fis = new FileInputStream(file); WorkbookSettings ws = new WorkbookSettings(); ws.setLocale(Locale.SIMPLIFIED_CHINESE); //get the excel workbook Workbook rwb = Workbook.getWorkbook(fis, ws); //return how many sheets the workbook have Sheet[] sheets = rwb.getSheets(); for (int i = 0; i < sheets.length; ++i) { System.out.println("number" + sheets[i].getRows()); //operate every row in the sheet for (int row = 1; row < sheets[i].getRows(); ++row) { //get all the cells of a certain column System.out.println("sheet"+i+"row"+row); Cell[] cells = sheets[i].getRow(row); String comName = ""; String comState = ""; String comReject = "1"; String comTelPerson = ""; String comAplusTime = ""; String comSalesPerson = ""; String comScore = ""; //检查公司名是否合法,由公司名称得到vcompany try { //if company name is null comName = cells[0].getContents(); if (comName.equals("") || comName == null) { log.add("工作表" + i + " 第" + (row + 1) + "行:没有公司名称,批量导入停止"); break; } //if company name does not exist if (super.findBy("comName", comName).size()== 0) { log.add("工作表" + i + " 第" + (row + 1) + "行:公司:" + comName + "不存在"); break; } } catch (java.lang.ArrayIndexOutOfBoundsException e) { log.add("工作表" + i + " 第" + (row + 1) + "行:没有公司名称,批量导入停止"); break; } Vcompany vcompany = null; vcompany = super.findUniqueBy("comName", comName); System.out.println(vcompany.getComName()); //检查状态是否合法,得到导入状态 try{ comState = cells[1].getContents(); if(comState == null || comState.equals("")){ log.add("工作表" + i + " 第" + (row + 1) + "行:状态不能为空,批量导入停止"); break; } if(!states.contains(comState)){ log.add("工作表" + i + " 第" + (row + 1) + "行:状态值非法,批量导入停止"); break; } }catch(java.lang.ArrayIndexOutOfBoundsException e){ log.add("工作表" + i + " 第" + (row + 1) + "行:状态值不能为空,批量导入停止"); break; } Integer state = new Integer(comState); System.out.println("import state:"+state); //检查拒绝理由是否合法,得到拒绝理由 try{ comReject = cells[2].getContents(); if (comReject ==""||comReject==null){ log.add("工作表" + i + " 第" + (row + 1) + "行:拒绝理由不能为空,批量导入停止"); break; } if(!rejects.contains(comReject)){ log.add("工作表" + i + " 第" + (row + 1) + "行:拒绝理由输入值非法,批量导入停止"); break; } }catch(java.lang.ArrayIndexOutOfBoundsException e){ e.printStackTrace(); log.add("工作表" + i + " 第" + (row + 1) + "行:拒绝理由不能为空,批量导入停止"); break; } Reject reject = rejectManager.getReject(new Integer(comReject)); System.out.println("reject:"+reject.getId()); //检查电话专员,若输入不为空 User telPerson = null; try{ comTelPerson = cells[3].getContents(); if(comTelPerson!=null && comTelPerson!=""){ if(userManager.findBy("loginid", comTelPerson).size() == 0){ log.add("工作表" + i + " 第" + (row + 1) + "行:该电话专员不存在,批量导入停止"); break; } } }catch(Exception e){ e.printStackTrace(); // continue; } if(comTelPerson!=null && comTelPerson!=""){ telPerson = userManager.findUniqueBy("loginid", comTelPerson); } //得到销售专员 User salesPerson = null; try{ comSalesPerson = cells[4].getContents(); System.out.println("get user from excl:"+ comSalesPerson); if(comSalesPerson!=null && comSalesPerson!=""){ System.out.println("get user from excl:"+ userManager.findBy("loginid", comSalesPerson).size()); if(userManager.findBy("loginid", comSalesPerson).size()== 0){ log.add("工作表" + i + " 第" + (row + 1) + "行:该销售专员不存在,批量导入停止"); break; } } }catch(Exception e){ System.out.println("Exception when get salesperson"); e.printStackTrace(); } if(comSalesPerson!=null && comSalesPerson!=""){ salesPerson = userManager.findUniqueBy("loginid", comSalesPerson); } //得到返单评分 Integer score = null; try{ comScore = cells[5].getContents(); if(comScore!=null && comScore!=""){ if(!scores.contains(comScore)){ log.add("工作表" + i + " 第" + (row + 1) + "行:评分输入值非法,批量导入停止"); break; } } }catch(Exception e){ // continue; } if(comScore!=null && comScore!=""){ score = new Integer(comScore); } //aplus time Date aplus = null; try{ comAplusTime = cells[6].getContents(); if(comAplusTime!=null && comAplusTime!=""){ System.out.println("get time:"+comAplusTime); char[] ArrayTime = comAplusTime.toCharArray(); char[] finalTime = new char[11]; finalTime[0] = ArrayTime[6]; finalTime[1] = ArrayTime[7]; finalTime[2] = ArrayTime[8]; finalTime[3] = ArrayTime[9]; finalTime[4] = '-'; finalTime[5] = ArrayTime[3]; finalTime[6] = ArrayTime[4]; finalTime[7] = '-'; finalTime[8] = ArrayTime[0]; finalTime[9] = ArrayTime[1]; String gettime = String.copyValueOf(finalTime);; System.out.println("get time:"+gettime); java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd"); aplus = sdf.parse(gettime); System.out.println("prase time:"+aplus); } }catch(Exception e){ log.add("工作表" + i + " 第" + (row + 1) + "行:时间格式错误"); } /************************************************按状态向各个表插入记录*********************************************/ TelephoneCompany telephoneCompany = new TelephoneCompany(); SalesCompany salesCompany = new SalesCompany(); MsalesCompany msalesCompany = new MsalesCompany(); Dcompany dcompany = new Dcompany(); int duplicate = 0; int k=0; //未分配 if(state==0){ if (telPerson != null){ //若电话专员属于中型部门,则向中型表中插入数据 if(telPerson.getUnitInfo().getUnitType().getId()==5){ //若表中公司已存在且状态和导入状态一样,则为重复数据 duplicate = 0; List msalesconpanys = msalesCompanyManager.findBy("vcompany", vcompany); if(msalesconpanys.size()!=0){ for(k=0;k msalesconpanys = msalesCompanyManager.findBy("vcompany", vcompany); if(msalesconpanys.size()!=0){ for(k=0;k telcompanys = telephoneCompanyManager.findBy("vcompany", vcompany); if(telcompanys.size()!=0){ /*for(k=0;k msalesconpanys = msalesCompanyManager.findBy("vcompany", vcompany); if(msalesconpanys.size()!=0){ for(k=0;k telcompanys = telephoneCompanyManager.findBy("vcompany", vcompany); if(telcompanys.size()!=0){ /*for(k=0;k telcompanys = telephoneCompanyManager.findBy("vcompany", vcompany); if(telcompanys.size()!=0){ /*for(k=0;k msalesconpanys = msalesCompanyManager.findBy("vcompany", vcompany); if(msalesconpanys.size()!=0){ for(k=0;k salescompanys = salesCompanyManager.findBy("vcompany", vcompany); if(salescompanys.size()!=0){ for(k=0;k telcompanys = telephoneCompanyManager.findBy("vcompany", vcompany); if(telcompanys.size()!=0){ /*for(k=0;k msalesconpanys = msalesCompanyManager.findBy("vcompany", vcompany); if(msalesconpanys.size()!=0){ for(k=0;k salescompanys = salesCompanyManager.findBy("vcompany", vcompany); if(salescompanys.size()!=0){ for(k=0;k telcompanys = telephoneCompanyManager.findBy("vcompany", vcompany); if(telcompanys.size()!=0){ /*for(k=0;k salescompanys = salesCompanyManager.findBy("vcompany", vcompany); if(salescompanys.size()!=0){ for(k=0;k telcompanys = telephoneCompanyManager.findBy("vcompany", vcompany); if(telcompanys.size()!=0){ /*for(k=0;k telcompanys = telephoneCompanyManager.findBy("vcompany", vcompany); if(telcompanys.size()!=0){ /*for(k=0;k msalesconpanys = msalesCompanyManager.findBy("vcompany", vcompany); if(msalesconpanys.size()!=0){ for(k=0;k salescompanys = salesCompanyManager.findBy("vcompany", vcompany); if(salescompanys.size()!=0){ for(k=0;k telcompanys = telephoneCompanyManager.findBy("vcompany", vcompany); if(telcompanys.size()!=0){ /*for(k=0;k msalesconpanys = msalesCompanyManager.findBy("vcompany", vcompany); if(msalesconpanys.size()!=0){ for(k=0;k salescompanys = salesCompanyManager.findBy("vcompany", vcompany); if(salescompanys.size()!=0){ for(k=0;k telcompanys = telephoneCompanyManager.findBy("vcompany", vcompany); if(telcompanys.size()!=0){ /*for(k=0;k msalesconpanys = msalesCompanyManager.findBy("vcompany", vcompany); if(msalesconpanys.size()!=0){ for(k=0;k salescompanys = salesCompanyManager.findBy("vcompany", vcompany); if(salescompanys.size()!=0){ for(k=0;k telcompanys = telephoneCompanyManager.findBy("vcompany", vcompany); if(telcompanys.size()!=0){ /*for(k=0;k dcompanys = dcompanyManager.findBy("vcompany", vcompany); if(dcompanys.size()== 0){ dcompany.setVcompany(vcompany); dcompany.setEstate(new Integer(0)); dcompanyManager.save(dcompany); } else{ log.add("工作表" + i + " 第" + (row + 1) + "行:该公司已存在于签约公司记录中,批量导入停止"); break; } vcompany.setReject(reject); vcompany.setState(new Integer(1)); vcompany.setAplusTime(aplus); if(reject.getRejectid()!=1){ vcompany.setRejecttimes(new Integer(1)); } save(vcompany); count++; } //E else if(state==11){ if (salesPerson != null){ if(salesPerson.getUnitInfo().getUnitType().getId()==5){ //若表中公司已存在且状态和导入状态一样,则为重复数据 duplicate = 0; List msalesconpanys = msalesCompanyManager.findBy("vcompany", vcompany); if(msalesconpanys.size()!=0){ for(k=0;k salescompanys = salesCompanyManager.findBy("vcompany", vcompany); if(salescompanys.size()!=0){ for(k=0;k telcompanys = telephoneCompanyManager.findBy("vcompany", vcompany); if(telcompanys.size()!=0){ /*for(k=0;k dcompanys = dcompanyManager.findBy("vcompany", vcompany); if(dcompanys.size()== 0){ dcompany.setVcompany(vcompany); dcompany.setEstate(new Integer(1)); dcompanyManager.save(dcompany); } else{ log.add("工作表" + i + " 第" + (row + 1) + "行:该公司已存在于签约公司记录中,批量导入停止"); break; } vcompany.setReject(reject); vcompany.setState(new Integer(1)); vcompany.setAplusTime(aplus); if(reject.getRejectid()!=1){ vcompany.setRejecttimes(new Integer(1)); } save(vcompany); count++; } }//for cells }//for rows log.add("成功导入" + count + "条数据"); rwb.close(); fis.close(); } catch (BiffException e) { System.err.println("Open file ERROR! - BiffException"); e.printStackTrace(); log.add("系统错误"); return log; } catch (IOException e) { System.err.println("Open file ERROR! - IOException"); e.printStackTrace(); log.add("系统错误"); return log; } catch (NumberFormatException e) { System.err.println("Open file ERROR! - IOException"); e.printStackTrace(); log.add("系统错误,但成功导入" + count + "条数据"); return log; } catch (java.lang.OutOfMemoryError e) { log.add("系统错误,xls文件出错"); return log; } catch (Exception e) { System.err.println("Some Error! - Exception"); e.printStackTrace(); log.add("发生错误,但成功导入" + count + "条数据"); return log; } return log; } /** * 查询公司信息 */ @SuppressWarnings("unchecked") public List searchVcompany(Map filter, BusinessManager businessManager, TradeTypeManager tradeTypeManager, RejectManager rejectManager, Integer apply) { Criteria criteria = createCriteria(); Integer valuetradetype = Integer.valueOf(filter.get("tradetypeid") .toString()); Integer valuebusiness = Integer.valueOf(filter.get("businessid") .toString()); String valueregion = filter.get("region").toString(); Integer valueservice = Integer.valueOf(filter.get("service").toString()); if(valueservice.intValue()!=-1){ criteria.add(Restrictions.eq("service", serviceManager.get(valueservice))); } if (valuetradetype.intValue() != -1) { System.out.println("in if valuetradetype"+valuetradetype); // 获取所有在Vcompany中是tradeTypeManager.getTradeType(valuetradetype)类型的所有Vcompany criteria.add(Restrictions.eq("tradetype", tradeTypeManager .getTradeType(valuetradetype))); } if (valuebusiness.intValue() != -1) { // System.out.println("in if valuebusiness"+valuebusiness); criteria.add(Restrictions.eq("business", businessManager .getBusiness(valuebusiness))); } if (!(valueregion.equals("") || valueregion == null)) { criteria.add(Restrictions.like("region", "%" + valueregion + "%")); } criteria.add(Restrictions.like("comName", "%" + filter.get("name").toString() + "%")); if (!filter.get("mainfunction").toString().equals("")) { criteria.add(Restrictions.like("mainfunction", "%" + filter.get("mainfunction").toString() + "%")); } if (apply == 1) { criteria.add(Restrictions.or(Restrictions.eq("state", new Integer(0)), Restrictions.eq("state", new Integer(2)))); Integer valuereject = Integer.valueOf(filter.get("rejectid") .toString()); if (valuereject.intValue() != -1) { criteria.add(Restrictions.eq("reject", rejectManager .getReject(valuereject))); criteria.addOrder(Order.asc("aplusTime")); } } List vcompanys = criteria.list(); return vcompanys; } @SuppressWarnings("unchecked") public List searchVcompany(Map filter) { Criteria criteria = createCriteria(); criteria.add(Restrictions.like("comName", "%" + filter.get("name").toString() + "%")); List vcompanys = criteria.list(); return vcompanys; } /** * 得到待分配公司信息(电话经理) */ @SuppressWarnings("unchecked") public Page getApplyCom(int pageNo, int pageSize,Limit limit) { Criteria criteria = createCriteria(); //列出状态是未分配,拒绝可再打,返单再打的公司 criteria.add(Restrictions.in("state", states)); if(limit.getSort().getProperty()!= null){ if(limit.getSort().getSortOrder()=="asc"){ criteria.addOrder(Order.asc(limit.getSort().getProperty())); } else{ criteria.addOrder(Order.desc(limit.getSort().getProperty())); } } Assert.isTrue(pageNo >= 1, "pageNo should start from 1"); CriteriaImpl impl = (CriteriaImpl) criteria; System.out.println("apply here1"); // 先把Projection和OrderBy条件取出来,清空两者来执行Count操作 Projection projection = impl.getProjection(); List orderEntries; try { orderEntries = (List) BeanUtils.forceGetProperty(impl, "orderEntries"); BeanUtils.forceSetProperty(impl, "orderEntries", new ArrayList()); } catch (Exception e) { throw new InternalError(" Runtime Exception impossibility throw "); } System.out.println("apply here2"); // 执行查询 long totalCount = (Integer)criteria.setProjection(Projections.rowCount()).uniqueResult(); System.out.println("totalCount:"+totalCount); // 将之前的Projection和OrderBy条件重新设回去 criteria.setProjection(projection); if (projection == null) { criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY); } try { BeanUtils.forceSetProperty(impl, "orderEntries", orderEntries); } catch (Exception e) { throw new InternalError(" Runtime Exception impossibility throw "); } // 返回分页对象 if (totalCount < 1) return new Page(); System.out.println("Mypage here3"); int startIndex = Page.getStartOfPage(pageNo, pageSize); System.out.println("startIndex:"+startIndex); List list = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list(); return new Page(startIndex, totalCount, pageSize, list); } /*列出无效客户pjj*/ public List getInvalidCom(){ Criteria criteria = createCriteria(); criteria.add(Restrictions.eq("state", new Integer(3))); List invalidComs = criteria.list(); return invalidComs; } /*search invalid companys pjj*/ public List searchInvalidCom(Map filter, BusinessManager businessManager, TradeTypeManager tradeTypeManager){ Criteria criteria = createCriteria(); Integer valuetradetype = Integer.valueOf(filter.get("tradetypeid") .toString()); Integer valuebusiness = Integer.valueOf(filter.get("businessid") .toString()); String valueregion = filter.get("region").toString(); Integer valueservice = Integer.valueOf(filter.get("service").toString()); if(valueservice.intValue()!=-1){ criteria.add(Restrictions.eq("service", serviceManager.get(valueservice))); } if (valuetradetype.intValue() != -1) { criteria.add(Restrictions.eq("tradetype", tradeTypeManager .getTradeType(valuetradetype))); } if (valuebusiness.intValue() != -1) { criteria.add(Restrictions.eq("business", businessManager .getBusiness(valuebusiness))); } if (!(valueregion.equals("") || valueregion == null)) { criteria.add(Restrictions.like("region", "%" + valueregion + "%")); } criteria.add(Restrictions.like("comName", "%" + filter.get("name").toString() + "%")); if (!filter.get("mainfunction").toString().equals("")) { criteria.add(Restrictions.like("mainfunction", "%" + filter.get("mainfunction").toString() + "%")); } criteria.add(Restrictions.eq("state", new Integer(3))); List vcompanys = criteria.list(); return vcompanys; } /** * 分页得到公司信息 */ @SuppressWarnings("unchecked") public Page getPageAll(int pageNo,int pageSize,Limit limit) { System.out.println("page here1"); Criteria criteria = createCriteria(); System.out.println("page here2"); //若页面需要排序 if(limit.getSort().getProperty()!= null){ if(limit.getSort().getSortOrder()=="asc"){ criteria.addOrder(Order.asc(limit.getSort().getProperty())); } else{ criteria.addOrder(Order.desc(limit.getSort().getProperty())); } } // return this.pagedQuery(hql, new Integer(1), new Integer(5), null); Page result = this.pagedQuery(criteria,pageNo,pageSize); System.out.println("result.getPageSize:"+result.getPageSize()); return result; } /** * 分页查询函数,使用已设好查询条件与排序的Criteria. * @param pageNo 页号,从1开始. * @return 含总记录数和当前页数据的Page对象. * 分页查询公司信息(apply=1则为电话经理查询未分配公司信息) */ public Page MypagedQuery(Map filter,BusinessManager businessManager, TradeTypeManager tradeTypeManager,RejectManager rejectManager, Integer apply, int pageNo, int pageSize,Limit limit) { System.out.println("MypagedQuery"); Criteria criteria = createCriteria(); //若页面需要排序 if(limit.getSort().getProperty()!= null){ if(limit.getSort().getSortOrder()=="asc"){ criteria.addOrder(Order.asc(limit.getSort().getProperty())); } else{ criteria.addOrder(Order.desc(limit.getSort().getProperty())); } } System.out.println("filter:"+filter); System.out.println("filter valuetradetype:"+filter.get("tradetypeid")); Integer valuetradetype = Integer.valueOf(filter.get("tradetypeid") .toString()); Integer valuebusiness = Integer.valueOf(filter.get("businessid") .toString()); Integer valuerejecttimes = Integer.valueOf(filter.get("rejecttimes").toString()); String valueregion = filter.get("region").toString(); Integer valueservice = Integer.valueOf(filter.get("service").toString()); if(valueservice.intValue()!=-1){ criteria.add(Restrictions.eq("service", serviceManager.get(valueservice))); } if (valuetradetype.intValue() != -1) { System.out.println("in if valuetradetype"+valuetradetype); // 获取所有在Vcompany中是tradeTypeManager.getTradeType(valuetradetype)类型的所有Vcompany criteria.add(Restrictions.eq("tradetype", tradeTypeManager .getTradeType(valuetradetype))); } if (valuebusiness.intValue() != -1) { // System.out.println("in if valuebusiness"+valuebusiness); criteria.add(Restrictions.eq("business", businessManager .getBusiness(valuebusiness))); } if (!(valueregion.equals("") || valueregion == null)) { criteria.add(Restrictions.like("region", "%" + valueregion + "%")); } System.out.println("comname in search:"+filter.get("name").toString()); criteria.add(Restrictions.like("comName", "%" + filter.get("name").toString() + "%")); if (!filter.get("mainfunction").toString().equals("")) { criteria.add(Restrictions.like("mainfunction", "%" + filter.get("mainfunction").toString() + "%")); } //apply=1为电话经理搜索未分配的v公司 if (apply == 1) { criteria.add(Restrictions.in("state", states)); Integer valuereject = Integer.valueOf(filter.get("rejectid") .toString()); if (valuereject.intValue() != -1) { criteria.add(Restrictions.eq("reject", rejectManager .getReject(valuereject))); criteria.addOrder(Order.asc("aplusTime")); } } if(valuerejecttimes != -1){ if(valuerejecttimes==6){ criteria.add(Restrictions.gt("rejecttimes", 5)); } else{ criteria.add(Restrictions.eq("rejecttimes",valuerejecttimes)); } } System.out.println(criteria); Assert.isTrue(pageNo >= 1, "pageNo should start from 1"); CriteriaImpl impl = (CriteriaImpl) criteria; System.out.println("Mypage here1"); // 先把Projection和OrderBy条件取出来,清空两者来执行Count操作 Projection projection = impl.getProjection(); List orderEntries; try { orderEntries = (List) BeanUtils.forceGetProperty(impl, "orderEntries"); BeanUtils.forceSetProperty(impl, "orderEntries", new ArrayList()); } catch (Exception e) { throw new InternalError(" Runtime Exception impossibility throw "); } System.out.println("Mypage here2"); // 执行查询 long totalCount = (Integer)criteria.setProjection(Projections.rowCount()).uniqueResult(); System.out.println("totalCount:"+totalCount); // 将之前的Projection和OrderBy条件重新设回去 criteria.setProjection(projection); if (projection == null) { criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY); } try { BeanUtils.forceSetProperty(impl, "orderEntries", orderEntries); } catch (Exception e) { throw new InternalError(" Runtime Exception impossibility throw "); } // 返回分页对象 if (totalCount < 1) return new Page(); System.out.println("Mypage here3"); int startIndex = Page.getStartOfPage(pageNo, pageSize); System.out.println("startIndex:"+startIndex); List list = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list(); return new Page(startIndex, totalCount, pageSize, list); } }

你可能感兴趣的:(JAVA)