jimingminlovefly

统计

最新评论

案例-excel导入,数据导入数据库

一、二、三点如下:
一、JxlExcelImportFacade
package com.icicle.goldenfly.web.order.report;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import common.Logger;

import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public abstract class JxlExcelImportFacade<T> {
 
 private static final Logger logger = Logger.getLogger(JxlExcelImportFacade.class);
 
 protected InputStream in;
 protected Workbook workbook;
 
 public void init(File file) throws BiffException, IOException{
  this.in = new FileInputStream(file);
  workbook = Workbook.getWorkbook(in);
 }
 
 public void init(InputStream in) throws BiffException, IOException{
  this.in = in;
  workbook = Workbook.getWorkbook(in); 
 }
 
 public Sheet getSheet(int index){
  return workbook.getSheet(index);
 }

 public abstract T readContent(Cell[] cells);
 
 public List<T> readContents(List<Cell[]> rows){
  List<T> result = new ArrayList<T>();
  for(Cell[] cells : rows){
   T content = readContent(cells);
   result.add(content);
  }
  return result;
 }
 
 public void close() throws IOException{
  workbook.close();
  in.close();
 }
 
 public boolean isEmpty(Cell[] cells){
  for(Cell cell : cells){
   if( cell != null && StringUtils.isNotEmpty(cell.getContents())){
    return false;
   }
  }
  return true;
 }
 
 public Number readNumber(Cell cell, String defaultFormat){
  String formatStr = defaultFormat;
  if(cell.getType().equals(CellType.NUMBER)){
   formatStr = cell.getCellFormat().getFormat().getFormatString();
   if(StringUtils.isEmpty(formatStr)){
    formatStr = defaultFormat;
   }
  }
  String content = readString(cell);
  try {
   return new DecimalFormat(formatStr).parse(content);
  } catch (ParseException e) {
   logger.error("NumberFormat:" + formatStr + " NumberString:" + content , e);
   return null;
  }
 }
 
 public Number readNumber(Cell[] cells, int index, String defaultFormat){
  if(cells.length > index){
   return readNumber(cells[index], defaultFormat);
  }
  return null;
 }
 
 public Date readDate(Cell cell, String defaultFormat){
  Date date = null;
  String content = readString(cell);
  try{
   if(cell.getType().equals(CellType.DATE)){
    if(cell instanceof DateCell){
     date = DateUtils.truncate(((DateCell)cell).getDate(), Calendar.DATE);
    }else{
     String formatStr = cell.getCellFormat().getFormat().getFormatString();
     try {
      date = new SimpleDateFormat(formatStr).parse(content);
     } catch (Exception e) {
      date = new SimpleDateFormat(defaultFormat).parse(content);
     }     
    }
   }else{
    date = new SimpleDateFormat(defaultFormat).parse(content);
   }   
  }catch(ParseException e){
   logger.error("Date:" + content + " Format:" + defaultFormat, e);
  }

  return date;
 }
 
 public Date readDate(Cell[] cells, int index, String defaultFormat){
  if(cells.length > index){
   return readDate(cells[index], defaultFormat);
  }
  return null;
 }
 
 public String readString(Cell cell){
  return cell.getContents();
 }
 
 public String readString(Cell[] cells, int index){
  if(cells.length > index){
   return readString(cells[index]);
  }
  return "";
 }
 
}

二、ImportBusTicketPrice

package com.icicle.goldenfly.web.order.report;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;

import org.apache.commons.lang.StringUtils;

import com.icicle.framework.member.client.MemberClientUtils;
import com.icicle.framework.order.client.enums.BusTicketSource;
import com.icicle.framework.order.client.models.BusTicketPrice;
import com.icicle.goldenfly.bus.server.BusTicketList;
/**
 * 车票价格导入   处理Excel数据
 * @author James pu
 *
 */
public class ImportBusTicketPrice extends JxlExcelImportFacade<BusTicketPrice>{

