Commit b6518a3fca1d9bf21c7d55e80ae25f62d70764cc
Merge branch 'pudong_jdk8' of http://101.95.0.106:8888/panzhaov5/bsth_control into pudong_jdk8
Showing
7 changed files
with
2265 additions
and
2243 deletions
Too many changes to show.
To preserve performance only 7 of 10 files are displayed.
src/main/java/com/bsth/data/directive/DirectivesPstThread.java
| 1 | -package com.bsth.data.directive; | |
| 2 | - | |
| 3 | -import com.alibaba.fastjson.JSON; | |
| 4 | -import com.bsth.data.schedule.DayOfSchedule; | |
| 5 | -import com.bsth.entity.directive.D60; | |
| 6 | -import com.bsth.entity.directive.D64; | |
| 7 | -import com.bsth.entity.directive.Directive; | |
| 8 | -import com.bsth.repository.directive.D60Repository; | |
| 9 | -import com.bsth.repository.directive.D64Repository; | |
| 10 | -import org.joda.time.format.DateTimeFormat; | |
| 11 | -import org.joda.time.format.DateTimeFormatter; | |
| 12 | -import org.slf4j.Logger; | |
| 13 | -import org.slf4j.LoggerFactory; | |
| 14 | -import org.springframework.beans.factory.annotation.Autowired; | |
| 15 | -import org.springframework.jdbc.core.BatchPreparedStatementSetter; | |
| 16 | -import org.springframework.jdbc.core.JdbcTemplate; | |
| 17 | -import org.springframework.jdbc.datasource.DataSourceTransactionManager; | |
| 18 | -import org.springframework.stereotype.Component; | |
| 19 | -import org.springframework.transaction.TransactionDefinition; | |
| 20 | -import org.springframework.transaction.TransactionStatus; | |
| 21 | -import org.springframework.transaction.support.DefaultTransactionDefinition; | |
| 22 | - | |
| 23 | -import java.sql.PreparedStatement; | |
| 24 | -import java.sql.SQLException; | |
| 25 | -import java.sql.Types; | |
| 26 | -import java.util.ArrayList; | |
| 27 | -import java.util.List; | |
| 28 | -import java.util.concurrent.ConcurrentLinkedQueue; | |
| 29 | - | |
| 30 | -/** | |
| 31 | - * 指令持久化线程 | |
| 32 | - * Created by panzhao on 2017/3/6. | |
| 33 | - */ | |
| 34 | -@Component | |
| 35 | -public class DirectivesPstThread extends Thread { | |
| 36 | - | |
| 37 | - Logger logger = LoggerFactory.getLogger(this.getClass()); | |
| 38 | - | |
| 39 | - @Autowired | |
| 40 | - D60Repository d60Repository; | |
| 41 | - | |
| 42 | - @Autowired | |
| 43 | - D64Repository d64Repository; | |
| 44 | - | |
| 45 | - @Autowired | |
| 46 | - DayOfSchedule dayOfSchedule; | |
| 47 | - | |
| 48 | - @Autowired | |
| 49 | - JdbcTemplate jdbcTemplate; | |
| 50 | - | |
| 51 | - private static DateTimeFormatter fmtyyyyMMdd = DateTimeFormat.forPattern("yyyy-MM-dd"); | |
| 52 | - | |
| 53 | - @Override | |
| 54 | - public void run() { | |
| 55 | - try{ | |
| 56 | - ConcurrentLinkedQueue<Directive> list = DayOfDirectives.pstDirectives; | |
| 57 | - | |
| 58 | - List<D60> d60s = new ArrayList<>(); | |
| 59 | - List<D64> d64s = new ArrayList<>(); | |
| 60 | - //按 60 和 64 分组 | |
| 61 | - Directive directive; | |
| 62 | - D60 d60; | |
| 63 | - for (int i = 0; i < 2000; i++) { | |
| 64 | - directive = list.poll(); | |
| 65 | - if(null == directive) | |
| 66 | - break; | |
| 67 | - | |
| 68 | - //日期 | |
| 69 | - directive.setRq(fmtyyyyMMdd.print(directive.getTimestamp())); | |
| 70 | - | |
| 71 | - if (directive instanceof D60) { | |
| 72 | - d60 = (D60) directive; | |
| 73 | - if(isDelete(d60)) | |
| 74 | - continue; | |
| 75 | - d60s.add(d60); | |
| 76 | - } | |
| 77 | - else if(directive instanceof D64) | |
| 78 | - d64s.add((D64) directive); | |
| 79 | - } | |
| 80 | - | |
| 81 | - //入库60 | |
| 82 | - save60(d60s); | |
| 83 | - //入库64 | |
| 84 | - save64(d64s); | |
| 85 | - | |
| 86 | - | |
| 87 | - // 60 指令更新(车载响应) | |
| 88 | - ConcurrentLinkedQueue<D60> updateD60s = DayOfDirectives.pstD60s; | |
| 89 | - d60s = new ArrayList<>(); | |
| 90 | - for (int i = 0; i < 2000; i++) { | |
| 91 | - d60 = updateD60s.poll(); | |
| 92 | - if(null == d60) | |
| 93 | - break; | |
| 94 | - d60s.add(d60); | |
| 95 | - } | |
| 96 | - | |
| 97 | - if(d60s.size() > 0) | |
| 98 | - update60(d60s); | |
| 99 | - }catch (Exception e){ | |
| 100 | - logger.error("指令入库出现异常", e); | |
| 101 | - } | |
| 102 | - } | |
| 103 | - | |
| 104 | - private void save64(final List<D64> d64s) { | |
| 105 | - if(null == d64s || d64s.size() == 0) | |
| 106 | - return; | |
| 107 | - | |
| 108 | - String sql = "insert into bsth_v_directive_64(device_id,error_text,http_code,oper_code,rq,sender,timestamp,city_code,line_id,txt_content,resp_ack) " + | |
| 109 | - " values(?,?,?,?,?,?,?,?,?,?,?)"; | |
| 110 | - | |
| 111 | - //编程式事务 | |
| 112 | - DataSourceTransactionManager tran = new DataSourceTransactionManager(jdbcTemplate.getDataSource()); | |
| 113 | - DefaultTransactionDefinition def = new DefaultTransactionDefinition(); | |
| 114 | - def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); | |
| 115 | - TransactionStatus status = tran.getTransaction(def); | |
| 116 | - | |
| 117 | - try{ | |
| 118 | - jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() { | |
| 119 | - @Override | |
| 120 | - public void setValues(PreparedStatement ps, int i) throws SQLException { | |
| 121 | - D64 d64 = d64s.get(i); | |
| 122 | - ps.setString(1 , d64.getDeviceId()); | |
| 123 | - ps.setString(2, isNvl(d64.getErrorText())); | |
| 124 | - ps.setInt(3, d64.getHttpCode()); | |
| 125 | - ps.setShort(4, isNvl(d64.getOperCode())); | |
| 126 | - ps.setString(5, d64.getRq()); | |
| 127 | - | |
| 128 | - ps.setString(6, isNvl(d64.getSender())); | |
| 129 | - ps.setLong(7, d64.getTimestamp()); | |
| 130 | - | |
| 131 | - ps.setShort(8, isNvl(d64.getData().getCityCode())); | |
| 132 | - ps.setString(9, isNvl(d64.getData().getLineId())); | |
| 133 | - ps.setString(10, isNvl(d64.getData().getTxtContent())); | |
| 134 | - ps.setShort(11, isNvl(d64.getRespAck())); | |
| 135 | - } | |
| 136 | - | |
| 137 | - @Override | |
| 138 | - public int getBatchSize() { | |
| 139 | - return d64s.size(); | |
| 140 | - } | |
| 141 | - }); | |
| 142 | - | |
| 143 | - tran.commit(status); | |
| 144 | - | |
| 145 | - logger.info("64 入库成功: " + d64s.size()); | |
| 146 | - }catch (Exception e){ | |
| 147 | - tran.rollback(status); | |
| 148 | - logger.error("", e); | |
| 149 | - logger.warn("失败的数据:" + JSON.toJSONString(d64s)); | |
| 150 | - } | |
| 151 | - } | |
| 152 | - | |
| 153 | - private void update60(final List<D60> d60s) { | |
| 154 | - if(null == d60s || d60s.size() == 0) | |
| 155 | - return; | |
| 156 | - | |
| 157 | - String sql = "update bsth_v_directive_60 set reply46=?,reply46time=?,reply47=?,reply47time=? where device_id=? and timestamp=? and msg_id=?"; | |
| 158 | - | |
| 159 | - //编程式事务 | |
| 160 | - DataSourceTransactionManager tran = new DataSourceTransactionManager(jdbcTemplate.getDataSource()); | |
| 161 | - DefaultTransactionDefinition def = new DefaultTransactionDefinition(); | |
| 162 | - def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); | |
| 163 | - TransactionStatus status = tran.getTransaction(def); | |
| 164 | - | |
| 165 | - try{ | |
| 166 | - jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() { | |
| 167 | - @Override | |
| 168 | - public void setValues(PreparedStatement ps, int i) throws SQLException { | |
| 169 | - D60 d60 = d60s.get(i); | |
| 170 | - ps.setShort(1, isNvl(d60.getReply46())); | |
| 171 | - if(null == d60.getReply46Time()) | |
| 172 | - ps.setNull(2, Types.BIGINT); | |
| 173 | - else | |
| 174 | - ps.setLong(2, d60.getReply46Time()); | |
| 175 | - | |
| 176 | - ps.setShort(3, isNvl(d60.getReply47())); | |
| 177 | - | |
| 178 | - if(null == d60.getReply47Time()) | |
| 179 | - ps.setNull(4, Types.BIGINT); | |
| 180 | - else | |
| 181 | - ps.setLong(4, d60.getReply47Time()); | |
| 182 | - ps.setString(5, d60.getDeviceId()); | |
| 183 | - ps.setLong(6, d60.getTimestamp()); | |
| 184 | - ps.setInt(7, d60.getMsgId()); | |
| 185 | - } | |
| 186 | - | |
| 187 | - @Override | |
| 188 | - public int getBatchSize() { | |
| 189 | - return d60s.size(); | |
| 190 | - } | |
| 191 | - }); | |
| 192 | - | |
| 193 | - tran.commit(status); | |
| 194 | - | |
| 195 | - logger.info("60 更新成功: " + d60s.size()); | |
| 196 | - }catch (Exception e){ | |
| 197 | - tran.rollback(status); | |
| 198 | - logger.error("", e); | |
| 199 | - logger.warn("失败的数据:" + JSON.toJSONString(d60s)); | |
| 200 | - } | |
| 201 | - } | |
| 202 | - | |
| 203 | - private void save60(final List<D60> d60s) { | |
| 204 | - if(null == d60s || d60s.size() == 0) | |
| 205 | - return; | |
| 206 | - | |
| 207 | - String sql = "insert into bsth_v_directive_60(device_id,error_text,http_code,oper_code,rq,sender,timestamp" + | |
| 208 | - ",alarm_time,company_code,dispatch_instruct,instruct_type,msg_id,service_state,txt_content,is_dispatch" + | |
| 209 | - ",line_code,reply46,reply46time,reply47,reply47time,sch) " + | |
| 210 | - " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; | |
| 211 | - | |
| 212 | - //编程式事务 | |
| 213 | - DataSourceTransactionManager tran = new DataSourceTransactionManager(jdbcTemplate.getDataSource()); | |
| 214 | - DefaultTransactionDefinition def = new DefaultTransactionDefinition(); | |
| 215 | - def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); | |
| 216 | - TransactionStatus status = tran.getTransaction(def); | |
| 217 | - | |
| 218 | - try{ | |
| 219 | - jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() { | |
| 220 | - @Override | |
| 221 | - public void setValues(PreparedStatement ps, int i) throws SQLException { | |
| 222 | - D60 d60 = d60s.get(i); | |
| 223 | - ps.setString(1, d60.getDeviceId()); | |
| 224 | - ps.setString(2, isNvl(d60.getErrorText())); | |
| 225 | - ps.setInt(3, d60.getHttpCode()); | |
| 226 | - ps.setShort(4, d60.getOperCode()); | |
| 227 | - ps.setString(5, d60.getRq()); | |
| 228 | - ps.setString(6, d60.getSender()); | |
| 229 | - ps.setLong(7, d60.getTimestamp()); | |
| 230 | - | |
| 231 | - ps.setLong(8, isNvl(d60.getData().getAlarmTime())); | |
| 232 | - ps.setShort(9, isNvl(d60.getData().getCompanyCode())); | |
| 233 | - ps.setShort(10, isNvl(d60.getData().getDispatchInstruct())); | |
| 234 | - ps.setInt(11, d60.getData().getInstructType()); | |
| 235 | - ps.setInt(12, d60.getData().getMsgId()); | |
| 236 | - ps.setLong(13, d60.getData().getServiceState()); | |
| 237 | - ps.setString(14, d60.getData().getTxtContent()); | |
| 238 | - ps.setBoolean(15, d60.isDispatch()); | |
| 239 | - | |
| 240 | - ps.setString(16, isNvl(d60.getLineCode())); | |
| 241 | - ps.setShort(17, isNvl(d60.getReply46())); | |
| 242 | - | |
| 243 | - if(null == d60.getReply46Time()) | |
| 244 | - ps.setNull(18, Types.BIGINT); | |
| 245 | - else | |
| 246 | - ps.setLong(18, d60.getReply46Time()); | |
| 247 | - | |
| 248 | - ps.setShort(19, isNvl(d60.getReply47())); | |
| 249 | - | |
| 250 | - if(null == d60.getReply47Time()) | |
| 251 | - ps.setNull(20, Types.BIGINT); | |
| 252 | - else | |
| 253 | - ps.setLong(20, d60.getReply47Time()); | |
| 254 | - | |
| 255 | - if(d60.getSch()==null) | |
| 256 | - ps.setNull(21, Types.BIGINT); | |
| 257 | - else | |
| 258 | - ps.setLong(21, d60.getSch().getId()); | |
| 259 | - } | |
| 260 | - | |
| 261 | - @Override | |
| 262 | - public int getBatchSize() { | |
| 263 | - return d60s.size(); | |
| 264 | - } | |
| 265 | - }); | |
| 266 | - | |
| 267 | - tran.commit(status); | |
| 268 | - | |
| 269 | - logger.info("60 入库成功: " + d60s.size()); | |
| 270 | - }catch (Exception e){ | |
| 271 | - tran.rollback(status); | |
| 272 | - logger.error("", e); | |
| 273 | - logger.warn("失败的数据:" + JSON.toJSONString(d60s)); | |
| 274 | - } | |
| 275 | - } | |
| 276 | - | |
| 277 | - private String isNvl(String v) { | |
| 278 | - return v==null?"":v; | |
| 279 | - } | |
| 280 | - | |
| 281 | - private short isNvl(Short v) { | |
| 282 | - return v==null?0:v; | |
| 283 | - } | |
| 284 | - | |
| 285 | - private long isNvl(Long v) { | |
| 286 | - return v==null?0:v; | |
| 287 | - } | |
| 288 | - | |
| 289 | - private boolean isDelete(D60 d60){ | |
| 290 | - try{ | |
| 291 | - //如果关联的班次已经不存在了,放弃入库,很低概率出现 | |
| 292 | - if(d60.isDispatch() && d60.getSch().isDeleted()){ | |
| 293 | - logger.warn("save 指令,发现 deleted=true 的班次,id=" + d60.getSch().getId()); | |
| 294 | - return true; | |
| 295 | - } | |
| 296 | - }catch (Exception e){ | |
| 297 | - logger.error("", e); | |
| 298 | - } | |
| 299 | - | |
| 300 | - return false; | |
| 301 | - } | |
| 302 | -} | |
| 1 | +package com.bsth.data.directive; | |
| 2 | + | |
| 3 | +import com.alibaba.fastjson.JSON; | |
| 4 | +import com.bsth.data.schedule.DayOfSchedule; | |
| 5 | +import com.bsth.entity.directive.D60; | |
| 6 | +import com.bsth.entity.directive.D64; | |
| 7 | +import com.bsth.entity.directive.Directive; | |
| 8 | +import com.bsth.repository.directive.D60Repository; | |
| 9 | +import com.bsth.repository.directive.D64Repository; | |
| 10 | +import org.joda.time.format.DateTimeFormat; | |
| 11 | +import org.joda.time.format.DateTimeFormatter; | |
| 12 | +import org.slf4j.Logger; | |
| 13 | +import org.slf4j.LoggerFactory; | |
| 14 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 15 | +import org.springframework.jdbc.core.BatchPreparedStatementSetter; | |
| 16 | +import org.springframework.jdbc.core.JdbcTemplate; | |
| 17 | +import org.springframework.jdbc.datasource.DataSourceTransactionManager; | |
| 18 | +import org.springframework.stereotype.Component; | |
| 19 | +import org.springframework.transaction.TransactionDefinition; | |
| 20 | +import org.springframework.transaction.TransactionStatus; | |
| 21 | +import org.springframework.transaction.support.DefaultTransactionDefinition; | |
| 22 | + | |
| 23 | +import java.sql.PreparedStatement; | |
| 24 | +import java.sql.SQLException; | |
| 25 | +import java.sql.Types; | |
| 26 | +import java.util.ArrayList; | |
| 27 | +import java.util.List; | |
| 28 | +import java.util.concurrent.ConcurrentLinkedQueue; | |
| 29 | + | |
| 30 | +/** | |
| 31 | + * 指令持久化线程 | |
| 32 | + * Created by panzhao on 2017/3/6. | |
| 33 | + */ | |
| 34 | +@Component | |
| 35 | +public class DirectivesPstThread extends Thread { | |
| 36 | + | |
| 37 | + Logger logger = LoggerFactory.getLogger(this.getClass()); | |
| 38 | + | |
| 39 | + @Autowired | |
| 40 | + D60Repository d60Repository; | |
| 41 | + | |
| 42 | + @Autowired | |
| 43 | + D64Repository d64Repository; | |
| 44 | + | |
| 45 | + @Autowired | |
| 46 | + DayOfSchedule dayOfSchedule; | |
| 47 | + | |
| 48 | + @Autowired | |
| 49 | + JdbcTemplate jdbcTemplate; | |
| 50 | + | |
| 51 | + private static DateTimeFormatter fmtyyyyMMdd = DateTimeFormat.forPattern("yyyy-MM-dd"); | |
| 52 | + | |
| 53 | + @Override | |
| 54 | + public void run() { | |
| 55 | + try{ | |
| 56 | + ConcurrentLinkedQueue<Directive> list = DayOfDirectives.pstDirectives; | |
| 57 | + | |
| 58 | + List<D60> d60s = new ArrayList<>(); | |
| 59 | + List<D64> d64s = new ArrayList<>(); | |
| 60 | + //按 60 和 64 分组 | |
| 61 | + Directive directive; | |
| 62 | + D60 d60; | |
| 63 | + for (int i = 0; i < 2000; i++) { | |
| 64 | + directive = list.poll(); | |
| 65 | + if(null == directive) | |
| 66 | + break; | |
| 67 | + | |
| 68 | + //日期 | |
| 69 | + directive.setRq(fmtyyyyMMdd.print(directive.getTimestamp())); | |
| 70 | + | |
| 71 | + if (directive instanceof D60) { | |
| 72 | + d60 = (D60) directive; | |
| 73 | + if(isDelete(d60)) | |
| 74 | + continue; | |
| 75 | + d60s.add(d60); | |
| 76 | + } | |
| 77 | + else if(directive instanceof D64) | |
| 78 | + d64s.add((D64) directive); | |
| 79 | + } | |
| 80 | + | |
| 81 | + //入库60 | |
| 82 | + save60(d60s); | |
| 83 | + //入库64 | |
| 84 | + save64(d64s); | |
| 85 | + | |
| 86 | + | |
| 87 | + // 60 指令更新(车载响应) | |
| 88 | + ConcurrentLinkedQueue<D60> updateD60s = DayOfDirectives.pstD60s; | |
| 89 | + d60s = new ArrayList<>(); | |
| 90 | + for (int i = 0; i < 2000; i++) { | |
| 91 | + d60 = updateD60s.poll(); | |
| 92 | + if(null == d60) | |
| 93 | + break; | |
| 94 | + d60s.add(d60); | |
| 95 | + } | |
| 96 | + | |
| 97 | + if(d60s.size() > 0) | |
| 98 | + update60(d60s); | |
| 99 | + }catch (Exception e){ | |
| 100 | + logger.error("指令入库出现异常", e); | |
| 101 | + } | |
| 102 | + } | |
| 103 | + | |
| 104 | + private void save64(final List<D64> d64s) { | |
| 105 | + if(null == d64s || d64s.size() == 0) | |
| 106 | + return; | |
| 107 | + | |
| 108 | + String sql = "insert into bsth_v_directive_64(device_id,error_text,http_code,oper_code,rq,sender,timestamp,city_code,line_id,txt_content,resp_ack) " + | |
| 109 | + " values(?,?,?,?,?,?,?,?,?,?,?)"; | |
| 110 | + | |
| 111 | + //编程式事务 | |
| 112 | + DataSourceTransactionManager tran = new DataSourceTransactionManager(jdbcTemplate.getDataSource()); | |
| 113 | + DefaultTransactionDefinition def = new DefaultTransactionDefinition(); | |
| 114 | + def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); | |
| 115 | + TransactionStatus status = tran.getTransaction(def); | |
| 116 | + | |
| 117 | + try{ | |
| 118 | + jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() { | |
| 119 | + @Override | |
| 120 | + public void setValues(PreparedStatement ps, int i) throws SQLException { | |
| 121 | + D64 d64 = d64s.get(i); | |
| 122 | + ps.setString(1 , d64.getDeviceId()); | |
| 123 | + ps.setString(2, isNvl(d64.getErrorText())); | |
| 124 | + ps.setInt(3, d64.getHttpCode()); | |
| 125 | + ps.setShort(4, isNvl(d64.getOperCode())); | |
| 126 | + ps.setString(5, d64.getRq()); | |
| 127 | + | |
| 128 | + ps.setString(6, isNvl(d64.getSender())); | |
| 129 | + ps.setLong(7, d64.getTimestamp()); | |
| 130 | + | |
| 131 | + ps.setShort(8, isNvl(d64.getData().getCityCode())); | |
| 132 | + ps.setString(9, isNvl(d64.getData().getLineId())); | |
| 133 | + ps.setString(10, isNvl(d64.getData().getTxtContent())); | |
| 134 | + ps.setShort(11, isNvl(d64.getRespAck())); | |
| 135 | + } | |
| 136 | + | |
| 137 | + @Override | |
| 138 | + public int getBatchSize() { | |
| 139 | + return d64s.size(); | |
| 140 | + } | |
| 141 | + }); | |
| 142 | + | |
| 143 | + tran.commit(status); | |
| 144 | + | |
| 145 | + logger.info("64 入库成功: " + d64s.size()); | |
| 146 | + }catch (Exception e){ | |
| 147 | + logger.error(String.format("错误数据:%s", JSON.toJSONString(d64s)), e); | |
| 148 | + tran.rollback(status); | |
| 149 | + } | |
| 150 | + } | |
| 151 | + | |
| 152 | + private void update60(final List<D60> d60s) { | |
| 153 | + if(null == d60s || d60s.size() == 0) | |
| 154 | + return; | |
| 155 | + | |
| 156 | + String sql = "update bsth_v_directive_60 set reply46=?,reply46time=?,reply47=?,reply47time=? where device_id=? and timestamp=? and msg_id=?"; | |
| 157 | + | |
| 158 | + //编程式事务 | |
| 159 | + DataSourceTransactionManager tran = new DataSourceTransactionManager(jdbcTemplate.getDataSource()); | |
| 160 | + DefaultTransactionDefinition def = new DefaultTransactionDefinition(); | |
| 161 | + def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); | |
| 162 | + TransactionStatus status = tran.getTransaction(def); | |
| 163 | + | |
| 164 | + try{ | |
| 165 | + jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() { | |
| 166 | + @Override | |
| 167 | + public void setValues(PreparedStatement ps, int i) throws SQLException { | |
| 168 | + D60 d60 = d60s.get(i); | |
| 169 | + ps.setShort(1, isNvl(d60.getReply46())); | |
| 170 | + if(null == d60.getReply46Time()) | |
| 171 | + ps.setNull(2, Types.BIGINT); | |
| 172 | + else | |
| 173 | + ps.setLong(2, d60.getReply46Time()); | |
| 174 | + | |
| 175 | + ps.setShort(3, isNvl(d60.getReply47())); | |
| 176 | + | |
| 177 | + if(null == d60.getReply47Time()) | |
| 178 | + ps.setNull(4, Types.BIGINT); | |
| 179 | + else | |
| 180 | + ps.setLong(4, d60.getReply47Time()); | |
| 181 | + ps.setString(5, d60.getDeviceId()); | |
| 182 | + ps.setLong(6, d60.getTimestamp()); | |
| 183 | + ps.setInt(7, d60.getMsgId()); | |
| 184 | + } | |
| 185 | + | |
| 186 | + @Override | |
| 187 | + public int getBatchSize() { | |
| 188 | + return d60s.size(); | |
| 189 | + } | |
| 190 | + }); | |
| 191 | + | |
| 192 | + tran.commit(status); | |
| 193 | + | |
| 194 | + logger.info("60 更新成功: " + d60s.size()); | |
| 195 | + }catch (Exception e){ | |
| 196 | + logger.error(String.format("错误数据:%s", JSON.toJSONString(d60s)), e); | |
| 197 | + tran.rollback(status); | |
| 198 | + } | |
| 199 | + } | |
| 200 | + | |
| 201 | + private void save60(final List<D60> d60s) { | |
| 202 | + if(null == d60s || d60s.size() == 0) | |
| 203 | + return; | |
| 204 | + | |
| 205 | + String sql = "insert into bsth_v_directive_60(device_id,error_text,http_code,oper_code,rq,sender,timestamp" + | |
| 206 | + ",alarm_time,company_code,dispatch_instruct,instruct_type,msg_id,service_state,txt_content,is_dispatch" + | |
| 207 | + ",line_code,reply46,reply46time,reply47,reply47time,sch) " + | |
| 208 | + " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; | |
| 209 | + | |
| 210 | + //编程式事务 | |
| 211 | + DataSourceTransactionManager tran = new DataSourceTransactionManager(jdbcTemplate.getDataSource()); | |
| 212 | + DefaultTransactionDefinition def = new DefaultTransactionDefinition(); | |
| 213 | + def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); | |
| 214 | + TransactionStatus status = tran.getTransaction(def); | |
| 215 | + | |
| 216 | + try{ | |
| 217 | + jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() { | |
| 218 | + @Override | |
| 219 | + public void setValues(PreparedStatement ps, int i) throws SQLException { | |
| 220 | + D60 d60 = d60s.get(i); | |
| 221 | + ps.setString(1, d60.getDeviceId()); | |
| 222 | + ps.setString(2, isNvl(d60.getErrorText())); | |
| 223 | + ps.setInt(3, d60.getHttpCode()); | |
| 224 | + ps.setShort(4, d60.getOperCode()); | |
| 225 | + ps.setString(5, d60.getRq()); | |
| 226 | + ps.setString(6, d60.getSender()); | |
| 227 | + ps.setLong(7, d60.getTimestamp()); | |
| 228 | + | |
| 229 | + ps.setLong(8, isNvl(d60.getData().getAlarmTime())); | |
| 230 | + ps.setShort(9, isNvl(d60.getData().getCompanyCode())); | |
| 231 | + ps.setShort(10, isNvl(d60.getData().getDispatchInstruct())); | |
| 232 | + ps.setInt(11, d60.getData().getInstructType()); | |
| 233 | + ps.setInt(12, d60.getData().getMsgId()); | |
| 234 | + ps.setLong(13, d60.getData().getServiceState()); | |
| 235 | + ps.setString(14, d60.getData().getTxtContent()); | |
| 236 | + ps.setBoolean(15, d60.isDispatch()); | |
| 237 | + | |
| 238 | + ps.setString(16, isNvl(d60.getLineCode())); | |
| 239 | + ps.setShort(17, isNvl(d60.getReply46())); | |
| 240 | + | |
| 241 | + if(null == d60.getReply46Time()) | |
| 242 | + ps.setNull(18, Types.BIGINT); | |
| 243 | + else | |
| 244 | + ps.setLong(18, d60.getReply46Time()); | |
| 245 | + | |
| 246 | + ps.setShort(19, isNvl(d60.getReply47())); | |
| 247 | + | |
| 248 | + if(null == d60.getReply47Time()) | |
| 249 | + ps.setNull(20, Types.BIGINT); | |
| 250 | + else | |
| 251 | + ps.setLong(20, d60.getReply47Time()); | |
| 252 | + | |
| 253 | + if(d60.getSch()==null) | |
| 254 | + ps.setNull(21, Types.BIGINT); | |
| 255 | + else | |
| 256 | + ps.setLong(21, d60.getSch().getId()); | |
| 257 | + } | |
| 258 | + | |
| 259 | + @Override | |
| 260 | + public int getBatchSize() { | |
| 261 | + return d60s.size(); | |
| 262 | + } | |
| 263 | + }); | |
| 264 | + | |
| 265 | + tran.commit(status); | |
| 266 | + | |
| 267 | + logger.info("60 入库成功: " + d60s.size()); | |
| 268 | + }catch (Exception e){ | |
| 269 | + logger.error(String.format("错误数据:%s", JSON.toJSONString(d60s)), e); | |
| 270 | + tran.rollback(status); | |
| 271 | + } | |
| 272 | + } | |
| 273 | + | |
| 274 | + private String isNvl(String v) { | |
| 275 | + return v==null?"":v; | |
| 276 | + } | |
| 277 | + | |
| 278 | + private short isNvl(Short v) { | |
| 279 | + return v==null?0:v; | |
| 280 | + } | |
| 281 | + | |
| 282 | + private long isNvl(Long v) { | |
| 283 | + return v==null?0:v; | |
| 284 | + } | |
| 285 | + | |
| 286 | + private boolean isDelete(D60 d60){ | |
| 287 | + try{ | |
| 288 | + //如果关联的班次已经不存在了,放弃入库,很低概率出现 | |
| 289 | + if(d60.isDispatch() && d60.getSch().isDeleted()){ | |
| 290 | + logger.warn("save 指令,发现 deleted=true 的班次,id=" + d60.getSch().getId()); | |
| 291 | + return true; | |
| 292 | + } | |
| 293 | + }catch (Exception e){ | |
| 294 | + logger.error("", e); | |
| 295 | + } | |
| 296 | + | |
| 297 | + return false; | |
| 298 | + } | |
| 299 | +} | ... | ... |
src/main/java/com/bsth/filter/WhiteIpFilter.java
src/main/java/com/bsth/repository/calc/CalcInvestigateMonthRepository.java
| 1 | -package com.bsth.repository.calc; | |
| 2 | - | |
| 3 | - | |
| 4 | -import java.util.List; | |
| 5 | - | |
| 6 | -import org.springframework.data.jpa.repository.Query; | |
| 7 | -import org.springframework.stereotype.Repository; | |
| 8 | - | |
| 9 | -import com.bsth.entity.calc.CalcInvestigateMonth; | |
| 10 | -import com.bsth.repository.BaseRepository; | |
| 11 | - | |
| 12 | -@Repository | |
| 13 | -public interface CalcInvestigateMonthRepository extends BaseRepository<CalcInvestigateMonth, Integer>{ | |
| 14 | - | |
| 15 | - @Query(value="select c from CalcInvestigateMonth c where month = ?1") | |
| 16 | - List<CalcInvestigateMonth> findByMonth(String month); | |
| 17 | - | |
| 18 | - @Query(value="select c from CalcInvestigateMonth c where month = ?1 and gsbm like %?2% and fgsbm like %?3%") | |
| 19 | - List<CalcInvestigateMonth> findByMonth(String month, String gsbm, String fgsbm); | |
| 20 | - | |
| 21 | - @Query(value="select c from CalcInvestigateMonth c where month = ?1 and xl = ?2") | |
| 22 | - List<CalcInvestigateMonth> findByMonthAndLine(String month, String line); | |
| 23 | - | |
| 24 | -} | |
| 1 | +package com.bsth.repository.calc; | |
| 2 | + | |
| 3 | + | |
| 4 | +import java.util.List; | |
| 5 | + | |
| 6 | +import org.springframework.data.jpa.repository.Query; | |
| 7 | +import org.springframework.stereotype.Repository; | |
| 8 | + | |
| 9 | +import com.bsth.entity.calc.CalcInvestigateMonth; | |
| 10 | +import com.bsth.repository.BaseRepository; | |
| 11 | + | |
| 12 | +@Repository | |
| 13 | +public interface CalcInvestigateMonthRepository extends BaseRepository<CalcInvestigateMonth, Integer>{ | |
| 14 | + | |
| 15 | + @Query(value="select c from CalcInvestigateMonth c where month = ?1 order by c.xlmc") | |
| 16 | + List<CalcInvestigateMonth> findByMonth(String month); | |
| 17 | + | |
| 18 | + @Query(value="select c from CalcInvestigateMonth c where month = ?1 and gsbm like %?2% and fgsbm like %?3% order by c.xlmc") | |
| 19 | + List<CalcInvestigateMonth> findByMonth(String month, String gsbm, String fgsbm); | |
| 20 | + | |
| 21 | + @Query(value="select c from CalcInvestigateMonth c where month = ?1 and xl = ?2 order by c.xlmc") | |
| 22 | + List<CalcInvestigateMonth> findByMonthAndLine(String month, String line); | |
| 23 | + | |
| 24 | +} | ... | ... |
src/main/java/com/bsth/service/calc/impl/CalcMixServiceImpl.java
| 1 | -package com.bsth.service.calc.impl; | |
| 2 | - | |
| 3 | -import java.sql.ResultSet; | |
| 4 | -import java.sql.SQLException; | |
| 5 | -import java.text.SimpleDateFormat; | |
| 6 | -import java.util.ArrayList; | |
| 7 | -import java.util.HashMap; | |
| 8 | -import java.util.Iterator; | |
| 9 | -import java.util.List; | |
| 10 | -import java.util.Map; | |
| 11 | - | |
| 12 | -import com.bsth.data.BasicData; | |
| 13 | -import com.bsth.entity.calc.CalcWaybill; | |
| 14 | -import com.bsth.entity.calc.CalcInvestigateMonth; | |
| 15 | -import com.bsth.entity.mcy_forms.Singledata; | |
| 16 | -import com.bsth.repository.calc.CalcInvestigateMonthRepository; | |
| 17 | -import com.bsth.repository.calc.CalcWaybillRepository; | |
| 18 | -import com.bsth.service.LineService; | |
| 19 | -import com.bsth.service.calc.CalcMixService; | |
| 20 | -import com.bsth.util.Arith; | |
| 21 | -import com.bsth.util.ReportUtils; | |
| 22 | - | |
| 23 | -import org.slf4j.Logger; | |
| 24 | -import org.slf4j.LoggerFactory; | |
| 25 | -import org.springframework.beans.factory.annotation.Autowired; | |
| 26 | -import org.springframework.jdbc.core.JdbcTemplate; | |
| 27 | -import org.springframework.jdbc.core.RowMapper; | |
| 28 | -import org.springframework.stereotype.Service; | |
| 29 | - | |
| 30 | -/** | |
| 31 | - * Created by 19/02/28. | |
| 32 | - */ | |
| 33 | -@Service | |
| 34 | -public class CalcMixServiceImpl implements CalcMixService { | |
| 35 | - | |
| 36 | - @Autowired | |
| 37 | - private CalcWaybillRepository calcRepository; | |
| 38 | - | |
| 39 | - @Autowired | |
| 40 | - private CalcInvestigateMonthRepository calcInvestigateMonthRepository; | |
| 41 | - | |
| 42 | - @Autowired | |
| 43 | - private LineService lineService; | |
| 44 | - | |
| 45 | - @Autowired | |
| 46 | - JdbcTemplate jdbcTemplate; | |
| 47 | - | |
| 48 | - | |
| 49 | - Logger logger = LoggerFactory.getLogger(this.getClass()); | |
| 50 | - | |
| 51 | - | |
| 52 | - @Override | |
| 53 | - public List<Map<String, Object>> calcjsyspy(String line, String date, String date2, | |
| 54 | - String empnames, String cont, String gsdm, String fgsdm) { | |
| 55 | - | |
| 56 | - List<Map<String, Object>> resList = new ArrayList<Map<String, Object>>(); | |
| 57 | - List<CalcWaybill> list = null; | |
| 58 | - | |
| 59 | - int flag = 0; | |
| 60 | - if("驾驶员".equals(empnames)){ | |
| 61 | - flag = 1; | |
| 62 | - list = calcRepository.scheduleByJsy(line, date, date2, gsdm, fgsdm, cont); | |
| 63 | - } else if("售票员".equals(empnames)){ | |
| 64 | - flag = 2; | |
| 65 | - list = calcRepository.scheduleBySpy(line, date, date2, gsdm, fgsdm, cont); | |
| 66 | - } else if("车辆自编号".equals(empnames)){ | |
| 67 | - flag = 3; | |
| 68 | - list = calcRepository.scheduleByZbh(line, date, date2, gsdm, fgsdm, cont); | |
| 69 | - } | |
| 70 | - | |
| 71 | - List<CalcWaybill> calcList = new ArrayList<CalcWaybill>(); | |
| 72 | - Map<String, CalcWaybill> calcMap = new HashMap<String, CalcWaybill>(); | |
| 73 | - | |
| 74 | - CalcWaybill zjCalc = this.initCalcWaybill(); | |
| 75 | - zjCalc.setjName("合计"); | |
| 76 | - for(CalcWaybill c : list){ | |
| 77 | - String key = ""; | |
| 78 | - if(flag == 1){ | |
| 79 | - if(c.getjGh() != null && c.getjName() != null) | |
| 80 | - key = c.getjGh() + "/" + c.getjName(); | |
| 81 | - } else if(flag == 2){ | |
| 82 | - if(c.getsGh() != null && c.getsName() != null) | |
| 83 | - key = c.getsGh() + "/" + c.getsName(); | |
| 84 | - } else if(flag == 3){ | |
| 85 | - if(c.getCl() != null) | |
| 86 | - key = c.getCl(); | |
| 87 | - } | |
| 88 | - | |
| 89 | - if(key.trim().length() == 0 || "/".equals(key)) | |
| 90 | - continue; | |
| 91 | - | |
| 92 | - CalcWaybill calc = null; | |
| 93 | - if(calcMap.containsKey(key)){ | |
| 94 | - calc = calcMap.get(key); | |
| 95 | - } else { | |
| 96 | - calc = this.initCalcWaybill(); | |
| 97 | - calc.setjName(key); | |
| 98 | - calcList.add(calc); | |
| 99 | - calcMap.put(key, calc); | |
| 100 | - } | |
| 101 | - | |
| 102 | - this.summation(calc, c); | |
| 103 | - | |
| 104 | - this.summation(zjCalc, c); | |
| 105 | - | |
| 106 | - } | |
| 107 | - calcList.add(zjCalc); | |
| 108 | - calcMap.put("合计", zjCalc); | |
| 109 | - | |
| 110 | - for(CalcWaybill c : calcList){ | |
| 111 | - Map<String, Object> m = new HashMap<String, Object>(); | |
| 112 | - m.put("jName", c.getjName()); | |
| 113 | - m.put("jhyybc", c.getJhyybc()); | |
| 114 | - m.put("jhfyybc", c.getJhfyybc()); | |
| 115 | - m.put("sjyybc", c.getSjyybc()); | |
| 116 | - m.put("sjfyybc", c.getSjfyybc()); | |
| 117 | - m.put("lbbc", c.getLbbc()); | |
| 118 | - m.put("ljbc", c.getLjbc()); | |
| 119 | - m.put("jhzlc", Arith.add(c.getJhyylc(), c.getJhfyylc())); | |
| 120 | - m.put("jhyylc", c.getJhyylc()); | |
| 121 | - m.put("jhfyylc", c.getJhfyylc()); | |
| 122 | - m.put("sjzlc", Arith.add(c.getSjyylc(), c.getSjfyylc())); | |
| 123 | - m.put("sjyylc", c.getSjyylc()); | |
| 124 | - m.put("sjfyylc", c.getSjfyylc()); | |
| 125 | - m.put("lblc", c.getLblc()); | |
| 126 | - m.put("ljyylc", c.getLjyylc()); | |
| 127 | - m.put("ljfyylc", c.getLjfyylc()); | |
| 128 | - resList.add(m); | |
| 129 | - } | |
| 130 | - | |
| 131 | - return resList; | |
| 132 | - } | |
| 133 | - | |
| 134 | - public CalcWaybill initCalcWaybill(){ | |
| 135 | - CalcWaybill calc = new CalcWaybill(); | |
| 136 | - calc.setJhyybc(0); | |
| 137 | - calc.setJhyylc(0d); | |
| 138 | - calc.setJhfyybc(0); | |
| 139 | - calc.setJhfyylc(0d); | |
| 140 | - calc.setSjyybc(0); | |
| 141 | - calc.setSjyylc(0d); | |
| 142 | - calc.setSjfyybc(0); | |
| 143 | - calc.setSjfyylc(0d); | |
| 144 | - calc.setLbbc(0); | |
| 145 | - calc.setLblc(0d); | |
| 146 | - calc.setLjbc(0); | |
| 147 | - calc.setLjyylc(0d); | |
| 148 | - calc.setLjfyylc(0d); | |
| 149 | - return calc; | |
| 150 | - } | |
| 151 | - | |
| 152 | - public CalcWaybill summation(CalcWaybill c1, CalcWaybill c2){ | |
| 153 | - c1.setJhyybc(c1.getJhyybc() + c2.getJhyybc()); | |
| 154 | - c1.setJhyylc(Arith.add(c1.getJhyylc(), c2.getJhyylc())); | |
| 155 | - c1.setJhfyybc(c1.getJhfyybc() + c2.getJhfyybc()); | |
| 156 | - c1.setJhfyylc(Arith.add(c1.getJhfyylc(), c2.getJhfyylc())); | |
| 157 | - c1.setSjyybc(c1.getSjyybc() + c2.getSjyybc()); | |
| 158 | - c1.setSjyylc(Arith.add(c1.getSjyylc(), c2.getSjyylc())); | |
| 159 | - c1.setSjyylc(Arith.add(c1.getSjyylc(), c2.getLjyylc())); | |
| 160 | - c1.setSjfyybc(c1.getSjfyybc() + c2.getSjfyybc()); | |
| 161 | - c1.setSjfyylc(Arith.add(c1.getSjfyylc(), c2.getSjfyylc())); | |
| 162 | - c1.setSjfyylc(Arith.add(c1.getSjfyylc(), c2.getLjfyylc())); | |
| 163 | - c1.setLbbc(c1.getLbbc() + c2.getLbbc()); | |
| 164 | - c1.setLblc(Arith.add(c1.getLblc(), c2.getLblc())); | |
| 165 | - c1.setLjbc(c1.getLjbc() + c2.getLjbc()); | |
| 166 | - c1.setLjyylc(Arith.add(c1.getLjyylc(), c2.getLjyylc())); | |
| 167 | - c1.setLjfyylc(Arith.add(c1.getLjfyylc(), c2.getLjfyylc())); | |
| 168 | - return c1; | |
| 169 | - } | |
| 170 | - | |
| 171 | - @Override | |
| 172 | - public List<Map<String, Object>> singledatatj(String line, | |
| 173 | - String date, String date2, | |
| 174 | - String tjtype, String cont, | |
| 175 | - String gsdm, String fgsdm, String sfdc) { | |
| 176 | - | |
| 177 | - List<Map<String, Object>> resList = new ArrayList<Map<String, Object>>(); | |
| 178 | - List<CalcWaybill> list = null; | |
| 179 | - | |
| 180 | - int flag = 0; | |
| 181 | - if("驾驶员".equals(tjtype)){ | |
| 182 | - flag = 1; | |
| 183 | - list = calcRepository.scheduleByJsy(line, date, date2, gsdm, fgsdm, cont); | |
| 184 | - } else if("售票员".equals(tjtype)){ | |
| 185 | - flag = 2; | |
| 186 | - list = calcRepository.scheduleBySpy(line, date, date2, gsdm, fgsdm, cont); | |
| 187 | - } else if("车辆自编号".equals(tjtype)){ | |
| 188 | - flag = 3; | |
| 189 | - if("1".equals(sfdc)){ | |
| 190 | - list = calcRepository.scheduleByZbh(line, date, date2, gsdm, fgsdm, cont, true); | |
| 191 | - } else if("0".equals(sfdc)){ | |
| 192 | - list = calcRepository.scheduleByZbh(line, date, date2, gsdm, fgsdm, cont, false); | |
| 193 | - } else { | |
| 194 | - list = calcRepository.scheduleByZbh(line, date, date2, gsdm, fgsdm, cont); | |
| 195 | - } | |
| 196 | - } | |
| 197 | - | |
| 198 | - List<CalcWaybill> calcList = new ArrayList<CalcWaybill>(); | |
| 199 | - Map<String, CalcWaybill> calcMap = new HashMap<String, CalcWaybill>(); | |
| 200 | - | |
| 201 | - CalcWaybill zjCalc = this.initCalcWaybill(); | |
| 202 | - zjCalc.setjName("合计"); | |
| 203 | - for(CalcWaybill c : list){ | |
| 204 | - String key = ""; | |
| 205 | - if(flag == 1){ | |
| 206 | - if(c.getjGh() != null && c.getjName() != null) | |
| 207 | - key += c.getjGh() + "/" + c.getjName(); | |
| 208 | - } else if(flag == 2){ | |
| 209 | - if(c.getsGh() != null && c.getsName() != null) | |
| 210 | - key += c.getsGh() + "/" + c.getsName(); | |
| 211 | - } else if(flag == 3){ | |
| 212 | - if(c.getCl() != null) | |
| 213 | - key += c.getCl(); | |
| 214 | - } | |
| 215 | - | |
| 216 | - if(key.trim().length() == 0 || "/".equals(key)) | |
| 217 | - continue; | |
| 218 | - | |
| 219 | - String jName = key; | |
| 220 | - key = c.getXl() + "/" + key; | |
| 221 | - | |
| 222 | - CalcWaybill calc = null; | |
| 223 | - if(calcMap.containsKey(key)){ | |
| 224 | - calc = calcMap.get(key); | |
| 225 | - } else { | |
| 226 | - calc = this.initCalcWaybill(); | |
| 227 | - calc.setXlName(c.getXlName()); | |
| 228 | - calc.setXl(c.getXl()); | |
| 229 | - calc.setjName(jName); | |
| 230 | - calcList.add(calc); | |
| 231 | - calcMap.put(key, calc); | |
| 232 | - } | |
| 233 | - | |
| 234 | - this.summation(calc, c); | |
| 235 | - | |
| 236 | - this.summation(zjCalc, c); | |
| 237 | - | |
| 238 | - } | |
| 239 | - calcList.add(zjCalc); | |
| 240 | - calcMap.put("合计", zjCalc); | |
| 241 | - | |
| 242 | - Map<String, Map<String, Object>> keyMap = new HashMap<String, Map<String, Object>>(); | |
| 243 | - for(CalcWaybill c : calcList){ | |
| 244 | - Map<String, Object> m = new HashMap<String, Object>(); | |
| 245 | - m.put("gS", BasicData.businessFgsCodeNameMap.get(fgsdm+"_"+gsdm)); | |
| 246 | - m.put("xl", c.getXl()); | |
| 247 | - m.put("xlName", c.getXlName()); | |
| 248 | - m.put("jName", c.getjName()); | |
| 249 | - m.put("jhyybc", c.getJhyybc()); | |
| 250 | - m.put("jhfyybc", c.getJhfyybc()); | |
| 251 | - m.put("sjyybc", c.getSjyybc()); | |
| 252 | - m.put("sjfyybc", c.getSjfyybc()); | |
| 253 | - m.put("lbbc", c.getLbbc()); | |
| 254 | - m.put("ljbc", c.getLjbc()); | |
| 255 | - m.put("jhzlc", Arith.add(c.getJhyylc(), c.getJhfyylc())); | |
| 256 | - m.put("jhyylc", c.getJhyylc()); | |
| 257 | - m.put("jhfyylc", c.getJhfyylc()); | |
| 258 | - m.put("sjzlc", Arith.add(c.getSjyylc(), c.getSjfyylc())); | |
| 259 | - m.put("sjyylc", c.getSjyylc()); | |
| 260 | - m.put("sjfyylc", c.getSjfyylc()); | |
| 261 | - m.put("lblc", c.getLblc()); | |
| 262 | - m.put("ljyylc", c.getLjyylc()); | |
| 263 | - m.put("ljfyylc", c.getLjfyylc()); | |
| 264 | - if(flag != 2){ | |
| 265 | - m.put("hyl", 0); | |
| 266 | - m.put("jzl", 0); | |
| 267 | - m.put("sh", 0); | |
| 268 | - } | |
| 269 | - resList.add(m); | |
| 270 | - keyMap.put(c.getXl() + "/" + c.getjName(), m); | |
| 271 | - } | |
| 272 | - | |
| 273 | - String linesql=""; | |
| 274 | - if(!line.equals("")){ | |
| 275 | - linesql +=" and xlbm ='"+line+"' "; | |
| 276 | - } | |
| 277 | - if(!gsdm.equals("")){ | |
| 278 | - linesql +=" and ssgsdm ='"+gsdm+"' "; | |
| 279 | - } | |
| 280 | - if(!fgsdm.equals("")){ | |
| 281 | - linesql +=" and fgsdm ='"+fgsdm+"' "; | |
| 282 | - } | |
| 283 | - String nysql="SELECT id,xlbm,nbbm,jsy,jzl as jzl,yh as yh,sh as sh,fgsdm FROM bsth_c_ylb" | |
| 284 | - + " WHERE rq >= '"+date+"' and rq <= '"+date2+"'" | |
| 285 | - + linesql | |
| 286 | - + " union" | |
| 287 | - + " SELECT id,xlbm,nbbm,jsy,cdl as jzl,hd as yh,sh as sh,fgsdm FROM bsth_c_dlb" | |
| 288 | - + " WHERE rq >= '"+date+"' and rq <= '"+date2+"'" | |
| 289 | - + linesql; | |
| 290 | - List<Singledata> listNy = jdbcTemplate.query(nysql, new RowMapper<Singledata>() { | |
| 291 | - @Override | |
| 292 | - public Singledata mapRow(ResultSet arg0, int arg1) throws SQLException { | |
| 293 | - Singledata sin = new Singledata(); | |
| 294 | - sin.setxL(arg0.getString("xlbm")); | |
| 295 | - sin.setJsy(arg0.getString("jsy")); | |
| 296 | - sin.setClzbh(arg0.getString("nbbm")); | |
| 297 | - sin.setJzl(arg0.getString("jzl")); | |
| 298 | - sin.setHyl(arg0.getString("yh")); | |
| 299 | - sin.setUnyyyl(arg0.getString("sh")); | |
| 300 | - sin.setgS(arg0.getString("fgsdm")); | |
| 301 | - return sin; | |
| 302 | - } | |
| 303 | - }); | |
| 304 | - | |
| 305 | - if(flag != 2){ | |
| 306 | - Map<String, Object> last = resList.get(resList.size()-1); | |
| 307 | - last.put("hyl", 0); | |
| 308 | - last.put("jzl", 0); | |
| 309 | - last.put("sh", 0); | |
| 310 | - | |
| 311 | - //统计油,电表中手动添加的或者有加注没里程的数据 | |
| 312 | - for (int i = 0; i < listNy.size(); i++) { | |
| 313 | - Singledata sin_=listNy.get(i); | |
| 314 | - String xl=sin_.getxL(); | |
| 315 | - String str = ""; | |
| 316 | - if(flag == 1){ | |
| 317 | - str = sin_.getJsy() + "/" + BasicData.allPerson.get(gsdm+"-"+sin_.getJsy()); | |
| 318 | - } else { | |
| 319 | - str = sin_.getClzbh(); | |
| 320 | - } | |
| 321 | -// boolean fages=true; | |
| 322 | - if(keyMap.containsKey(xl + "/" + str)){ | |
| 323 | - Map<String, Object> m = keyMap.get(xl + "/" + str); | |
| 324 | - m.put("hyl", Arith.add(m.get("hyl"), sin_.getHyl()!=null?sin_.getHyl():0)); | |
| 325 | - m.put("jzl", Arith.add(m.get("jzl"), sin_.getJzl()!=null?sin_.getJzl():0)); | |
| 326 | - m.put("sh", Arith.add(m.get("sh"), sin_.getUnyyyl()!=null?sin_.getUnyyyl():0)); | |
| 327 | - last.put("hyl", Arith.add(last.get("hyl"), sin_.getHyl()!=null?sin_.getHyl():0)); | |
| 328 | - last.put("jzl", Arith.add(last.get("jzl"), sin_.getJzl()!=null?sin_.getJzl():0)); | |
| 329 | - last.put("sh", Arith.add(last.get("sh"), sin_.getUnyyyl()!=null?sin_.getUnyyyl():0)); | |
| 330 | - } else { | |
| 331 | - Map<String, Object> m = new HashMap<String, Object>(); | |
| 332 | - m.put("gS", BasicData.businessFgsCodeNameMap.get(fgsdm+"_"+gsdm)); | |
| 333 | - m.put("xl", xl); | |
| 334 | - m.put("xlName", BasicData.lineCodeAllNameMap.get(xl)); | |
| 335 | - m.put("jName", str); | |
| 336 | - m.put("jhzlc", 0); | |
| 337 | - m.put("sjzlc", 0); | |
| 338 | - m.put("sjfyylc", 0); | |
| 339 | - m.put("hyl", sin_.getHyl()!=null?sin_.getHyl():0); | |
| 340 | - m.put("jzl", sin_.getJzl()!=null?sin_.getJzl():0); | |
| 341 | - m.put("sh", sin_.getUnyyyl()!=null?sin_.getUnyyyl():0); | |
| 342 | - last.put("hyl", Arith.add(last.get("hyl"), m.get("hyl"))); | |
| 343 | - last.put("jzl", Arith.add(last.get("jzl"), m.get("jzl"))); | |
| 344 | - last.put("sh", Arith.add(last.get("sh"), m.get("sh"))); | |
| 345 | - | |
| 346 | - keyMap.put(xl + "/" + str, m); | |
| 347 | - } | |
| 348 | - } | |
| 349 | - resList.remove(last); | |
| 350 | - resList.add(last); | |
| 351 | - } | |
| 352 | - | |
| 353 | - return resList; | |
| 354 | - } | |
| 355 | - | |
| 356 | - //浦东公交线路调查表 | |
| 357 | - @Override | |
| 358 | - public List<CalcInvestigateMonth> calcInvestigateMonth(String gsbm, String fgsbm, String month, String line, | |
| 359 | - String xlName, String nature, String type) { | |
| 360 | - List<CalcInvestigateMonth> resList = new ArrayList<CalcInvestigateMonth>(); | |
| 361 | - | |
| 362 | - Map<String, Boolean> lineNature = lineService.lineNature(); | |
| 363 | - | |
| 364 | - List<CalcInvestigateMonth> list = new ArrayList<CalcInvestigateMonth>(); | |
| 365 | - if(line != null && line.length() > 0){ | |
| 366 | - list = calcInvestigateMonthRepository.findByMonthAndLine(month, line); | |
| 367 | - } else if(gsbm != null && gsbm.length() > 0){ | |
| 368 | - list = calcInvestigateMonthRepository.findByMonth(month, gsbm, fgsbm); | |
| 369 | - } else { | |
| 370 | - list = calcInvestigateMonthRepository.findByMonth(month); | |
| 371 | - } | |
| 372 | - | |
| 373 | - for(CalcInvestigateMonth c : list){ | |
| 374 | - if("1".equals(nature)){ | |
| 375 | - if(lineNature.containsKey(c.getXl()) && lineNature.get(c.getXl())){ | |
| 376 | - resList.add(c); | |
| 377 | - } | |
| 378 | - } else if("2".equals(nature)){ | |
| 379 | - if(!lineNature.containsKey(c.getXl()) || !lineNature.get(c.getXl())){ | |
| 380 | - resList.add(c); | |
| 381 | - } | |
| 382 | - } else { | |
| 383 | - resList.add(c); | |
| 384 | - } | |
| 385 | - } | |
| 386 | - | |
| 387 | - if (type != null && type.length() != 0 && type.equals("export")) { | |
| 388 | - SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyy-MM-dd"), | |
| 389 | - sdfSimple = new SimpleDateFormat("yyyyMMdd"), | |
| 390 | - monSimple = new SimpleDateFormat("yyyy-MM"); | |
| 391 | - List<Iterator<?>> listI = new ArrayList<Iterator<?>>(); | |
| 392 | - Map<String, Object> m = new HashMap<String, Object>(); | |
| 393 | - m.put("date", month); | |
| 394 | - m.put("currMonth", "(" + month + ")"); | |
| 395 | - ReportUtils ee = new ReportUtils(); | |
| 396 | - try { | |
| 397 | - List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>(); | |
| 398 | - for(CalcInvestigateMonth c : resList){ | |
| 399 | - Map<String, Object> map = new HashMap<String, Object>(); | |
| 400 | - map.put("xlmc", c.getXlmc()!=null?c.getXlmc():""); | |
| 401 | - map.put("qzpcs", c.getQzpcs()!=null?c.getQzpcs():""); | |
| 402 | - map.put("xlsx", c.getXlsx()!=null?c.getXlsx():""); | |
| 403 | - map.put("xllx", c.getXllx()!=null?c.getXllx():""); | |
| 404 | - map.put("xlcd", c.getXlcd()!=null?c.getXlcd():""); | |
| 405 | - map.put("sfgp", c.getSfgp()!=null?c.getSfgp():""); | |
| 406 | - map.put("sflpxl", c.getSflpxl()!=null?c.getSflpxl():""); | |
| 407 | - map.put("smbsj", c.getSmbsj()!=null?c.getSmbsj():""); | |
| 408 | - if("常规".equals(c.getDdfs())){ | |
| 409 | - map.put("ddfsCg", "√"); | |
| 410 | - map.put("ddfsDyh", ""); | |
| 411 | - } else if("多样化".equals(c.getDdfs())){ | |
| 412 | - map.put("ddfsCg", ""); | |
| 413 | - map.put("ddfsDyh", "√"); | |
| 414 | - } else { | |
| 415 | - map.put("ddfsCg", ""); | |
| 416 | - map.put("ddfsDyh", ""); | |
| 417 | - } | |
| 418 | - map.put("gfjgsj", c.getGfjgsj()!=null?c.getGfjgsj():""); | |
| 419 | - map.put("dgjgsj", c.getDgjgsj()!=null?c.getDgjgsj():""); | |
| 420 | - map.put("sfxjgj", c.getSfxjgj()!=null?c.getSfxjgj():""); | |
| 421 | - map.put("rjlc", c.getRjlc()!=null?c.getRjlc():""); | |
| 422 | - map.put("rjbc", c.getRjbc()!=null?c.getRjbc():""); | |
| 423 | - map.put("rjrc", c.getRjrc()!=null?c.getRjrc():""); | |
| 424 | - map.put("rjdcrc", c.getRjdcrc()!=null?c.getRjdcrc():""); | |
| 425 | - map.put("mbcrc", c.getMbcrc()!=null?c.getMbcrc():""); | |
| 426 | - map.put("jhzgl", c.getJhzgl()!=null?c.getJhzgl():""); | |
| 427 | - map.put("sjzgl", c.getSjzgl()!=null?c.getSjzgl():""); | |
| 428 | - map.put("sjyylc", c.getSjyylc()!=null?c.getSjyylc():""); | |
| 429 | - map.put("lclyl", c.getLclyl()!=null?c.getLclyl():""); | |
| 430 | - map.put("bglrc", c.getBglrc()!=null?c.getBglrc():""); | |
| 431 | - map.put("yrc", c.getYrc()!=null?c.getYrc():""); | |
| 432 | - map.put("yys", c.getYys()!=null?c.getYys():""); | |
| 433 | - map.put("pj", c.getPj()!=null?c.getPj():""); | |
| 434 | - map.put("bglys", c.getBglys()!=null?c.getBglys():""); | |
| 435 | - map.put("tjgjz", c.getTjgjz()!=null?c.getTjgjz():""); | |
| 436 | - map.put("tjsjyy", c.getTjsjyy()!=null?c.getTjsjyy():""); | |
| 437 | - map.put("wyx", c.getWyx()!=null?c.getWyx():""); | |
| 438 | - map.put("xlzx", c.getXlzx()!=null?c.getXlzx():""); | |
| 439 | - map.put("xlzd", c.getXlzd()!=null?c.getXlzd():""); | |
| 440 | - map.put("bz", c.getBz()!=null?c.getBz():""); | |
| 441 | - mapList.add(map); | |
| 442 | - } | |
| 443 | - listI.add(mapList.iterator()); | |
| 444 | - String path = this.getClass().getResource("/").getPath() + "static/pages/forms/"; | |
| 445 | - ee.excelReplace(listI, new Object[]{m}, path + "mould/calcInvestigateMonth.xls", | |
| 446 | - path + "export/" + "浦东公交线路调查表"+month+".xls"); | |
| 447 | - } catch (Exception e) { | |
| 448 | - // TODO: handle exception | |
| 449 | - e.printStackTrace(); | |
| 450 | - logger.info("" , e); | |
| 451 | - } | |
| 452 | - } | |
| 453 | - return resList; | |
| 454 | - } | |
| 455 | - | |
| 456 | -} | |
| 1 | +package com.bsth.service.calc.impl; | |
| 2 | + | |
| 3 | +import java.sql.ResultSet; | |
| 4 | +import java.sql.SQLException; | |
| 5 | +import java.text.SimpleDateFormat; | |
| 6 | +import java.util.ArrayList; | |
| 7 | +import java.util.HashMap; | |
| 8 | +import java.util.Iterator; | |
| 9 | +import java.util.List; | |
| 10 | +import java.util.Map; | |
| 11 | + | |
| 12 | +import com.bsth.data.BasicData; | |
| 13 | +import com.bsth.entity.calc.CalcWaybill; | |
| 14 | +import com.bsth.entity.calc.CalcInvestigateMonth; | |
| 15 | +import com.bsth.entity.mcy_forms.Singledata; | |
| 16 | +import com.bsth.repository.calc.CalcInvestigateMonthRepository; | |
| 17 | +import com.bsth.repository.calc.CalcWaybillRepository; | |
| 18 | +import com.bsth.service.LineService; | |
| 19 | +import com.bsth.service.calc.CalcMixService; | |
| 20 | +import com.bsth.util.Arith; | |
| 21 | +import com.bsth.util.ReportUtils; | |
| 22 | + | |
| 23 | +import org.slf4j.Logger; | |
| 24 | +import org.slf4j.LoggerFactory; | |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 26 | +import org.springframework.jdbc.core.JdbcTemplate; | |
| 27 | +import org.springframework.jdbc.core.RowMapper; | |
| 28 | +import org.springframework.stereotype.Service; | |
| 29 | + | |
| 30 | +/** | |
| 31 | + * Created by 19/02/28. | |
| 32 | + */ | |
| 33 | +@Service | |
| 34 | +public class CalcMixServiceImpl implements CalcMixService { | |
| 35 | + | |
| 36 | + @Autowired | |
| 37 | + private CalcWaybillRepository calcRepository; | |
| 38 | + | |
| 39 | + @Autowired | |
| 40 | + private CalcInvestigateMonthRepository calcInvestigateMonthRepository; | |
| 41 | + | |
| 42 | + @Autowired | |
| 43 | + private LineService lineService; | |
| 44 | + | |
| 45 | + @Autowired | |
| 46 | + JdbcTemplate jdbcTemplate; | |
| 47 | + | |
| 48 | + | |
| 49 | + Logger logger = LoggerFactory.getLogger(this.getClass()); | |
| 50 | + | |
| 51 | + | |
| 52 | + @Override | |
| 53 | + public List<Map<String, Object>> calcjsyspy(String line, String date, String date2, | |
| 54 | + String empnames, String cont, String gsdm, String fgsdm) { | |
| 55 | + | |
| 56 | + List<Map<String, Object>> resList = new ArrayList<Map<String, Object>>(); | |
| 57 | + List<CalcWaybill> list = null; | |
| 58 | + | |
| 59 | + int flag = 0; | |
| 60 | + if("驾驶员".equals(empnames)){ | |
| 61 | + flag = 1; | |
| 62 | + list = calcRepository.scheduleByJsy(line, date, date2, gsdm, fgsdm, cont); | |
| 63 | + } else if("售票员".equals(empnames)){ | |
| 64 | + flag = 2; | |
| 65 | + list = calcRepository.scheduleBySpy(line, date, date2, gsdm, fgsdm, cont); | |
| 66 | + } else if("车辆自编号".equals(empnames)){ | |
| 67 | + flag = 3; | |
| 68 | + list = calcRepository.scheduleByZbh(line, date, date2, gsdm, fgsdm, cont); | |
| 69 | + } | |
| 70 | + | |
| 71 | + List<CalcWaybill> calcList = new ArrayList<CalcWaybill>(); | |
| 72 | + Map<String, CalcWaybill> calcMap = new HashMap<String, CalcWaybill>(); | |
| 73 | + | |
| 74 | + CalcWaybill zjCalc = this.initCalcWaybill(); | |
| 75 | + zjCalc.setjName("合计"); | |
| 76 | + for(CalcWaybill c : list){ | |
| 77 | + String key = ""; | |
| 78 | + if(flag == 1){ | |
| 79 | + if(c.getjGh() != null && c.getjName() != null) | |
| 80 | + key = c.getjGh() + "/" + c.getjName(); | |
| 81 | + } else if(flag == 2){ | |
| 82 | + if(c.getsGh() != null && c.getsName() != null) | |
| 83 | + key = c.getsGh() + "/" + c.getsName(); | |
| 84 | + } else if(flag == 3){ | |
| 85 | + if(c.getCl() != null) | |
| 86 | + key = c.getCl(); | |
| 87 | + } | |
| 88 | + | |
| 89 | + if(key.trim().length() == 0 || "/".equals(key)) | |
| 90 | + continue; | |
| 91 | + | |
| 92 | + CalcWaybill calc = null; | |
| 93 | + if(calcMap.containsKey(key)){ | |
| 94 | + calc = calcMap.get(key); | |
| 95 | + } else { | |
| 96 | + calc = this.initCalcWaybill(); | |
| 97 | + calc.setjName(key); | |
| 98 | + calcList.add(calc); | |
| 99 | + calcMap.put(key, calc); | |
| 100 | + } | |
| 101 | + | |
| 102 | + this.summation(calc, c); | |
| 103 | + | |
| 104 | + this.summation(zjCalc, c); | |
| 105 | + | |
| 106 | + } | |
| 107 | + calcList.add(zjCalc); | |
| 108 | + calcMap.put("合计", zjCalc); | |
| 109 | + | |
| 110 | + for(CalcWaybill c : calcList){ | |
| 111 | + Map<String, Object> m = new HashMap<String, Object>(); | |
| 112 | + m.put("jName", c.getjName()); | |
| 113 | + m.put("jhyybc", c.getJhyybc()); | |
| 114 | + m.put("jhfyybc", c.getJhfyybc()); | |
| 115 | + m.put("sjyybc", c.getSjyybc()); | |
| 116 | + m.put("sjfyybc", c.getSjfyybc()); | |
| 117 | + m.put("lbbc", c.getLbbc()); | |
| 118 | + m.put("ljbc", c.getLjbc()); | |
| 119 | + m.put("jhzlc", Arith.add(c.getJhyylc(), c.getJhfyylc())); | |
| 120 | + m.put("jhyylc", c.getJhyylc()); | |
| 121 | + m.put("jhfyylc", c.getJhfyylc()); | |
| 122 | + m.put("sjzlc", Arith.add(c.getSjyylc(), c.getSjfyylc())); | |
| 123 | + m.put("sjyylc", c.getSjyylc()); | |
| 124 | + m.put("sjfyylc", c.getSjfyylc()); | |
| 125 | + m.put("lblc", c.getLblc()); | |
| 126 | + m.put("ljyylc", c.getLjyylc()); | |
| 127 | + m.put("ljfyylc", c.getLjfyylc()); | |
| 128 | + resList.add(m); | |
| 129 | + } | |
| 130 | + | |
| 131 | + return resList; | |
| 132 | + } | |
| 133 | + | |
| 134 | + public CalcWaybill initCalcWaybill(){ | |
| 135 | + CalcWaybill calc = new CalcWaybill(); | |
| 136 | + calc.setJhyybc(0); | |
| 137 | + calc.setJhyylc(0d); | |
| 138 | + calc.setJhfyybc(0); | |
| 139 | + calc.setJhfyylc(0d); | |
| 140 | + calc.setSjyybc(0); | |
| 141 | + calc.setSjyylc(0d); | |
| 142 | + calc.setSjfyybc(0); | |
| 143 | + calc.setSjfyylc(0d); | |
| 144 | + calc.setLbbc(0); | |
| 145 | + calc.setLblc(0d); | |
| 146 | + calc.setLjbc(0); | |
| 147 | + calc.setLjyylc(0d); | |
| 148 | + calc.setLjfyylc(0d); | |
| 149 | + return calc; | |
| 150 | + } | |
| 151 | + | |
| 152 | + public CalcWaybill summation(CalcWaybill c1, CalcWaybill c2){ | |
| 153 | + c1.setJhyybc(c1.getJhyybc() + c2.getJhyybc()); | |
| 154 | + c1.setJhyylc(Arith.add(c1.getJhyylc(), c2.getJhyylc())); | |
| 155 | + c1.setJhfyybc(c1.getJhfyybc() + c2.getJhfyybc()); | |
| 156 | + c1.setJhfyylc(Arith.add(c1.getJhfyylc(), c2.getJhfyylc())); | |
| 157 | + c1.setSjyybc(c1.getSjyybc() + c2.getSjyybc()); | |
| 158 | + c1.setSjyylc(Arith.add(c1.getSjyylc(), c2.getSjyylc())); | |
| 159 | + c1.setSjyylc(Arith.add(c1.getSjyylc(), c2.getLjyylc())); | |
| 160 | + c1.setSjfyybc(c1.getSjfyybc() + c2.getSjfyybc()); | |
| 161 | + c1.setSjfyylc(Arith.add(c1.getSjfyylc(), c2.getSjfyylc())); | |
| 162 | + c1.setSjfyylc(Arith.add(c1.getSjfyylc(), c2.getLjfyylc())); | |
| 163 | + c1.setLbbc(c1.getLbbc() + c2.getLbbc()); | |
| 164 | + c1.setLblc(Arith.add(c1.getLblc(), c2.getLblc())); | |
| 165 | + c1.setLjbc(c1.getLjbc() + c2.getLjbc()); | |
| 166 | + c1.setLjyylc(Arith.add(c1.getLjyylc(), c2.getLjyylc())); | |
| 167 | + c1.setLjfyylc(Arith.add(c1.getLjfyylc(), c2.getLjfyylc())); | |
| 168 | + return c1; | |
| 169 | + } | |
| 170 | + | |
| 171 | + @Override | |
| 172 | + public List<Map<String, Object>> singledatatj(String line, | |
| 173 | + String date, String date2, | |
| 174 | + String tjtype, String cont, | |
| 175 | + String gsdm, String fgsdm, String sfdc) { | |
| 176 | + | |
| 177 | + List<Map<String, Object>> resList = new ArrayList<Map<String, Object>>(); | |
| 178 | + List<CalcWaybill> list = null; | |
| 179 | + | |
| 180 | + int flag = 0; | |
| 181 | + if("驾驶员".equals(tjtype)){ | |
| 182 | + flag = 1; | |
| 183 | + list = calcRepository.scheduleByJsy(line, date, date2, gsdm, fgsdm, cont); | |
| 184 | + } else if("售票员".equals(tjtype)){ | |
| 185 | + flag = 2; | |
| 186 | + list = calcRepository.scheduleBySpy(line, date, date2, gsdm, fgsdm, cont); | |
| 187 | + } else if("车辆自编号".equals(tjtype)){ | |
| 188 | + flag = 3; | |
| 189 | + if("1".equals(sfdc)){ | |
| 190 | + list = calcRepository.scheduleByZbh(line, date, date2, gsdm, fgsdm, cont, true); | |
| 191 | + } else if("0".equals(sfdc)){ | |
| 192 | + list = calcRepository.scheduleByZbh(line, date, date2, gsdm, fgsdm, cont, false); | |
| 193 | + } else { | |
| 194 | + list = calcRepository.scheduleByZbh(line, date, date2, gsdm, fgsdm, cont); | |
| 195 | + } | |
| 196 | + } | |
| 197 | + | |
| 198 | + List<CalcWaybill> calcList = new ArrayList<CalcWaybill>(); | |
| 199 | + Map<String, CalcWaybill> calcMap = new HashMap<String, CalcWaybill>(); | |
| 200 | + | |
| 201 | + CalcWaybill zjCalc = this.initCalcWaybill(); | |
| 202 | + zjCalc.setjName("合计"); | |
| 203 | + for(CalcWaybill c : list){ | |
| 204 | + String key = ""; | |
| 205 | + if(flag == 1){ | |
| 206 | + if(c.getjGh() != null && c.getjName() != null) | |
| 207 | + key += c.getjGh() + "/" + c.getjName(); | |
| 208 | + } else if(flag == 2){ | |
| 209 | + if(c.getsGh() != null && c.getsName() != null) | |
| 210 | + key += c.getsGh() + "/" + c.getsName(); | |
| 211 | + } else if(flag == 3){ | |
| 212 | + if(c.getCl() != null) | |
| 213 | + key += c.getCl(); | |
| 214 | + } | |
| 215 | + | |
| 216 | + if(key.trim().length() == 0 || "/".equals(key)) | |
| 217 | + continue; | |
| 218 | + | |
| 219 | + String jName = key; | |
| 220 | + key = c.getXl() + "/" + key; | |
| 221 | + | |
| 222 | + CalcWaybill calc = null; | |
| 223 | + if(calcMap.containsKey(key)){ | |
| 224 | + calc = calcMap.get(key); | |
| 225 | + } else { | |
| 226 | + calc = this.initCalcWaybill(); | |
| 227 | + calc.setXlName(c.getXlName()); | |
| 228 | + calc.setXl(c.getXl()); | |
| 229 | + calc.setjName(jName); | |
| 230 | + calcList.add(calc); | |
| 231 | + calcMap.put(key, calc); | |
| 232 | + } | |
| 233 | + | |
| 234 | + this.summation(calc, c); | |
| 235 | + | |
| 236 | + this.summation(zjCalc, c); | |
| 237 | + | |
| 238 | + } | |
| 239 | + calcList.add(zjCalc); | |
| 240 | + calcMap.put("合计", zjCalc); | |
| 241 | + | |
| 242 | + Map<String, Map<String, Object>> keyMap = new HashMap<String, Map<String, Object>>(); | |
| 243 | + for(CalcWaybill c : calcList){ | |
| 244 | + Map<String, Object> m = new HashMap<String, Object>(); | |
| 245 | + m.put("gS", BasicData.businessFgsCodeNameMap.get(fgsdm+"_"+gsdm)); | |
| 246 | + m.put("xl", c.getXl()); | |
| 247 | + m.put("xlName", c.getXlName()); | |
| 248 | + m.put("jName", c.getjName()); | |
| 249 | + m.put("jhyybc", c.getJhyybc()); | |
| 250 | + m.put("jhfyybc", c.getJhfyybc()); | |
| 251 | + m.put("sjyybc", c.getSjyybc()); | |
| 252 | + m.put("sjfyybc", c.getSjfyybc()); | |
| 253 | + m.put("lbbc", c.getLbbc()); | |
| 254 | + m.put("ljbc", c.getLjbc()); | |
| 255 | + m.put("jhzlc", Arith.add(c.getJhyylc(), c.getJhfyylc())); | |
| 256 | + m.put("jhyylc", c.getJhyylc()); | |
| 257 | + m.put("jhfyylc", c.getJhfyylc()); | |
| 258 | + m.put("sjzlc", Arith.add(c.getSjyylc(), c.getSjfyylc())); | |
| 259 | + m.put("sjyylc", c.getSjyylc()); | |
| 260 | + m.put("sjfyylc", c.getSjfyylc()); | |
| 261 | + m.put("lblc", c.getLblc()); | |
| 262 | + m.put("ljyylc", c.getLjyylc()); | |
| 263 | + m.put("ljfyylc", c.getLjfyylc()); | |
| 264 | + if(flag != 2){ | |
| 265 | + m.put("hyl", 0); | |
| 266 | + m.put("jzl", 0); | |
| 267 | + m.put("sh", 0); | |
| 268 | + } | |
| 269 | + resList.add(m); | |
| 270 | + keyMap.put(c.getXl() + "/" + c.getjName(), m); | |
| 271 | + } | |
| 272 | + | |
| 273 | + String linesql=""; | |
| 274 | + if(!line.equals("")){ | |
| 275 | + linesql +=" and xlbm ='"+line+"' "; | |
| 276 | + } | |
| 277 | + if(!gsdm.equals("")){ | |
| 278 | + linesql +=" and ssgsdm ='"+gsdm+"' "; | |
| 279 | + } | |
| 280 | + if(!fgsdm.equals("")){ | |
| 281 | + linesql +=" and fgsdm ='"+fgsdm+"' "; | |
| 282 | + } | |
| 283 | + String nysql="SELECT id,xlbm,nbbm,jsy,jzl as jzl,yh as yh,sh as sh,fgsdm FROM bsth_c_ylb" | |
| 284 | + + " WHERE rq >= '"+date+"' and rq <= '"+date2+"'" | |
| 285 | + + linesql | |
| 286 | + + " union" | |
| 287 | + + " SELECT id,xlbm,nbbm,jsy,cdl as jzl,hd as yh,sh as sh,fgsdm FROM bsth_c_dlb" | |
| 288 | + + " WHERE rq >= '"+date+"' and rq <= '"+date2+"'" | |
| 289 | + + linesql; | |
| 290 | + List<Singledata> listNy = jdbcTemplate.query(nysql, new RowMapper<Singledata>() { | |
| 291 | + @Override | |
| 292 | + public Singledata mapRow(ResultSet arg0, int arg1) throws SQLException { | |
| 293 | + Singledata sin = new Singledata(); | |
| 294 | + sin.setxL(arg0.getString("xlbm")); | |
| 295 | + sin.setJsy(arg0.getString("jsy")); | |
| 296 | + sin.setClzbh(arg0.getString("nbbm")); | |
| 297 | + sin.setJzl(arg0.getString("jzl")); | |
| 298 | + sin.setHyl(arg0.getString("yh")); | |
| 299 | + sin.setUnyyyl(arg0.getString("sh")); | |
| 300 | + sin.setgS(arg0.getString("fgsdm")); | |
| 301 | + return sin; | |
| 302 | + } | |
| 303 | + }); | |
| 304 | + | |
| 305 | + if(flag != 2){ | |
| 306 | + Map<String, Object> last = resList.get(resList.size()-1); | |
| 307 | + last.put("hyl", 0); | |
| 308 | + last.put("jzl", 0); | |
| 309 | + last.put("sh", 0); | |
| 310 | + | |
| 311 | + //统计油,电表中手动添加的或者有加注没里程的数据 | |
| 312 | + for (int i = 0; i < listNy.size(); i++) { | |
| 313 | + Singledata sin_=listNy.get(i); | |
| 314 | + String xl=sin_.getxL(); | |
| 315 | + String str = ""; | |
| 316 | + if(flag == 1){ | |
| 317 | + str = sin_.getJsy() + "/" + BasicData.allPerson.get(gsdm+"-"+sin_.getJsy()); | |
| 318 | + } else { | |
| 319 | + str = sin_.getClzbh(); | |
| 320 | + } | |
| 321 | +// boolean fages=true; | |
| 322 | + if(keyMap.containsKey(xl + "/" + str)){ | |
| 323 | + Map<String, Object> m = keyMap.get(xl + "/" + str); | |
| 324 | + m.put("hyl", Arith.add(m.get("hyl"), sin_.getHyl()!=null?sin_.getHyl():0)); | |
| 325 | + m.put("jzl", Arith.add(m.get("jzl"), sin_.getJzl()!=null?sin_.getJzl():0)); | |
| 326 | + m.put("sh", Arith.add(m.get("sh"), sin_.getUnyyyl()!=null?sin_.getUnyyyl():0)); | |
| 327 | + last.put("hyl", Arith.add(last.get("hyl"), sin_.getHyl()!=null?sin_.getHyl():0)); | |
| 328 | + last.put("jzl", Arith.add(last.get("jzl"), sin_.getJzl()!=null?sin_.getJzl():0)); | |
| 329 | + last.put("sh", Arith.add(last.get("sh"), sin_.getUnyyyl()!=null?sin_.getUnyyyl():0)); | |
| 330 | + } else { | |
| 331 | + Map<String, Object> m = new HashMap<String, Object>(); | |
| 332 | + m.put("gS", BasicData.businessFgsCodeNameMap.get(fgsdm+"_"+gsdm)); | |
| 333 | + m.put("xl", xl); | |
| 334 | + m.put("xlName", BasicData.lineCodeAllNameMap.get(xl)); | |
| 335 | + m.put("jName", str); | |
| 336 | + m.put("jhzlc", 0); | |
| 337 | + m.put("sjzlc", 0); | |
| 338 | + m.put("sjfyylc", 0); | |
| 339 | + m.put("hyl", sin_.getHyl()!=null?sin_.getHyl():0); | |
| 340 | + m.put("jzl", sin_.getJzl()!=null?sin_.getJzl():0); | |
| 341 | + m.put("sh", sin_.getUnyyyl()!=null?sin_.getUnyyyl():0); | |
| 342 | + last.put("hyl", Arith.add(last.get("hyl"), m.get("hyl"))); | |
| 343 | + last.put("jzl", Arith.add(last.get("jzl"), m.get("jzl"))); | |
| 344 | + last.put("sh", Arith.add(last.get("sh"), m.get("sh"))); | |
| 345 | + | |
| 346 | + keyMap.put(xl + "/" + str, m); | |
| 347 | + } | |
| 348 | + } | |
| 349 | + resList.remove(last); | |
| 350 | + resList.add(last); | |
| 351 | + } | |
| 352 | + | |
| 353 | + return resList; | |
| 354 | + } | |
| 355 | + | |
| 356 | + //浦东公交线路调查表 | |
| 357 | + @Override | |
| 358 | + public List<CalcInvestigateMonth> calcInvestigateMonth(String gsbm, String fgsbm, String month, String line, | |
| 359 | + String xlName, String nature, String type) { | |
| 360 | + List<CalcInvestigateMonth> resList = new ArrayList<CalcInvestigateMonth>(); | |
| 361 | + | |
| 362 | + Map<String, Boolean> lineNature = lineService.lineNature(); | |
| 363 | + | |
| 364 | + List<CalcInvestigateMonth> list = new ArrayList<CalcInvestigateMonth>(); | |
| 365 | + if(line != null && line.length() > 0){ | |
| 366 | + list = calcInvestigateMonthRepository.findByMonthAndLine(month, line); | |
| 367 | + } else if(gsbm != null && gsbm.length() > 0){ | |
| 368 | + list = calcInvestigateMonthRepository.findByMonth(month, gsbm, fgsbm); | |
| 369 | + } else { | |
| 370 | + list = calcInvestigateMonthRepository.findByMonth(month); | |
| 371 | + } | |
| 372 | + | |
| 373 | + for(CalcInvestigateMonth c : list){ | |
| 374 | + if("1".equals(nature)){ | |
| 375 | + if(lineNature.containsKey(c.getXl()) && lineNature.get(c.getXl())){ | |
| 376 | + resList.add(c); | |
| 377 | + } | |
| 378 | + } else if("2".equals(nature)){ | |
| 379 | + if(!lineNature.containsKey(c.getXl()) || !lineNature.get(c.getXl())){ | |
| 380 | + resList.add(c); | |
| 381 | + } | |
| 382 | + } else { | |
| 383 | + resList.add(c); | |
| 384 | + } | |
| 385 | + } | |
| 386 | + | |
| 387 | + if (type != null && type.length() != 0 && type.equals("export")) { | |
| 388 | + SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyy-MM-dd"), | |
| 389 | + sdfSimple = new SimpleDateFormat("yyyyMMdd"), | |
| 390 | + monSimple = new SimpleDateFormat("yyyy-MM"); | |
| 391 | + List<Iterator<?>> listI = new ArrayList<Iterator<?>>(); | |
| 392 | + Map<String, Object> m = new HashMap<String, Object>(); | |
| 393 | + m.put("date", month); | |
| 394 | + m.put("currMonth", "(" + month + ")"); | |
| 395 | + ReportUtils ee = new ReportUtils(); | |
| 396 | + try { | |
| 397 | + List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>(); | |
| 398 | + int row = 0; | |
| 399 | + for(CalcInvestigateMonth c : resList){ | |
| 400 | + Map<String, Object> map = new HashMap<String, Object>(); | |
| 401 | + map.put("row", ++row); | |
| 402 | + map.put("xlmc", c.getXlmc()!=null?c.getXlmc():""); | |
| 403 | + map.put("qzpcs", c.getQzpcs()!=null?c.getQzpcs():""); | |
| 404 | + map.put("xlsx", c.getXlsx()!=null?c.getXlsx():""); | |
| 405 | + map.put("xllx", c.getXllx()!=null?c.getXllx():""); | |
| 406 | + map.put("xlcd", c.getXlcd()!=null?c.getXlcd():""); | |
| 407 | + map.put("sfgp", c.getSfgp()!=null?c.getSfgp():""); | |
| 408 | + map.put("sflpxl", c.getSflpxl()!=null?c.getSflpxl():""); | |
| 409 | + map.put("smbsj", c.getSmbsj()!=null?c.getSmbsj():""); | |
| 410 | + if("常规".equals(c.getDdfs())){ | |
| 411 | + map.put("ddfsCg", "√"); | |
| 412 | + map.put("ddfsDyh", ""); | |
| 413 | + } else if("多样化".equals(c.getDdfs())){ | |
| 414 | + map.put("ddfsCg", ""); | |
| 415 | + map.put("ddfsDyh", "√"); | |
| 416 | + } else { | |
| 417 | + map.put("ddfsCg", ""); | |
| 418 | + map.put("ddfsDyh", ""); | |
| 419 | + } | |
| 420 | + map.put("gfjgsj", c.getGfjgsj()!=null?c.getGfjgsj():""); | |
| 421 | + map.put("dgjgsj", c.getDgjgsj()!=null?c.getDgjgsj():""); | |
| 422 | + map.put("sfxjgj", c.getSfxjgj()!=null?c.getSfxjgj():""); | |
| 423 | + map.put("rjlc", c.getRjlc()!=null?c.getRjlc():""); | |
| 424 | + map.put("rjbc", c.getRjbc()!=null?c.getRjbc():""); | |
| 425 | + map.put("rjrc", c.getRjrc()!=null?c.getRjrc():""); | |
| 426 | + map.put("rjdcrc", c.getRjdcrc()!=null?c.getRjdcrc():""); | |
| 427 | + map.put("mbcrc", c.getMbcrc()!=null?c.getMbcrc():""); | |
| 428 | + map.put("jhzgl", c.getJhzgl()!=null?c.getJhzgl():""); | |
| 429 | + map.put("sjzgl", c.getSjzgl()!=null?c.getSjzgl():""); | |
| 430 | + map.put("sjyylc", c.getSjyylc()!=null?c.getSjyylc():""); | |
| 431 | + map.put("lclyl", c.getLclyl()!=null?c.getLclyl():""); | |
| 432 | + map.put("bglrc", c.getBglrc()!=null?c.getBglrc():""); | |
| 433 | + map.put("yrc", c.getYrc()!=null?c.getYrc():""); | |
| 434 | + map.put("yys", c.getYys()!=null?c.getYys():""); | |
| 435 | + map.put("pj", c.getPj()!=null?c.getPj():""); | |
| 436 | + map.put("bglys", c.getBglys()!=null?c.getBglys():""); | |
| 437 | + map.put("tjgjz", c.getTjgjz()!=null?c.getTjgjz():""); | |
| 438 | + map.put("tjsjyy", c.getTjsjyy()!=null?c.getTjsjyy():""); | |
| 439 | + map.put("wyx", c.getWyx()!=null?c.getWyx():""); | |
| 440 | + map.put("xlzx", c.getXlzx()!=null?c.getXlzx():""); | |
| 441 | + map.put("xlzd", c.getXlzd()!=null?c.getXlzd():""); | |
| 442 | + map.put("bz", c.getBz()!=null?c.getBz():""); | |
| 443 | + mapList.add(map); | |
| 444 | + } | |
| 445 | + listI.add(mapList.iterator()); | |
| 446 | + String path = this.getClass().getResource("/").getPath() + "static/pages/forms/"; | |
| 447 | + ee.excelReplace(listI, new Object[]{m}, path + "mould/calcInvestigateMonth.xls", | |
| 448 | + path + "export/" + "浦东公交线路调查表"+month+".xls"); | |
| 449 | + } catch (Exception e) { | |
| 450 | + // TODO: handle exception | |
| 451 | + e.printStackTrace(); | |
| 452 | + logger.info("" , e); | |
| 453 | + } | |
| 454 | + } | |
| 455 | + return resList; | |
| 456 | + } | |
| 457 | + | |
| 458 | +} | ... | ... |
src/main/java/com/bsth/util/ReportUtils.java
| 1 | -package com.bsth.util; | |
| 2 | - | |
| 3 | -import java.io.File; | |
| 4 | -import java.io.FileInputStream; | |
| 5 | -import java.io.FileOutputStream; | |
| 6 | -import java.util.ArrayList; | |
| 7 | -import java.util.HashMap; | |
| 8 | -import java.util.Iterator; | |
| 9 | -import java.util.List; | |
| 10 | -import java.util.Map; | |
| 11 | -import java.util.TreeMap; | |
| 12 | -import java.util.regex.Matcher; | |
| 13 | -import java.util.regex.Pattern; | |
| 14 | - | |
| 15 | -import com.bsth.common.ResponseCode; | |
| 16 | -import org.apache.poi.hssf.usermodel.HSSFCell; | |
| 17 | -import org.apache.poi.hssf.usermodel.HSSFCellStyle; | |
| 18 | -import org.apache.poi.hssf.usermodel.HSSFFont; | |
| 19 | -import org.apache.poi.hssf.usermodel.HSSFRow; | |
| 20 | -import org.apache.poi.hssf.usermodel.HSSFSheet; | |
| 21 | -import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |
| 22 | -import org.apache.poi.poifs.filesystem.POIFSFileSystem; | |
| 23 | -import org.apache.poi.ss.usermodel.Cell; | |
| 24 | -import org.apache.poi.ss.usermodel.Workbook; | |
| 25 | -import org.apache.poi.ss.util.CellRangeAddress; | |
| 26 | - | |
| 27 | -import com.bsth.entity.Line; | |
| 28 | -import com.bsth.entity.realcontrol.ScheduleRealInfo; | |
| 29 | -import org.apache.poi.ss.util.RegionUtil; | |
| 30 | -import org.apache.poi.xssf.usermodel.XSSFCell; | |
| 31 | - | |
| 32 | -public class ReportUtils { | |
| 33 | - // private final String packaegName = "com.bsth.entity."; | |
| 34 | - private final String packaegName = "com.bsth.entity.realcontrol."; | |
| 35 | - | |
| 36 | - /** | |
| 37 | - * /** | |
| 38 | - * | |
| 39 | - * @param list | |
| 40 | - * 模板中,需要重复显示的行所需的数据 | |
| 41 | - * @param map | |
| 42 | - * 模板中除以上list外所有的数据 | |
| 43 | - * @param index | |
| 44 | - * 需要重复的行号,该值为行号减1 | |
| 45 | - * @param sourcePath | |
| 46 | - * 模板路径 | |
| 47 | - * @param targetPath | |
| 48 | - * 生成路径 | |
| 49 | - */ | |
| 50 | - public void excelReplace(List<Iterator<?>> list, Object[] tArray, | |
| 51 | - String sourcePath, String targetPath) { | |
| 52 | - try { | |
| 53 | - // 把源文件放入流中 | |
| 54 | - POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream( | |
| 55 | - sourcePath)); | |
| 56 | - HSSFWorkbook wb = new HSSFWorkbook(fs); | |
| 57 | - if(tArray.length != 0 && tArray[0] instanceof java.util.Map){ | |
| 58 | - Map<String, Object> m = (Map<String, Object>)tArray[0]; | |
| 59 | - if(m.containsKey("sheetName") && m.get("sheetName")!=null | |
| 60 | - && m.get("sheetName").toString().trim().length()!=0) | |
| 61 | - wb.setSheetName(0, m.get("sheetName").toString()); | |
| 62 | - } | |
| 63 | - HSSFSheet sheet = wb.getSheetAt(0); | |
| 64 | - HSSFRow row; | |
| 65 | - HSSFCell cell = null; | |
| 66 | - String key; | |
| 67 | - // 取得总行数 | |
| 68 | - int rowNum = sheet.getLastRowNum(); | |
| 69 | - // 取得总列数 | |
| 70 | - int cellNum = sheet.getRow(0).getLastCellNum(); | |
| 71 | - | |
| 72 | - // 遍历行 | |
| 73 | - for (int i = 0; i < rowNum; i++) { | |
| 74 | - row = sheet.getRow(i); | |
| 75 | - // 遍历列 | |
| 76 | - for (int j = 0; j < cellNum; j++) { | |
| 77 | - if (row == null) { | |
| 78 | - continue; | |
| 79 | - } | |
| 80 | - cell = row.getCell(j); | |
| 81 | - if (cell == null) { | |
| 82 | - continue; | |
| 83 | - } | |
| 84 | - // 取得每列的内容,如果列内容是$key$格式,则替换内容 | |
| 85 | - key = getCellValue(cell); | |
| 86 | - if (key != null && (key.indexOf("$") != -1 || key.indexOf("#list#") != -1)) { | |
| 87 | - // * 列中内容有#list#,则表示该行为模板行,需要以该行为模板 | |
| 88 | - // * 例如:模板行格式 #list#0_0 $Car.id$ | |
| 89 | - // * 第一个0表示需要在list中取iterator的索引值 | |
| 90 | - // * 第二个0表示在iterator中取的第几个对象,如果不为0,则取下一个对象,而不是沿用前面获取的对象 | |
| 91 | - // * $Car.id$表示所取的对象为Car的对象,并且取值为id的值 | |
| 92 | - if (key.indexOf("#list#") != -1) { | |
| 93 | - key = key.replace("#list#", "").trim(); | |
| 94 | - String[] lists = key.split(" "); | |
| 95 | - // 取得list中的索引值 | |
| 96 | - int listIndex = Integer | |
| 97 | - .valueOf(lists[0].split("_")[0]); | |
| 98 | - Iterator<?> iterator = list.get(listIndex); | |
| 99 | - // 根据模板创建行并填弃数据,返回增加的行数 | |
| 100 | - int rowCount = iteratorFillCellValue(wb, sheet, | |
| 101 | - cell, iterator, i, rowNum, key); | |
| 102 | - rowNum += rowCount; | |
| 103 | - i += rowCount; | |
| 104 | - break; | |
| 105 | - } else { | |
| 106 | - // 直接填充数据的列,从对象数组中取得值,这里的数组不传值 | |
| 107 | - getValueAndSetCellValue(cell, key, tArray, new String[]{""}); | |
| 108 | - } | |
| 109 | - } | |
| 110 | - | |
| 111 | - } | |
| 112 | - } | |
| 113 | - // 创建目标文件夹 | |
| 114 | - createFolder(targetPath); | |
| 115 | - // 输出文件 | |
| 116 | - FileOutputStream fileOut = new FileOutputStream(targetPath); | |
| 117 | - wb.write(fileOut); | |
| 118 | - fileOut.close(); | |
| 119 | - } catch (Exception e) { | |
| 120 | - e.printStackTrace(); | |
| 121 | - } | |
| 122 | - } | |
| 123 | - | |
| 124 | - | |
| 125 | - /** | |
| 126 | - * | |
| 127 | - * @param sheetList 多sheet模板中,需要重复显示的行所需的数据 | |
| 128 | - * @param tArray | |
| 129 | - * @param sourcePath 模板路径 | |
| 130 | - * @param targetPath 生成路径 | |
| 131 | - */ | |
| 132 | - public void excelMoreSheetReplace(List<List<Iterator<?>>> sheetList, Object[] tArray, | |
| 133 | - String sourcePath, String targetPath) { | |
| 134 | - try { | |
| 135 | - // 把源文件放入流中 | |
| 136 | - POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream( | |
| 137 | - sourcePath)); | |
| 138 | - HSSFWorkbook wb = new HSSFWorkbook(fs); | |
| 139 | - for (int s=0; s<sheetList.size(); s++){ | |
| 140 | - List<Iterator<?>> list = sheetList.get(s); | |
| 141 | - if(tArray.length != 0 && tArray[0] instanceof java.util.Map){ | |
| 142 | - Map<String, Object> m = (Map<String, Object>)tArray[0]; | |
| 143 | - if(m.containsKey("sheetName"+s+1) && m.get("sheetName"+s+1)!=null | |
| 144 | - && m.get("sheetName"+s+1).toString().trim().length()!=0) | |
| 145 | - wb.setSheetName(0, m.get("sheetName"+s+1).toString()); | |
| 146 | - } | |
| 147 | - HSSFSheet sheet = wb.getSheetAt(s); | |
| 148 | - HSSFRow row; | |
| 149 | - HSSFCell cell = null; | |
| 150 | - String key; | |
| 151 | - // 取得总行数 | |
| 152 | - int rowNum = sheet.getLastRowNum(); | |
| 153 | - // 取得总列数 | |
| 154 | - int cellNum = sheet.getRow(0).getLastCellNum(); | |
| 155 | - | |
| 156 | - // 遍历行 | |
| 157 | - for (int i = 0; i <= rowNum; i++) { | |
| 158 | - row = sheet.getRow(i); | |
| 159 | - // 遍历列 | |
| 160 | - for (int j = 0; j < cellNum; j++) { | |
| 161 | - if (row == null) { | |
| 162 | - continue; | |
| 163 | - } | |
| 164 | - cell = row.getCell(j); | |
| 165 | - if (cell == null) { | |
| 166 | - continue; | |
| 167 | - } | |
| 168 | - // 取得每列的内容,如果列内容是$key$格式,则替换内容 | |
| 169 | - key = getCellValue(cell); | |
| 170 | - if (key != null && (key.indexOf("$") != -1 || key.indexOf("#list#") != -1)) { | |
| 171 | - // * 列中内容有#list#,则表示该行为模板行,需要以该行为模板 | |
| 172 | - // * 例如:模板行格式 #list#0_0 $Car.id$ | |
| 173 | - // * 第一个0表示需要在list中取iterator的索引值 | |
| 174 | - // * 第二个0表示在iterator中取的第几个对象,如果不为0,则取下一个对象,而不是沿用前面获取的对象 | |
| 175 | - // * $Car.id$表示所取的对象为Car的对象,并且取值为id的值 | |
| 176 | - if (key.indexOf("#list#") != -1) { | |
| 177 | - key = key.replace("#list#", "").trim(); | |
| 178 | - String[] lists = key.split(" "); | |
| 179 | - // 取得list中的索引值 | |
| 180 | - int listIndex = Integer | |
| 181 | - .valueOf(lists[0].split("_")[0]); | |
| 182 | - Iterator<?> iterator = list.get(listIndex); | |
| 183 | - // 根据模板创建行并填充数据,返回增加的行数 | |
| 184 | - int rowCount = iteratorFillCellValue(wb, sheet, | |
| 185 | - cell, iterator, i, rowNum, key); | |
| 186 | - rowNum += rowCount; | |
| 187 | - i += rowCount; | |
| 188 | - break; | |
| 189 | - } else { | |
| 190 | - // 直接填充数据的列,从对象数组中取得值,这里的数组不传值 | |
| 191 | - getValueAndSetCellValue(cell, key, tArray, new String[]{""}); | |
| 192 | - } | |
| 193 | - } | |
| 194 | - | |
| 195 | - } | |
| 196 | - } | |
| 197 | - } | |
| 198 | - | |
| 199 | - // 创建目标文件夹 | |
| 200 | - createFolder(targetPath); | |
| 201 | - // 输出文件 | |
| 202 | - FileOutputStream fileOut = new FileOutputStream(targetPath); | |
| 203 | - wb.write(fileOut); | |
| 204 | - fileOut.close(); | |
| 205 | - } catch (Exception e) { | |
| 206 | - e.printStackTrace(); | |
| 207 | - } | |
| 208 | - } | |
| 209 | - | |
| 210 | - /** | |
| 211 | - * 将file1中的一页sheet复制到file2中 | |
| 212 | - * | |
| 213 | - * @param file1 | |
| 214 | - * 原sheet所在的excel文件 | |
| 215 | - * @param file2 | |
| 216 | - * 目标excel文件 | |
| 217 | - * @param page | |
| 218 | - * 原excel中要被复制的sheet的位置(从0开始) | |
| 219 | - * @param rate | |
| 220 | - * 调整复制后的缩放倍率(列如:145,则为缩放145%) | |
| 221 | - */ | |
| 222 | - public void copySheetByFile(File file1, File file2, int page, int rate) { | |
| 223 | - try { | |
| 224 | - // 把源文件放入流中 | |
| 225 | - POIFSFileSystem fs1 = new POIFSFileSystem(new FileInputStream(file1)); | |
| 226 | - HSSFWorkbook wb1 = new HSSFWorkbook(fs1); | |
| 227 | - HSSFSheet sheet = wb1.getSheetAt(page); | |
| 228 | - POIFSFileSystem fs2 = new POIFSFileSystem(new FileInputStream(file2)); | |
| 229 | - HSSFWorkbook wb2 = new HSSFWorkbook(fs2); | |
| 230 | - HSSFSheet createSheet = wb2.createSheet(sheet.getSheetName()); | |
| 231 | - HSSFCellStyle createCellStyle = wb2.createCellStyle(); | |
| 232 | - HSSFRow row; | |
| 233 | - | |
| 234 | - createSheet.setZoom(rate, 100); | |
| 235 | - for(int i = 0; i < sheet.getRow(0).getPhysicalNumberOfCells(); i++){ | |
| 236 | - createSheet.setColumnWidth(i, sheet.getColumnWidth(i)); | |
| 237 | - } | |
| 238 | - | |
| 239 | - List<CellRangeAddress> mergedRegions = sheet.getMergedRegions(); | |
| 240 | - for(int l = 0; l < mergedRegions.size(); l++){ | |
| 241 | - //复制源表中的合并单元格 | |
| 242 | - createSheet.addMergedRegion(mergedRegions.get(l)); | |
| 243 | - } | |
| 244 | - int firstRow = sheet.getFirstRowNum(); | |
| 245 | - int lastRow = sheet.getLastRowNum(); | |
| 246 | - for(int k = firstRow; k <= lastRow; k++){ | |
| 247 | - // 创建新建excel Sheet的行 | |
| 248 | - HSSFRow rowCreat = createSheet.createRow(k); | |
| 249 | - // 取得源有excel Sheet的行 | |
| 250 | - row = sheet.getRow(k); | |
| 251 | -// rowCreat.setHeight(row.getHeight()); //设置行高 | |
| 252 | - // 单元格式样 | |
| 253 | - int firstCell = row.getFirstCellNum(); | |
| 254 | - int lastCell = row.getLastCellNum(); | |
| 255 | - for (int j = firstCell; j < lastCell; j++) { | |
| 256 | - // 自动适应列宽 貌似不起作用 | |
| 257 | -// createSheet.autoSizeColumn(j); | |
| 258 | -// System.out.println(row.getCell(j)); | |
| 259 | - rowCreat.createCell(j); | |
| 260 | - String strVal = ""; | |
| 261 | - if (row.getCell(j)==null) { | |
| 262 | - | |
| 263 | - } else { | |
| 264 | - strVal = row.getCell(j).getStringCellValue(); | |
| 265 | - rowCreat.getCell(j).setCellValue(strVal); | |
| 266 | - copyCellStyle(wb1, row.getCell(j).getCellStyle(), createCellStyle); | |
| 267 | - createCellStyle.setBorderTop((short)1); | |
| 268 | - createCellStyle.setBorderLeft((short)1); | |
| 269 | - createCellStyle.setBorderRight((short)1); | |
| 270 | - createCellStyle.setBorderBottom((short)1); | |
| 271 | - rowCreat.getCell(j).setCellStyle(createCellStyle); | |
| 272 | - } | |
| 273 | - } | |
| 274 | - } | |
| 275 | - | |
| 276 | -// int firstRowNum = createSheet.getFirstRowNum(); | |
| 277 | -// int lastRowNum = createSheet.getLastRowNum(); | |
| 278 | -// int test = 0; | |
| 279 | -// for(int k = firstRowNum; k <= lastRowNum; k++){ | |
| 280 | -// HSSFRow createRow = createSheet.getRow(k); | |
| 281 | -// int firstCellNum = createRow.getFirstCellNum(); | |
| 282 | -// int lastCellNum = createRow.getLastCellNum(); | |
| 283 | -// for(int i = firstCellNum; i < lastCellNum; i++){ | |
| 284 | -// HSSFCell cell = createRow.getCell(i); | |
| 285 | -// cell.getCellStyle().setBorderTop(HSSFCellStyle.BORDER_THIN); | |
| 286 | -// cell.getCellStyle().setBorderLeft(HSSFCellStyle.BORDER_THIN); | |
| 287 | -// cell.getCellStyle().setBorderRight(HSSFCellStyle.BORDER_THIN); | |
| 288 | -// cell.getCellStyle().setBorderBottom(HSSFCellStyle.BORDER_THIN); | |
| 289 | -// test ++; | |
| 290 | -// } | |
| 291 | -// } | |
| 292 | -// System.out.println("test = " + test); | |
| 293 | - | |
| 294 | - FileOutputStream fileOut = new FileOutputStream(file2); | |
| 295 | - wb2.write(fileOut); | |
| 296 | - fileOut.close(); | |
| 297 | - wb2.close(); | |
| 298 | - wb1.close(); | |
| 299 | - fs2.close(); | |
| 300 | - fs1.close(); | |
| 301 | - file1.delete(); | |
| 302 | -// // 创建目标文件夹 | |
| 303 | -// createFolder(targetPath); | |
| 304 | - // 输出文件 | |
| 305 | - } catch (Exception e) { | |
| 306 | - e.printStackTrace(); | |
| 307 | - } | |
| 308 | - } | |
| 309 | - | |
| 310 | - public void test(File file){ | |
| 311 | - POIFSFileSystem fs; | |
| 312 | - try { | |
| 313 | - fs = new POIFSFileSystem(new FileInputStream(file)); | |
| 314 | - HSSFWorkbook wb = new HSSFWorkbook(fs); | |
| 315 | - for(int j = 0; j < wb.getNumberOfSheets(); j++){ | |
| 316 | - HSSFSheet sheet = wb.getSheetAt(j); | |
| 317 | - int firstRowNum = sheet.getFirstRowNum(); | |
| 318 | - int lastRowNum = sheet.getLastRowNum(); | |
| 319 | - int test = 0; | |
| 320 | - for(int k = firstRowNum; k <= lastRowNum; k++){ | |
| 321 | - HSSFRow createRow = sheet.getRow(k); | |
| 322 | - int firstCellNum = createRow.getFirstCellNum(); | |
| 323 | - int lastCellNum = createRow.getLastCellNum(); | |
| 324 | - for(int i = firstCellNum; i < lastCellNum; i++){ | |
| 325 | - HSSFCell cell = createRow.getCell(i); | |
| 326 | - HSSFCellStyle cellStyle = wb.createCellStyle(); | |
| 327 | - | |
| 328 | - cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); | |
| 329 | - cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); | |
| 330 | - cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); | |
| 331 | - cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); | |
| 332 | - cell.setCellStyle(cellStyle); | |
| 333 | - test ++; | |
| 334 | - } | |
| 335 | - } | |
| 336 | - System.out.println("test = " + test); | |
| 337 | - | |
| 338 | - FileOutputStream fileOut = new FileOutputStream(file); | |
| 339 | - wb.write(fileOut); | |
| 340 | - fileOut.close(); | |
| 341 | - } | |
| 342 | - } catch (Exception e) { | |
| 343 | - // TODO Auto-generated catch block | |
| 344 | - e.printStackTrace(); | |
| 345 | - } | |
| 346 | - | |
| 347 | - } | |
| 348 | - | |
| 349 | - public String getCellValue(HSSFCell cell) { | |
| 350 | - int cellType = 0; | |
| 351 | - String result = ""; | |
| 352 | - double d; | |
| 353 | - if(cell != null) { | |
| 354 | - // 获取列数据类型 | |
| 355 | - cellType = cell.getCellType(); | |
| 356 | - // 不同列数据类型,取值方法不同 | |
| 357 | - switch(cellType) { | |
| 358 | - // String | |
| 359 | - case HSSFCell.CELL_TYPE_STRING: | |
| 360 | - result = cell.getStringCellValue().toString(); | |
| 361 | - break; | |
| 362 | - // numeric类型,excel中,日期格式会转成数字格式存储 | |
| 363 | - case HSSFCell.CELL_TYPE_NUMERIC: | |
| 364 | - result = cell.getNumericCellValue()+""; | |
| 365 | - break; | |
| 366 | - // 公式类型 | |
| 367 | - case HSSFCell.CELL_TYPE_FORMULA: | |
| 368 | - result = cell.getCellFormula() + ""; | |
| 369 | - break; | |
| 370 | - // boolean类型 | |
| 371 | - case HSSFCell.CELL_TYPE_BOOLEAN: | |
| 372 | - result = cell.getBooleanCellValue() + ""; | |
| 373 | - break; | |
| 374 | - // 空格 | |
| 375 | - case HSSFCell.CELL_TYPE_BLANK: | |
| 376 | - result = null; | |
| 377 | - break; | |
| 378 | - // 错误值 | |
| 379 | - case HSSFCell.CELL_TYPE_ERROR: | |
| 380 | - result = null; | |
| 381 | - break; | |
| 382 | - default : | |
| 383 | - System.out.println("其它"); | |
| 384 | - break; | |
| 385 | - } | |
| 386 | - } | |
| 387 | - return result; | |
| 388 | - } | |
| 389 | - | |
| 390 | - /** | |
| 391 | - * 根据iterator,以及模板中的标识,填充模板 | |
| 392 | - * | |
| 393 | - * @param wb | |
| 394 | - * @param sheet | |
| 395 | - * @param cell | |
| 396 | - * @param iterator | |
| 397 | - * iterator | |
| 398 | - * @param index | |
| 399 | - * 模板行索引 | |
| 400 | - * @param rowNum | |
| 401 | - * 表格总行数 | |
| 402 | - * @param key | |
| 403 | - * 表格内容 | |
| 404 | - * @return | |
| 405 | - */ | |
| 406 | - private int iteratorFillCellValue(HSSFWorkbook wb, HSSFSheet sheet, | |
| 407 | - HSSFCell cell, Iterator<?> iterator, int index, int rowNum, | |
| 408 | - String key) { | |
| 409 | - int rowCount = 0; | |
| 410 | - Object obj = null; | |
| 411 | - int p = 0; | |
| 412 | - int i = index; | |
| 413 | - HSSFRow newRow = null; | |
| 414 | - int tmpCellNum = 0; | |
| 415 | - int k = 0; | |
| 416 | - int listIndex = 0; | |
| 417 | - // 取得模板行 | |
| 418 | - HSSFRow orgRow = sheet.getRow(index); | |
| 419 | - HSSFCellStyle style= wb.createCellStyle(); | |
| 420 | - try { | |
| 421 | - while (iterator.hasNext()) { | |
| 422 | - // 取得iterator的对象 | |
| 423 | - obj = iterator.next(); | |
| 424 | - // 移动当前编辑行以下的所有行 | |
| 425 | - if (p != 0) { | |
| 426 | - rowNum += 1; | |
| 427 | - i += 1; | |
| 428 | - rowCount += 1;// 增加的总行数 | |
| 429 | - // 把当前行以下的所有行往下移动1行 | |
| 430 | - sheet.shiftRows(i, rowNum, 1); | |
| 431 | - } | |
| 432 | - p = 1; | |
| 433 | - // 创建新行 | |
| 434 | - newRow = sheet.createRow(index + k++); | |
| 435 | - // 把新行的内容换成和模板行一样 | |
| 436 | - copyRow(wb, orgRow, newRow, true,style); | |
| 437 | - tmpCellNum = newRow.getLastCellNum(); | |
| 438 | - for (int l = 0; l < tmpCellNum; l++) { | |
| 439 | - cell = newRow.getCell(l); | |
| 440 | - key = getCellValue(cell); | |
| 441 | - /** | |
| 442 | - * 如果单无格内容为#list#,表示该行是模板行 #list#0_0 | |
| 443 | - * 第一个0表示需要在list中取iterator的索引值 | |
| 444 | - * 第二个0表示在iterator中取的第几个对象,如果不为0,则取下一个对象,而不是沿用前面获取的对象 | |
| 445 | - */ | |
| 446 | - if(key == null || key.equals("")){ | |
| 447 | - obj = iterator.next(); | |
| 448 | - } | |
| 449 | - if (key.indexOf("#list#") != -1 && key.indexOf("_0") == -1) { | |
| 450 | - if (iterator.hasNext()) { | |
| 451 | - obj = iterator.next(); | |
| 452 | - } else { | |
| 453 | - obj = null; | |
| 454 | - } | |
| 455 | - } | |
| 456 | - if (key.trim().indexOf(" ") != -1) { | |
| 457 | - key = key.split(" ")[1]; | |
| 458 | - } | |
| 459 | - getValueAndSetCellValue(cell, key, obj,new String[]{listIndex+""}); | |
| 460 | - } | |
| 461 | - // list的数量 | |
| 462 | - listIndex ++; | |
| 463 | - } | |
| 464 | - } catch (Exception e) { | |
| 465 | - e.printStackTrace(); | |
| 466 | - } | |
| 467 | - return rowCount; | |
| 468 | - } | |
| 469 | - | |
| 470 | - /** | |
| 471 | - * 取到相应的值并填入相应的列中 | |
| 472 | - * | |
| 473 | - * @param cell 列 | |
| 474 | - * @param key 列内容 | |
| 475 | - * @param obj 数据源对象 | |
| 476 | - * @param args 其他参数 数组 | |
| 477 | - * 数组内容: | |
| 478 | - * 0位:在list中的第几条数据 | |
| 479 | - */ | |
| 480 | - private void getValueAndSetCellValue(HSSFCell cell, String key, Object obj, String[] args) { | |
| 481 | - try { | |
| 482 | - // 保有存单元格的内容 | |
| 483 | - String cellValue = key = key.replace("\\n", ""); | |
| 484 | - String tmpKey; | |
| 485 | - // 判断单元格内容是否是公式 | |
| 486 | - if(cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA){ | |
| 487 | - Pattern p=Pattern.compile("(\\d+)"); // | |
| 488 | - Matcher m=p.matcher(key); | |
| 489 | - if(m.find()){ | |
| 490 | - cellValue = key.replace(m.group(1), | |
| 491 | - Integer.valueOf(m.group(1))+Integer.valueOf(args[0])+""); | |
| 492 | - cell.setCellFormula(cellValue); | |
| 493 | - return; | |
| 494 | - } | |
| 495 | - }else{//其他格式 | |
| 496 | - | |
| 497 | - // 循环截取两个$中间的内容,反射出值 | |
| 498 | - while (key.indexOf("$") != -1) { | |
| 499 | - key = key.substring(key.indexOf("$") + 1); | |
| 500 | - // 取两个$中间的内容 | |
| 501 | - tmpKey = key.substring(0, key.indexOf("$")); | |
| 502 | - key = key.substring(key.indexOf("$") + 1); | |
| 503 | - // 如果内容是如下格式Cars.id,则从obj中值得相应的对象的值 | |
| 504 | - if (tmpKey.indexOf(".") != -1) { | |
| 505 | - String className = tmpKey.substring(0, tmpKey.indexOf(".")); | |
| 506 | - // 取得类的全限定名 | |
| 507 | - String classWholeName = packaegName + className; | |
| 508 | - String fieldName = tmpKey.substring(tmpKey.indexOf(".") + 1); | |
| 509 | - // 如果obj是数组,循环判断哪个对象是对应的 | |
| 510 | - if (obj instanceof Object[]) { | |
| 511 | - Object[] objs = (Object[]) obj; | |
| 512 | - for (int k = 0; k < objs.length; k++) { | |
| 513 | - if (objs[k].getClass().getName().equals(classWholeName)) { | |
| 514 | - cellValue = cellValue.replace("$" + tmpKey | |
| 515 | - + "$", getKeyValue(objs[k], fieldName) | |
| 516 | - + ""); | |
| 517 | - } else if(objs[k].getClass().getName().equals("java.util.HashMap")){ | |
| 518 | - Map<String,Object> map = (HashMap<String,Object>)objs[k]; | |
| 519 | - cellValue = cellValue.replace("$" + tmpKey + "$",map.get(fieldName)+""); | |
| 520 | - } | |
| 521 | - } | |
| 522 | - } else if (obj.getClass().getName().equals(classWholeName)) { | |
| 523 | - cellValue = cellValue.replace("$" + tmpKey + "$",getKeyValue(obj, fieldName) + ""); | |
| 524 | - } else if (obj.getClass().getName().equals("java.util.HashMap")){ | |
| 525 | - Map<String,Object> map = (HashMap<String,Object>)obj; | |
| 526 | - cellValue = cellValue.replace("$" + tmpKey + "$",map.get(fieldName)+""); | |
| 527 | - } | |
| 528 | - } | |
| 529 | - } | |
| 530 | - } | |
| 531 | - cell.setCellValue(cellValue); | |
| 532 | - } catch (Exception e) { | |
| 533 | - e.printStackTrace(); | |
| 534 | - } | |
| 535 | - | |
| 536 | - } | |
| 537 | - | |
| 538 | - /** | |
| 539 | - * 给列填充数据 | |
| 540 | - * | |
| 541 | - * @param cell | |
| 542 | - * 列 | |
| 543 | - * @param obj | |
| 544 | - * 数据源对象 | |
| 545 | - * @param fieldName | |
| 546 | - * 需要取数据的字段 | |
| 547 | - */ | |
| 548 | - private Object getKeyValue(Object obj, String fieldName) { | |
| 549 | - Object value = ""; | |
| 550 | - try { | |
| 551 | - if (obj != null) { | |
| 552 | - ReportRelatedUtils test = new ReportRelatedUtils(); | |
| 553 | - value = test.getValue(obj, fieldName) == null ? "" : test .getValue(obj, fieldName); | |
| 554 | - } | |
| 555 | - } catch (Exception e) { | |
| 556 | - e.printStackTrace(); | |
| 557 | - } | |
| 558 | - return value; | |
| 559 | - } | |
| 560 | - | |
| 561 | - public static void main(String[] args) { | |
| 562 | - | |
| 563 | - try { | |
| 564 | - ReportUtils ee = new ReportUtils(); | |
| 565 | - List<Iterator<?>> list = new ArrayList<Iterator<?>>(); | |
| 566 | - Line line = new Line(); | |
| 567 | - line.setId(1); | |
| 568 | - line.setName("line1"); | |
| 569 | - | |
| 570 | - List<Object> dataList = new ArrayList<Object>(); | |
| 571 | - | |
| 572 | - | |
| 573 | - ScheduleRealInfo srr = new ScheduleRealInfo(); | |
| 574 | - srr.setId((long) 111); | |
| 575 | - srr.setXlName("abc11"); | |
| 576 | - | |
| 577 | - ScheduleRealInfo sr = new ScheduleRealInfo(); | |
| 578 | - sr.setZdsj("06:10"); | |
| 579 | - sr.setZdsjActual("06:25"); | |
| 580 | - dataList.add(sr); | |
| 581 | - sr = new ScheduleRealInfo(); | |
| 582 | - sr.setZdsj("06:20"); | |
| 583 | - sr.setZdsjActual(""); | |
| 584 | - dataList.add(sr); | |
| 585 | - list.add(dataList.iterator()); | |
| 586 | - | |
| 587 | - ee.excelReplace(list, new Object[] { srr }, "D:/waybill.xls", | |
| 588 | - "D:/22.xls"); | |
| 589 | - System.out.println("ok"); | |
| 590 | - } catch (Exception e) { | |
| 591 | - e.printStackTrace(); | |
| 592 | - } | |
| 593 | - } | |
| 594 | - | |
| 595 | - /** | |
| 596 | - * 行复制功能 | |
| 597 | - * | |
| 598 | - * @param fromRow | |
| 599 | - * @param toRow | |
| 600 | - */ | |
| 601 | - private void copyRow(HSSFWorkbook wb, HSSFRow fromRow, HSSFRow toRow, | |
| 602 | - boolean copyValueFlag, HSSFCellStyle style) { | |
| 603 | - for (Iterator<Cell> cellIt = fromRow.cellIterator(); cellIt.hasNext();) { | |
| 604 | - HSSFCell tmpCell = (HSSFCell) cellIt.next(); | |
| 605 | - HSSFCell newCell = toRow.createCell(tmpCell.getColumnIndex(), 0); | |
| 606 | - copyCell(wb, tmpCell, newCell, copyValueFlag, tmpCell.getCellStyle()); | |
| 607 | - } | |
| 608 | - } | |
| 609 | - | |
| 610 | - /** | |
| 611 | - * 复制单元格 | |
| 612 | - * | |
| 613 | - * @param srcCell | |
| 614 | - * @param distCell | |
| 615 | - * @param copyValueFlag | |
| 616 | - * true则连同cell的内容一起复制 | |
| 617 | - */ | |
| 618 | - public void copyCell(HSSFWorkbook wb, HSSFCell srcCell, HSSFCell distCell, | |
| 619 | - boolean copyValueFlag, HSSFCellStyle newstyle) { | |
| 620 | -// HSSFCellStyle newstyle = wb.createCellStyle(); | |
| 621 | - copyCellStyle(wb, srcCell.getCellStyle(), newstyle); | |
| 622 | - // 样式 | |
| 623 | - distCell.setCellStyle(newstyle); | |
| 624 | - // 评论 | |
| 625 | - if (srcCell.getCellComment() != null) { | |
| 626 | - distCell.setCellComment(srcCell.getCellComment()); | |
| 627 | - } | |
| 628 | - // 不同数据类型处理 | |
| 629 | - int srcCellType = srcCell.getCellType(); | |
| 630 | - distCell.setCellType(srcCellType); | |
| 631 | - if (copyValueFlag) { | |
| 632 | - if (srcCellType == HSSFCell.CELL_TYPE_NUMERIC) { | |
| 633 | - distCell.setCellValue(srcCell.getDateCellValue()); | |
| 634 | - } else if (srcCellType == HSSFCell.CELL_TYPE_STRING) { | |
| 635 | - distCell.setCellValue(srcCell.getRichStringCellValue()); | |
| 636 | - } else if (srcCellType == HSSFCell.CELL_TYPE_BLANK) { | |
| 637 | - | |
| 638 | - } else if (srcCellType == HSSFCell.CELL_TYPE_BOOLEAN) { | |
| 639 | - distCell.setCellValue(srcCell.getBooleanCellValue()); | |
| 640 | - } else if (srcCellType == HSSFCell.CELL_TYPE_ERROR) { | |
| 641 | - distCell.setCellErrorValue(srcCell.getErrorCellValue()); | |
| 642 | - } else if (srcCellType == HSSFCell.CELL_TYPE_FORMULA) { | |
| 643 | - distCell.setCellFormula(srcCell.getCellFormula()); | |
| 644 | - } else { | |
| 645 | - } | |
| 646 | - } | |
| 647 | - } | |
| 648 | - | |
| 649 | - /** | |
| 650 | - * 复制一个单元格样式到目的单元格样式 | |
| 651 | - * | |
| 652 | - * @param fromStyle | |
| 653 | - * @param toStyle | |
| 654 | - */ | |
| 655 | - public void copyCellStyle(HSSFWorkbook wb, HSSFCellStyle fromStyle, | |
| 656 | - HSSFCellStyle toStyle) { | |
| 657 | - toStyle.setAlignment(fromStyle.getAlignment()); | |
| 658 | - // 边框和边框颜色 | |
| 659 | - toStyle.setBorderBottom(fromStyle.getBorderBottom()); | |
| 660 | - toStyle.setBorderLeft(fromStyle.getBorderLeft()); | |
| 661 | - toStyle.setBorderRight(fromStyle.getBorderRight()); | |
| 662 | - toStyle.setBorderTop(fromStyle.getBorderTop()); | |
| 663 | - toStyle.setTopBorderColor(fromStyle.getTopBorderColor()); | |
| 664 | - toStyle.setBottomBorderColor(fromStyle.getBottomBorderColor()); | |
| 665 | - toStyle.setRightBorderColor(fromStyle.getRightBorderColor()); | |
| 666 | - toStyle.setLeftBorderColor(fromStyle.getLeftBorderColor()); | |
| 667 | - | |
| 668 | - // 背景和前景 | |
| 669 | - toStyle.setFillBackgroundColor(fromStyle.getFillBackgroundColor()); | |
| 670 | - toStyle.setFillForegroundColor(fromStyle.getFillForegroundColor()); | |
| 671 | - | |
| 672 | - toStyle.setDataFormat(fromStyle.getDataFormat()); | |
| 673 | - toStyle.setFillPattern(fromStyle.getFillPattern()); | |
| 674 | - toStyle.setHidden(fromStyle.getHidden()); | |
| 675 | - toStyle.setIndention(fromStyle.getIndention());// 首行缩进 | |
| 676 | - toStyle.setLocked(fromStyle.getLocked()); | |
| 677 | - toStyle.setRotation(fromStyle.getRotation());// 旋转 | |
| 678 | - toStyle.setVerticalAlignment(fromStyle.getVerticalAlignment()); | |
| 679 | - toStyle.setWrapText(fromStyle.getWrapText()); | |
| 680 | - // 字体 | |
| 681 | - toStyle.setFont(fromStyle.getFont(wb)); | |
| 682 | - | |
| 683 | - } | |
| 684 | - | |
| 685 | - /** | |
| 686 | - * 创建文件夹,并删除原有文件 | |
| 687 | - * | |
| 688 | - * @param path | |
| 689 | - */ | |
| 690 | - private void createFolder(String path) { | |
| 691 | - File targetFile = null; | |
| 692 | - targetFile = new File(path); | |
| 693 | - if (targetFile.exists()) {// 删除原有文件 | |
| 694 | - targetFile.delete(); | |
| 695 | - } | |
| 696 | - // 创建目标文件夹 | |
| 697 | - targetFile = new File(path.substring(0, path.lastIndexOf("/"))); | |
| 698 | - if (!targetFile.exists()) { | |
| 699 | - targetFile.mkdirs(); | |
| 700 | - } | |
| 701 | - } | |
| 702 | - | |
| 703 | - public void createFlie(List<List<String>> list, String name, String type){ | |
| 704 | - HSSFWorkbook workbook = new HSSFWorkbook(); | |
| 705 | - // 生成一个样式 | |
| 706 | - HSSFCellStyle style = workbook.createCellStyle(); | |
| 707 | - // 设置这些样式 | |
| 708 | -// style.setFillForegroundColor(HSSFColor.SKY_BLUE.index); | |
| 709 | -// style.setFillPattern(HSSFCellStyle.BORDER_THIN); | |
| 710 | - style.setBorderBottom(HSSFCellStyle.BORDER_THIN); | |
| 711 | - style.setBorderLeft(HSSFCellStyle.BORDER_THIN); | |
| 712 | - style.setBorderRight(HSSFCellStyle.BORDER_THIN); | |
| 713 | - style.setBorderTop(HSSFCellStyle.BORDER_THIN); | |
| 714 | - style.setAlignment(HSSFCellStyle.ALIGN_CENTER); | |
| 715 | - // 生成一个字体 | |
| 716 | - HSSFFont font = workbook.createFont(); | |
| 717 | -// font.setColor(HSSFColor.VIOLET.index); | |
| 718 | - font.setFontHeightInPoints((short) 12); | |
| 719 | - font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); | |
| 720 | - // 把字体应用到当前的样式 | |
| 721 | - style.setFont(font); | |
| 722 | - HSSFCellStyle cellStyle =workbook.createCellStyle(); | |
| 723 | - cellStyle.setFont(font); | |
| 724 | - cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局:居中 | |
| 725 | - cellStyle.setWrapText(true); | |
| 726 | - | |
| 727 | - //设置wordsheet名 | |
| 728 | - HSSFSheet sheetYS = workbook.createSheet();//设置wordsheet名 | |
| 729 | - HSSFRow row = sheetYS.createRow(0); | |
| 730 | - setCellStyleAndValue(row, style, 0, name); | |
| 731 | - CellRangeAddress callRangeAddress = new CellRangeAddress(0,0,0,list.get(0).size()-1); | |
| 732 | - sheetYS.addMergedRegion(callRangeAddress); | |
| 733 | - // 样式 | |
| 734 | - setMergeCellStyle (callRangeAddress, sheetYS, workbook); | |
| 735 | - | |
| 736 | - try{ | |
| 737 | - for(int i=0; i<list.size(); i++){ | |
| 738 | - HSSFRow rowYSi = sheetYS.createRow(i+1); | |
| 739 | - List<String> stringList = list.get(i); | |
| 740 | - int num = 4; | |
| 741 | - if("xl".equals(type)) | |
| 742 | - num = 2; | |
| 743 | - else if("cl".equals(type)) | |
| 744 | - num = 5; | |
| 745 | - for(int j=0; j<stringList.size(); j++){ | |
| 746 | - String str = stringList.get(j); | |
| 747 | - if(i == list.size()-1){ | |
| 748 | - if(j==0) { | |
| 749 | - setCellStyleAndValue(rowYSi, style, j, str); | |
| 750 | - CellRangeAddress callRangeAddressYSi = new CellRangeAddress(i+1,i+1,0,num); | |
| 751 | - sheetYS.addMergedRegion(callRangeAddressYSi); | |
| 752 | - // 样式 | |
| 753 | - setMergeCellStyle (callRangeAddressYSi, sheetYS, workbook); | |
| 754 | - }else | |
| 755 | - setCellStyleAndValue(rowYSi,style,j+num,str); | |
| 756 | - } else { | |
| 757 | - setCellStyleAndValue(rowYSi,style,j,str); | |
| 758 | - } | |
| 759 | - } | |
| 760 | - } | |
| 761 | - | |
| 762 | - // 给列设置宽度自适应 | |
| 763 | - setSizeColumn1(sheetYS,1,list.get(0).size()); | |
| 764 | - String path = this.getClass().getResource("/").getPath() + "static/pages/forms/export/"; | |
| 765 | - String targetPath = path+name+".xls"; | |
| 766 | - createFolder(targetPath); | |
| 767 | - FileOutputStream fout = new FileOutputStream(targetPath); | |
| 768 | - //5.输出 | |
| 769 | - workbook.write(fout); | |
| 770 | - fout.close(); | |
| 771 | - } catch (Exception e) { | |
| 772 | - e.printStackTrace(); | |
| 773 | - } | |
| 774 | - } | |
| 775 | - | |
| 776 | - /** | |
| 777 | - * 自适应宽度(中文支持) | |
| 778 | - * @param sheet | |
| 779 | - * @param size | |
| 780 | - */ | |
| 781 | - private static void setSizeColumn(HSSFSheet sheet, int size) { | |
| 782 | - for (int columnNum = 0; columnNum < size; columnNum++) { | |
| 783 | - int columnWidth = sheet.getColumnWidth(columnNum) / 256; | |
| 784 | - for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { | |
| 785 | - HSSFRow currentRow; | |
| 786 | - //当前行未被使用过 | |
| 787 | - if (sheet.getRow(rowNum) == null) { | |
| 788 | - currentRow = sheet.createRow(rowNum); | |
| 789 | - } else { | |
| 790 | - currentRow = sheet.getRow(rowNum); | |
| 791 | - } | |
| 792 | - if (currentRow.getCell(columnNum) != null) { | |
| 793 | - HSSFCell currentCell = currentRow.getCell(columnNum); | |
| 794 | - if (currentCell.getCellType() == XSSFCell.CELL_TYPE_STRING) { | |
| 795 | - int length = currentCell.getStringCellValue().getBytes().length; | |
| 796 | - if (columnWidth < length) { | |
| 797 | - columnWidth = length; | |
| 798 | - } | |
| 799 | - } | |
| 800 | - } | |
| 801 | - } | |
| 802 | - sheet.setColumnWidth(columnNum, columnWidth * 300); | |
| 803 | -// sheet.setColumnWidth(columnNum, columnWidth * 256); | |
| 804 | - } | |
| 805 | - } | |
| 806 | - | |
| 807 | - /** | |
| 808 | - * 自适应宽度(中文支持) | |
| 809 | - * @param sheet | |
| 810 | - * @param index 从那一行开始自适应 | |
| 811 | - * @param size | |
| 812 | - */ | |
| 813 | - private static void setSizeColumn1(HSSFSheet sheet,int index, int size) { | |
| 814 | - for (int columnNum = index; columnNum < size; columnNum++) { | |
| 815 | - int columnWidth = sheet.getColumnWidth(columnNum) / 256; | |
| 816 | - for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { | |
| 817 | - HSSFRow currentRow; | |
| 818 | - //当前行未被使用过 | |
| 819 | - if (sheet.getRow(rowNum) == null) { | |
| 820 | - currentRow = sheet.createRow(rowNum); | |
| 821 | - } else { | |
| 822 | - currentRow = sheet.getRow(rowNum); | |
| 823 | - } | |
| 824 | - if (currentRow.getCell(columnNum) != null) { | |
| 825 | - HSSFCell currentCell = currentRow.getCell(columnNum); | |
| 826 | - if (currentCell.getCellType() == XSSFCell.CELL_TYPE_STRING) { | |
| 827 | - int length = currentCell.getStringCellValue().getBytes().length; | |
| 828 | - if (columnWidth < length) { | |
| 829 | - columnWidth = length; | |
| 830 | - } | |
| 831 | - } | |
| 832 | - } | |
| 833 | - } | |
| 834 | - sheet.setColumnWidth(columnNum, columnWidth * 300); | |
| 835 | -// sheet.setColumnWidth(columnNum, columnWidth * 256); | |
| 836 | - } | |
| 837 | - } | |
| 838 | - | |
| 839 | - /** | |
| 840 | - * 设置单元格值和样式 | |
| 841 | - * @param row | |
| 842 | - * @param style | |
| 843 | - * @param index | |
| 844 | - * @param value | |
| 845 | - */ | |
| 846 | - public static void setCellStyleAndValue(HSSFRow row,HSSFCellStyle style,int index,String value){ | |
| 847 | - HSSFCell cell = row.createCell(index); | |
| 848 | - cell.setCellValue(value); | |
| 849 | - cell.setCellStyle(style); | |
| 850 | - } | |
| 851 | - /** | |
| 852 | - * 设置合并单元格样式 | |
| 853 | - * @param cra | |
| 854 | - * @param sheet | |
| 855 | - * @param workbook | |
| 856 | - */ | |
| 857 | - public static void setMergeCellStyle (CellRangeAddress cra, HSSFSheet sheet, Workbook workbook){ | |
| 858 | - // 使用RegionUtil类为合并后的单元格添加边框 | |
| 859 | - RegionUtil.setBorderBottom(1, cra, sheet, workbook); // 下边框 | |
| 860 | - RegionUtil.setBorderLeft(1, cra, sheet, workbook); // 左边框 | |
| 861 | - RegionUtil.setBorderRight(1, cra, sheet, workbook); // 有边框 | |
| 862 | - RegionUtil.setBorderTop(1, cra, sheet, workbook); // 上边框 | |
| 863 | - } | |
| 864 | -} | |
| 1 | +package com.bsth.util; | |
| 2 | + | |
| 3 | +import java.io.File; | |
| 4 | +import java.io.FileInputStream; | |
| 5 | +import java.io.FileOutputStream; | |
| 6 | +import java.util.ArrayList; | |
| 7 | +import java.util.HashMap; | |
| 8 | +import java.util.Iterator; | |
| 9 | +import java.util.List; | |
| 10 | +import java.util.Map; | |
| 11 | +import java.util.TreeMap; | |
| 12 | +import java.util.regex.Matcher; | |
| 13 | +import java.util.regex.Pattern; | |
| 14 | + | |
| 15 | +import com.bsth.common.ResponseCode; | |
| 16 | +import org.apache.commons.lang3.StringUtils; | |
| 17 | +import org.apache.poi.hssf.usermodel.HSSFCell; | |
| 18 | +import org.apache.poi.hssf.usermodel.HSSFCellStyle; | |
| 19 | +import org.apache.poi.hssf.usermodel.HSSFFont; | |
| 20 | +import org.apache.poi.hssf.usermodel.HSSFRow; | |
| 21 | +import org.apache.poi.hssf.usermodel.HSSFSheet; | |
| 22 | +import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |
| 23 | +import org.apache.poi.poifs.filesystem.POIFSFileSystem; | |
| 24 | +import org.apache.poi.ss.usermodel.Cell; | |
| 25 | +import org.apache.poi.ss.usermodel.Workbook; | |
| 26 | +import org.apache.poi.ss.util.CellRangeAddress; | |
| 27 | + | |
| 28 | +import com.bsth.entity.Line; | |
| 29 | +import com.bsth.entity.realcontrol.ScheduleRealInfo; | |
| 30 | +import org.apache.poi.ss.util.RegionUtil; | |
| 31 | +import org.apache.poi.xssf.usermodel.XSSFCell; | |
| 32 | + | |
| 33 | +public class ReportUtils { | |
| 34 | + // private final String packaegName = "com.bsth.entity."; | |
| 35 | + private final String packaegName = "com.bsth.entity.realcontrol."; | |
| 36 | + | |
| 37 | + private final Pattern pattern = Pattern.compile("^\\d+(\\.\\d*)?$"); | |
| 38 | + | |
| 39 | + /** | |
| 40 | + * /** | |
| 41 | + * | |
| 42 | + * @param list | |
| 43 | + * 模板中,需要重复显示的行所需的数据 | |
| 44 | + * @param map | |
| 45 | + * 模板中除以上list外所有的数据 | |
| 46 | + * @param index | |
| 47 | + * 需要重复的行号,该值为行号减1 | |
| 48 | + * @param sourcePath | |
| 49 | + * 模板路径 | |
| 50 | + * @param targetPath | |
| 51 | + * 生成路径 | |
| 52 | + */ | |
| 53 | + public void excelReplace(List<Iterator<?>> list, Object[] tArray, | |
| 54 | + String sourcePath, String targetPath) { | |
| 55 | + try { | |
| 56 | + // 把源文件放入流中 | |
| 57 | + POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream( | |
| 58 | + sourcePath)); | |
| 59 | + HSSFWorkbook wb = new HSSFWorkbook(fs); | |
| 60 | + if(tArray.length != 0 && tArray[0] instanceof java.util.Map){ | |
| 61 | + Map<String, Object> m = (Map<String, Object>)tArray[0]; | |
| 62 | + if(m.containsKey("sheetName") && m.get("sheetName")!=null | |
| 63 | + && m.get("sheetName").toString().trim().length()!=0) | |
| 64 | + wb.setSheetName(0, m.get("sheetName").toString()); | |
| 65 | + } | |
| 66 | + HSSFSheet sheet = wb.getSheetAt(0); | |
| 67 | + HSSFRow row; | |
| 68 | + HSSFCell cell = null; | |
| 69 | + String key; | |
| 70 | + // 取得总行数 | |
| 71 | + int rowNum = sheet.getLastRowNum(); | |
| 72 | + // 取得总列数 | |
| 73 | + int cellNum = sheet.getRow(0).getLastCellNum(); | |
| 74 | + | |
| 75 | + // 遍历行 | |
| 76 | + for (int i = 0; i < rowNum; i++) { | |
| 77 | + row = sheet.getRow(i); | |
| 78 | + // 遍历列 | |
| 79 | + for (int j = 0; j < cellNum; j++) { | |
| 80 | + if (row == null) { | |
| 81 | + continue; | |
| 82 | + } | |
| 83 | + cell = row.getCell(j); | |
| 84 | + if (cell == null) { | |
| 85 | + continue; | |
| 86 | + } | |
| 87 | + // 取得每列的内容,如果列内容是$key$格式,则替换内容 | |
| 88 | + key = getCellValue(cell); | |
| 89 | + if (key != null && (key.indexOf("$") != -1 || key.indexOf("#list#") != -1)) { | |
| 90 | + // * 列中内容有#list#,则表示该行为模板行,需要以该行为模板 | |
| 91 | + // * 例如:模板行格式 #list#0_0 $Car.id$ | |
| 92 | + // * 第一个0表示需要在list中取iterator的索引值 | |
| 93 | + // * 第二个0表示在iterator中取的第几个对象,如果不为0,则取下一个对象,而不是沿用前面获取的对象 | |
| 94 | + // * $Car.id$表示所取的对象为Car的对象,并且取值为id的值 | |
| 95 | + if (key.indexOf("#list#") != -1) { | |
| 96 | + key = key.replace("#list#", "").trim(); | |
| 97 | + String[] lists = key.split(" "); | |
| 98 | + // 取得list中的索引值 | |
| 99 | + int listIndex = Integer | |
| 100 | + .valueOf(lists[0].split("_")[0]); | |
| 101 | + Iterator<?> iterator = list.get(listIndex); | |
| 102 | + // 根据模板创建行并填弃数据,返回增加的行数 | |
| 103 | + int rowCount = iteratorFillCellValue(wb, sheet, | |
| 104 | + cell, iterator, i, rowNum, key); | |
| 105 | + rowNum += rowCount; | |
| 106 | + i += rowCount; | |
| 107 | + break; | |
| 108 | + } else { | |
| 109 | + // 直接填充数据的列,从对象数组中取得值,这里的数组不传值 | |
| 110 | + getValueAndSetCellValue(cell, key, tArray, new String[]{""}); | |
| 111 | + } | |
| 112 | + } | |
| 113 | + | |
| 114 | + } | |
| 115 | + } | |
| 116 | + // 创建目标文件夹 | |
| 117 | + createFolder(targetPath); | |
| 118 | + // 输出文件 | |
| 119 | + FileOutputStream fileOut = new FileOutputStream(targetPath); | |
| 120 | + wb.write(fileOut); | |
| 121 | + fileOut.close(); | |
| 122 | + } catch (Exception e) { | |
| 123 | + e.printStackTrace(); | |
| 124 | + } | |
| 125 | + } | |
| 126 | + | |
| 127 | + | |
| 128 | + /** | |
| 129 | + * | |
| 130 | + * @param sheetList 多sheet模板中,需要重复显示的行所需的数据 | |
| 131 | + * @param tArray | |
| 132 | + * @param sourcePath 模板路径 | |
| 133 | + * @param targetPath 生成路径 | |
| 134 | + */ | |
| 135 | + public void excelMoreSheetReplace(List<List<Iterator<?>>> sheetList, Object[] tArray, | |
| 136 | + String sourcePath, String targetPath) { | |
| 137 | + try { | |
| 138 | + // 把源文件放入流中 | |
| 139 | + POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream( | |
| 140 | + sourcePath)); | |
| 141 | + HSSFWorkbook wb = new HSSFWorkbook(fs); | |
| 142 | + for (int s=0; s<sheetList.size(); s++){ | |
| 143 | + List<Iterator<?>> list = sheetList.get(s); | |
| 144 | + if(tArray.length != 0 && tArray[0] instanceof java.util.Map){ | |
| 145 | + Map<String, Object> m = (Map<String, Object>)tArray[0]; | |
| 146 | + if(m.containsKey("sheetName"+s+1) && m.get("sheetName"+s+1)!=null | |
| 147 | + && m.get("sheetName"+s+1).toString().trim().length()!=0) | |
| 148 | + wb.setSheetName(0, m.get("sheetName"+s+1).toString()); | |
| 149 | + } | |
| 150 | + HSSFSheet sheet = wb.getSheetAt(s); | |
| 151 | + HSSFRow row; | |
| 152 | + HSSFCell cell = null; | |
| 153 | + String key; | |
| 154 | + // 取得总行数 | |
| 155 | + int rowNum = sheet.getLastRowNum(); | |
| 156 | + // 取得总列数 | |
| 157 | + int cellNum = sheet.getRow(0).getLastCellNum(); | |
| 158 | + | |
| 159 | + // 遍历行 | |
| 160 | + for (int i = 0; i <= rowNum; i++) { | |
| 161 | + row = sheet.getRow(i); | |
| 162 | + // 遍历列 | |
| 163 | + for (int j = 0; j < cellNum; j++) { | |
| 164 | + if (row == null) { | |
| 165 | + continue; | |
| 166 | + } | |
| 167 | + cell = row.getCell(j); | |
| 168 | + if (cell == null) { | |
| 169 | + continue; | |
| 170 | + } | |
| 171 | + // 取得每列的内容,如果列内容是$key$格式,则替换内容 | |
| 172 | + key = getCellValue(cell); | |
| 173 | + if (key != null && (key.indexOf("$") != -1 || key.indexOf("#list#") != -1)) { | |
| 174 | + // * 列中内容有#list#,则表示该行为模板行,需要以该行为模板 | |
| 175 | + // * 例如:模板行格式 #list#0_0 $Car.id$ | |
| 176 | + // * 第一个0表示需要在list中取iterator的索引值 | |
| 177 | + // * 第二个0表示在iterator中取的第几个对象,如果不为0,则取下一个对象,而不是沿用前面获取的对象 | |
| 178 | + // * $Car.id$表示所取的对象为Car的对象,并且取值为id的值 | |
| 179 | + if (key.indexOf("#list#") != -1) { | |
| 180 | + key = key.replace("#list#", "").trim(); | |
| 181 | + String[] lists = key.split(" "); | |
| 182 | + // 取得list中的索引值 | |
| 183 | + int listIndex = Integer | |
| 184 | + .valueOf(lists[0].split("_")[0]); | |
| 185 | + Iterator<?> iterator = list.get(listIndex); | |
| 186 | + // 根据模板创建行并填充数据,返回增加的行数 | |
| 187 | + int rowCount = iteratorFillCellValue(wb, sheet, | |
| 188 | + cell, iterator, i, rowNum, key); | |
| 189 | + rowNum += rowCount; | |
| 190 | + i += rowCount; | |
| 191 | + break; | |
| 192 | + } else { | |
| 193 | + // 直接填充数据的列,从对象数组中取得值,这里的数组不传值 | |
| 194 | + getValueAndSetCellValue(cell, key, tArray, new String[]{""}); | |
| 195 | + } | |
| 196 | + } | |
| 197 | + | |
| 198 | + } | |
| 199 | + } | |
| 200 | + } | |
| 201 | + | |
| 202 | + // 创建目标文件夹 | |
| 203 | + createFolder(targetPath); | |
| 204 | + // 输出文件 | |
| 205 | + FileOutputStream fileOut = new FileOutputStream(targetPath); | |
| 206 | + wb.write(fileOut); | |
| 207 | + fileOut.close(); | |
| 208 | + } catch (Exception e) { | |
| 209 | + e.printStackTrace(); | |
| 210 | + } | |
| 211 | + } | |
| 212 | + | |
| 213 | + /** | |
| 214 | + * 将file1中的一页sheet复制到file2中 | |
| 215 | + * | |
| 216 | + * @param file1 | |
| 217 | + * 原sheet所在的excel文件 | |
| 218 | + * @param file2 | |
| 219 | + * 目标excel文件 | |
| 220 | + * @param page | |
| 221 | + * 原excel中要被复制的sheet的位置(从0开始) | |
| 222 | + * @param rate | |
| 223 | + * 调整复制后的缩放倍率(列如:145,则为缩放145%) | |
| 224 | + */ | |
| 225 | + public void copySheetByFile(File file1, File file2, int page, int rate) { | |
| 226 | + try { | |
| 227 | + // 把源文件放入流中 | |
| 228 | + POIFSFileSystem fs1 = new POIFSFileSystem(new FileInputStream(file1)); | |
| 229 | + HSSFWorkbook wb1 = new HSSFWorkbook(fs1); | |
| 230 | + HSSFSheet sheet = wb1.getSheetAt(page); | |
| 231 | + POIFSFileSystem fs2 = new POIFSFileSystem(new FileInputStream(file2)); | |
| 232 | + HSSFWorkbook wb2 = new HSSFWorkbook(fs2); | |
| 233 | + HSSFSheet createSheet = wb2.createSheet(sheet.getSheetName()); | |
| 234 | + HSSFCellStyle createCellStyle = wb2.createCellStyle(); | |
| 235 | + HSSFRow row; | |
| 236 | + | |
| 237 | + createSheet.setZoom(rate, 100); | |
| 238 | + for(int i = 0; i < sheet.getRow(0).getPhysicalNumberOfCells(); i++){ | |
| 239 | + createSheet.setColumnWidth(i, sheet.getColumnWidth(i)); | |
| 240 | + } | |
| 241 | + | |
| 242 | + List<CellRangeAddress> mergedRegions = sheet.getMergedRegions(); | |
| 243 | + for(int l = 0; l < mergedRegions.size(); l++){ | |
| 244 | + //复制源表中的合并单元格 | |
| 245 | + createSheet.addMergedRegion(mergedRegions.get(l)); | |
| 246 | + } | |
| 247 | + int firstRow = sheet.getFirstRowNum(); | |
| 248 | + int lastRow = sheet.getLastRowNum(); | |
| 249 | + for(int k = firstRow; k <= lastRow; k++){ | |
| 250 | + // 创建新建excel Sheet的行 | |
| 251 | + HSSFRow rowCreat = createSheet.createRow(k); | |
| 252 | + // 取得源有excel Sheet的行 | |
| 253 | + row = sheet.getRow(k); | |
| 254 | +// rowCreat.setHeight(row.getHeight()); //设置行高 | |
| 255 | + // 单元格式样 | |
| 256 | + int firstCell = row.getFirstCellNum(); | |
| 257 | + int lastCell = row.getLastCellNum(); | |
| 258 | + for (int j = firstCell; j < lastCell; j++) { | |
| 259 | + // 自动适应列宽 貌似不起作用 | |
| 260 | +// createSheet.autoSizeColumn(j); | |
| 261 | +// System.out.println(row.getCell(j)); | |
| 262 | + rowCreat.createCell(j); | |
| 263 | + String strVal = ""; | |
| 264 | + if (row.getCell(j)==null) { | |
| 265 | + | |
| 266 | + } else { | |
| 267 | + strVal = row.getCell(j).getStringCellValue(); | |
| 268 | + rowCreat.getCell(j).setCellValue(strVal); | |
| 269 | + copyCellStyle(wb1, row.getCell(j).getCellStyle(), createCellStyle); | |
| 270 | + createCellStyle.setBorderTop((short)1); | |
| 271 | + createCellStyle.setBorderLeft((short)1); | |
| 272 | + createCellStyle.setBorderRight((short)1); | |
| 273 | + createCellStyle.setBorderBottom((short)1); | |
| 274 | + rowCreat.getCell(j).setCellStyle(createCellStyle); | |
| 275 | + } | |
| 276 | + } | |
| 277 | + } | |
| 278 | + | |
| 279 | +// int firstRowNum = createSheet.getFirstRowNum(); | |
| 280 | +// int lastRowNum = createSheet.getLastRowNum(); | |
| 281 | +// int test = 0; | |
| 282 | +// for(int k = firstRowNum; k <= lastRowNum; k++){ | |
| 283 | +// HSSFRow createRow = createSheet.getRow(k); | |
| 284 | +// int firstCellNum = createRow.getFirstCellNum(); | |
| 285 | +// int lastCellNum = createRow.getLastCellNum(); | |
| 286 | +// for(int i = firstCellNum; i < lastCellNum; i++){ | |
| 287 | +// HSSFCell cell = createRow.getCell(i); | |
| 288 | +// cell.getCellStyle().setBorderTop(HSSFCellStyle.BORDER_THIN); | |
| 289 | +// cell.getCellStyle().setBorderLeft(HSSFCellStyle.BORDER_THIN); | |
| 290 | +// cell.getCellStyle().setBorderRight(HSSFCellStyle.BORDER_THIN); | |
| 291 | +// cell.getCellStyle().setBorderBottom(HSSFCellStyle.BORDER_THIN); | |
| 292 | +// test ++; | |
| 293 | +// } | |
| 294 | +// } | |
| 295 | +// System.out.println("test = " + test); | |
| 296 | + | |
| 297 | + FileOutputStream fileOut = new FileOutputStream(file2); | |
| 298 | + wb2.write(fileOut); | |
| 299 | + fileOut.close(); | |
| 300 | + wb2.close(); | |
| 301 | + wb1.close(); | |
| 302 | + fs2.close(); | |
| 303 | + fs1.close(); | |
| 304 | + file1.delete(); | |
| 305 | +// // 创建目标文件夹 | |
| 306 | +// createFolder(targetPath); | |
| 307 | + // 输出文件 | |
| 308 | + } catch (Exception e) { | |
| 309 | + e.printStackTrace(); | |
| 310 | + } | |
| 311 | + } | |
| 312 | + | |
| 313 | + public void test(File file){ | |
| 314 | + POIFSFileSystem fs; | |
| 315 | + try { | |
| 316 | + fs = new POIFSFileSystem(new FileInputStream(file)); | |
| 317 | + HSSFWorkbook wb = new HSSFWorkbook(fs); | |
| 318 | + for(int j = 0; j < wb.getNumberOfSheets(); j++){ | |
| 319 | + HSSFSheet sheet = wb.getSheetAt(j); | |
| 320 | + int firstRowNum = sheet.getFirstRowNum(); | |
| 321 | + int lastRowNum = sheet.getLastRowNum(); | |
| 322 | + int test = 0; | |
| 323 | + for(int k = firstRowNum; k <= lastRowNum; k++){ | |
| 324 | + HSSFRow createRow = sheet.getRow(k); | |
| 325 | + int firstCellNum = createRow.getFirstCellNum(); | |
| 326 | + int lastCellNum = createRow.getLastCellNum(); | |
| 327 | + for(int i = firstCellNum; i < lastCellNum; i++){ | |
| 328 | + HSSFCell cell = createRow.getCell(i); | |
| 329 | + HSSFCellStyle cellStyle = wb.createCellStyle(); | |
| 330 | + | |
| 331 | + cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); | |
| 332 | + cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); | |
| 333 | + cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); | |
| 334 | + cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); | |
| 335 | + cell.setCellStyle(cellStyle); | |
| 336 | + test ++; | |
| 337 | + } | |
| 338 | + } | |
| 339 | + System.out.println("test = " + test); | |
| 340 | + | |
| 341 | + FileOutputStream fileOut = new FileOutputStream(file); | |
| 342 | + wb.write(fileOut); | |
| 343 | + fileOut.close(); | |
| 344 | + } | |
| 345 | + } catch (Exception e) { | |
| 346 | + // TODO Auto-generated catch block | |
| 347 | + e.printStackTrace(); | |
| 348 | + } | |
| 349 | + | |
| 350 | + } | |
| 351 | + | |
| 352 | + public String getCellValue(HSSFCell cell) { | |
| 353 | + int cellType = 0; | |
| 354 | + String result = ""; | |
| 355 | + double d; | |
| 356 | + if(cell != null) { | |
| 357 | + // 获取列数据类型 | |
| 358 | + cellType = cell.getCellType(); | |
| 359 | + // 不同列数据类型,取值方法不同 | |
| 360 | + switch(cellType) { | |
| 361 | + // String | |
| 362 | + case HSSFCell.CELL_TYPE_STRING: | |
| 363 | + result = cell.getStringCellValue().toString(); | |
| 364 | + break; | |
| 365 | + // numeric类型,excel中,日期格式会转成数字格式存储 | |
| 366 | + case HSSFCell.CELL_TYPE_NUMERIC: | |
| 367 | + result = cell.getNumericCellValue()+""; | |
| 368 | + break; | |
| 369 | + // 公式类型 | |
| 370 | + case HSSFCell.CELL_TYPE_FORMULA: | |
| 371 | + result = cell.getCellFormula() + ""; | |
| 372 | + break; | |
| 373 | + // boolean类型 | |
| 374 | + case HSSFCell.CELL_TYPE_BOOLEAN: | |
| 375 | + result = cell.getBooleanCellValue() + ""; | |
| 376 | + break; | |
| 377 | + // 空格 | |
| 378 | + case HSSFCell.CELL_TYPE_BLANK: | |
| 379 | + result = null; | |
| 380 | + break; | |
| 381 | + // 错误值 | |
| 382 | + case HSSFCell.CELL_TYPE_ERROR: | |
| 383 | + result = null; | |
| 384 | + break; | |
| 385 | + default : | |
| 386 | + System.out.println("其它"); | |
| 387 | + break; | |
| 388 | + } | |
| 389 | + } | |
| 390 | + return result; | |
| 391 | + } | |
| 392 | + | |
| 393 | + /** | |
| 394 | + * 根据iterator,以及模板中的标识,填充模板 | |
| 395 | + * | |
| 396 | + * @param wb | |
| 397 | + * @param sheet | |
| 398 | + * @param cell | |
| 399 | + * @param iterator | |
| 400 | + * iterator | |
| 401 | + * @param index | |
| 402 | + * 模板行索引 | |
| 403 | + * @param rowNum | |
| 404 | + * 表格总行数 | |
| 405 | + * @param key | |
| 406 | + * 表格内容 | |
| 407 | + * @return | |
| 408 | + */ | |
| 409 | + private int iteratorFillCellValue(HSSFWorkbook wb, HSSFSheet sheet, | |
| 410 | + HSSFCell cell, Iterator<?> iterator, int index, int rowNum, | |
| 411 | + String key) { | |
| 412 | + int rowCount = 0; | |
| 413 | + Object obj = null; | |
| 414 | + int p = 0; | |
| 415 | + int i = index; | |
| 416 | + HSSFRow newRow = null; | |
| 417 | + int tmpCellNum = 0; | |
| 418 | + int k = 0; | |
| 419 | + int listIndex = 0; | |
| 420 | + // 取得模板行 | |
| 421 | + HSSFRow orgRow = sheet.getRow(index); | |
| 422 | + HSSFCellStyle style= wb.createCellStyle(); | |
| 423 | + try { | |
| 424 | + while (iterator.hasNext()) { | |
| 425 | + // 取得iterator的对象 | |
| 426 | + obj = iterator.next(); | |
| 427 | + // 移动当前编辑行以下的所有行 | |
| 428 | + if (p != 0) { | |
| 429 | + rowNum += 1; | |
| 430 | + i += 1; | |
| 431 | + rowCount += 1;// 增加的总行数 | |
| 432 | + // 把当前行以下的所有行往下移动1行 | |
| 433 | + sheet.shiftRows(i, rowNum, 1); | |
| 434 | + } | |
| 435 | + p = 1; | |
| 436 | + // 创建新行 | |
| 437 | + newRow = sheet.createRow(index + k++); | |
| 438 | + // 把新行的内容换成和模板行一样 | |
| 439 | + copyRow(wb, orgRow, newRow, true,style); | |
| 440 | + tmpCellNum = newRow.getLastCellNum(); | |
| 441 | + for (int l = 0; l < tmpCellNum; l++) { | |
| 442 | + cell = newRow.getCell(l); | |
| 443 | + key = getCellValue(cell); | |
| 444 | + /** | |
| 445 | + * 如果单无格内容为#list#,表示该行是模板行 #list#0_0 | |
| 446 | + * 第一个0表示需要在list中取iterator的索引值 | |
| 447 | + * 第二个0表示在iterator中取的第几个对象,如果不为0,则取下一个对象,而不是沿用前面获取的对象 | |
| 448 | + */ | |
| 449 | + if(key == null || key.equals("")){ | |
| 450 | + obj = iterator.next(); | |
| 451 | + } | |
| 452 | + if (key.indexOf("#list#") != -1 && key.indexOf("_0") == -1) { | |
| 453 | + if (iterator.hasNext()) { | |
| 454 | + obj = iterator.next(); | |
| 455 | + } else { | |
| 456 | + obj = null; | |
| 457 | + } | |
| 458 | + } | |
| 459 | + if (key.trim().indexOf(" ") != -1) { | |
| 460 | + key = key.split(" ")[1]; | |
| 461 | + } | |
| 462 | + getValueAndSetCellValue(cell, key, obj,new String[]{listIndex+""}); | |
| 463 | + } | |
| 464 | + // list的数量 | |
| 465 | + listIndex ++; | |
| 466 | + } | |
| 467 | + } catch (Exception e) { | |
| 468 | + e.printStackTrace(); | |
| 469 | + } | |
| 470 | + return rowCount; | |
| 471 | + } | |
| 472 | + | |
| 473 | + /** | |
| 474 | + * 取到相应的值并填入相应的列中 | |
| 475 | + * | |
| 476 | + * @param cell 列 | |
| 477 | + * @param key 列内容 | |
| 478 | + * @param obj 数据源对象 | |
| 479 | + * @param args 其他参数 数组 | |
| 480 | + * 数组内容: | |
| 481 | + * 0位:在list中的第几条数据 | |
| 482 | + */ | |
| 483 | + private void getValueAndSetCellValue(HSSFCell cell, String key, Object obj, String[] args) { | |
| 484 | + try { | |
| 485 | + // 保有存单元格的内容 | |
| 486 | + String cellValue = key = key.replace("\\n", ""); | |
| 487 | + String tmpKey; | |
| 488 | + // 判断单元格内容是否是公式 | |
| 489 | + if(cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA){ | |
| 490 | + Pattern p=Pattern.compile("(\\d+)"); // | |
| 491 | + Matcher m=p.matcher(key); | |
| 492 | + if(m.find()){ | |
| 493 | + cellValue = key.replace(m.group(1), | |
| 494 | + Integer.valueOf(m.group(1))+Integer.valueOf(args[0])+""); | |
| 495 | + cell.setCellFormula(cellValue); | |
| 496 | + return; | |
| 497 | + } | |
| 498 | + }else{//其他格式 | |
| 499 | + | |
| 500 | + // 循环截取两个$中间的内容,反射出值 | |
| 501 | + while (key.indexOf("$") != -1) { | |
| 502 | + key = key.substring(key.indexOf("$") + 1); | |
| 503 | + // 取两个$中间的内容 | |
| 504 | + tmpKey = key.substring(0, key.indexOf("$")); | |
| 505 | + key = key.substring(key.indexOf("$") + 1); | |
| 506 | + // 如果内容是如下格式Cars.id,则从obj中值得相应的对象的值 | |
| 507 | + if (tmpKey.indexOf(".") != -1) { | |
| 508 | + String className = tmpKey.substring(0, tmpKey.indexOf(".")); | |
| 509 | + // 取得类的全限定名 | |
| 510 | + String classWholeName = packaegName + className; | |
| 511 | + String fieldName = tmpKey.substring(tmpKey.indexOf(".") + 1); | |
| 512 | + // 如果obj是数组,循环判断哪个对象是对应的 | |
| 513 | + if (obj instanceof Object[]) { | |
| 514 | + Object[] objs = (Object[]) obj; | |
| 515 | + for (int k = 0; k < objs.length; k++) { | |
| 516 | + if (objs[k].getClass().getName().equals(classWholeName)) { | |
| 517 | + cellValue = cellValue.replace("$" + tmpKey | |
| 518 | + + "$", getKeyValue(objs[k], fieldName) | |
| 519 | + + ""); | |
| 520 | + } else if(objs[k].getClass().getName().equals("java.util.HashMap")){ | |
| 521 | + Map<String,Object> map = (HashMap<String,Object>)objs[k]; | |
| 522 | + cellValue = cellValue.replace("$" + tmpKey + "$",map.get(fieldName)+""); | |
| 523 | + } | |
| 524 | + } | |
| 525 | + } else if (obj.getClass().getName().equals(classWholeName)) { | |
| 526 | + cellValue = cellValue.replace("$" + tmpKey + "$",getKeyValue(obj, fieldName) + ""); | |
| 527 | + } else if (obj.getClass().getName().equals("java.util.HashMap")){ | |
| 528 | + Map<String,Object> map = (HashMap<String,Object>)obj; | |
| 529 | + cellValue = cellValue.replace("$" + tmpKey + "$",map.get(fieldName)+""); | |
| 530 | + } | |
| 531 | + } | |
| 532 | + } | |
| 533 | + } | |
| 534 | + cell.setCellValue(cellValue); | |
| 535 | + Matcher matcher = pattern.matcher(cellValue); | |
| 536 | + if (matcher.matches()) { | |
| 537 | + if (cellValue.indexOf(".") > -1) { | |
| 538 | + cell.setCellValue(Double.parseDouble(cellValue)); | |
| 539 | + } else { | |
| 540 | + cell.setCellValue(Integer.parseInt(cellValue)); | |
| 541 | + } | |
| 542 | + } | |
| 543 | + } catch (Exception e) { | |
| 544 | + e.printStackTrace(); | |
| 545 | + } | |
| 546 | + | |
| 547 | + } | |
| 548 | + | |
| 549 | + /** | |
| 550 | + * 给列填充数据 | |
| 551 | + * | |
| 552 | + * @param cell | |
| 553 | + * 列 | |
| 554 | + * @param obj | |
| 555 | + * 数据源对象 | |
| 556 | + * @param fieldName | |
| 557 | + * 需要取数据的字段 | |
| 558 | + */ | |
| 559 | + private Object getKeyValue(Object obj, String fieldName) { | |
| 560 | + Object value = ""; | |
| 561 | + try { | |
| 562 | + if (obj != null) { | |
| 563 | + ReportRelatedUtils test = new ReportRelatedUtils(); | |
| 564 | + value = test.getValue(obj, fieldName) == null ? "" : test .getValue(obj, fieldName); | |
| 565 | + } | |
| 566 | + } catch (Exception e) { | |
| 567 | + e.printStackTrace(); | |
| 568 | + } | |
| 569 | + return value; | |
| 570 | + } | |
| 571 | + | |
| 572 | + public static void main(String[] args) { | |
| 573 | + | |
| 574 | + try { | |
| 575 | + ReportUtils ee = new ReportUtils(); | |
| 576 | + List<Iterator<?>> list = new ArrayList<Iterator<?>>(); | |
| 577 | + Line line = new Line(); | |
| 578 | + line.setId(1); | |
| 579 | + line.setName("line1"); | |
| 580 | + | |
| 581 | + List<Object> dataList = new ArrayList<Object>(); | |
| 582 | + | |
| 583 | + | |
| 584 | + ScheduleRealInfo srr = new ScheduleRealInfo(); | |
| 585 | + srr.setId((long) 111); | |
| 586 | + srr.setXlName("abc11"); | |
| 587 | + | |
| 588 | + ScheduleRealInfo sr = new ScheduleRealInfo(); | |
| 589 | + sr.setZdsj("06:10"); | |
| 590 | + sr.setZdsjActual("06:25"); | |
| 591 | + dataList.add(sr); | |
| 592 | + sr = new ScheduleRealInfo(); | |
| 593 | + sr.setZdsj("06:20"); | |
| 594 | + sr.setZdsjActual(""); | |
| 595 | + dataList.add(sr); | |
| 596 | + list.add(dataList.iterator()); | |
| 597 | + | |
| 598 | + ee.excelReplace(list, new Object[] { srr }, "D:/waybill.xls", | |
| 599 | + "D:/22.xls"); | |
| 600 | + System.out.println("ok"); | |
| 601 | + } catch (Exception e) { | |
| 602 | + e.printStackTrace(); | |
| 603 | + } | |
| 604 | + } | |
| 605 | + | |
| 606 | + /** | |
| 607 | + * 行复制功能 | |
| 608 | + * | |
| 609 | + * @param fromRow | |
| 610 | + * @param toRow | |
| 611 | + */ | |
| 612 | + private void copyRow(HSSFWorkbook wb, HSSFRow fromRow, HSSFRow toRow, | |
| 613 | + boolean copyValueFlag, HSSFCellStyle style) { | |
| 614 | + for (Iterator<Cell> cellIt = fromRow.cellIterator(); cellIt.hasNext();) { | |
| 615 | + HSSFCell tmpCell = (HSSFCell) cellIt.next(); | |
| 616 | + HSSFCell newCell = toRow.createCell(tmpCell.getColumnIndex(), 0); | |
| 617 | + copyCell(wb, tmpCell, newCell, copyValueFlag, tmpCell.getCellStyle()); | |
| 618 | + } | |
| 619 | + } | |
| 620 | + | |
| 621 | + /** | |
| 622 | + * 复制单元格 | |
| 623 | + * | |
| 624 | + * @param srcCell | |
| 625 | + * @param distCell | |
| 626 | + * @param copyValueFlag | |
| 627 | + * true则连同cell的内容一起复制 | |
| 628 | + */ | |
| 629 | + public void copyCell(HSSFWorkbook wb, HSSFCell srcCell, HSSFCell distCell, | |
| 630 | + boolean copyValueFlag, HSSFCellStyle newstyle) { | |
| 631 | +// HSSFCellStyle newstyle = wb.createCellStyle(); | |
| 632 | + copyCellStyle(wb, srcCell.getCellStyle(), newstyle); | |
| 633 | + // 样式 | |
| 634 | + distCell.setCellStyle(newstyle); | |
| 635 | + // 评论 | |
| 636 | + if (srcCell.getCellComment() != null) { | |
| 637 | + distCell.setCellComment(srcCell.getCellComment()); | |
| 638 | + } | |
| 639 | + // 不同数据类型处理 | |
| 640 | + int srcCellType = srcCell.getCellType(); | |
| 641 | + distCell.setCellType(srcCellType); | |
| 642 | + if (copyValueFlag) { | |
| 643 | + if (srcCellType == HSSFCell.CELL_TYPE_NUMERIC) { | |
| 644 | + distCell.setCellValue(srcCell.getDateCellValue()); | |
| 645 | + } else if (srcCellType == HSSFCell.CELL_TYPE_STRING) { | |
| 646 | + distCell.setCellValue(srcCell.getRichStringCellValue()); | |
| 647 | + } else if (srcCellType == HSSFCell.CELL_TYPE_BLANK) { | |
| 648 | + | |
| 649 | + } else if (srcCellType == HSSFCell.CELL_TYPE_BOOLEAN) { | |
| 650 | + distCell.setCellValue(srcCell.getBooleanCellValue()); | |
| 651 | + } else if (srcCellType == HSSFCell.CELL_TYPE_ERROR) { | |
| 652 | + distCell.setCellErrorValue(srcCell.getErrorCellValue()); | |
| 653 | + } else if (srcCellType == HSSFCell.CELL_TYPE_FORMULA) { | |
| 654 | + distCell.setCellFormula(srcCell.getCellFormula()); | |
| 655 | + } else { | |
| 656 | + } | |
| 657 | + } | |
| 658 | + } | |
| 659 | + | |
| 660 | + /** | |
| 661 | + * 复制一个单元格样式到目的单元格样式 | |
| 662 | + * | |
| 663 | + * @param fromStyle | |
| 664 | + * @param toStyle | |
| 665 | + */ | |
| 666 | + public void copyCellStyle(HSSFWorkbook wb, HSSFCellStyle fromStyle, | |
| 667 | + HSSFCellStyle toStyle) { | |
| 668 | + toStyle.setAlignment(fromStyle.getAlignment()); | |
| 669 | + // 边框和边框颜色 | |
| 670 | + toStyle.setBorderBottom(fromStyle.getBorderBottom()); | |
| 671 | + toStyle.setBorderLeft(fromStyle.getBorderLeft()); | |
| 672 | + toStyle.setBorderRight(fromStyle.getBorderRight()); | |
| 673 | + toStyle.setBorderTop(fromStyle.getBorderTop()); | |
| 674 | + toStyle.setTopBorderColor(fromStyle.getTopBorderColor()); | |
| 675 | + toStyle.setBottomBorderColor(fromStyle.getBottomBorderColor()); | |
| 676 | + toStyle.setRightBorderColor(fromStyle.getRightBorderColor()); | |
| 677 | + toStyle.setLeftBorderColor(fromStyle.getLeftBorderColor()); | |
| 678 | + | |
| 679 | + // 背景和前景 | |
| 680 | + toStyle.setFillBackgroundColor(fromStyle.getFillBackgroundColor()); | |
| 681 | + toStyle.setFillForegroundColor(fromStyle.getFillForegroundColor()); | |
| 682 | + | |
| 683 | + toStyle.setDataFormat(fromStyle.getDataFormat()); | |
| 684 | + toStyle.setFillPattern(fromStyle.getFillPattern()); | |
| 685 | + toStyle.setHidden(fromStyle.getHidden()); | |
| 686 | + toStyle.setIndention(fromStyle.getIndention());// 首行缩进 | |
| 687 | + toStyle.setLocked(fromStyle.getLocked()); | |
| 688 | + toStyle.setRotation(fromStyle.getRotation());// 旋转 | |
| 689 | + toStyle.setVerticalAlignment(fromStyle.getVerticalAlignment()); | |
| 690 | + toStyle.setWrapText(fromStyle.getWrapText()); | |
| 691 | + // 字体 | |
| 692 | + toStyle.setFont(fromStyle.getFont(wb)); | |
| 693 | + | |
| 694 | + } | |
| 695 | + | |
| 696 | + /** | |
| 697 | + * 创建文件夹,并删除原有文件 | |
| 698 | + * | |
| 699 | + * @param path | |
| 700 | + */ | |
| 701 | + private void createFolder(String path) { | |
| 702 | + File targetFile = null; | |
| 703 | + targetFile = new File(path); | |
| 704 | + if (targetFile.exists()) {// 删除原有文件 | |
| 705 | + targetFile.delete(); | |
| 706 | + } | |
| 707 | + // 创建目标文件夹 | |
| 708 | + targetFile = new File(path.substring(0, path.lastIndexOf("/"))); | |
| 709 | + if (!targetFile.exists()) { | |
| 710 | + targetFile.mkdirs(); | |
| 711 | + } | |
| 712 | + } | |
| 713 | + | |
| 714 | + public void createFlie(List<List<String>> list, String name, String type){ | |
| 715 | + HSSFWorkbook workbook = new HSSFWorkbook(); | |
| 716 | + // 生成一个样式 | |
| 717 | + HSSFCellStyle style = workbook.createCellStyle(); | |
| 718 | + // 设置这些样式 | |
| 719 | +// style.setFillForegroundColor(HSSFColor.SKY_BLUE.index); | |
| 720 | +// style.setFillPattern(HSSFCellStyle.BORDER_THIN); | |
| 721 | + style.setBorderBottom(HSSFCellStyle.BORDER_THIN); | |
| 722 | + style.setBorderLeft(HSSFCellStyle.BORDER_THIN); | |
| 723 | + style.setBorderRight(HSSFCellStyle.BORDER_THIN); | |
| 724 | + style.setBorderTop(HSSFCellStyle.BORDER_THIN); | |
| 725 | + style.setAlignment(HSSFCellStyle.ALIGN_CENTER); | |
| 726 | + // 生成一个字体 | |
| 727 | + HSSFFont font = workbook.createFont(); | |
| 728 | +// font.setColor(HSSFColor.VIOLET.index); | |
| 729 | + font.setFontHeightInPoints((short) 12); | |
| 730 | + font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); | |
| 731 | + // 把字体应用到当前的样式 | |
| 732 | + style.setFont(font); | |
| 733 | + HSSFCellStyle cellStyle =workbook.createCellStyle(); | |
| 734 | + cellStyle.setFont(font); | |
| 735 | + cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局:居中 | |
| 736 | + cellStyle.setWrapText(true); | |
| 737 | + | |
| 738 | + //设置wordsheet名 | |
| 739 | + HSSFSheet sheetYS = workbook.createSheet();//设置wordsheet名 | |
| 740 | + HSSFRow row = sheetYS.createRow(0); | |
| 741 | + setCellStyleAndValue(row, style, 0, name); | |
| 742 | + CellRangeAddress callRangeAddress = new CellRangeAddress(0,0,0,list.get(0).size()-1); | |
| 743 | + sheetYS.addMergedRegion(callRangeAddress); | |
| 744 | + // 样式 | |
| 745 | + setMergeCellStyle (callRangeAddress, sheetYS, workbook); | |
| 746 | + | |
| 747 | + try{ | |
| 748 | + for(int i=0; i<list.size(); i++){ | |
| 749 | + HSSFRow rowYSi = sheetYS.createRow(i+1); | |
| 750 | + List<String> stringList = list.get(i); | |
| 751 | + int num = 4; | |
| 752 | + if("xl".equals(type)) | |
| 753 | + num = 2; | |
| 754 | + else if("cl".equals(type)) | |
| 755 | + num = 5; | |
| 756 | + for(int j=0; j<stringList.size(); j++){ | |
| 757 | + String str = stringList.get(j); | |
| 758 | + if(i == list.size()-1){ | |
| 759 | + if(j==0) { | |
| 760 | + setCellStyleAndValue(rowYSi, style, j, str); | |
| 761 | + CellRangeAddress callRangeAddressYSi = new CellRangeAddress(i+1,i+1,0,num); | |
| 762 | + sheetYS.addMergedRegion(callRangeAddressYSi); | |
| 763 | + // 样式 | |
| 764 | + setMergeCellStyle (callRangeAddressYSi, sheetYS, workbook); | |
| 765 | + }else | |
| 766 | + setCellStyleAndValue(rowYSi,style,j+num,str); | |
| 767 | + } else { | |
| 768 | + setCellStyleAndValue(rowYSi,style,j,str); | |
| 769 | + } | |
| 770 | + } | |
| 771 | + } | |
| 772 | + | |
| 773 | + // 给列设置宽度自适应 | |
| 774 | + setSizeColumn1(sheetYS,1,list.get(0).size()); | |
| 775 | + String path = this.getClass().getResource("/").getPath() + "static/pages/forms/export/"; | |
| 776 | + String targetPath = path+name+".xls"; | |
| 777 | + createFolder(targetPath); | |
| 778 | + FileOutputStream fout = new FileOutputStream(targetPath); | |
| 779 | + //5.输出 | |
| 780 | + workbook.write(fout); | |
| 781 | + fout.close(); | |
| 782 | + } catch (Exception e) { | |
| 783 | + e.printStackTrace(); | |
| 784 | + } | |
| 785 | + } | |
| 786 | + | |
| 787 | + /** | |
| 788 | + * 自适应宽度(中文支持) | |
| 789 | + * @param sheet | |
| 790 | + * @param size | |
| 791 | + */ | |
| 792 | + private static void setSizeColumn(HSSFSheet sheet, int size) { | |
| 793 | + for (int columnNum = 0; columnNum < size; columnNum++) { | |
| 794 | + int columnWidth = sheet.getColumnWidth(columnNum) / 256; | |
| 795 | + for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { | |
| 796 | + HSSFRow currentRow; | |
| 797 | + //当前行未被使用过 | |
| 798 | + if (sheet.getRow(rowNum) == null) { | |
| 799 | + currentRow = sheet.createRow(rowNum); | |
| 800 | + } else { | |
| 801 | + currentRow = sheet.getRow(rowNum); | |
| 802 | + } | |
| 803 | + if (currentRow.getCell(columnNum) != null) { | |
| 804 | + HSSFCell currentCell = currentRow.getCell(columnNum); | |
| 805 | + if (currentCell.getCellType() == XSSFCell.CELL_TYPE_STRING) { | |
| 806 | + int length = currentCell.getStringCellValue().getBytes().length; | |
| 807 | + if (columnWidth < length) { | |
| 808 | + columnWidth = length; | |
| 809 | + } | |
| 810 | + } | |
| 811 | + } | |
| 812 | + } | |
| 813 | + sheet.setColumnWidth(columnNum, columnWidth * 300); | |
| 814 | +// sheet.setColumnWidth(columnNum, columnWidth * 256); | |
| 815 | + } | |
| 816 | + } | |
| 817 | + | |
| 818 | + /** | |
| 819 | + * 自适应宽度(中文支持) | |
| 820 | + * @param sheet | |
| 821 | + * @param index 从那一行开始自适应 | |
| 822 | + * @param size | |
| 823 | + */ | |
| 824 | + private static void setSizeColumn1(HSSFSheet sheet,int index, int size) { | |
| 825 | + for (int columnNum = index; columnNum < size; columnNum++) { | |
| 826 | + int columnWidth = sheet.getColumnWidth(columnNum) / 256; | |
| 827 | + for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { | |
| 828 | + HSSFRow currentRow; | |
| 829 | + //当前行未被使用过 | |
| 830 | + if (sheet.getRow(rowNum) == null) { | |
| 831 | + currentRow = sheet.createRow(rowNum); | |
| 832 | + } else { | |
| 833 | + currentRow = sheet.getRow(rowNum); | |
| 834 | + } | |
| 835 | + if (currentRow.getCell(columnNum) != null) { | |
| 836 | + HSSFCell currentCell = currentRow.getCell(columnNum); | |
| 837 | + if (currentCell.getCellType() == XSSFCell.CELL_TYPE_STRING) { | |
| 838 | + int length = currentCell.getStringCellValue().getBytes().length; | |
| 839 | + if (columnWidth < length) { | |
| 840 | + columnWidth = length; | |
| 841 | + } | |
| 842 | + } | |
| 843 | + } | |
| 844 | + } | |
| 845 | + sheet.setColumnWidth(columnNum, columnWidth * 300); | |
| 846 | +// sheet.setColumnWidth(columnNum, columnWidth * 256); | |
| 847 | + } | |
| 848 | + } | |
| 849 | + | |
| 850 | + /** | |
| 851 | + * 设置单元格值和样式 | |
| 852 | + * @param row | |
| 853 | + * @param style | |
| 854 | + * @param index | |
| 855 | + * @param value | |
| 856 | + */ | |
| 857 | + public static void setCellStyleAndValue(HSSFRow row,HSSFCellStyle style,int index,String value){ | |
| 858 | + HSSFCell cell = row.createCell(index); | |
| 859 | + cell.setCellValue(value); | |
| 860 | + cell.setCellStyle(style); | |
| 861 | + } | |
| 862 | + /** | |
| 863 | + * 设置合并单元格样式 | |
| 864 | + * @param cra | |
| 865 | + * @param sheet | |
| 866 | + * @param workbook | |
| 867 | + */ | |
| 868 | + public static void setMergeCellStyle (CellRangeAddress cra, HSSFSheet sheet, Workbook workbook){ | |
| 869 | + // 使用RegionUtil类为合并后的单元格添加边框 | |
| 870 | + RegionUtil.setBorderBottom(1, cra, sheet, workbook); // 下边框 | |
| 871 | + RegionUtil.setBorderLeft(1, cra, sheet, workbook); // 左边框 | |
| 872 | + RegionUtil.setBorderRight(1, cra, sheet, workbook); // 有边框 | |
| 873 | + RegionUtil.setBorderTop(1, cra, sheet, workbook); // 上边框 | |
| 874 | + } | |
| 875 | +} | ... | ... |
src/main/resources/application-cloud.properties
| ... | ... | @@ -57,7 +57,7 @@ admin.mail= 3090342880@qq.com |
| 57 | 57 | ## enabled |
| 58 | 58 | enabled.whiteip= true |
| 59 | 59 | |
| 60 | -sso.enabled= true | |
| 60 | +sso.enabled= false | |
| 61 | 61 | sso.systemcode = SYS0019 |
| 62 | 62 | sso.http.url.login= http://180.169.154.251:28090/portal/index.html#/login |
| 63 | 63 | sso.http.url.logout= http://180.169.154.251:18080/information/api/v1/logout | ... | ... |
src/main/resources/static/pages/control/lineallot_v2/main.html
| 1 | -<!DOCTYPE html> | |
| 2 | -<html lang="zh-cn"> | |
| 3 | - | |
| 4 | -<head> | |
| 5 | - <meta charset="UTF-8"> | |
| 6 | - <link rel="stylesheet" href="/assets/plugins/uk3.0/uikit.min.css"/> | |
| 7 | - <link rel="stylesheet" href="/real_control_v2/assets/plugins/perfect-scrollbar/perfect-scrollbar.css"/> | |
| 8 | - <!-- flatpickr --> | |
| 9 | - <link rel="stylesheet" href="/real_control_v2/assets/plugins/flatpickr/flatpickr.min.css"> | |
| 10 | - <style> | |
| 11 | - html, body { | |
| 12 | - height: 100%; | |
| 13 | - } | |
| 14 | - | |
| 15 | - .ct_page { | |
| 16 | - padding: 25px 15px; | |
| 17 | - height: 100%; | |
| 18 | - height: calc(100% - 65px); | |
| 19 | - } | |
| 20 | - | |
| 21 | - .ct_cont { | |
| 22 | - height: calc(100% - 41px); | |
| 23 | - } | |
| 24 | - | |
| 25 | - .ct_cont > div > div.uk-card { | |
| 26 | - height: 99%; | |
| 27 | - } | |
| 28 | - | |
| 29 | - .loading { | |
| 30 | - height: 100%; | |
| 31 | - text-align: center; | |
| 32 | - } | |
| 33 | - | |
| 34 | - .loading .uk-spinner { | |
| 35 | - margin-top: 200px; | |
| 36 | - } | |
| 37 | - | |
| 38 | - .loading circle { | |
| 39 | - stroke: red; | |
| 40 | - } | |
| 41 | - | |
| 42 | - .ps-container > .ps-scrollbar-x-rail, .ps-container > .ps-scrollbar-y-rail { | |
| 43 | - opacity: 0.6 !important; | |
| 44 | - padding: 0 !important; | |
| 45 | - } | |
| 46 | - | |
| 47 | - .line_list_all_wrap { | |
| 48 | - height: 200px; | |
| 49 | - overflow: auto; | |
| 50 | - position: relative; | |
| 51 | - -webkit-user-select:none; | |
| 52 | - -moz-user-select:none; | |
| 53 | - -ms-user-select:none; | |
| 54 | - user-select:none; | |
| 55 | - } | |
| 56 | - | |
| 57 | - .line_list_all_wrap > .item { | |
| 58 | - background: #fff; | |
| 59 | - color: #666; | |
| 60 | - box-shadow: 0px 3px 12px rgba(0, 0, 0, 0.08); | |
| 61 | - padding: 7px 12px; | |
| 62 | - border: 1px solid #ececec; | |
| 63 | - display: inline-block; | |
| 64 | - margin: 5px; | |
| 65 | - min-width: 70px; | |
| 66 | - text-align: center; | |
| 67 | - cursor: default; | |
| 68 | - } | |
| 69 | - | |
| 70 | - .line_list_all_wrap > .item.active{ | |
| 71 | - background: #ffe9a5; | |
| 72 | - } | |
| 73 | - | |
| 74 | - .checked_list { | |
| 75 | - padding-left: 4px; | |
| 76 | - -webkit-user-select:none; | |
| 77 | - -moz-user-select:none; | |
| 78 | - -ms-user-select:none; | |
| 79 | - user-select:none; | |
| 80 | - } | |
| 81 | - | |
| 82 | - .checked_list > .item { | |
| 83 | - color: #fbfbfb; | |
| 84 | - box-shadow: 0px 3px 12px rgba(50, 197, 210, 0.3); | |
| 85 | - padding: 7px 12px; | |
| 86 | - border: 1px solid #ececec; | |
| 87 | - display: inline-block; | |
| 88 | - margin: 5px 2px; | |
| 89 | - min-width: 70px; | |
| 90 | - text-align: center; | |
| 91 | - background: #32C5D2; | |
| 92 | - cursor: move; | |
| 93 | - } | |
| 94 | - | |
| 95 | - .company_search_wrap { | |
| 96 | - width: 610px; | |
| 97 | - margin-left: 6px; | |
| 98 | - } | |
| 99 | - | |
| 100 | - .company_search_wrap select { | |
| 101 | - display: inline-block; | |
| 102 | - width: 128px; | |
| 103 | - margin-right: 12px; | |
| 104 | - } | |
| 105 | - | |
| 106 | - .uk-card { | |
| 107 | - border: 1px solid #eeeeee; | |
| 108 | - } | |
| 109 | - | |
| 110 | - .footer_tools_card { | |
| 111 | - margin-top: 20px; | |
| 112 | - padding: 20px 40px; | |
| 113 | - } | |
| 114 | - | |
| 115 | - .footer_tools_card label { | |
| 116 | - vertical-align: bottom; | |
| 117 | - display: inline-block; | |
| 118 | - font-size: 13px; | |
| 119 | - margin-left: 13px; | |
| 120 | - margin-bottom: 3px !important; | |
| 121 | - } | |
| 122 | - | |
| 123 | - .uk-button-default{ | |
| 124 | - background: #e0e0e0 !important; | |
| 125 | - } | |
| 126 | - | |
| 127 | - .line_list_all_wrap > .item.destroy{ | |
| 128 | - color: #f64242; | |
| 129 | - } | |
| 130 | - | |
| 131 | - .checked_list > .item.destroy{ | |
| 132 | - background: #ff5c5c; | |
| 133 | - } | |
| 134 | - </style> | |
| 135 | -</head> | |
| 136 | - | |
| 137 | -<body> | |
| 138 | -<div class="loading"> | |
| 139 | - <div uk-spinner></div> | |
| 140 | -</div> | |
| 141 | -<div class="ct_page" style="display: none;"> | |
| 142 | - <h2 class="uk-heading-line uk-heading-bullet"><span>线调 <h5 style="display: inline-block;">-选择线路</h5></span></h2> | |
| 143 | - <div class="ct_cont"> | |
| 144 | - <div class="allot_wrap uk-flex-center" uk-grid> | |
| 145 | - <div class="uk-card uk-card-hover uk-card-body uk-width-2-3@m" | |
| 146 | - style="padding-top: 20px;padding-bottom: 20px;"> | |
| 147 | - <div class="company_search_wrap"> | |
| 148 | - <select class="uk-select company_search_select"></select> | |
| 149 | - <select class="uk-select fgs_search_select" disabled> | |
| 150 | - <option value="">分公司搜索</option> | |
| 151 | - </select> | |
| 152 | - <div class="uk-inline"> | |
| 153 | - <span class="uk-form-icon uk-form-icon-flip" uk-icon="icon: search"></span> | |
| 154 | - <input class="uk-input line_search_input" style="width: 310px;" placeholder="搜索线路"> | |
| 155 | - </div> | |
| 156 | - </div> | |
| 157 | - <hr> | |
| 158 | - <div class="line_list_all_wrap"></div> | |
| 159 | - <hr> | |
| 160 | - <div class="checked_list" uk-sortable> </div> | |
| 161 | - </div> | |
| 162 | - | |
| 163 | - <div class="uk-card uk-card-hover uk-card-body uk-width-2-3@m footer_tools_card"> | |
| 164 | - <button class="uk-button uk-button-default uk-button-large" style="font-size: 16px" id="go_to_real_system_btn"><i | |
| 165 | - uk-icon=" icon: sign-in" style="margin-right: 5px;" ></i>进入线路调度 | |
| 166 | - </button> | |
| 167 | - <label class="pattern_type_label"><input class="uk-checkbox" type="checkbox" > 主调模式登录</label> | |
| 168 | - <label class="new_window_open_label" title="可能会被浏览器阻止新建窗口" uk-tooltip><input class="uk-checkbox" type="checkbox"> 新窗口打开</label> | |
| 169 | - </div> | |
| 170 | - | |
| 171 | - <div class="uk-alert-warning uk-width-2-3@m " uk-alert> | |
| 172 | - <p>注意!!! 相关“设备调试人员”和“测试人员”不要以主调模式进入线调。</p> | |
| 173 | - </div> | |
| 174 | - </div> | |
| 175 | - </div> | |
| 176 | -</div> | |
| 177 | - | |
| 178 | -<script id="line_list_items_temp" type="text/html"> | |
| 179 | - {{each list as obj i}} | |
| 180 | - <span class="item {{obj.destroy==1?'destroy':''}}" data-id="{{obj.lineCode}}" data-gsbm="{{obj.company}}_{{obj.brancheCompany}}">{{obj.name}}</span> | |
| 181 | - {{/each}} | |
| 182 | -</script> | |
| 183 | - | |
| 184 | -<script src="/metronic_v4.5.4/plugins/jquery.min.js"></script> | |
| 185 | -<script src="/assets/plugins/uk3.0/uikit.min.js"></script> | |
| 186 | -<script src="/assets/plugins/uk3.0/uikit-icons.min.js"></script> | |
| 187 | -<script src="/real_control_v2/assets/plugins/perfect-scrollbar/perfect-scrollbar.jquery.js"></script> | |
| 188 | -<!-- EventProxy --> | |
| 189 | -<script src="/assets/js/eventproxy.js"></script> | |
| 190 | -<!-- art-template 模版引擎 --> | |
| 191 | -<script src="/assets/plugins/template.js"></script> | |
| 192 | -<script src="/real_control_v2/assets/plugins/moment/moment.min.js"></script> | |
| 193 | -<script src="/assets/plugins/pinyin.js"></script> | |
| 194 | -<script> | |
| 195 | - var allMapps = {};//线路搜索映射 | |
| 196 | - var companyData = {}; | |
| 197 | - var codeMapps={}; //线路编码映射 | |
| 198 | - //滚动条 | |
| 199 | - $('.line_list_all_wrap').perfectScrollbar(); | |
| 200 | - | |
| 201 | - var ep = EventProxy.create('all_lines', 'user_author', 'company_data', function (allLines, userAuthor, company_data) { | |
| 202 | - //按用户权限过滤线路 | |
| 203 | - var authArray = userAuthor.lineCodeStr.split(','), | |
| 204 | - newArray = []; | |
| 205 | - $.each(allLines, function () { | |
| 206 | - if (this.lineCode && authArray.indexOf(this.lineCode) != -1) { | |
| 207 | - newArray.push(this); | |
| 208 | - codeMapps[this.lineCode] = this; | |
| 209 | - } | |
| 210 | - }); | |
| 211 | - //排序 | |
| 212 | - newArray.sort(function (a, b) { | |
| 213 | - return a.name.localeCompare(b.name); | |
| 214 | - }); | |
| 215 | - //创建线路搜索映射 | |
| 216 | - createLineSearchMapps(newArray); | |
| 217 | - | |
| 218 | - var htmlStr = template('line_list_items_temp', {list: newArray}); | |
| 219 | - $('.line_list_all_wrap').html(htmlStr); | |
| 220 | - | |
| 221 | - //过滤公司编码数据 | |
| 222 | - filter_company_data.filter(newArray, convert_buss_data(company_data)); | |
| 223 | - initCompanySelect(); | |
| 224 | - //更新滚动条高度 | |
| 225 | - setTimeout(function () { | |
| 226 | - $('.line_list_all_wrap').perfectScrollbar('update'); | |
| 227 | - }, 1000); | |
| 228 | - | |
| 229 | - //登录模式 | |
| 230 | - changePattern(userAuthor.pattern); | |
| 231 | - if(!userAuthor.pattern) | |
| 232 | - $('.pattern_type_label').hide().find('input')[0].checked=false; | |
| 233 | - else | |
| 234 | - $('input','.pattern_type_label')[0].checked=true; | |
| 235 | - | |
| 236 | - $('.loading').remove(); | |
| 237 | - $('.ct_page').show(); | |
| 238 | - | |
| 239 | - //默认选中上次记录 | |
| 240 | - var lineControlItems = window.localStorage.getItem('lineControlItems'); | |
| 241 | - if (lineControlItems) { | |
| 242 | - var array = JSON.parse(lineControlItems); | |
| 243 | - $.each(array, function (i, line) { | |
| 244 | - $('.item[data-id='+line.lineCode+']','.line_list_all_wrap').trigger('click'); | |
| 245 | - }); | |
| 246 | - } | |
| 247 | - }); | |
| 248 | - | |
| 249 | - $('.pattern_type_label').on('click', function () { | |
| 250 | - var checked = $('input',this)[0].checked; | |
| 251 | - changePattern(checked); | |
| 252 | - }); | |
| 253 | - | |
| 254 | - function changePattern(status) { | |
| 255 | - var btn = $('#go_to_real_system_btn'), | |
| 256 | - def='uk-button-default', | |
| 257 | - prim='uk-button-primary'; | |
| 258 | - if(status){ | |
| 259 | - btn.removeClass(def).addClass(prim); | |
| 260 | - } | |
| 261 | - else{ | |
| 262 | - btn.removeClass(prim).addClass(def); | |
| 263 | - } | |
| 264 | - } | |
| 265 | - | |
| 266 | - /** | |
| 267 | - * 查询所有线路 | |
| 268 | - */ | |
| 269 | - $.get('/line/all', {remove_ne: 1}, function (list) { | |
| 270 | - ep.emit('all_lines', list); | |
| 271 | - }); | |
| 272 | - | |
| 273 | - /**用户分配的线路*/ | |
| 274 | - $.get('/realControAuthority/findByCurrentUser', function (t) { | |
| 275 | - ep.emit('user_author', t); | |
| 276 | - }); | |
| 277 | - | |
| 278 | - /** 查询公司编码 */ | |
| 279 | - $.get('/business/all', function (rs) { | |
| 280 | - ep.emit('company_data', rs); | |
| 281 | - }); | |
| 282 | - | |
| 283 | - //点击选择线路 | |
| 284 | - $('.line_list_all_wrap').on('click', 'span.item', function () { | |
| 285 | - var lineCode = $(this).data('id'), | |
| 286 | - name = $(this).text(); | |
| 287 | - if ($(this).hasClass('active')) { | |
| 288 | - $(this).removeClass('active'); | |
| 289 | - | |
| 290 | - $('.item[data-id='+lineCode+']','.checked_list').remove(); | |
| 291 | - } | |
| 292 | - else { | |
| 293 | - $(this).addClass('active'); | |
| 294 | - var span = $('<span data-id="'+lineCode+'" class="item uk-card uk-card-default uk-animation-slide-bottom '+($(this).hasClass('destroy')?"destroy":"")+'">'+name+'</span>') | |
| 295 | - .one('webkitAnimationEnd', function () { | |
| 296 | - $(this).removeClass('uk-animation-slide-bottom'); | |
| 297 | - }); | |
| 298 | - $('.checked_list').append(span); | |
| 299 | - } | |
| 300 | - }); | |
| 301 | - | |
| 302 | - $('.checked_list').on('click', '.item', function () { | |
| 303 | - var lineCode = $(this).data('id'); | |
| 304 | - $('.item[data-id='+lineCode+']','.line_list_all_wrap').removeClass('active'); | |
| 305 | - $(this).remove(); | |
| 306 | - }); | |
| 307 | - | |
| 308 | - /** | |
| 309 | - * 搜索线路 | |
| 310 | - */ | |
| 311 | - function search_line_list() { | |
| 312 | - var comp = $('select.company_search_select').val(), | |
| 313 | - fgs = $('select.fgs_search_select').val(), | |
| 314 | - gsbm = comp + '_' + fgs; | |
| 315 | - | |
| 316 | - if(gsbm=='_') | |
| 317 | - gsbm=null; | |
| 318 | - var allDoms = $('.line_list_all_wrap span.item'); | |
| 319 | - //线路 | |
| 320 | - var lineCodeArray = []; | |
| 321 | - var t = $('.line_search_input').val(); | |
| 322 | - if(t){ | |
| 323 | - t = t.toUpperCase(); | |
| 324 | - for(var m in allMapps){ | |
| 325 | - if(m.indexOf(t)!=-1) | |
| 326 | - lineCodeArray.push(allMapps[m]); | |
| 327 | - } | |
| 328 | - } | |
| 329 | - | |
| 330 | - //搜索 | |
| 331 | - var show_elems = []; | |
| 332 | - for (var i=0, dom; dom = allDoms[i++];) { | |
| 333 | - if(gsbm && $(dom).data('gsbm').indexOf(gsbm)==-1) | |
| 334 | - continue; | |
| 335 | - | |
| 336 | - if(t && lineCodeArray.indexOf($(dom).data('id')+'') == -1) | |
| 337 | - continue; | |
| 338 | - | |
| 339 | - show_elems.push($(dom)); | |
| 340 | - } | |
| 341 | - | |
| 342 | - //show | |
| 343 | - allDoms.hide(); | |
| 344 | - for(var j=0,$dom;$dom=show_elems[j++];) | |
| 345 | - $dom.show(); | |
| 346 | - } | |
| 347 | - | |
| 348 | - function createLineSearchMapps(list) { | |
| 349 | - allMapps = {}; | |
| 350 | - var name, code; | |
| 351 | - for (var i = 0, line; line = list[i++];) { | |
| 352 | - name = line.name; | |
| 353 | - code = line.lineCode; | |
| 354 | - //拼音映射 | |
| 355 | - allMapps[pinyin.getCamelChars(name)] = code; | |
| 356 | - allMapps[pinyin.getFullChars(name).toUpperCase()] = code; | |
| 357 | - //中文映射 | |
| 358 | - allMapps[name] = code; | |
| 359 | - } | |
| 360 | - } | |
| 361 | - | |
| 362 | - | |
| 363 | - //构建公司级联下拉框 | |
| 364 | - function initCompanySelect() { | |
| 365 | - var opts = '<option value="">公司搜索</option>'; | |
| 366 | - for (var k in companyData) { | |
| 367 | - opts += '<option value="' + k + '">' + companyData[k].name + '</option>'; | |
| 368 | - } | |
| 369 | - $('select.company_search_select').html(opts); | |
| 370 | - } | |
| 371 | - | |
| 372 | - //公司切换 | |
| 373 | - $('select.company_search_select').on('change', function () { | |
| 374 | - var opts = '<option value="">分公司搜索</option>', | |
| 375 | - t = $(this).val(), | |
| 376 | - fgsSelect = $('select.fgs_search_select'); | |
| 377 | - if (!t) { | |
| 378 | - fgsSelect.html(opts).attr('disabled', 'disabled'); | |
| 379 | - } | |
| 380 | - else{ | |
| 381 | - var array = companyData[t].childs; | |
| 382 | - for (var i = 0, fgs; fgs = array[i++];) { | |
| 383 | - opts += '<option value="' + fgs.businessCode + '">' + fgs.businessName + '</option>'; | |
| 384 | - } | |
| 385 | - | |
| 386 | - fgsSelect.html(opts).removeAttr('disabled'); | |
| 387 | - } | |
| 388 | - search_line_list(); | |
| 389 | - }); | |
| 390 | - | |
| 391 | - //分公司切换 | |
| 392 | - $('select.fgs_search_select').on('change', search_line_list); | |
| 393 | - | |
| 394 | - //线路文本框输入 | |
| 395 | - var flag; | |
| 396 | - $('.line_search_input').on('input', function () { | |
| 397 | - if(flag) | |
| 398 | - return; | |
| 399 | - flag = true; | |
| 400 | - setTimeout(function () { | |
| 401 | - //延迟 | |
| 402 | - search_line_list(); | |
| 403 | - flag = false; | |
| 404 | - }, 200); | |
| 405 | - }); | |
| 406 | - | |
| 407 | - function convert_buss_data(rs) { | |
| 408 | - var baseCode; | |
| 409 | - //找到跟节点 | |
| 410 | - $.each(rs, function () { | |
| 411 | - if (this.upCode == 0) { | |
| 412 | - baseCode = this.businessCode; | |
| 413 | - return false; | |
| 414 | - } | |
| 415 | - }); | |
| 416 | - if (!baseCode)return; | |
| 417 | - //提取二级节点 | |
| 418 | - var secondMap = {}; | |
| 419 | - $.each(rs, function () { | |
| 420 | - if (this.upCode == baseCode) | |
| 421 | - secondMap[this.businessCode] = {name: this.businessName, childs: []}; | |
| 422 | - }); | |
| 423 | - //分公司节点 | |
| 424 | - $.each(rs, function () { | |
| 425 | - if (secondMap[this.upCode]) | |
| 426 | - secondMap[this.upCode].childs.push(this); | |
| 427 | - }); | |
| 428 | - //排序 | |
| 429 | - for (var sid in secondMap) | |
| 430 | - secondMap[sid].childs.sort(naturalSort); | |
| 431 | - return secondMap; | |
| 432 | - } | |
| 433 | - | |
| 434 | - var naturalSort = function (a, b) { | |
| 435 | - return a.businessName.localeCompare(b.businessName); | |
| 436 | - }; | |
| 437 | - | |
| 438 | - /** | |
| 439 | - * 根据用户线路权限过滤公司编码数据 | |
| 440 | - */ | |
| 441 | - var filter_company_data = (function () { | |
| 442 | - var filter = function (lineArray, data) { | |
| 443 | - companyData = {}; | |
| 444 | - var com, fCom; | |
| 445 | - $.each(lineArray, function () { | |
| 446 | - com = this.company; | |
| 447 | - fCom = this.brancheCompany; | |
| 448 | - if (!companyData[com]) { | |
| 449 | - companyData[com] = {name: data[com].name, childs: []}; | |
| 450 | - } | |
| 451 | - | |
| 452 | - if (!get(fCom, companyData[com].childs)) { | |
| 453 | - companyData[com].childs.push(get(fCom, data[com].childs)); | |
| 454 | - } | |
| 455 | - }); | |
| 456 | - }; | |
| 457 | - | |
| 458 | - function get(fgsCode, fgsArray) { | |
| 459 | - for (var i = 0, fgs; fgs = fgsArray[i++];) { | |
| 460 | - if (fgs.businessCode == fgsCode) | |
| 461 | - return fgs; | |
| 462 | - } | |
| 463 | - return null; | |
| 464 | - } | |
| 465 | - | |
| 466 | - return { | |
| 467 | - filter: filter | |
| 468 | - } | |
| 469 | - })(); | |
| 470 | - var storage = window.localStorage; | |
| 471 | - $('#go_to_real_system_btn').on('click', go_to_xd_sys); | |
| 472 | - /** | |
| 473 | - * 进入线调 | |
| 474 | - */ | |
| 475 | - function go_to_xd_sys() { | |
| 476 | - var items = $('.checked_list .item'); | |
| 477 | - if(items.length==0) | |
| 478 | - return UIkit.notification({message: '你还没有选择线路!', pos: 'bottom-center', status: 'danger'}); | |
| 479 | - $(this).attr('disabled', 'disabled'); | |
| 480 | - | |
| 481 | - show_btn_load(this, '更新缓存数据...'); | |
| 482 | - (function () { | |
| 483 | - var ls_line_data = []; | |
| 484 | - $.each(items, function () { | |
| 485 | - ls_line_data.push(codeMapps[$(this).data('id')]); | |
| 486 | - }); | |
| 487 | - //监控模式还是主调模式 | |
| 488 | - storage.setItem('operationMode', $('.pattern_type_label>input')[0].checked?1:0); | |
| 489 | - | |
| 490 | - //进入线调 | |
| 491 | - var eq = EventProxy.create('cache_line_level', 'cache_mv_route', 'cache_route', 'check_line_config', function (lineLevel) { | |
| 492 | - debugger; | |
| 493 | - var newWinOpen = $('input','.new_window_open_label')[0].checked; | |
| 494 | - if(!newWinOpen) | |
| 495 | - top.window.location.href = "/real_control/v2"; | |
| 496 | - else{ | |
| 497 | - window.open("/real_control/v2"); | |
| 498 | - $('#go_to_real_system_btn').html('已经尝试打开新窗口,如看不到,可能被浏览器阻止了'); | |
| 499 | - } | |
| 500 | - | |
| 501 | - // 将线路级别赋值 | |
| 502 | - $.each(ls_line_data, function () { | |
| 503 | - this.lineLevel = lineLevel[this.lineCode]; | |
| 504 | - }); | |
| 505 | - // 将线路基础信息写入localStorage | |
| 506 | - storage.setItem('lineControlItems', JSON.stringify(ls_line_data)); | |
| 507 | - }); | |
| 508 | - | |
| 509 | - //拼接线路编码 | |
| 510 | - var idx=''; | |
| 511 | - $.each(ls_line_data, function () { | |
| 512 | - idx+=(this.lineCode + ','); | |
| 513 | - }); | |
| 514 | - //缓存路由 | |
| 515 | - idx=idx.substr(0, idx.length - 1); | |
| 516 | - | |
| 517 | - $.get('/realSchedule/lineLevel', {idx: idx}, function (rs) { | |
| 518 | - if (rs) { | |
| 519 | - eq.emit('cache_line_level', rs); | |
| 520 | - } | |
| 521 | - }); | |
| 522 | - | |
| 523 | - $.get('/realMap/findRouteAndVersionByLine', {idx: idx}, function (rs) { | |
| 524 | - if (rs) { | |
| 525 | - for(var lineCode in rs) | |
| 526 | - storage.setItem(lineCode + '_route', JSON.stringify(rs[lineCode])); | |
| 527 | - eq.emit('cache_mv_route'); | |
| 528 | - } | |
| 529 | - }); | |
| 530 | - | |
| 531 | - $.get('/realMap/findRouteByLine', {idx: idx}, function (rs) { | |
| 532 | - if (rs) { | |
| 533 | - for(var lineCode in rs) | |
| 534 | - storage.setItem(lineCode + '_route', JSON.stringify(rs[lineCode])); | |
| 535 | - | |
| 536 | - eq.emit('cache_route'); | |
| 537 | - } | |
| 538 | - }); | |
| 539 | - | |
| 540 | - | |
| 541 | - //检查线路配置 | |
| 542 | - checkLineConfig(ls_line_data, function (rs) { | |
| 543 | - if (rs.status == 0) | |
| 544 | - eq.emit('check_line_config'); | |
| 545 | - else if (rs.status == 1){ | |
| 546 | - initLineConfig(rs.not, function () { | |
| 547 | - eq.emit('check_line_config'); | |
| 548 | - }); | |
| 549 | - } | |
| 550 | - }) | |
| 551 | - })(); | |
| 552 | - } | |
| 553 | - | |
| 554 | - function checkLineConfig(lsData, cb) { | |
| 555 | - var lineCodeArray = []; | |
| 556 | - $.each(lsData, function () { | |
| 557 | - lineCodeArray.push(this.lineCode); | |
| 558 | - }); | |
| 559 | - | |
| 560 | - $.ajax({ | |
| 561 | - url: '/lineConfig/check', | |
| 562 | - traditional: true, | |
| 563 | - data: {codeArray: lineCodeArray}, | |
| 564 | - method: 'POST', | |
| 565 | - success: cb | |
| 566 | - }); | |
| 567 | - } | |
| 568 | - | |
| 569 | - function initLineConfig(arr, cb) { | |
| 570 | - var i = 0; | |
| 571 | - (function () { | |
| 572 | - if (i >= arr.length) { | |
| 573 | - cb && cb(); | |
| 574 | - return; | |
| 575 | - } | |
| 576 | - var f = arguments.callee | |
| 577 | - , lineCode = arr[i]; | |
| 578 | - | |
| 579 | - //showLoad('初始化' + lineIds[lineCode] + '配置信息...'); | |
| 580 | - $.post('/lineConfig/init/' + lineCode, function (rs) { | |
| 581 | - if (rs == 1) { | |
| 582 | - i++; | |
| 583 | - f(); | |
| 584 | - } | |
| 585 | - }); | |
| 586 | - })(); | |
| 587 | - } | |
| 588 | - | |
| 589 | - /** | |
| 590 | - * 按钮转菊花 | |
| 591 | - */ | |
| 592 | - function show_btn_load(btn, msg) { | |
| 593 | - $(btn).html('<div uk-spinner></div> ' + msg); | |
| 594 | - } | |
| 595 | -</script> | |
| 596 | -</body> | |
| 1 | +<!DOCTYPE html> | |
| 2 | +<html lang="zh-cn"> | |
| 3 | + | |
| 4 | +<head> | |
| 5 | + <meta charset="UTF-8"> | |
| 6 | + <link rel="stylesheet" href="/assets/plugins/uk3.0/uikit.min.css"/> | |
| 7 | + <link rel="stylesheet" href="/real_control_v2/assets/plugins/perfect-scrollbar/perfect-scrollbar.css"/> | |
| 8 | + <!-- flatpickr --> | |
| 9 | + <link rel="stylesheet" href="/real_control_v2/assets/plugins/flatpickr/flatpickr.min.css"> | |
| 10 | + <style> | |
| 11 | + html, body { | |
| 12 | + height: 100%; | |
| 13 | + } | |
| 14 | + | |
| 15 | + .ct_page { | |
| 16 | + padding: 25px 15px; | |
| 17 | + height: 100%; | |
| 18 | + height: calc(100% - 65px); | |
| 19 | + } | |
| 20 | + | |
| 21 | + .ct_cont { | |
| 22 | + height: calc(100% - 41px); | |
| 23 | + } | |
| 24 | + | |
| 25 | + .ct_cont > div > div.uk-card { | |
| 26 | + height: 99%; | |
| 27 | + } | |
| 28 | + | |
| 29 | + .loading { | |
| 30 | + height: 100%; | |
| 31 | + text-align: center; | |
| 32 | + } | |
| 33 | + | |
| 34 | + .loading .uk-spinner { | |
| 35 | + margin-top: 200px; | |
| 36 | + } | |
| 37 | + | |
| 38 | + .loading circle { | |
| 39 | + stroke: red; | |
| 40 | + } | |
| 41 | + | |
| 42 | + .ps-container > .ps-scrollbar-x-rail, .ps-container > .ps-scrollbar-y-rail { | |
| 43 | + opacity: 0.6 !important; | |
| 44 | + padding: 0 !important; | |
| 45 | + } | |
| 46 | + | |
| 47 | + .line_list_all_wrap { | |
| 48 | + height: 200px; | |
| 49 | + overflow: auto; | |
| 50 | + position: relative; | |
| 51 | + -webkit-user-select:none; | |
| 52 | + -moz-user-select:none; | |
| 53 | + -ms-user-select:none; | |
| 54 | + user-select:none; | |
| 55 | + } | |
| 56 | + | |
| 57 | + .line_list_all_wrap > .item { | |
| 58 | + background: #fff; | |
| 59 | + color: #666; | |
| 60 | + box-shadow: 0px 3px 12px rgba(0, 0, 0, 0.08); | |
| 61 | + padding: 7px 12px; | |
| 62 | + border: 1px solid #ececec; | |
| 63 | + display: inline-block; | |
| 64 | + margin: 5px; | |
| 65 | + min-width: 70px; | |
| 66 | + text-align: center; | |
| 67 | + cursor: default; | |
| 68 | + } | |
| 69 | + | |
| 70 | + .line_list_all_wrap > .item.active{ | |
| 71 | + background: #ffe9a5; | |
| 72 | + } | |
| 73 | + | |
| 74 | + .checked_list { | |
| 75 | + padding-left: 4px; | |
| 76 | + -webkit-user-select:none; | |
| 77 | + -moz-user-select:none; | |
| 78 | + -ms-user-select:none; | |
| 79 | + user-select:none; | |
| 80 | + } | |
| 81 | + | |
| 82 | + .checked_list > .item { | |
| 83 | + color: #fbfbfb; | |
| 84 | + box-shadow: 0px 3px 12px rgba(50, 197, 210, 0.3); | |
| 85 | + padding: 7px 12px; | |
| 86 | + border: 1px solid #ececec; | |
| 87 | + display: inline-block; | |
| 88 | + margin: 5px 2px; | |
| 89 | + min-width: 70px; | |
| 90 | + text-align: center; | |
| 91 | + background: #32C5D2; | |
| 92 | + cursor: move; | |
| 93 | + } | |
| 94 | + | |
| 95 | + .company_search_wrap { | |
| 96 | + width: 610px; | |
| 97 | + margin-left: 6px; | |
| 98 | + } | |
| 99 | + | |
| 100 | + .company_search_wrap select { | |
| 101 | + display: inline-block; | |
| 102 | + width: 128px; | |
| 103 | + margin-right: 12px; | |
| 104 | + } | |
| 105 | + | |
| 106 | + .uk-card { | |
| 107 | + border: 1px solid #eeeeee; | |
| 108 | + } | |
| 109 | + | |
| 110 | + .footer_tools_card { | |
| 111 | + margin-top: 20px; | |
| 112 | + padding: 20px 40px; | |
| 113 | + } | |
| 114 | + | |
| 115 | + .footer_tools_card label { | |
| 116 | + vertical-align: bottom; | |
| 117 | + display: inline-block; | |
| 118 | + font-size: 13px; | |
| 119 | + margin-left: 13px; | |
| 120 | + margin-bottom: 3px !important; | |
| 121 | + } | |
| 122 | + | |
| 123 | + .uk-button-default{ | |
| 124 | + background: #e0e0e0 !important; | |
| 125 | + } | |
| 126 | + | |
| 127 | + .line_list_all_wrap > .item.destroy{ | |
| 128 | + color: #f64242; | |
| 129 | + } | |
| 130 | + | |
| 131 | + .checked_list > .item.destroy{ | |
| 132 | + background: #ff5c5c; | |
| 133 | + } | |
| 134 | + </style> | |
| 135 | +</head> | |
| 136 | + | |
| 137 | +<body> | |
| 138 | +<div class="loading"> | |
| 139 | + <div uk-spinner></div> | |
| 140 | +</div> | |
| 141 | +<div class="ct_page" style="display: none;"> | |
| 142 | + <h2 class="uk-heading-line uk-heading-bullet"><span>线调 <h5 style="display: inline-block;">-选择线路</h5></span></h2> | |
| 143 | + <div class="ct_cont"> | |
| 144 | + <div class="allot_wrap uk-flex-center" uk-grid> | |
| 145 | + <div class="uk-card uk-card-hover uk-card-body uk-width-2-3@m" | |
| 146 | + style="padding-top: 20px;padding-bottom: 20px;"> | |
| 147 | + <div class="company_search_wrap"> | |
| 148 | + <select class="uk-select company_search_select"></select> | |
| 149 | + <select class="uk-select fgs_search_select" disabled> | |
| 150 | + <option value="">分公司搜索</option> | |
| 151 | + </select> | |
| 152 | + <div class="uk-inline"> | |
| 153 | + <span class="uk-form-icon uk-form-icon-flip" uk-icon="icon: search"></span> | |
| 154 | + <input class="uk-input line_search_input" style="width: 310px;" placeholder="搜索线路"> | |
| 155 | + </div> | |
| 156 | + </div> | |
| 157 | + <hr> | |
| 158 | + <div class="line_list_all_wrap"></div> | |
| 159 | + <hr> | |
| 160 | + <div class="checked_list" uk-sortable> </div> | |
| 161 | + </div> | |
| 162 | + | |
| 163 | + <div class="uk-card uk-card-hover uk-card-body uk-width-2-3@m footer_tools_card"> | |
| 164 | + <button class="uk-button uk-button-default uk-button-large" style="font-size: 16px" id="go_to_real_system_btn"><i | |
| 165 | + uk-icon=" icon: sign-in" style="margin-right: 5px;" ></i>进入线路调度 | |
| 166 | + </button> | |
| 167 | + <label class="pattern_type_label"><input class="uk-checkbox" type="checkbox" > 主调模式登录</label> | |
| 168 | + <label class="new_window_open_label" title="可能会被浏览器阻止新建窗口" uk-tooltip><input class="uk-checkbox" type="checkbox"> 新窗口打开</label> | |
| 169 | + </div> | |
| 170 | + | |
| 171 | + <div class="uk-alert-warning uk-width-2-3@m " uk-alert> | |
| 172 | + <p>注意!!! 相关“设备调试人员”和“测试人员”不要以主调模式进入线调。</p> | |
| 173 | + </div> | |
| 174 | + </div> | |
| 175 | + </div> | |
| 176 | +</div> | |
| 177 | + | |
| 178 | +<script id="line_list_items_temp" type="text/html"> | |
| 179 | + {{each list as obj i}} | |
| 180 | + <span class="item {{obj.destroy==1?'destroy':''}}" data-id="{{obj.lineCode}}" data-gsbm="{{obj.company}}_{{obj.brancheCompany}}">{{obj.name}}</span> | |
| 181 | + {{/each}} | |
| 182 | +</script> | |
| 183 | + | |
| 184 | +<script src="/metronic_v4.5.4/plugins/jquery.min.js"></script> | |
| 185 | +<script src="/assets/plugins/uk3.0/uikit.min.js"></script> | |
| 186 | +<script src="/assets/plugins/uk3.0/uikit-icons.min.js"></script> | |
| 187 | +<script src="/real_control_v2/assets/plugins/perfect-scrollbar/perfect-scrollbar.jquery.js"></script> | |
| 188 | +<!-- EventProxy --> | |
| 189 | +<script src="/assets/js/eventproxy.js"></script> | |
| 190 | +<!-- art-template 模版引擎 --> | |
| 191 | +<script src="/assets/plugins/template.js"></script> | |
| 192 | +<script src="/real_control_v2/assets/plugins/moment/moment.min.js"></script> | |
| 193 | +<script src="/assets/plugins/pinyin.js"></script> | |
| 194 | +<script> | |
| 195 | + var allMapps = {};//线路搜索映射 | |
| 196 | + var companyData = {}; | |
| 197 | + var codeMapps={}; //线路编码映射 | |
| 198 | + //滚动条 | |
| 199 | + $('.line_list_all_wrap').perfectScrollbar(); | |
| 200 | + | |
| 201 | + var ep = EventProxy.create('all_lines', 'user_author', 'company_data', function (allLines, userAuthor, company_data) { | |
| 202 | + //按用户权限过滤线路 | |
| 203 | + var authArray = userAuthor.lineCodeStr.split(','), | |
| 204 | + newArray = []; | |
| 205 | + $.each(allLines, function () { | |
| 206 | + if (this.lineCode && authArray.indexOf(this.lineCode) != -1) { | |
| 207 | + newArray.push(this); | |
| 208 | + codeMapps[this.lineCode] = this; | |
| 209 | + } | |
| 210 | + }); | |
| 211 | + //排序 | |
| 212 | + newArray.sort(function (a, b) { | |
| 213 | + return a.name.localeCompare(b.name); | |
| 214 | + }); | |
| 215 | + //创建线路搜索映射 | |
| 216 | + createLineSearchMapps(newArray); | |
| 217 | + | |
| 218 | + var htmlStr = template('line_list_items_temp', {list: newArray}); | |
| 219 | + $('.line_list_all_wrap').html(htmlStr); | |
| 220 | + | |
| 221 | + //过滤公司编码数据 | |
| 222 | + filter_company_data.filter(newArray, convert_buss_data(company_data)); | |
| 223 | + initCompanySelect(); | |
| 224 | + //更新滚动条高度 | |
| 225 | + setTimeout(function () { | |
| 226 | + $('.line_list_all_wrap').perfectScrollbar('update'); | |
| 227 | + }, 1000); | |
| 228 | + | |
| 229 | + //登录模式 | |
| 230 | + changePattern(userAuthor.pattern); | |
| 231 | + if(!userAuthor.pattern) | |
| 232 | + $('.pattern_type_label').hide().find('input')[0].checked=false; | |
| 233 | + else | |
| 234 | + $('input','.pattern_type_label')[0].checked=true; | |
| 235 | + | |
| 236 | + $('.loading').remove(); | |
| 237 | + $('.ct_page').show(); | |
| 238 | + | |
| 239 | + //默认选中上次记录 | |
| 240 | + var lineControlItems = window.localStorage.getItem('lineControlItems'); | |
| 241 | + if (lineControlItems) { | |
| 242 | + var array = JSON.parse(lineControlItems); | |
| 243 | + $.each(array, function (i, line) { | |
| 244 | + $('.item[data-id='+line.lineCode+']','.line_list_all_wrap').trigger('click'); | |
| 245 | + }); | |
| 246 | + } | |
| 247 | + | |
| 248 | + var storage = window.localStorage, keys = new Array(); | |
| 249 | + for (var i = 0;i < storage.length;i++) { | |
| 250 | + var key=storage.key(i); | |
| 251 | + if (key.indexOf('_route') > 0) { | |
| 252 | + keys.push(key); | |
| 253 | + } | |
| 254 | + } | |
| 255 | + for (var i = 0;i < keys.length;i++) { | |
| 256 | + storage.removeItem(keys[i]); | |
| 257 | + } | |
| 258 | + }); | |
| 259 | + | |
| 260 | + $('.pattern_type_label').on('click', function () { | |
| 261 | + var checked = $('input',this)[0].checked; | |
| 262 | + changePattern(checked); | |
| 263 | + }); | |
| 264 | + | |
| 265 | + function changePattern(status) { | |
| 266 | + var btn = $('#go_to_real_system_btn'), | |
| 267 | + def='uk-button-default', | |
| 268 | + prim='uk-button-primary'; | |
| 269 | + if(status){ | |
| 270 | + btn.removeClass(def).addClass(prim); | |
| 271 | + } | |
| 272 | + else{ | |
| 273 | + btn.removeClass(prim).addClass(def); | |
| 274 | + } | |
| 275 | + } | |
| 276 | + | |
| 277 | + /** | |
| 278 | + * 查询所有线路 | |
| 279 | + */ | |
| 280 | + $.get('/line/all', {remove_ne: 1}, function (list) { | |
| 281 | + ep.emit('all_lines', list); | |
| 282 | + }); | |
| 283 | + | |
| 284 | + /**用户分配的线路*/ | |
| 285 | + $.get('/realControAuthority/findByCurrentUser', function (t) { | |
| 286 | + ep.emit('user_author', t); | |
| 287 | + }); | |
| 288 | + | |
| 289 | + /** 查询公司编码 */ | |
| 290 | + $.get('/business/all', function (rs) { | |
| 291 | + ep.emit('company_data', rs); | |
| 292 | + }); | |
| 293 | + | |
| 294 | + //点击选择线路 | |
| 295 | + $('.line_list_all_wrap').on('click', 'span.item', function () { | |
| 296 | + var lineCode = $(this).data('id'), | |
| 297 | + name = $(this).text(); | |
| 298 | + if ($(this).hasClass('active')) { | |
| 299 | + $(this).removeClass('active'); | |
| 300 | + | |
| 301 | + $('.item[data-id='+lineCode+']','.checked_list').remove(); | |
| 302 | + } | |
| 303 | + else { | |
| 304 | + $(this).addClass('active'); | |
| 305 | + var span = $('<span data-id="'+lineCode+'" class="item uk-card uk-card-default uk-animation-slide-bottom '+($(this).hasClass('destroy')?"destroy":"")+'">'+name+'</span>') | |
| 306 | + .one('webkitAnimationEnd', function () { | |
| 307 | + $(this).removeClass('uk-animation-slide-bottom'); | |
| 308 | + }); | |
| 309 | + $('.checked_list').append(span); | |
| 310 | + } | |
| 311 | + }); | |
| 312 | + | |
| 313 | + $('.checked_list').on('click', '.item', function () { | |
| 314 | + var lineCode = $(this).data('id'); | |
| 315 | + $('.item[data-id='+lineCode+']','.line_list_all_wrap').removeClass('active'); | |
| 316 | + $(this).remove(); | |
| 317 | + }); | |
| 318 | + | |
| 319 | + /** | |
| 320 | + * 搜索线路 | |
| 321 | + */ | |
| 322 | + function search_line_list() { | |
| 323 | + var comp = $('select.company_search_select').val(), | |
| 324 | + fgs = $('select.fgs_search_select').val(), | |
| 325 | + gsbm = comp + '_' + fgs; | |
| 326 | + | |
| 327 | + if(gsbm=='_') | |
| 328 | + gsbm=null; | |
| 329 | + var allDoms = $('.line_list_all_wrap span.item'); | |
| 330 | + //线路 | |
| 331 | + var lineCodeArray = []; | |
| 332 | + var t = $('.line_search_input').val(); | |
| 333 | + if(t){ | |
| 334 | + t = t.toUpperCase(); | |
| 335 | + for(var m in allMapps){ | |
| 336 | + if(m.indexOf(t)!=-1) | |
| 337 | + lineCodeArray.push(allMapps[m]); | |
| 338 | + } | |
| 339 | + } | |
| 340 | + | |
| 341 | + //搜索 | |
| 342 | + var show_elems = []; | |
| 343 | + for (var i=0, dom; dom = allDoms[i++];) { | |
| 344 | + if(gsbm && $(dom).data('gsbm').indexOf(gsbm)==-1) | |
| 345 | + continue; | |
| 346 | + | |
| 347 | + if(t && lineCodeArray.indexOf($(dom).data('id')+'') == -1) | |
| 348 | + continue; | |
| 349 | + | |
| 350 | + show_elems.push($(dom)); | |
| 351 | + } | |
| 352 | + | |
| 353 | + //show | |
| 354 | + allDoms.hide(); | |
| 355 | + for(var j=0,$dom;$dom=show_elems[j++];) | |
| 356 | + $dom.show(); | |
| 357 | + } | |
| 358 | + | |
| 359 | + function createLineSearchMapps(list) { | |
| 360 | + allMapps = {}; | |
| 361 | + var name, code; | |
| 362 | + for (var i = 0, line; line = list[i++];) { | |
| 363 | + name = line.name; | |
| 364 | + code = line.lineCode; | |
| 365 | + //拼音映射 | |
| 366 | + allMapps[pinyin.getCamelChars(name)] = code; | |
| 367 | + allMapps[pinyin.getFullChars(name).toUpperCase()] = code; | |
| 368 | + //中文映射 | |
| 369 | + allMapps[name] = code; | |
| 370 | + } | |
| 371 | + } | |
| 372 | + | |
| 373 | + | |
| 374 | + //构建公司级联下拉框 | |
| 375 | + function initCompanySelect() { | |
| 376 | + var opts = '<option value="">公司搜索</option>'; | |
| 377 | + for (var k in companyData) { | |
| 378 | + opts += '<option value="' + k + '">' + companyData[k].name + '</option>'; | |
| 379 | + } | |
| 380 | + $('select.company_search_select').html(opts); | |
| 381 | + } | |
| 382 | + | |
| 383 | + //公司切换 | |
| 384 | + $('select.company_search_select').on('change', function () { | |
| 385 | + var opts = '<option value="">分公司搜索</option>', | |
| 386 | + t = $(this).val(), | |
| 387 | + fgsSelect = $('select.fgs_search_select'); | |
| 388 | + if (!t) { | |
| 389 | + fgsSelect.html(opts).attr('disabled', 'disabled'); | |
| 390 | + } | |
| 391 | + else{ | |
| 392 | + var array = companyData[t].childs; | |
| 393 | + for (var i = 0, fgs; fgs = array[i++];) { | |
| 394 | + opts += '<option value="' + fgs.businessCode + '">' + fgs.businessName + '</option>'; | |
| 395 | + } | |
| 396 | + | |
| 397 | + fgsSelect.html(opts).removeAttr('disabled'); | |
| 398 | + } | |
| 399 | + search_line_list(); | |
| 400 | + }); | |
| 401 | + | |
| 402 | + //分公司切换 | |
| 403 | + $('select.fgs_search_select').on('change', search_line_list); | |
| 404 | + | |
| 405 | + //线路文本框输入 | |
| 406 | + var flag; | |
| 407 | + $('.line_search_input').on('input', function () { | |
| 408 | + if(flag) | |
| 409 | + return; | |
| 410 | + flag = true; | |
| 411 | + setTimeout(function () { | |
| 412 | + //延迟 | |
| 413 | + search_line_list(); | |
| 414 | + flag = false; | |
| 415 | + }, 200); | |
| 416 | + }); | |
| 417 | + | |
| 418 | + function convert_buss_data(rs) { | |
| 419 | + var baseCode; | |
| 420 | + //找到跟节点 | |
| 421 | + $.each(rs, function () { | |
| 422 | + if (this.upCode == 0) { | |
| 423 | + baseCode = this.businessCode; | |
| 424 | + return false; | |
| 425 | + } | |
| 426 | + }); | |
| 427 | + if (!baseCode)return; | |
| 428 | + //提取二级节点 | |
| 429 | + var secondMap = {}; | |
| 430 | + $.each(rs, function () { | |
| 431 | + if (this.upCode == baseCode) | |
| 432 | + secondMap[this.businessCode] = {name: this.businessName, childs: []}; | |
| 433 | + }); | |
| 434 | + //分公司节点 | |
| 435 | + $.each(rs, function () { | |
| 436 | + if (secondMap[this.upCode]) | |
| 437 | + secondMap[this.upCode].childs.push(this); | |
| 438 | + }); | |
| 439 | + //排序 | |
| 440 | + for (var sid in secondMap) | |
| 441 | + secondMap[sid].childs.sort(naturalSort); | |
| 442 | + return secondMap; | |
| 443 | + } | |
| 444 | + | |
| 445 | + var naturalSort = function (a, b) { | |
| 446 | + return a.businessName.localeCompare(b.businessName); | |
| 447 | + }; | |
| 448 | + | |
| 449 | + /** | |
| 450 | + * 根据用户线路权限过滤公司编码数据 | |
| 451 | + */ | |
| 452 | + var filter_company_data = (function () { | |
| 453 | + var filter = function (lineArray, data) { | |
| 454 | + companyData = {}; | |
| 455 | + var com, fCom; | |
| 456 | + $.each(lineArray, function () { | |
| 457 | + com = this.company; | |
| 458 | + fCom = this.brancheCompany; | |
| 459 | + if (!companyData[com]) { | |
| 460 | + companyData[com] = {name: data[com].name, childs: []}; | |
| 461 | + } | |
| 462 | + | |
| 463 | + if (!get(fCom, companyData[com].childs)) { | |
| 464 | + companyData[com].childs.push(get(fCom, data[com].childs)); | |
| 465 | + } | |
| 466 | + }); | |
| 467 | + }; | |
| 468 | + | |
| 469 | + function get(fgsCode, fgsArray) { | |
| 470 | + for (var i = 0, fgs; fgs = fgsArray[i++];) { | |
| 471 | + if (fgs.businessCode == fgsCode) | |
| 472 | + return fgs; | |
| 473 | + } | |
| 474 | + return null; | |
| 475 | + } | |
| 476 | + | |
| 477 | + return { | |
| 478 | + filter: filter | |
| 479 | + } | |
| 480 | + })(); | |
| 481 | + var storage = window.localStorage; | |
| 482 | + $('#go_to_real_system_btn').on('click', go_to_xd_sys); | |
| 483 | + /** | |
| 484 | + * 进入线调 | |
| 485 | + */ | |
| 486 | + function go_to_xd_sys() { | |
| 487 | + var items = $('.checked_list .item'); | |
| 488 | + if(items.length==0) | |
| 489 | + return UIkit.notification({message: '你还没有选择线路!', pos: 'bottom-center', status: 'danger'}); | |
| 490 | + $(this).attr('disabled', 'disabled'); | |
| 491 | + | |
| 492 | + show_btn_load(this, '更新缓存数据...'); | |
| 493 | + (function () { | |
| 494 | + var ls_line_data = []; | |
| 495 | + $.each(items, function () { | |
| 496 | + ls_line_data.push(codeMapps[$(this).data('id')]); | |
| 497 | + }); | |
| 498 | + //监控模式还是主调模式 | |
| 499 | + storage.setItem('operationMode', $('.pattern_type_label>input')[0].checked?1:0); | |
| 500 | + | |
| 501 | + //进入线调 | |
| 502 | + var eq = EventProxy.create('cache_line_level', 'cache_mv_route', 'cache_route', 'check_line_config', function (lineLevel) { | |
| 503 | + debugger; | |
| 504 | + var newWinOpen = $('input','.new_window_open_label')[0].checked; | |
| 505 | + if(!newWinOpen) | |
| 506 | + top.window.location.href = "/real_control/v2"; | |
| 507 | + else{ | |
| 508 | + window.open("/real_control/v2"); | |
| 509 | + $('#go_to_real_system_btn').html('已经尝试打开新窗口,如看不到,可能被浏览器阻止了'); | |
| 510 | + } | |
| 511 | + | |
| 512 | + // 将线路级别赋值 | |
| 513 | + $.each(ls_line_data, function () { | |
| 514 | + this.lineLevel = lineLevel[this.lineCode]; | |
| 515 | + }); | |
| 516 | + // 将线路基础信息写入localStorage | |
| 517 | + storage.setItem('lineControlItems', JSON.stringify(ls_line_data)); | |
| 518 | + }); | |
| 519 | + | |
| 520 | + //拼接线路编码 | |
| 521 | + var idx=''; | |
| 522 | + $.each(ls_line_data, function () { | |
| 523 | + idx+=(this.lineCode + ','); | |
| 524 | + }); | |
| 525 | + //缓存路由 | |
| 526 | + idx=idx.substr(0, idx.length - 1); | |
| 527 | + | |
| 528 | + $.get('/realSchedule/lineLevel', {idx: idx}, function (rs) { | |
| 529 | + if (rs) { | |
| 530 | + eq.emit('cache_line_level', rs); | |
| 531 | + } | |
| 532 | + }); | |
| 533 | + | |
| 534 | + $.get('/realMap/findRouteAndVersionByLine', {idx: idx}, function (rs) { | |
| 535 | + if (rs) { | |
| 536 | + for(var lineCode in rs) | |
| 537 | + storage.setItem(lineCode + '_route', JSON.stringify(rs[lineCode])); | |
| 538 | + eq.emit('cache_mv_route'); | |
| 539 | + } | |
| 540 | + }); | |
| 541 | + | |
| 542 | + $.get('/realMap/findRouteByLine', {idx: idx}, function (rs) { | |
| 543 | + if (rs) { | |
| 544 | + for(var lineCode in rs) | |
| 545 | + storage.setItem(lineCode + '_route', JSON.stringify(rs[lineCode])); | |
| 546 | + | |
| 547 | + eq.emit('cache_route'); | |
| 548 | + } | |
| 549 | + }); | |
| 550 | + | |
| 551 | + | |
| 552 | + //检查线路配置 | |
| 553 | + checkLineConfig(ls_line_data, function (rs) { | |
| 554 | + if (rs.status == 0) | |
| 555 | + eq.emit('check_line_config'); | |
| 556 | + else if (rs.status == 1){ | |
| 557 | + initLineConfig(rs.not, function () { | |
| 558 | + eq.emit('check_line_config'); | |
| 559 | + }); | |
| 560 | + } | |
| 561 | + }) | |
| 562 | + })(); | |
| 563 | + } | |
| 564 | + | |
| 565 | + function checkLineConfig(lsData, cb) { | |
| 566 | + var lineCodeArray = []; | |
| 567 | + $.each(lsData, function () { | |
| 568 | + lineCodeArray.push(this.lineCode); | |
| 569 | + }); | |
| 570 | + | |
| 571 | + $.ajax({ | |
| 572 | + url: '/lineConfig/check', | |
| 573 | + traditional: true, | |
| 574 | + data: {codeArray: lineCodeArray}, | |
| 575 | + method: 'POST', | |
| 576 | + success: cb | |
| 577 | + }); | |
| 578 | + } | |
| 579 | + | |
| 580 | + function initLineConfig(arr, cb) { | |
| 581 | + var i = 0; | |
| 582 | + (function () { | |
| 583 | + if (i >= arr.length) { | |
| 584 | + cb && cb(); | |
| 585 | + return; | |
| 586 | + } | |
| 587 | + var f = arguments.callee | |
| 588 | + , lineCode = arr[i]; | |
| 589 | + | |
| 590 | + //showLoad('初始化' + lineIds[lineCode] + '配置信息...'); | |
| 591 | + $.post('/lineConfig/init/' + lineCode, function (rs) { | |
| 592 | + if (rs == 1) { | |
| 593 | + i++; | |
| 594 | + f(); | |
| 595 | + } | |
| 596 | + }); | |
| 597 | + })(); | |
| 598 | + } | |
| 599 | + | |
| 600 | + /** | |
| 601 | + * 按钮转菊花 | |
| 602 | + */ | |
| 603 | + function show_btn_load(btn, msg) { | |
| 604 | + $(btn).html('<div uk-spinner></div> ' + msg); | |
| 605 | + } | |
| 606 | +</script> | |
| 607 | +</body> | |
| 597 | 608 | </html> |
| 598 | 609 | \ No newline at end of file | ... | ... |