打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
银行网点列表生成
package com.sangame.cms.web.action.back.generate;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sangame.cms.constant.OperCodeConstant;
import com.sangame.cms.constant.SgcmsConstant;
import com.sangame.cms.constant.VarConstant;
import com.sangame.cms.constant.EnumConstants.ChannelModelType;
import com.sangame.cms.constant.EnumConstants.OperLogType;
import com.sangame.cms.dto.AffectFile;
import com.sangame.cms.dto.BankConfigDto;
import com.sangame.cms.dto.BankDto;
import com.sangame.cms.generate.DefaultPageUrl;
import com.sangame.cms.generate.GenBankDocListIndex;
import com.sangame.cms.persistence.model.core.Bank;
import com.sangame.cms.persistence.model.core.StepSelect;
import com.sangame.cms.persistence.model.system.Website;
import com.sangame.cms.service.core.BankService;
import com.sangame.cms.service.core.StepSelectService;
import com.sangame.cms.util.SgcmsDoubleUtil;
import freemarker.template.TemplateException;

/**
 * 更新银行网点HTML
 * @author    hasau 
 * 2010-11-3  上午03:51:55
 */
@Namespace("/back/generate/bankDocListPage")
public class BankDocListPageGenerateAction extends GenerateAction {
 
 private static final long serialVersionUID = 1L;
 static Logger log = LoggerFactory.getLogger(BankDocListPageGenerateAction.class);
 static final long TYPE_SPEC = 1;
 static final long TYPE_INDEX = 2;
 static final String BANK_DTOS = "bankDtos";
 static final String BANK_GENERATE_CONFIG_DTO = "bankGenerateConfigDto";
 
 private Long docId;
 private Long cityId;
 private Long bankId;
 private Long countyId;
 private Long provinceId;
 private BankDto bankDto ;
 private int start=0;
 private long generatePercent=0;  //生成百分比
 private String generateTimeCountStr;
 private Long beginGenTime;
 private String mark="bankDocList";
 private Long flag=0L;
 
 private List<Bank> bankList = new ArrayList<Bank>();
 private List<StepSelect> provinceList = new ArrayList<StepSelect>();
 private List<AffectFile> affectFileList = new ArrayList<AffectFile>();
 
 @Resource
 private BankService bankService;
 @Resource
 private StepSelectService stepSelectService;
 
 @Action(INDEX)
 public String index(){ 
 
  if(!hasOper(getCurrentWebsiteId(), OperCodeConstant.OPER_CODE_2_5_8))
      return needPermissionPage();
  bankList = bankService.findAll();
  provinceList = stepSelectService.findSecondStep(1L);  //初始化数据  省市县项 的 id
  return INDEX;
 }
    /**
     *  生成
     * @return
     * @throws TemplateException
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
 @Action("bankDocListGenerate")
 public String bankDocListGenerate() throws IOException, TemplateException {
       
 
      if(!hasOper(getCurrentWebsiteId(), OperCodeConstant.OPER_CODE_2_5_8))
       return needPermissionPage();
      
      generateTitle = "生成银行列表";
      if(beginGenTime == null){   
       beginGenTime = System.currentTimeMillis();
      }
      Website website = getCurrentWebsite();
      forwardPage = this.checkWebsiteExists(website);
      if(forwardPage != null) return forwardPage;
      
      
      /* form中带回的查询条件(bankDto)放置到session中
       * (只在第一次进入生成过程页面前会执行,只在第二次进入生成过程页面前会用到查询条件)。
       * */
      Object object = getSession().getAttribute(BANK_DTOS);
      if(object == null){   
       setRedirectUrl(nextProgressUrl(0));
       getSession().setAttribute(BANK_DTOS, bankDto);
       setSuccessPopMessage("正在查询相关信息,请稍候……");
       return "waiting";
      }
      
      /* 第二次进入生成页面前获取到 session中的组合查询条件(bankDto)
       * 查询出所有的 相关组合  dtoList。生个生成过程只查询一次,放置在session中
       * */
      if(!(object  instanceof List)){
       List<BankDto> dtoList = haveAllDtoList();//第一次查询出所有的 组合
       getSession().setAttribute(BANK_DTOS, dtoList);
       object = dtoList;
      } 
      
