java110
2023-05-16 d516e38fd44e186f393bbb05c4e7ca34b323b609
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
327
328
329
330
331
332
333
package com.java110.job.printer.manufactor;
 
import com.java110.core.client.RestTemplate;
import com.java110.core.log.LoggerFactory;
import com.java110.dto.account.AccountReceiptDto;
import com.java110.dto.fee.FeeDetailDto;
import com.java110.dto.fee.FeeDto;
import com.java110.dto.fee.FeeReceiptDetailDto;
import com.java110.dto.fee.FeeReceiptDto;
import com.java110.dto.machine.MachinePrinterDto;
import com.java110.dto.repair.RepairDto;
import com.java110.dto.repair.RepairUserDto;
import com.java110.intf.community.IRepairInnerServiceSMO;
import com.java110.intf.community.IRepairUserInnerServiceSMO;
import com.java110.intf.fee.*;
import com.java110.intf.store.ISmallWeChatInnerServiceSMO;
import com.java110.intf.user.IUserV1InnerServiceSMO;
import com.java110.job.printer.IPrinter;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.DateUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
 
/**
 * 飞蛾厂家云打印机
 * <p>
 * http://www.feieyun.com/open/apidoc-cn.html
 */
 
@Service("feieManufactor")
public class FeieManufactor implements IPrinter {
 
    private Logger logger = LoggerFactory.getLogger(FeieManufactor.class);
 
    @Autowired
    private RestTemplate formRestTemplate;
 
    /**
     * 请求地址
     * http://api.feieyun.cn/Api/Open/
     */
    public static final String REQUEST_URL = "http://api.feieyun.cn/Api/Open/";
 
    @Autowired
    private IFeeInnerServiceSMO feeInnerServiceSMOImpl;
 
    @Autowired
    private IFeeDetailInnerServiceSMO feeDetailInnerServiceSMOImpl;
 
    @Autowired
    private IFeeReceiptDetailInnerServiceSMO feeReceiptDetailInnerServiceSMOImpl;
    @Autowired
    private IFeeReceiptInnerServiceSMO feeReceiptInnerServiceSMOImpl;
 
    @Autowired
    private ISmallWeChatInnerServiceSMO smallWeChatInnerServiceSMOImpl;
 
    @Autowired
    private IRepairUserInnerServiceSMO repairUserInnerServiceSMO;
 
    @Autowired
    private IRepairInnerServiceSMO repairInnerServiceSMO;
 
    @Autowired
    private IUserV1InnerServiceSMO userV1InnerServiceSMOImpl;
 
    @Autowired
    private IAccountReceiptV1InnerServiceSMO accountReceiptV1InnerServiceSMOImpl;
 