 public List<BusTicketPrice> readContents(){
  List<BusTicketPrice> busTicketPriceList = new ArrayList<BusTicketPrice>();
  Sheet sheet = getSheet(0);
  int rows = sheet.getRows();
  for(int i = 1; i < rows; i++){
   Cell[] cells = sheet.getRow(i);
   if( !isEmpty(cells)){
    BusTicketPrice busTicketPrice = readContent(cells);
    busTicketPriceList.add(busTicketPrice);
   }
  }
  return busTicketPriceList;
 } 
 
 @Override
 public BusTicketPrice readContent(Cell[] cells) {  
  //来源
  String source = StringUtils.defaultString(readString(cells, 0));
  //起始站点代码
  String fromBusStop = StringUtils.defaultString(readString(cells, 1));  
  //目的站点代码
  String toBusStop = StringUtils.defaultString(readString(cells, 2));  
     //单程  卖价
  double priceOfSingle =MemberClientUtils.getFirstNotNull(readNumber(cells, 3, "###0.00"), 0).doubleValue();
  //双程 卖价
  double priceOfRound =MemberClientUtils.getFirstNotNull(readNumber(cells, 4, "###0.00"), 0).doubleValue();
  //单程 买价
  double dealPriceOfSingle =MemberClientUtils.getFirstNotNull(readNumber(cells, 5, "###0.00"), 0).doubleValue();
  //双程 买价
  double dealPriceOfRound =MemberClientUtils.getFirstNotNull(readNumber(cells, 6, "###0.00"), 0).doubleValue();
  //单程 门市价
  double realPriceOfSingle =MemberClientUtils.getFirstNotNull(readNumber(cells, 7, "###0.00"), 0).doubleValue();
  //双程 门市价
  double realPriceOfRound =MemberClientUtils.getFirstNotNull(readNumber(cells, 8, "###0.00"), 0).doubleValue();
  //票数量
  Integer restNumber =MemberClientUtils.getFirstNotNull(readNumber(cells, 9, "###0"), 0).intValue();
  
  //有效期
  Date expiredDate=null;
  if(StringUtils.isNotEmpty(readString(cells,10))){
   expiredDate = readDate(cells,10, "yyyy-MM-dd");
  }
  //备注
  String remark = StringUtils.defaultString(readString(cells, 11));
  
  BusTicketPrice busTicketPrice = new BusTicketPrice();
  busTicketPrice.setSource(BusTicketSource.valueOf(source));
  busTicketPrice.setFromBusStop(fromBusStop);
  busTicketPrice.setFromBusStopDesc(BusTicketList.getStationDescByKey(fromBusStop));
  busTicketPrice.setToBusStop(toBusStop);
  busTicketPrice.setToBusStopDesc(BusTicketList.getStationDescByKey(toBusStop));
  busTicketPrice.setPriceOfSingle(BigDecimal.valueOf(priceOfSingle));
  busTicketPrice.setPriceOfRound(BigDecimal.valueOf(priceOfRound));
  busTicketPrice.setDealPriceOfSingle(BigDecimal.valueOf(dealPriceOfSingle));
  busTicketPrice.setDealPriceOfRound(BigDecimal.valueOf(dealPriceOfRound));
  busTicketPrice.setRealPriceOfSingle(BigDecimal.valueOf(realPriceOfSingle));
  busTicketPrice.setRealPriceOfRound(BigDecimal.valueOf(realPriceOfRound));
  busTicketPrice.setRestNumber(restNumber);
  busTicketPrice.setRemark(remark);
  busTicketPrice.setExpiredDate(expiredDate);
  
  return busTicketPrice;
 } 

}

三、HotNewsManagerEngine

package com.icicle.framework.member.server.driver;

import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;