      //开始生成
      List<BankDto> dtoList= new ArrayList<BankDto>();  
      dtoList = (List<BankDto>) object;
      Long batchSize=10L;
      if(generateTaskCount==0L){
       if(dtoList.isEmpty()){
        generateTaskCount=0L;
        
       }else{
        
        generateTaskCount = Long.valueOf(dtoList.size()); //总任务
       }
      }
      
      // 分页生成 :  batchSize--每页数量  end:本次的结束索引 下次的开始索引
      int end = start+(batchSize.intValue());
      if(end>generateTaskCount.intValue()){
       end =generateTaskCount.intValue();
      }
       
      BankConfigDto bcDto = getBankConfigDtoInfos();
      
      for(int i=start;i<end;i++){   
       forwardPage = genBankDocListIndex(bcDto,  dtoList.get(i));
       bankService.getEntityDao().getSession().clear(); //clear session
       if(forwardPage != null) return forwardPage;
       generatedTaskCount++;
      }
      if(generateTaskCount!=0L)
       generatePercent = (long) (SgcmsDoubleUtil.round(SgcmsDoubleUtil.divide(generatedTaskCount, generateTaskCount), 4) * 100);
      
      generateTimeCountStr = getElapsedTime(beginGenTime);
      if(generateTaskCount>generatedTaskCount){    //已完成的数量<总数量 继续生成
       setRedirectUrl(nextProgressUrl(end));
       return GENERATE_PROGRESS;
      }else{ // 任务完成   session 中的组合信息 dtoList 清空。 注意不能直接 remove session, 页面上(或右键)点击返回 dtoList不存在会抛异常。
       addLog(OperLogType.GENERATE.getValue(), getUser().getName()+"-"+getCurrentWebsite().getWebsiteName()+"-生成银行列表");
       dtoList.clear();
       getSession().setAttribute(BANK_DTOS, dtoList);
       generatedCount=generatedTaskCount;
      } 
      return GENERATE_RESULT;
//  } catch (Exception e) {
//    getSession().setAttribute(BANK_DTOS, new ArrayList<BankDto>() ); // 生成过程中发生异常  必须清空session中的 组合信息 dtoList。
//    log.error("列表生成异常", e);
//    setFailPopMessage("列表生成异常");
//  }
//  //异常后 返回 组合条件选择页面
//  setRedirectUrl(getContextPath()+"/back/generate/bankDocListPage/index.do");
//  return SHOW_MSG_PAGE;
 }
    
   
    /**
     * 跳转URL  附带本次生成的 相关信息
     * @param end  每次生成的从 dtoList 中获取的起始索引
     * @return
     */
    private String nextProgressUrl(int end) {     
   StringBuffer buf = new StringBuffer();
   buf.append(getContextPath()+"/back/generate/bankDocListPage/bankDocListGenerate.do?");
   buf.append("start=");
   buf.append(SgcmsDoubleUtil.formatDouble(end));
   
   buf.append("&generatedTaskCount=");
   buf.append(SgcmsDoubleUtil.formatDouble(generatedTaskCount));
 
   buf.append("&beginGenTime=");
   buf.append(SgcmsDoubleUtil.formatDouble(beginGenTime));
   
   buf.append("&flag=");
   buf.append(flag);
   return buf.toString();
 }
   
   
    /**
     * 获取所有的 银行 省 市 县 的组合dtoList
     * 用于生成
     * @return
     */
    private List<BankDto> haveAllDtoList(){
     bankDto= (BankDto) getSession().getAttribute(BANK_DTOS);// session 中获取  组合 查询条件
     bankId = bankDto.getBank();         // bankDto 中获取页面查询条件(银行省市县ID,没有选择默认该项Id=0L)
     provinceId = bankDto.getProvince();
     cityId = bankDto.getCity();
     countyId = bankDto.getCounty();
     if(bankId==null || bankId.intValue()==0)  bankId=0L;  
     if(provinceId==null || provinceId.intValue()==0)  provinceId=0L;
     if(cityId==null || cityId.intValue()==0)  cityId=0L;
     if(countyId==null || countyId.intValue()==0)  countyId=0L;
     List<BankDto>  list = null;
     
     if(flag.intValue() == SgcmsConstant.MARK_FLAG_LIST_LIST_3){
      list = haveBankDtoListAllAssembled(-1);// 取所有的组合  
     }else if (flag.intValue() == SgcmsConstant.MARK_FLAG_CITY_LIST_4) {
   list = haveBankDtoListJustBankAndprovince(-1);
  }else {
   throw new IllegalArgumentException("生成类型异常!flag 只能为 3  或者 4"    );
  }
     
     return  list;
    }
 /**
  * 按照 银行 省市县 组合 装载 bankDto
  * @param dtoList 
  * @param bankId    银行ID
  * @param provinceId  省ID
  * @param cityId 市ID
  * @param countyId   县ID
  * @param index   // 文档列表Id索引
  */
 private void loadBankDtoInfos(List<BankDto> dtoList,BankDto dto, Long bankId,Long provinceId, Long cityId, Long countyId, Long index) {
  dto = new BankDto();
  dto.setBank(bankId);
  dto.setProvince(provinceId);
  dto.setCity(cityId);
  dto.setCounty(countyId);
  dto.setModelType(ChannelModelType.BANK.getValue());
  dto.setDocId(index);
  dtoList.add(dto);
 }  
   
 
 /**
  * @param limit  <=0 无限制 所有的银行省市县组合   >0 例10  仅取10个组合
  * @return
  */
 private List<BankDto> haveBankDtoListAllAssembled(int  limit){
  List<Long> bankList = new ArrayList<Long>();
  if(bankId.intValue() ==0){
   bankList.addAll(bankService.findAllBankIds());
   bankList.add(0L);
  }else{
   bankList.add(bankId);
  }
 
  // 获取要生成的文件信息的查询条件(bankDto)只展示10个
  List<BankDto> dtoList = new ArrayList<BankDto>();  
  BankDto dto = null;
  Long count =0L;
  for(Long i:bankList){     //银行
   List<Long> provinceList= new ArrayList<Long>();
   if(provinceId.intValue() ==0){
    provinceList.addAll(stepSelectService.findAllProvinceIds());
    provinceList.add(0L);
 
   }else{
    provinceList.add(provinceId);
   }   
   for(Long j: provinceList){ //  省
    List<Long> cityList = new ArrayList<Long>();
    if(j.intValue()==0){
     cityList.add(0L);
    }else{     
     if(cityId.intValue() ==0){
      cityList.addAll(stepSelectService.findSonIdsByFatherId(j));
      cityList.add(0L);
     }else{
      cityList.add(cityId); 
     }
    }     
    for(Long k: cityList){ //  市
     List<Long> countyList = new ArrayList<Long>();
     if(k.intValue()==0){
      countyList.add(0L);
     }else {      
      if(countyId.intValue() ==0){
       countyList = stepSelectService.findSonIdsByFatherId(k);
       countyList.add(0L);
      }else {
       countyList.add(countyId);
      }     
     }
     if(countyList.isEmpty()){
      loadBankDtoInfos(dtoList,dto, i, j, k, 0L, count);
      count++;      
      if(limit > 0)
       if(count>limit-1) break;
     }else{
      for(Long m: countyList){
       loadBankDtoInfos(dtoList,dto, i, j, k, m, count);
       count++;
       if(limit > 0)
        if(count>limit-1) break;
      }      
     }
     if(limit > 0)
      if(count>limit-1) break;
    }
    if(limit > 0)
     if(count>limit-1) break;
   } 
   if(limit > 0)
    if(count>limit-1) break;
  }
  return dtoList;
 }
 
 /**
  * @param limit  <=0 无限制 所有的 银行 省   组合   >0 例10  仅取10个组合
  * @return
  */
 private List<BankDto> haveBankDtoListJustBankAndprovince(int  limit){
  List<Long> bankList = new ArrayList<Long>();
  if(bankId.intValue() ==0){
   bankList.addAll(bankService.findAllBankIds());
  }else{
   bankList.add(bankId);
  }
 
  // 获取要生成的文件信息的查询条件(bankDto)只展示10个
  List<BankDto> dtoList = new ArrayList<BankDto>();  
  BankDto dto = null;
  Long count =0L;
  for(Long i:bankList){     //银行
   List<Long> provinceList= new ArrayList<Long>();
   if(provinceId.intValue() ==0){
    provinceList.addAll(stepSelectService.findAllProvinceIds());
   }else{
    provinceList.add(provinceId);
   }
   for(Long j : provinceList){
    loadBankDtoInfos(dtoList,dto, i, j, null, null, count);
    count++;      
    if(limit > 0)
     if(count>limit-1) break;
   }     
   if(limit > 0)
    if(count>limit-1) break;
  }
  return dtoList;
 }
 
 
 /**
  * 生成列表展示! 只显示10个
  * @return
  */
 @Action("generatePageList")
 public String generatePageList() {
 
  if(!hasOper(getCurrentWebsiteId(), OperCodeConstant.OPER_CODE_2_5_8))
      return needPermissionPage();
  getSession().setAttribute(BANK_DTOS, null );
  getSession().setAttribute(BANK_GENERATE_CONFIG_DTO,getBankConfigDtoFromSystemConfig(flag.intValue()));
  
  if(flag.intValue() == SgcmsConstant.MARK_FLAG_LIST_LIST_3){   
   haveAffectFileList(haveBankDtoListAllAssembled(10)); //取10个 银行省市县 列表组合
  }else if (flag.intValue() == SgcmsConstant.MARK_FLAG_CITY_LIST_4) {
   haveAffectFileList(haveBankDtoListJustBankAndprovince(10));
  }else {
   throw new IllegalArgumentException("生成类型异常!flag 只能为 3  或者 4"    );
  }
  return LIST;
 }
 
 
 private void haveAffectFileList(List<BankDto> dtoList){
  //按查询信息(bankdto) 转化成页面展示所需的的AffectFile对象
  affectFileList = new ArrayList<AffectFile>();   
  AffectFile affectFile = null;
  Bank bank = null;
  StepSelect  province = null;  
  StepSelect  city = null; 
  StepSelect  county = null; 
  
  for(BankDto bd :dtoList){
   affectFile = new AffectFile();
   affectFile.setType(flag);
   affectFile.setDocId(bd.getDocId() );
   affectFile.setBankDto(bd);
   affectFile.setPageUrl(SgcmsConstant.BACK_BANK_DOC_LIST_INDEX_PREVIEW_PAGINATION_URL.replaceAll(VarConstant.escape(VarConstant.PAGE), "1"));
   //组合文件名称 例:农行 北京市 东城 东城1区 银行列表页
   if(bd.getBank() != null && bd.getBank().intValue()!=0)
    bank = bankService.find(bd.getBank());
   if(bd.getProvince() != null && bd.getProvince().intValue()!=0)
    province = stepSelectService.find(bd.getProvince());
   if(bd.getCity() != null && bd.getCity().intValue()!=0)
    city = stepSelectService.find(bd.getCity());
   if(bd.getCounty() != null && bd.getCounty().intValue()!=0)
    county = stepSelectService.find(bd.getCounty());
  
   StringBuffer sb = new StringBuffer();   
   if(bank!=null) sb.append(bank.getBankShortName()+" ");
   if(province!=null) sb.append(province.getDataName()+" ");
   if(city!=null) sb.append(city.getDataName()+" ");
   if(county!=null) sb.append(county.getDataName()+" ");
   
   if(flag.intValue() == SgcmsConstant.MARK_FLAG_LIST_LIST_3){   
    affectFile.setPageName(sb.toString()+"银行列表页");
   }else if (flag.intValue() == SgcmsConstant.MARK_FLAG_CITY_LIST_4) {
    affectFile.setPageName(sb.toString()+"市列表页");
   }else {
    throw new IllegalArgumentException("生成类型异常!flag 只能为 3  或者 4"    );
   }
   affectFileList.add(affectFile);
  }
 }
 
 
 
 
 @Action("preview")
 public String preview() throws Exception {
  if(log.isDebugEnabled()) log.debug("start bankDocListIndex preview");
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  
  GenBankDocListIndex gen = new GenBankDocListIndex(this.getFreemarkerConfiguration(), getBankConfigDtoInfos() ,bankDto);
  gen.setStreamUrl(new DefaultPageUrl(SgcmsConstant.BACK_BANK_DOC_LIST_INDEX_PREVIEW_PAGINATION_URL.replaceAll(VarConstant.escape(VarConstant.CMS_DOMAIN), getContextPath())));
  outputStream = gen.genStream(Long.valueOf(getCurrentPage()));
  getResponse().getOutputStream().write(outputStream.toByteArray());
  
  if(log.isDebugEnabled()) log.debug("end bankDocListIndex preview");
  
  return null;
 }
 
 private BankConfigDto getBankConfigDtoInfos(){
  Object obj = getSession().getAttribute(BANK_GENERATE_CONFIG_DTO);
  BankConfigDto bcDto = null;
  if(obj == null){
   bcDto = getBankConfigDtoFromSystemConfig(flag.intValue());
   getSession().setAttribute(BANK_GENERATE_CONFIG_DTO, bcDto);
  }else {
   bcDto = (BankConfigDto) obj;
  }
  return bcDto;
 }
 
 
 public List<Bank> getBankList() {
  return bankList;
 }
 public void setBankList(List<Bank> bankList) {
  this.bankList = bankList;
 }
 public List<StepSelect> getProvinceList() {
  return provinceList;
 }
 public void setProvinceList(List<StepSelect> provinceList) {
  this.provinceList = provinceList;
 }
 public List<AffectFile> getAffectFileList() {
  return affectFileList;
 }
 public void setAffectFileList(List<AffectFile> affectFileList) {
  this.affectFileList = affectFileList;
 }
 public Long getDocId() {
  return docId;
 }
 public void setDocId(Long docId) {
  this.docId = docId;
 }
 public BankDto getBankDto() {
  return bankDto;
 }
 public void setBankDto(BankDto bankDto) {   
  this.bankDto = bankDto;
 }
 public Long getCityId() {
  return cityId;
 }
 public void setCityId(Long cityId) {
  this.cityId = cityId;
 }
 public Long getBankId() {
  return bankId;
 }
 public void setBankId(Long bankId) {
  this.bankId = bankId;
 }
 public Long getCountyId() {
  return countyId;
 }
 public void setCountyId(Long countyId) {
  this.countyId = countyId;
 }
 public Long getProvinceId() {
  return provinceId;
 }
 public void setProvinceId(Long provinceId) {
  this.provinceId = provinceId;
 }
 public long getGeneratePercent() {
  return generatePercent;
 }
 public void setGeneratePercent(long generatePercent) {
  this.generatePercent = generatePercent;
 }
 public int getStart() {
  return start;
 }
 public void setStart(int start) {
  this.start = start;
 }
 

 public String getGenerateTimeCountStr() {
  return generateTimeCountStr;
 }
 public void setGenerateTimeCountStr(String generateTimeCountStr) {
  this.generateTimeCountStr = generateTimeCountStr;
 }
 public Long getBeginGenTime() {
  return beginGenTime;
 }
 public void setBeginGenTime(Long beginGenTime) {
  this.beginGenTime = beginGenTime;
 }
 public String getMark() {
  return mark;
 }
 public void setMark(String mark) {
  this.mark = mark;
 }
 public Long getFlag() {
  return flag;
 }
 public void setFlag(Long flag) {
  this.flag = flag;
 }
 

}
 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
基于Spring实现领域模型模式 - RUP实践者指南 - JavaEye技术网站
用jsp实现一个简单的购物车web应用系统。
CXF方式发布WebService全步骤
C#中ref和out的区别
Jbpm流程图显示
VC 笔记: unicode、ascii、16进制bytedata 互转
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服