    /**
     * **************************
     * 单号:832023020800440026
     * 房号:1-1-1001
     * 业主:李四1
     * 时间:2023-02-08 14:30:17
     * **************************
     * 收费项目:高层物业费
     * 收费范围:2024-01-01 至2024-12-31
     * 单价/固定费:1.3/0.0
     * 面积/用量:90.00
     * 金额: 1404.00
     * 备注:123
     * **************************
     * 总计:1404
     * **************************
     *
     * @param detailIds
     * @param communityId
     * @param quantity
     * @return
     */
    @Override
    public ResultVo printPayFeeDetail(String[] detailIds, String communityId, int quantity, MachinePrinterDto machinePrinterDto, String staffName) {
 
        String printStr = "";
        FeeDetailDto feeDetailDto = new FeeDetailDto();
        feeDetailDto.setDetailId(detailIds[0]);
        feeDetailDto.setCommunityId(communityId);
        List<FeeDetailDto> feeDetailDtos = feeDetailInnerServiceSMOImpl.queryFeeDetails(feeDetailDto);
        Assert.listOnlyOne(feeDetailDtos, "交费明细不存在");
 
        FeeReceiptDetailDto feeReceiptDetailDto = new FeeReceiptDetailDto();
        feeReceiptDetailDto.setDetailIds(detailIds);
        feeReceiptDetailDto.setCommunityId(communityId);
        List<FeeReceiptDetailDto> feeReceiptDetailDtos = feeReceiptDetailInnerServiceSMOImpl.queryFeeReceiptDetails(feeReceiptDetailDto);
        if (feeReceiptDetailDtos == null || feeReceiptDetailDtos.size() < 1) {
            throw new CmdException("未生成收据");
        }
 
        FeeReceiptDto feeReceiptDto = new FeeReceiptDto();
        feeReceiptDto.setReceiptId(feeReceiptDetailDtos.get(0).getReceiptId());
        feeReceiptDto.setCommunityId(communityId);
        List<FeeReceiptDto> feeReceiptDtos = feeReceiptInnerServiceSMOImpl.queryFeeReceipts(feeReceiptDto);
        Assert.listOnlyOne(feeReceiptDtos, "未生成收据");
 
        /*************************************头部******************************************/
        List<FeieLine> feieLines = new ArrayList<>();
        feieLines.add(new FeieLine("单号", feeReceiptDetailDtos.get(0).getPayOrderId()));
        feieLines.add(new FeieLine("房号", feeReceiptDtos.get(0).getObjName()));
        feieLines.add(new FeieLine("业主", feeReceiptDtos.get(0).getPayObjName()));
        feieLines.add(new FeieLine("时间", DateUtil.getFormatTimeString(DateUtil.getCurrentDate(), DateUtil.DATE_FORMATE_STRING_A)));
        printStr = FeieGetPayFeeDetail.getPrintPayFeeDetailHeaderContent(feieLines);
        /*************************************头部******************************************/
 
        feieLines = new ArrayList<>();
        Date startTime = null;
        Date endTime = null;
        Calendar endTimeCal = null;
        BigDecimal totalDecimal = new BigDecimal(0);
        for (FeeReceiptDetailDto tmpFeeReceiptDetailDto : feeReceiptDetailDtos) {
            feieLines.add(new FeieLine("收费项目", tmpFeeReceiptDetailDto.getFeeName()));
            startTime = DateUtil.getDateFromStringB(tmpFeeReceiptDetailDto.getStartTime());
            endTime = DateUtil.getDateFromStringB(tmpFeeReceiptDetailDto.getEndTime());
            //周期性和间接性
            if (!FeeDto.FEE_FLAG_ONCE.equals(tmpFeeReceiptDetailDto.getFeeFlag())) {
                endTimeCal = Calendar.getInstance();
                endTimeCal.setTime(endTime);
                endTimeCal.add(Calendar.DAY_OF_MONTH, -1);
                endTime = endTimeCal.getTime();
            }
            feieLines.add(new FeieLine("收费范围", DateUtil.getFormatTimeString(startTime, DateUtil.DATE_FORMATE_STRING_B) + "至" + DateUtil.getFormatTimeString(endTime, DateUtil.DATE_FORMATE_STRING_B)));
            feieLines.add(new FeieLine("单价/固定费", tmpFeeReceiptDetailDto.getSquarePrice()));
            feieLines.add(new FeieLine("面积/用量", tmpFeeReceiptDetailDto.getArea()));
            feieLines.add(new FeieLine("支付方式", tmpFeeReceiptDetailDto.getPrimeRate()));
            if (!StringUtil.isEmpty(tmpFeeReceiptDetailDto.getPreDegrees())) {
                feieLines.add(new FeieLine("表读数", tmpFeeReceiptDetailDto.getPreDegrees() + "至" + tmpFeeReceiptDetailDto.getCurDegrees()));
            }
            feieLines.add(new FeieLine("金额", tmpFeeReceiptDetailDto.getAmount()));
            feieLines.add(new FeieLine("备注", tmpFeeReceiptDetailDto.getRemark()));
            printStr += FeieGetPayFeeDetail.getPrintPayFeeDetailBodyContent(feieLines);
            totalDecimal = totalDecimal.add(new BigDecimal(Double.parseDouble(tmpFeeReceiptDetailDto.getAmount())));
        }
        printStr += FeieGetPayFeeDetail.getPrintPayFeeDetailFloorContent(communityId, totalDecimal.doubleValue(), staffName, smallWeChatInnerServiceSMOImpl);
 
        doPrint(quantity, machinePrinterDto, printStr);
 
        return new ResultVo(ResultVo.CODE_OK, "成功");
    }
 
 
    @Override
    public ResultVo printRepair(String ruId, String communityId, int quantity, MachinePrinterDto machinePrinterDto) {
 
        RepairUserDto repairUserDto = new RepairUserDto();
        repairUserDto.setRuId(ruId);
        repairUserDto.setCommunityId(communityId);
        List<RepairUserDto> repairUserDtos = repairUserInnerServiceSMO.queryRepairUsers(repairUserDto);
 
        Assert.listOnlyOne(repairUserDtos, "报修单不存在");
 
        if (RepairUserDto.STATE_SUBMIT.equals(repairUserDtos.get(0).getState())) {
            return new ResultVo(ResultVo.CODE_OK, "成功");
        }
 
        String repairId = repairUserDtos.get(0).getRepairId();
        RepairDto repairDto = new RepairDto();
        repairDto.setRepairId(repairId);
        repairDto.setCommunityId(communityId);
        List<RepairDto> repairDtos = repairInnerServiceSMO.queryRepairs(repairDto);
        Assert.listOnlyOne(repairDtos, "报修单不存在");
 
 
        String staffName = repairUserDtos.get(0).getStaffName();
 
        String printStr = "";
        /*************************************内容******************************************/
        List<FeieLine> feieLines = new ArrayList<>();
        feieLines.add(new FeieLine("标题", repairDtos.get(0).getRepairName()));
        feieLines.add(new FeieLine("联系人", repairDtos.get(0).getRepairName()));
        feieLines.add(new FeieLine("电话", repairDtos.get(0).getTel()));
        feieLines.add(new FeieLine("时间", repairDtos.get(0).getAppointmentTime()));
        feieLines.add(new FeieLine("位置", repairDtos.get(0).getRepairObjName()));
        feieLines.add(new FeieLine("维修师傅", staffName));
        feieLines.add(new FeieLine("单号", repairDtos.get(0).getRepairId()));
        feieLines.add(new FeieLine("内容", repairDtos.get(0).getContext()));
        printStr = FeieGetRepair.getPrintRepairHeaderContent(feieLines);
        /*************************************内容******************************************/
 
        doPrint(quantity, machinePrinterDto, printStr);
 
        return new ResultVo(ResultVo.CODE_OK, "成功");
    }
 