import com.icicle.framework.member.client.Principal;
import com.icicle.framework.member.client.ReturnResult;
import com.icicle.framework.member.client.criteria.GroupByResult;
import com.icicle.framework.member.client.criteria.HotNewsCriteria;
import com.icicle.framework.member.client.criteria.QuestionAnswerCriteria;
import com.icicle.framework.member.client.criteria.QuestionCriteria;
import com.icicle.framework.member.client.criteria.SetRestriction;
import com.icicle.framework.member.client.driver.HotNewsManagerDriver;
import com.icicle.framework.member.client.enums.ReturnCode;
import com.icicle.framework.member.client.exceptions.ClientGenericException;
import com.icicle.framework.member.client.models.Feedback;
import com.icicle.framework.member.client.models.FreeTravel;
import com.icicle.framework.member.client.models.FreeTravelHotel;
import com.icicle.framework.member.client.models.FreeTravelOther;
import com.icicle.framework.member.client.models.FreeTravelTraffic;
import com.icicle.framework.member.client.models.HotNews;
import com.icicle.framework.member.client.models.Question;
import com.icicle.framework.member.client.models.QuestionAnswer;
import com.icicle.framework.member.client.models.UploadImages;
import com.icicle.framework.member.client.models.WebSiteComment;
import com.icicle.framework.member.server.ApplicationContextLoader;
import com.icicle.framework.member.server.Constants;
import com.icicle.framework.member.server.service.BusTicketPriceService;
import com.icicle.framework.member.server.service.FeedbackService;
import com.icicle.framework.member.server.service.FreeTravelHotelService;
import com.icicle.framework.member.server.service.FreeTravelOtherService;
import com.icicle.framework.member.server.service.FreeTravelService;
import com.icicle.framework.member.server.service.FreeTravelTrafficService;
import com.icicle.framework.member.server.service.HotNewsService;
import com.icicle.framework.member.server.service.QuestionAnswerService;
import com.icicle.framework.member.server.service.QuestionService;
import com.icicle.framework.member.server.service.UploadImageService;
import com.icicle.framework.member.server.service.WebSiteCommentService;
import com.icicle.framework.order.client.criteria.FeedbackCriteria;
import com.icicle.framework.order.client.criteria.FreeTravelCriteria;
import com.icicle.framework.order.client.criteria.WebSiteCommentCriteria;
import com.icicle.framework.order.client.enums.BusTicketSource;
import com.icicle.framework.order.client.models.BusTicketPrice;
import com.icicle.goldenfly.bus.client.criteria.BusTicketPriceCriteria;

public class HotNewsManagerEngine implements HotNewsManagerDriver {

 private HotNewsService hotNewsService;
 private WebSiteCommentService webSiteCommentService;
 private QuestionService questionService;
 private QuestionAnswerService questionAnswerService;
 private FeedbackService feedbackService;
 private BusTicketPriceService busTicketPriceService;
 private FreeTravelService freeTravelService;
 private UploadImageService uploadImageService;
 private FreeTravelOtherService freeTravelOtherService;
 private FreeTravelHotelService freeTravelHotelService;
 private FreeTravelTrafficService freeTravelTrafficService;

 public HotNewsManagerEngine(HotNewsService hotNewsService,
   WebSiteCommentService webSiteCommentService,
   QuestionService questionService,
   QuestionAnswerService questionAnswerService,
   FeedbackService feedbackService,
   BusTicketPriceService busTicketPriceService,
   FreeTravelService freeTravelService,
   UploadImageService uploadImageService,
   FreeTravelOtherService freeTravelOtherService,FreeTravelHotelService freeTravelHotelService,FreeTravelTrafficService freeTravelTrafficService) {
  this.hotNewsService = hotNewsService;
  this.webSiteCommentService = webSiteCommentService;
  this.questionService = questionService;
  this.questionAnswerService = questionAnswerService;
  this.feedbackService = feedbackService;
  this.busTicketPriceService = busTicketPriceService;
  this.freeTravelService = freeTravelService;
  this.uploadImageService = uploadImageService;
  this.freeTravelOtherService = freeTravelOtherService;
  this.freeTravelHotelService = freeTravelHotelService;
  this.freeTravelTrafficService = freeTravelTrafficService;
 }

