1 package com.cn.elec.service.impl; 2 import java.util.HashMap; 3 import java.util.List; 4 import java.util.Map; 5 6 import javax.annotation.Resource; 7 import org.springframework.stereotype.Service; 8 9 10 import com.cn.elec.dao.ElecRunCommonMsgDao; 11 import com.cn.elec.dao.IElecRunSituationDao; 12 import com.cn.elec.domain.ElecRunCommonMsg; 13 import com.cn.elec.domain.ElecRunSituation; 14 import com.cn.elec.service.ElecRunSituationService; 15 import com.cn.elec.utils.StringUtils; 16 17 @Service(ElecRunSituationService.SERVICE_NAME) 18 public class ElecRunSituationServiceImpl implements ElecRunSituationService { 19 @Resource(name=IElecRunSituationDao.DAO_NAME) 20 private IElecRunSituationDao dao; 21 22 @Resource(name=ElecRunCommonMsgDao.DAO_NAME) 23 private ElecRunCommonMsgDao msg_dao; 24 25 26 public void saveRunMeseage(ElecRunSituation entity) { 27 //1. 清空碎片表里面所有的数据 28 List<ElecRunCommonMsg> list=msg_dao.findCollectionByConditionNoPage("", null, null); 29 msg_dao.deleteByCollection(list); 30 31 //2. 将站点运行情况和设备运行情况切片 32 String station=entity.getSituationRun(); 33 String devRun=entity.getDevRun(); 34 List<String> station_list=StringUtils.getContentByList(station, 20); 35 List<String> devRun_list=StringUtils.getContentByList(devRun, 20); 36 //3. 保存切片的数据 37 //保存站点的运行情况 38 for (int i = 0; i <station_list.size(); i++) { 39 ElecRunCommonMsg msgg=new ElecRunCommonMsg(); 40 41 msgg.setContent(station_list.get(i)); 42 msgg.setType("1"); 43 msgg.setOrderby(i+1); 44 msg_dao.save(msgg); 45 } 46 47 //3. 保存切片的数据 48 //保存站点的运行情况 49 for (int i = 0; i <devRun_list.size(); i++) { 50 ElecRunCommonMsg msge=new ElecRunCommonMsg(); 51 52 msge.setContent(devRun_list.get(i)); 53 msge.setType("2"); 54 msge.setOrderby(i+1); 55 msg_dao.save(msge); 56 } 57 //4. 更新或者保存ElecCommonMsg 58 dao.saveRunMeseage(entity); 59 } 60 61 public ElecRunSituation findRunMesage() { 62 //return dao.findRunMesage(); 63 //1. 查询所有的切片数据(分类型查询) 64 65 String condition = " and o.type = ?"; 66 Object[] params = new Object[]{"1"}; 67 Map<String,String> orderby = new HashMap<String, String>(); 68 orderby.put("o.orderby", "asc"); 69 List<ElecRunCommonMsg> list = 70 msg_dao.findCollectionByConditionNoPage(condition, params, orderby); 71 72 String station_content = ""; //站点的运行情况 73 for(ElecRunCommonMsg content : list) 74 { 75 station_content = station_content + content.getContent(); 76 } 77 78 //查询设备的运行情况 79 condition = " and o.type = ?"; 80 params = new Object[]{"2"}; 81 orderby = new HashMap<String, String>(); 82 orderby.put("o.orderby", "asc"); 83 list = 84 msg_dao.findCollectionByConditionNoPage(condition, params, orderby); 85 86 String dev_content = ""; //设备的运行情况 87 for(ElecRunCommonMsg content : list) 88 { 89 dev_content = dev_content + content.getContent(); 90 } 91 92 93 ElecRunSituation msg =dao.findRunMesage(); 94 if(null != msg) 95 { 96 //3. 设置站点的运行情况和设备的运行情况 97 msg.setDevRun(dev_content); 98 msg.setSituationRun(station_content); 99 100 return msg; 101 } 102 103 return null; 104 } 105 }
字符串的工具类
1 package com.cn.elec.utils; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 public class StringUtils { 7 8 /** 9 * @param wholecontent:传递的文本字符串 10 * @param cutcount:需要分割的字符串的长度 11 * @return,分割后的List集合,存放结果集 12 */ 13 14 public static List<String> getContentByList(String wholecontent,int cutcontent){ 15 16 List<String> list=new ArrayList<String>(); 17 //获取完整内容字符串的总长度 18 int contentlen = wholecontent.length(); 19 System.out.println(contentlen); 20 //内容截取,用内容总长和截取长度进行比较,无须截取的话直接插入 21 if (contentlen<cutcontent) { 22 list.add(wholecontent); 23 } 24 //内容长度超过截取长度 25 else{ 26 27 //定义并初始化内容段落 28 String contentpart=""; 29 //定义并初始化被截取的段落数量 30 int contentcount=0; 31 //开始截取的位置 32 int begincount=0; 33 //判断截取的段落数 34 int contentcutpart=contentlen/cutcontent; 35 int contentcutpars=contentlen%cutcontent;//求余数 36 //若余数为0,说明被整除,内容的长度正好是截取长度的倍数。 37 if (contentcutpars==0) { 38 contentcount=contentcutpart; 39 } else { 40 contentcount=contentcutpart+1; 41 } 42 //循环截取内容 43 for (int i = 1; i <=contentcount; i++) { 44 if (i!=contentcount) { 45 //按照截断长度截取内容 46 contentpart=wholecontent.substring(begincount,cutcontent*i); 47 }else{ 48 49 //截取最后一部分内容 50 contentpart=wholecontent.substring(begincount, contentlen); 51 } 52 //赋值下一截取部分的起点位置 53 begincount=cutcontent*i; 54 System.out.println(begincount); 55 56 57 list.add(contentpart); 58 59 } 60 61 } 62 63 return list; 64 } 65 66 测试 67 /*public static void main(String[] args) { 68 List list=getContentByList("slflsdfapif",3); 69 for (Object object : list) { 70 System.out.println(object); 71 } 72 System.out.println(); 73 }*/ 74 }