ChargerTicketServiceImpl.java
14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/**
*
*/
package com.zteits.pay.biz.ticket;
import cn.hutool.core.collection.CollectionUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.clouds.common.utils.ListCopyUtil;
import com.clouds.common.utils.SequenceUtil;
import com.zteits.clouds.api.apibase.bean.BizResult;
import com.zteits.clouds.api.apibase.constants.DataStatusEnum;
import com.zteits.clouds.api.apibase.constants.ErrorType;
import com.zteits.clouds.api.apibase.constants.SequenceTypeEnum;
import com.zteits.clouds.api.apibase.constants.WidgetTypeEnum;
import com.zteits.clouds.api.apibase.exception.BizException;
import com.zteits.clouds.api.dto.pay.ChargerTicketTemplateDTO;
import com.zteits.clouds.api.dto.pay.param.ChargerTicketConfigRequest;
import com.zteits.clouds.api.dto.pay.param.ChargerTicketRequest;
import com.zteits.clouds.api.dto.pay.param.SaveChargerTicketConfigsRequest;
import com.zteits.clouds.api.service.pay.ticket.ChargerTicketService;
import com.zteits.pay.dao.ChargerTicketConfigDao;
import com.zteits.pay.dao.ChargerTicketTemplateDao;
import com.zteits.pay.domain.ChargerTicketBaseConfig;
import com.zteits.pay.domain.ChargerTicketConfig;
import com.zteits.pay.domain.ChargerTicketTemplate;
import com.zteits.pay.domain.ChargerTicketTemplateExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
/**
* Copyright: Copyright (c) 2020 zteits
*
* @ClassName: ChargerTicketServiceImpl
* @Description:
* @version: v1.0.0
* @author: wangfei
* @date: 2020-02-20 上午11:45:31
* Modification History:
* Date Author Version Description
*---------------------------------------------------------*
* 2020-02-20 wangfei v1.0.0 创建
*/
@Service("com.zteits.pay.biz.ticket.ChargerTicketServiceImpl")
public class ChargerTicketServiceImpl implements ChargerTicketService {
private static final Logger logger = LoggerFactory.getLogger(ChargerTicketServiceImpl.class);
@Autowired
private ChargerTicketConfigDao chargerTicketConfigDao;
@Autowired
private ChargerTicketTemplateDao chargerTicketTemplateDao;
private final static Integer ticketWide = 18;
/**不需要切割的LABEL_INPUT控件*/
private final static List<String> noCutNames = Arrays.asList("orderId","parkInTimeStr","parkOutTimeStr","customerPhone","servicePhone","line");
/**需要分隔上下两段文字的图片控件*/
private final static List<String> picNames = Arrays.asList("appQrCode","inParkQrCode","outParkQrCode");
@Override
@Transactional
public BizResult<String> saveChargerTicketConfigsAndTemplate(SaveChargerTicketConfigsRequest request) {
String ticketNo = SequenceUtil.getNextId(SequenceTypeEnum.TICKET_NO);
request.setTicketNo(ticketNo);
logger.info("(预览)保存小票配置信息入参{}", JSONObject.toJSON(request));
if(request.getOrgId() == null){
throw new BizException(ErrorType.PARAMM_NULL,"组织编码为空!");
}
if(StringUtils.isBlank(request.getTicketTitle())){
throw new BizException(ErrorType.PARAMM_NULL,"小票标题为空!");
}
if(CollectionUtil.isEmpty(request.getConfigRequests())){
throw new BizException(ErrorType.PARAMM_NULL,"配置信息为空!");
}
if(request.getTicketCategory() == null){
throw new BizException(ErrorType.PARAMM_NULL,"小票类别为空!");
}
if(request.getTicketType() == null){
throw new BizException(ErrorType.PARAMM_NULL,"小票类型为空!");
}
//查询基础数据
List<String> wigetNos = request.getConfigRequests().stream().map(item -> item.getWidgetNo()).collect(Collectors.toList());
List<ChargerTicketBaseConfig> bases = chargerTicketConfigDao.queryChargerTicketBaseConfigsByCondition(wigetNos, request.getTicketType(),
request.getTicketCategory(), request.getOrgId());
if(CollectionUtils.isEmpty(bases)){
throw new BizException(ErrorType.BIZ_ERROR,"未查询到相关基础配置信息!");
}
//填充小票配置信息
this.fillTiketConfigs(request, ticketNo, bases);
List<ChargerTicketConfig> configs = new ArrayList<>();
ListCopyUtil.listCopyProperties(request.getConfigRequests(),configs,ChargerTicketConfig.class);
//生成模板信息
String ticketTemplateText = this.generateTemplateText(configs);
logger.info("模板生成:{},{}",ticketNo,ticketTemplateText);
int ins = chargerTicketConfigDao.bathSaveChargerTicketConfigs(configs);
if(ins < 1){
throw new BizException(ErrorType.BIZ_ERROR,"报存配置信息错误!");
}
//保存模板信息
ChargerTicketTemplate template = new ChargerTicketTemplate();
BeanUtils.copyProperties(request,template);
template.setTicketNo(ticketNo);
template.setTicketType(request.getTicketType());
template.setTicketCategory(request.getTicketCategory());
template.setCreateEmpId(request.getCreateEmpId());
template.setCreateEmpName(request.getCreateEmpName());
template.setOrgId(request.getOrgId());
template.setRemark(request.getRemark());
template.setTicketTitle(request.getTicketTitle());
template.setTicketTemplateText(ticketTemplateText);
template.setTicketState(DataStatusEnum.DATA_STATUS_NO_VALID.value());
logger.info("保存小票模板信息:{}",JSONObject.toJSON(template));
int ins2 = chargerTicketTemplateDao.saveChargerTicketTemplate(template);
if(ins2 < 1){
throw new BizException(ErrorType.BIZ_ERROR,"保存模板信息错误!");
}
return new BizResult<>(ticketNo);
}
/**
* 填充小票配置信息
* @param request
* @param ticketNo
* @param bases
*/
private void fillTiketConfigs(SaveChargerTicketConfigsRequest request, String ticketNo, List<ChargerTicketBaseConfig> bases) {
for (ChargerTicketConfigRequest configRequest : request.getConfigRequests()) {
configRequest.setTicketNo(ticketNo);
configRequest.setTicketType(request.getTicketType());
configRequest.setTicketCategory(request.getTicketCategory());
configRequest.setCreateEmpId(request.getCreateEmpId());
configRequest.setCreateEmpName(request.getCreateEmpName());
configRequest.setOrgId(request.getOrgId());
configRequest.setRemark(request.getRemark());
for(ChargerTicketBaseConfig b : bases) {
if((configRequest.getWidgetNo()).equals(b.getWidgetNo())){
if(StringUtils.isEmpty(configRequest.getWidgetName())){
configRequest.setWidgetName(b.getWidgetName());
}
if(null ==(configRequest.getWidgetText())){
configRequest.setWidgetText(b.getWidgetText());
}
if(StringUtils.isEmpty(configRequest.getWidgetType())){
configRequest.setWidgetType(b.getWidgetType());
}
if(StringUtils.isEmpty(configRequest.getWidgetValue())){
configRequest.setWidgetValue(b.getWidgetValue());
}
}
}
}
}
@Override
public BizResult<ChargerTicketTemplateDTO> queryChargerTicketTemplateByCondition(ChargerTicketRequest req) {
logger.info("根据小票模板查询小票预览信息{}",JSONObject.toJSON(req));
if(req.getOrgId() == null){
throw new BizException(ErrorType.PARAMM_NULL,"组织编码为空!");
}
ChargerTicketTemplate t = chargerTicketTemplateDao.queryChargerTicketTemplateByCondition(req);
if(t == null){
throw new BizException(ErrorType.BIZ_ERROR,"未查询到小票模板信息,请联系管理员!");
}
ChargerTicketTemplateDTO dto = new ChargerTicketTemplateDTO();
BeanUtils.copyProperties(t,dto);
return new BizResult<>(dto);
}
@Override
public BizResult<Boolean> openChargerTicketTemplate(ChargerTicketRequest request) {
if(request.getOrgId() == null){
throw new BizException(ErrorType.PARAMM_NULL,"组织编码为空!");
}
if(StringUtils.isBlank(request.getTicketNo())){
throw new BizException(ErrorType.PARAMM_NULL,"小票编码为空!");
}
//将该类型其他的小票置为预览中
ChargerTicketTemplate template = chargerTicketTemplateDao.queryChargerTicketTemplateByCondition(request);
if(template == null){
throw new BizException(ErrorType.BIZ_ERROR,"未查询到当前小票模板信息");
}
ChargerTicketTemplate record = new ChargerTicketTemplate();
record.setTicketState(DataStatusEnum.DATA_STATUS_FROZEN.value());
record.setModifyTime(new Date());
record.setModifyEmpId(request.getModifyEmpId());
record.setModifyEmpName(request.getModifyEmpName());
record.setRemark("有新模板启用,锁定旧的");
ChargerTicketTemplateExample example = new ChargerTicketTemplateExample();
example.createCriteria().andDataStateEqualTo(DataStatusEnum.DATA_STATUS_VALID.value())
.andOrgIdEqualTo(request.getOrgId()).andTicketCategoryEqualTo(template.getTicketCategory())
.andTicketTypeEqualTo(template.getTicketType()).andTicketStateEqualTo(DataStatusEnum.DATA_STATUS_VALID.value());
chargerTicketTemplateDao.updateChargerTicketTemplateByExample(example,record);
//启用当前模板
request.setTicketState(DataStatusEnum.DATA_STATUS_VALID.value());
logger.info("启用小票模板{}",JSONObject.toJSON(request));
request.setRemark("当前记录为最新启用模板");
Boolean result = chargerTicketTemplateDao.updateChargerTicketTemplateByNo(request);
return new BizResult<>(result);
}
/**
* 根据小票配置信息生成模板数据
* @param configs
* @return
*/
private static String generateTemplateText(List<ChargerTicketConfig> configs){
logger.info("小票配置信息,生成模板:{}",JSONObject.toJSON(configs));
int i=1;
for (ChargerTicketConfig config : configs) {
if (config.getWidgetName() == null) {
config.setWidgetName("");
}
if (config.getWidgetText() == null) {
config.setWidgetText("");
}
if (config.getWidgetValue() == null) {
config.setWidgetValue("");
}
if (config.getEnterFlag() == null) {
config.setEnterFlag(1);
}
config.setOrderNum(i++);
}
String text = "";
for (ChargerTicketConfig config : configs) {
String value = "";
if(
((WidgetTypeEnum.INPUT.value()).equals(config.getWidgetType())) ||//INPUT
((WidgetTypeEnum.TEXT.value()).equals(config.getWidgetType())) //TEXT
){
if(noCutNames.contains(config.getWidgetName())){//订单编号/电话号和时间不切割
value = (config.getWidgetText());
}else {
// value = cutTextValueByLength(config.getWidgetText(), ticketWide);
value = (config.getWidgetText()) ;
}
}else if(((WidgetTypeEnum.LABEL_INPUT.value()).equals(config.getWidgetType()))){//LABEL_INPUT
if(noCutNames.contains(config.getWidgetName())){//订单编号/电话号和时间不切割
value = (config.getWidgetText() + config.getWidgetValue());
}else{
// value = cutTextValueByLength(config.getWidgetText() + config.getWidgetValue(),ticketWide);//切割
value = (config.getWidgetText() + config.getWidgetValue()) +" ";//切割
}
}
else if(((WidgetTypeEnum.PICTURE.value()).equals(config.getWidgetType()))){//PICTURE
value = config.getWidgetText();
if(picNames.contains(config.getWidgetName())){//4个&来表示截取上下两段文字,中间放二维码
value = "&&&&" + value;
}
}
//换行
if(config.getEnterFlag() == 1){
value = "\n" + value;
}
//空格
if(config.getEnterFlag() == 0){
value = "" + value;
}
text += value;
}
//如果换行开头,去掉这个换行
if(text.startsWith("\n")){
text = text.substring(1,text.length());
}
text = text + "\n\n\n\n";//末尾4个换行
return text;
}
/**
* 根据长度以换行切割字符串
* @param value
* @param length
* @return
*/
public static String cutTextValueByLength(String value,Integer length){
String result = "";
Integer len = value.length();
if(len <= length){
return value;
}
Integer i = len / length;
StringBuffer str = new StringBuffer(value);
for ( ;i > 0;i--) {
str.insert(length * i,"\n");
}
result += str;
if(result.endsWith("\n")){
result = result.substring(0,result.length()-1);
}
return result;
}
public static void main(String[] args) {
//生成模板信息
List<ChargerTicketConfig> configs = new ArrayList<>();
ChargerTicketConfig a = new ChargerTicketConfig();
a.setEnterFlag(0);
a.setWidgetText("订单编号:");
a.setWidgetName("orderId");
a.setWidgetValue("q2394y284728入4728472");
a.setWidgetType("LABEL_INPUT");
ChargerTicketConfig b = new ChargerTicketConfig();
b.setEnterFlag(1);
b.setWidgetText("车场名称:");
b.setWidgetValue("单计费还得加");
b.setWidgetType("LABEL_INPUT");
configs.add(a);
configs.add(b);
String ticketTemplateText = generateTemplateText(configs);
System.out.println(StringUtils.isEmpty(""));
}
}