 public HotNewsManagerEngine() {
  hotNewsService = (HotNewsService) ApplicationContextLoader
    .getApplicationContext().getBean(Constants.HOTNEWS_SERVICE);
  webSiteCommentService = (WebSiteCommentService) ApplicationContextLoader
    .getApplicationContext().getBean(
      Constants.WEBSITE_COMMENT_SERVICE);
  questionService = (QuestionService) ApplicationContextLoader
    .getApplicationContext().getBean(Constants.QUESTION_SERVICE);
  questionAnswerService = (QuestionAnswerService) ApplicationContextLoader
    .getApplicationContext().getBean(
      Constants.QUESTION_ANSWER_SERVICE);
  feedbackService = (FeedbackService) ApplicationContextLoader
    .getApplicationContext().getBean(Constants.FEEDBACK_SERVICE);
  busTicketPriceService = (BusTicketPriceService) ApplicationContextLoader
    .getApplicationContext().getBean(
      Constants.BUSTICKETPRICE_SERVICE);
  freeTravelService = (FreeTravelService) ApplicationContextLoader
    .getApplicationContext().getBean(Constants.FREETRAVEL_SERVICE);
  uploadImageService = (UploadImageService) ApplicationContextLoader
    .getApplicationContext().getBean(Constants.UPLOADIMAGE_SERVICE);
  freeTravelOtherService = (FreeTravelOtherService) ApplicationContextLoader
    .getApplicationContext().getBean(
      Constants.FREETRAVELOTHER_SERVICE);
  freeTravelHotelService = (FreeTravelHotelService) ApplicationContextLoader
  .getApplicationContext().getBean(
    Constants.FREETRAVELHOTEL_SERVICE);
  freeTravelTrafficService = (FreeTravelTrafficService) ApplicationContextLoader
  .getApplicationContext().getBean(
    Constants.FREETRAVELTRAFFIC_SERVICE);
 }

 public ReturnResult<Long> addHotNews(HotNews hotNews, Principal principal)
   throws ClientGenericException {
  Principal.setInstance(principal);
  hotNewsService.save(hotNews);
  return ReturnCode.NORMAL_OKAY.toResult(hotNews.getId());
 }

 public Long countHotNews(HotNewsCriteria criteria, Principal principal)
   throws ClientGenericException {
  Principal.setInstance(principal);
  return hotNewsService.count(criteria, HotNews.class);
 }

 public ReturnResult<?> deleteHotNews(Long newsId, Principal principal)
   throws ClientGenericException {
  Principal.setInstance(principal);
  hotNewsService.deleteHotNews(newsId);
  return ReturnCode.NORMAL_OKAY.toResult();
 }

 public List<HotNews> findHotNews(HotNewsCriteria criteria,
   Principal principal) throws ClientGenericException {
  Principal.setInstance(principal);
  return hotNewsService.find(criteria, HotNews.class);
 }

 public GroupByResult statisticsHotNews(HotNewsCriteria criteria,
   Principal principal) throws ClientGenericException {
  Principal.setInstance(principal);
  return hotNewsService.group(criteria, HotNews.class);
 }

 public ReturnResult<?> updateHotNews(HotNews hotNews, Principal principal)
   throws ClientGenericException {
  Principal.setInstance(principal);
  hotNewsService.update(hotNews);
  return ReturnCode.NORMAL_OKAY.toResult();
 }

 @Override
 public ReturnResult<Long> addWebSiteComment(WebSiteComment comment,
   Principal principal) throws ClientGenericException {
  Principal.setInstance(principal);
  webSiteCommentService.save(comment);
  return ReturnCode.NORMAL_OKAY.toResult(comment.getId());
 }

 @Override
 public Long countWebSiteComment(WebSiteCommentCriteria criteria,
   Principal principal) throws ClientGenericException {
  Principal.setInstance(principal);
  return webSiteCommentService.count(criteria, WebSiteComment.class);
 }

 @Override
 public ReturnResult<?> deleteWebSiteComment(Long commentId,
   Principal principal) throws ClientGenericException {
  Principal.setInstance(principal);
  webSiteCommentService.deleteComment(commentId);
  return ReturnCode.NORMAL_OKAY.toResult();
 }