    @Override
    public ResultVo printAccountReceipt(String[] arIds, String communityId, int quantity, MachinePrinterDto machinePrinterDto, String name) {
        String printStr = "";
        AccountReceiptDto accountReceiptDto = new AccountReceiptDto();
        accountReceiptDto.setArIds(arIds);
        accountReceiptDto.setCommunityId(communityId);
        List<AccountReceiptDto> accountReceiptDtos = accountReceiptV1InnerServiceSMOImpl.queryAccountReceipts(accountReceiptDto);
        if(accountReceiptDtos == null || accountReceiptDtos.size() < 1){
            throw new CmdException("没有打印内容");
        }
 
        /*************************************头部******************************************/
        List<FeieLine> feieLines = new ArrayList<>();
        feieLines.add(new FeieLine("单号", accountReceiptDtos.get(0).getArId()));
        feieLines.add(new FeieLine("时间", DateUtil.getFormatTimeString(DateUtil.getCurrentDate(), DateUtil.DATE_FORMATE_STRING_A)));
        printStr = FeieGetPayFeeDetail.getPrintPayFeeDetailHeaderContent(feieLines);
        /*************************************头部******************************************/
 
        feieLines = new ArrayList<>();
        BigDecimal totalDecimal = new BigDecimal(0);
        for (AccountReceiptDto tmpAccountReceiptDto : accountReceiptDtos) {
            feieLines.add(new FeieLine("账户名称", tmpAccountReceiptDto.getAcctName()));
            feieLines.add(new FeieLine("账户类型", tmpAccountReceiptDto.getAcctTypeName()));
            feieLines.add(new FeieLine("业主", tmpAccountReceiptDto.getOwnerName()));
            feieLines.add(new FeieLine("预存金额", tmpAccountReceiptDto.getReceivedAmount()));
            feieLines.add(new FeieLine("预存方式", tmpAccountReceiptDto.getPrimeRateName()));
            feieLines.add(new FeieLine("当前余额", tmpAccountReceiptDto.getAmount()));
            feieLines.add(new FeieLine("备注", tmpAccountReceiptDto.getRemark()));
            printStr += FeieGetPayFeeDetail.getPrintPayFeeDetailBodyContent(feieLines);
            totalDecimal = totalDecimal.add(new BigDecimal(Double.parseDouble(tmpAccountReceiptDto.getAmount())));
        }
        printStr += FeieGetPayFeeDetail.getPrintPayFeeDetailFloorContent(communityId, totalDecimal.doubleValue(), name, smallWeChatInnerServiceSMOImpl);
 
        doPrint(quantity, machinePrinterDto, printStr);
 
        return new ResultVo(ResultVo.CODE_OK, "成功");
    }
 
 
    private static String signature(String USER, String UKEY, String STIME) {
        return DigestUtils.sha1Hex(USER + UKEY + STIME);
    }
 
 
    private static void doPrint(int quantity, MachinePrinterDto machinePrinterDto, String printStr) {
        String stime = String.valueOf(System.currentTimeMillis() / 1000);
        String user = MappingCache.getValue(MappingConstant.FEIE_DOMAIN, "user");
        String ukey = MappingCache.getValue(MappingConstant.FEIE_DOMAIN, "ukey");
        // 通过POST请求,发送打印信息到服务器
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(30000)// 读取超时
                .setConnectTimeout(30000)// 连接超时
                .build();
 
        CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
        HttpPost post = new HttpPost(REQUEST_URL);
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("user", user));
        nvps.add(new BasicNameValuePair("stime", stime));
        nvps.add(new BasicNameValuePair("sig", signature(user, ukey, stime)));
        nvps.add(new BasicNameValuePair("apiname", "Open_printMsg"));// 固定值,不需要修改
        nvps.add(new BasicNameValuePair("sn", machinePrinterDto.getMachineCode()));
        nvps.add(new BasicNameValuePair("content", printStr));
        nvps.add(new BasicNameValuePair("times", quantity + ""));// 打印联数
 
        CloseableHttpResponse response = null;
        String result = null;
        try {
            post.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
            response = httpClient.execute(post);
            int statecode = response.getStatusLine().getStatusCode();
            if (statecode == 200) {
                HttpEntity httpentity = response.getEntity();
                if (httpentity != null) {
                    // 服务器返回的JSON字符串,建议要当做日志记录起来
                    result = EntityUtils.toString(httpentity);
                    System.out.println(result);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(response, post, httpClient);
        }
    }
 
    public static void close(CloseableHttpResponse response, HttpPost post, CloseableHttpClient httpClient) {
        try {
            if (response != null) {
                response.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            post.abort();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            httpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}