 @Override
 public List<WebSiteComment> findWebSiteComment(
   WebSiteCommentCriteria criteria, Principal principal)
   throws ClientGenericException {
  Principal.setInstance(principal);
  return webSiteCommentService.find(criteria, WebSiteComment.class);
 }

 @Override
 public GroupByResult statisticsWebSiteComment(
   WebSiteCommentCriteria criteria, Principal principal)
   throws ClientGenericException {
  Principal.setInstance(principal);
  return webSiteCommentService.group(criteria, WebSiteComment.class);

 }

 @Override
 public ReturnResult<?> updateWebSiteComment(WebSiteComment comment,
   Principal principal) throws ClientGenericException {
  Principal.setInstance(principal);
  webSiteCommentService.update(comment);
  return ReturnCode.NORMAL_OKAY.toResult();
 }

 public ReturnResult<Long> addQuestion(Question question, Principal principal)
   throws ClientGenericException {
  Principal.setInstance(principal);
  questionService.save(question);
  return ReturnCode.NORMAL_OKAY.toResult(question.getId());
 }

 public ReturnResult<?> updateQuestion(Question question, Principal principal)
   throws ClientGenericException {
  Principal.setInstance(principal);
  questionService.update(question);
  return ReturnCode.NORMAL_OKAY.toResult();
 }

 public Long countQuestion(QuestionCriteria criteria, Principal principal)
   throws ClientGenericException {
  Principal.setInstance(principal);
  return questionService.count(criteria, Question.class);
 }

 public ReturnResult<?> deleteQuestion(Long questionId, Principal principal)
   throws ClientGenericException {
  Principal.setInstance(principal);
  questionService.deleteQuestion(questionId);
  return ReturnCode.NORMAL_OKAY.toResult();
 }

 public List<Question> findQuestion(QuestionCriteria criteria,
   Boolean lazyLoad, Principal principal)
   throws ClientGenericException {
  Principal.setInstance(principal);
  return questionService.findQuestion(criteria, lazyLoad);
 }

 public List<Question> findRelatedQuestions(Long questionId, Integer number,
   Boolean lazyLoad, Principal principal)
   throws ClientGenericException {
  Principal.setInstance(principal);
  return questionService.findRelatedQuestions(questionId, number,
    lazyLoad);
 }

 public ReturnResult<Long> answerQuestion(Long quesetionId,
   QuestionAnswer answer, Principal principal)
   throws ClientGenericException {
  Principal.setInstance(principal);
  return questionService.answerQuestion(quesetionId, answer);
 }

 public Long countQuestionAnswer(QuestionAnswerCriteria criteria,
   Principal principal) throws ClientGenericException {
  Principal.setInstance(principal);
  return questionAnswerService.count(criteria, QuestionAnswer.class);
 }

 public ReturnResult<?> deleteQuestionAnswer(Long answerId,
   Principal principal) throws ClientGenericException {
  Principal.setInstance(principal);
  questionAnswerService.deleteQuestionAnswer(answerId);
  return ReturnCode.NORMAL_OKAY.toResult();
 }

 public List<QuestionAnswer> findQuestionAnswer(
   QuestionAnswerCriteria criteria, Principal principal)
   throws ClientGenericException {
  Principal.setInstance(principal);
  return questionAnswerService.find(criteria, QuestionAnswer.class);
 }

 @Override
 public ReturnResult<Long> addFeedback(Feedback feedback, Principal principal)
   throws ClientGenericException {
  Principal.setInstance(principal);
  feedbackService.save(feedback);
  return ReturnCode.NORMAL_OKAY.toResult(feedback.getId());
 }

 @Override
 public Long countFeedback(FeedbackCriteria criteria, Principal principal)
   throws ClientGenericException {
  Principal.setInstance(principal);
  return feedbackService.count(criteria, Feedback.class);
 }


 public ReturnResult<Long> addFreeTravel(FreeTravel freeTravel,
   Principal principal) throws ClientGenericException {
  Principal.setInstance(principal);
  freeTravelService.save(freeTravel);
  return ReturnCode.NORMAL_OKAY.toResult(freeTravel.getId());
 }

 public ReturnResult<Long> addUploadImage(UploadImages uploadImage,
   Principal principal) throws ClientGenericException {
  Principal.setInstance(principal);
  uploadImageService.save(uploadImage);
  return ReturnCode.NORMAL_OKAY.toResult(uploadImage.getId());
 }
 
 public ReturnResult<Long> addFreeTravelOther(FreeTravelOther other, Principal principal) throws ClientGenericException
 {
  Principal.setInstance(principal);
  freeTravelOtherService.save(other);
  return ReturnCode.NORMAL_OKAY.toResult(other.getId());
 }
 
 public ReturnResult<Long> addFreeTravelHotel(FreeTravelHotel hotel, Principal principal) throws ClientGenericException
 {
  Principal.setInstance(principal);
  freeTravelHotelService.save(hotel);
  return ReturnCode.NORMAL_OKAY.toResult(hotel.getId());
 }
 
 public ReturnResult<Long> addFreeTravelTraffic(FreeTravelTraffic traffic, Principal principal) throws ClientGenericException
 {
  Principal.setInstance(principal);
  freeTravelTrafficService.save(traffic);
  return ReturnCode.NORMAL_OKAY.toResult(traffic.getId());
 }


 public Long countFreeTravel(FreeTravelCriteria criteria, Principal principal)
   throws ClientGenericException {
  Principal.setInstance(principal);
  return freeTravelService.count(criteria, FreeTravel.class);
 }

 @Override
 public ReturnResult<?> deleteFeedback(Long feedbackId, Principal principal)
   throws ClientGenericException {
  Principal.setInstance(principal);
  feedbackService.deleteFeedback(feedbackId);
  return ReturnCode.NORMAL_OKAY.toResult();
 }


 @Override
 public ReturnResult<?> deleteFreeTravel(Long freeTravelId,
   Principal principal) throws ClientGenericException {
  Principal.setInstance(principal);
  freeTravelService.deleteFreeTravel(freeTravelId);
  return ReturnCode.NORMAL_OKAY.toResult();
 }
 
 public ReturnResult<?> deleteUploadImage(Long imageId, Principal principal) throws ClientGenericException
 {
  Principal.setInstance(principal);
  uploadImageService.deleteUploadImage(imageId);
  return ReturnCode.NORMAL_OKAY.toResult();
 }
 
 public ReturnResult<?> deleteFreeTravelOther(Long otherId, Principal principal) throws ClientGenericException
 {
  Principal.setInstance(principal);
  freeTravelOtherService.deleteFreeTravelOther(otherId);
  return ReturnCode.NORMAL_OKAY.toResult();
 }
 
 public ReturnResult<?> deleteFreeTravelHotel(Long hotelId, Principal principal) throws ClientGenericException
 {
  Principal.setInstance(principal);
  freeTravelHotelService.deleteFreeTravelHotel(hotelId);
  return ReturnCode.NORMAL_OKAY.toResult();
 }

 @Override
 public List<Feedback> findFeedbackByList(FeedbackCriteria criteria,
   Principal principal) throws ClientGenericException {
  Principal.setInstance(principal);
  return feedbackService.find(criteria, Feedback.class);
 }


 public List<FreeTravel> findFreeTravelByList(FreeTravelCriteria criteria,
   Principal principal) throws ClientGenericException {
  Principal.setInstance(principal);
  return freeTravelService.find(criteria, FreeTravel.class);
 }

 public FreeTravel findFreeTravelById(FreeTravel freeTravel, Long freeTravelId ,Principal principal) throws ClientGenericException
 {
  Principal.setInstance(principal);
  return freeTravelService.getById(FreeTravel.class, freeTravelId);
 }
 /**
  * 更新反馈信息
  *
  * @param feedback
  * @param principal
  * @return
  * @throws ClientGenericException
  */
 @Override
 public ReturnResult<?> updateFreeTravel(FreeTravel freeTravel,
   Principal principal) throws ClientGenericException {
  Principal.setInstance(principal);
  freeTravelService.update(freeTravel);
  return ReturnCode.NORMAL_OKAY.toResult();
 }

 @Override
 public ReturnResult<?> updateFeedback(Feedback feedback, Principal principal)
   throws ClientGenericException {
  Principal.setInstance(principal);
  feedbackService.update(feedback);
  return ReturnCode.NORMAL_OKAY.toResult();
 }

 @Override
 public List<BusTicketPrice> findBusTicketPrice(
   BusTicketPriceCriteria criteria, Principal principal)
   throws ClientGenericException {
  Principal.setInstance(principal);
  return busTicketPriceService.find(criteria, BusTicketPrice.class);
 }

 @Override
 public ReturnResult<Integer> addBusTicketPrice(
   BusTicketPrice busTicketPrice, Principal principal)
   throws ClientGenericException {
  Principal.setInstance(principal);
  busTicketPriceService.save(busTicketPrice);
  return ReturnCode.NORMAL_OKAY.toResult(busTicketPrice.getId());
 }

 @Override
 public Long countBusTicketPrice(BusTicketPriceCriteria criteria,
   Principal principal) throws ClientGenericException {
  Principal.setInstance(principal);
  return busTicketPriceService.count(criteria, BusTicketPrice.class);
 }

 @Override
 public ReturnResult<?> deleteBusTicketPrice(Integer id, Principal principal)
   throws ClientGenericException {
  Principal.setInstance(principal);
  BusTicketPrice bean = new BusTicketPrice();
  bean.setId(id);
  busTicketPriceService.delete(bean);
  return ReturnCode.NORMAL_OKAY.toResult();
 }

 @Override
 public ReturnResult<?> updateBusTicketPrice(BusTicketPrice busTicketPrice,
   Principal principal) throws ClientGenericException {
  Principal.setInstance(principal);
  busTicketPriceService.update(busTicketPrice);
  return ReturnCode.NORMAL_OKAY.toResult();
 }

 @Override
 public String batchUpdateBusTicketPrice(
   List<BusTicketPrice> busTicketPriceList, Principal principal) {
  try {
   Principal.setInstance(principal);
   int s = 0, f = 0, t = 0;
   String msg = "";
   for (BusTicketPrice busTicketPrice : busTicketPriceList) {
    BusTicketSource source = busTicketPrice.getSource();
    String fromBusStop = busTicketPrice.getFromBusStop();
    String toBusStop = busTicketPrice.getToBusStop();
    if (source == null || source.getKey() == null) {
     s++;
     continue;
    }
    if (fromBusStop == null || "".equals(fromBusStop)) {
     f++;
     continue;
    }
    if (toBusStop == null || "".equals(toBusStop)) {
     t++;
     continue;
    }
    // 查询
    BusTicketPriceCriteria criteria = new BusTicketPriceCriteria();
    criteria.setSource(SetRestriction.in(source));
    criteria.setFromBusStop(fromBusStop);
    criteria.setToBusStop(toBusStop);
    List<BusTicketPrice> list = busTicketPriceService.find(
      criteria, BusTicketPrice.class);
    if (list != null && list.size() > 0) {
     BusTicketPrice tempBean = list.get(0);
     Integer id = tempBean.getId();
     PropertyUtils.copyProperties(tempBean, busTicketPrice);
     tempBean.setId(id);
     busTicketPriceService.update(tempBean);
    } else {
     busTicketPriceService.save(busTicketPrice);
    }
   }
   if (s > 0) {
    msg += "有 " + s + " 条数据因为没找到 [来源]字段信息导入失败;";
   }
   if (f > 0) {
    msg += "有 " + f + " 条数据因为没找到 [起始站点代码]字段信息导入失败;";
   }
   if (t > 0) {
    msg += "有 " + t + " 条数据因为没找到 [目的站点代码]字段信息导入失败;";
   }
   return msg;
  } catch (Exception e) {
   e.printStackTrace();
   return "程序异常";
  }

 }

}

 

posted on 2011-11-04 17:15 计明敏 阅读(178) 评论(0)  编辑  收藏 所属分类: java


只有注册用户登录后才能发表评论。


网站导航: