FeiYunUtils.java 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783
  1. package com.sqx.modules.utils.fieYun;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.sqx.modules.activity.service.ActivityService;
  5. import com.sqx.modules.activity.vo.ActivityOrderVO;
  6. import com.sqx.modules.activity.vo.ActivityVO;
  7. import com.sqx.modules.app.entity.UserEntity;
  8. import com.sqx.modules.app.service.UserService;
  9. import com.sqx.modules.common.service.CommonInfoService;
  10. import com.sqx.modules.coupon.entity.TbCoupon;
  11. import com.sqx.modules.coupon.entity.TbCouponUser;
  12. import com.sqx.modules.coupon.service.TbCouponService;
  13. import com.sqx.modules.goods.entity.GoodsShop;
  14. import com.sqx.modules.goods.service.GoodsShopService;
  15. import com.sqx.modules.order.entity.OrderGoods;
  16. import com.sqx.modules.order.entity.TbOrder;
  17. import com.sqx.modules.printInfo.entity.*;
  18. import com.sqx.modules.printInfo.service.PrintInfoService;
  19. import com.sqx.modules.printInfo.service.PrintInfoShopService;
  20. import com.sqx.modules.utils.fieYun.model.OrderForm;
  21. import lombok.extern.slf4j.Slf4j;
  22. import org.apache.commons.codec.digest.DigestUtils;
  23. import org.apache.commons.lang.StringUtils;
  24. import org.apache.http.HttpEntity;
  25. import org.apache.http.NameValuePair;
  26. import org.apache.http.client.config.RequestConfig;
  27. import org.apache.http.client.entity.UrlEncodedFormEntity;
  28. import org.apache.http.client.methods.CloseableHttpResponse;
  29. import org.apache.http.client.methods.HttpPost;
  30. import org.apache.http.impl.client.CloseableHttpClient;
  31. import org.apache.http.impl.client.HttpClients;
  32. import org.apache.http.message.BasicNameValuePair;
  33. import org.apache.http.util.EntityUtils;
  34. import org.springframework.beans.factory.annotation.Autowired;
  35. import org.springframework.stereotype.Component;
  36. import java.io.IOException;
  37. import java.math.BigDecimal;
  38. import java.util.ArrayList;
  39. import java.util.List;
  40. import java.util.Optional;
  41. import java.util.stream.Collectors;
  42. @Component
  43. @Slf4j
  44. public class FeiYunUtils {
  45. private static ActivityService activityService;
  46. private static UserService userService;
  47. private static TbCouponService tbCouponService;
  48. private static GoodsShopService goodsShopService;
  49. private static CommonInfoService commonInfoService;
  50. private static PrintInfoShopService printInfoShopService;
  51. private static PrintInfoService printInfoService;
  52. @Autowired
  53. public void setActivityService(ActivityService activityService) {
  54. FeiYunUtils.activityService = activityService;
  55. }
  56. @Autowired
  57. public void setUserService(UserService userService) {
  58. FeiYunUtils.userService = userService;
  59. }
  60. @Autowired
  61. public void setTbCouponService(TbCouponService tbCouponService) {
  62. FeiYunUtils.tbCouponService = tbCouponService;
  63. }
  64. @Autowired
  65. public void setGoodsShopService(GoodsShopService goodsShopService) {
  66. FeiYunUtils.goodsShopService = goodsShopService;
  67. }
  68. @Autowired
  69. public void setCommonInfoService(CommonInfoService commonInfoService) {
  70. FeiYunUtils.commonInfoService = commonInfoService;
  71. }
  72. @Autowired
  73. public void setPrintInfoShopService(PrintInfoShopService printInfoShopService) {
  74. FeiYunUtils.printInfoShopService = printInfoShopService;
  75. }
  76. @Autowired
  77. public void setPrintInfoService(PrintInfoService printInfoService) {
  78. FeiYunUtils.printInfoService = printInfoService;
  79. }
  80. /**
  81. * 添加打印机接口
  82. *
  83. * @param snlist 提示:打印机编号(必填) # 打印机识别码(必填) # 备注名称(选填) # 流量卡号码(选填),多台打印机请换行(\n)添加新打印机信息,每次最多100行(台)。
  84. * @return 正确例子:{"msg":"ok","ret":0,"data":{"ok":["sn#key#remark#carnum","316500011#abcdefgh#快餐前台"],"no":["316500012#abcdefgh#快餐前台#13688889999 (错误:识别码不正确)"]},"serverExecutedTime":3}
  85. * 错误:{"msg":"参数错误 : 该帐号未注册.","ret":-2,"data":null,"serverExecutedTime":37}
  86. */
  87. public static String addprinter(String snlist) {
  88. String URL = commonInfoService.findOne(325).getValue();
  89. String USER = commonInfoService.findOne(326).getValue();
  90. String UKEY = commonInfoService.findOne(327).getValue();
  91. //通过POST请求,发送打印信息到服务器
  92. RequestConfig requestConfig = RequestConfig.custom()
  93. .setSocketTimeout(30000)//读取超时
  94. .setConnectTimeout(30000)//连接超时
  95. .build();
  96. CloseableHttpClient httpClient = HttpClients.custom()
  97. .setDefaultRequestConfig(requestConfig)
  98. .build();
  99. HttpPost post = new HttpPost(URL);
  100. List<NameValuePair> nvps = new ArrayList<NameValuePair>();
  101. nvps.add(new BasicNameValuePair("user", USER));
  102. String STIME = String.valueOf(System.currentTimeMillis() / 1000);
  103. nvps.add(new BasicNameValuePair("stime", STIME));
  104. nvps.add(new BasicNameValuePair("sig", signature(USER, UKEY, STIME)));
  105. nvps.add(new BasicNameValuePair("apiname", "Open_printerAddlist"));//固定值,不需要修改
  106. nvps.add(new BasicNameValuePair("printerContent", snlist));
  107. CloseableHttpResponse response = null;
  108. String result = null;
  109. try {
  110. post.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
  111. response = httpClient.execute(post);
  112. int statecode = response.getStatusLine().getStatusCode();
  113. if (statecode == 200) {
  114. HttpEntity httpentity = response.getEntity();
  115. if (httpentity != null) {
  116. result = EntityUtils.toString(httpentity);
  117. }
  118. }
  119. } catch (Exception e) {
  120. e.printStackTrace();
  121. } finally {
  122. try {
  123. if (response != null) {
  124. response.close();
  125. }
  126. } catch (IOException e) {
  127. e.printStackTrace();
  128. }
  129. try {
  130. post.abort();
  131. } catch (Exception e) {
  132. e.printStackTrace();
  133. }
  134. try {
  135. httpClient.close();
  136. } catch (IOException e) {
  137. e.printStackTrace();
  138. }
  139. }
  140. return result;
  141. }
  142. /**
  143. * 修改打印机接口
  144. *
  145. * @param snlist 提示:打印机编号(必填) # 打印机识别码(必填) # 备注名称(选填) # 流量卡号码(选填),多台打印机请换行(\n)添加新打印机信息,每次最多100行(台)。
  146. * @return 正确例子:{"msg":"ok","ret":0,"data":{"ok":["sn#key#remark#carnum","316500011#abcdefgh#快餐前台"],"no":["316500012#abcdefgh#快餐前台#13688889999 (错误:识别码不正确)"]},"serverExecutedTime":3}
  147. * 错误:{"msg":"参数错误 : 该帐号未注册.","ret":-2,"data":null,"serverExecutedTime":37}
  148. */
  149. public static String updatePrinter(String snlist, String name) {
  150. String URL = commonInfoService.findOne(325).getValue();
  151. String USER = commonInfoService.findOne(326).getValue();
  152. String UKEY = commonInfoService.findOne(327).getValue();
  153. //通过POST请求,发送打印信息到服务器
  154. RequestConfig requestConfig = RequestConfig.custom()
  155. .setSocketTimeout(30000)//读取超时
  156. .setConnectTimeout(30000)//连接超时
  157. .build();
  158. CloseableHttpClient httpClient = HttpClients.custom()
  159. .setDefaultRequestConfig(requestConfig)
  160. .build();
  161. HttpPost post = new HttpPost(URL);
  162. List<NameValuePair> nvps = new ArrayList<NameValuePair>();
  163. nvps.add(new BasicNameValuePair("user", USER));
  164. String STIME = String.valueOf(System.currentTimeMillis() / 1000);
  165. nvps.add(new BasicNameValuePair("stime", STIME));
  166. nvps.add(new BasicNameValuePair("sig", signature(USER, UKEY, STIME)));
  167. nvps.add(new BasicNameValuePair("apiname", "Open_printerEdit"));//固定值,不需要修改
  168. nvps.add(new BasicNameValuePair("sn", snlist));
  169. nvps.add(new BasicNameValuePair("name", name));
  170. CloseableHttpResponse response = null;
  171. String result = null;
  172. try {
  173. post.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
  174. response = httpClient.execute(post);
  175. int statecode = response.getStatusLine().getStatusCode();
  176. if (statecode == 200) {
  177. HttpEntity httpentity = response.getEntity();
  178. if (httpentity != null) {
  179. result = EntityUtils.toString(httpentity);
  180. }
  181. }
  182. } catch (Exception e) {
  183. e.printStackTrace();
  184. } finally {
  185. try {
  186. if (response != null) {
  187. response.close();
  188. }
  189. } catch (IOException e) {
  190. e.printStackTrace();
  191. }
  192. try {
  193. post.abort();
  194. } catch (Exception e) {
  195. e.printStackTrace();
  196. }
  197. try {
  198. httpClient.close();
  199. } catch (IOException e) {
  200. e.printStackTrace();
  201. }
  202. }
  203. return result;
  204. }
  205. /**
  206. * 删除打印机接口
  207. *
  208. * @param snlist 提示:打印机编号(必填) # 打印机识别码(必填) # 备注名称(选填) # 流量卡号码(选填),多台打印机请换行(\n)添加新打印机信息,每次最多100行(台)。
  209. * @return 正确例子:{"msg":"ok","ret":0,"data":{"ok":["sn#key#remark#carnum","316500011#abcdefgh#快餐前台"],"no":["316500012#abcdefgh#快餐前台#13688889999 (错误:识别码不正确)"]},"serverExecutedTime":3}
  210. * 错误:{"msg":"参数错误 : 该帐号未注册.","ret":-2,"data":null,"serverExecutedTime":37}
  211. */
  212. public static String deletePrinter(String snlist) {
  213. try {
  214. String URL = commonInfoService.findOne(325).getValue();
  215. String USER = commonInfoService.findOne(326).getValue();
  216. String UKEY = commonInfoService.findOne(327).getValue();
  217. //通过POST请求,发送打印信息到服务器
  218. RequestConfig requestConfig = RequestConfig.custom()
  219. .setSocketTimeout(30000)//读取超时
  220. .setConnectTimeout(30000)//连接超时
  221. .build();
  222. CloseableHttpClient httpClient = HttpClients.custom()
  223. .setDefaultRequestConfig(requestConfig)
  224. .build();
  225. HttpPost post = new HttpPost(URL);
  226. List<NameValuePair> nvps = new ArrayList<NameValuePair>();
  227. nvps.add(new BasicNameValuePair("user", USER));
  228. String STIME = String.valueOf(System.currentTimeMillis() / 1000);
  229. nvps.add(new BasicNameValuePair("stime", STIME));
  230. nvps.add(new BasicNameValuePair("sig", signature(USER, UKEY, STIME)));
  231. nvps.add(new BasicNameValuePair("apiname", "Open_printerDelList"));//固定值,不需要修改
  232. nvps.add(new BasicNameValuePair("snlist", snlist));
  233. CloseableHttpResponse response = null;
  234. String result = null;
  235. try {
  236. post.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
  237. response = httpClient.execute(post);
  238. int statecode = response.getStatusLine().getStatusCode();
  239. if (statecode == 200) {
  240. HttpEntity httpentity = response.getEntity();
  241. if (httpentity != null) {
  242. result = EntityUtils.toString(httpentity);
  243. }
  244. }
  245. } catch (Exception e) {
  246. e.printStackTrace();
  247. } finally {
  248. try {
  249. if (response != null) {
  250. response.close();
  251. }
  252. } catch (IOException e) {
  253. e.printStackTrace();
  254. }
  255. try {
  256. post.abort();
  257. } catch (Exception e) {
  258. e.printStackTrace();
  259. }
  260. try {
  261. httpClient.close();
  262. } catch (IOException e) {
  263. e.printStackTrace();
  264. }
  265. }
  266. return result;
  267. } catch (Exception e) {
  268. e.printStackTrace();
  269. log.error("删除打印机异常:" + e.getMessage(), e);
  270. }
  271. return null;
  272. }
  273. /**
  274. * 小票机打印订单接口
  275. *
  276. * @param sn 打印机编号
  277. * @param classify 分类 1加油成功出票模版 2提货券推送模版 3加油订单结算模版 4商品消费结算
  278. * @return 成功:{"msg":"ok","ret":0,"data":"xxxxxxx_xxxxxxxx_xxxxxxxx","serverExecutedTime":5}
  279. * 失败:{"msg":"错误描述","ret":非0,"data":"null","serverExecutedTime":5}
  280. */
  281. public static String print(String sn, Integer classify, TbOrder tbOrder) {
  282. try {
  283. //标签说明:
  284. //单标签:
  285. //"<BR>"为换行,"<CUT>"为切刀指令(主动切纸,仅限切刀打印机使用才有效果)
  286. //"<LOGO>"为打印LOGO指令(前提是预先在机器内置LOGO图片),"<PLUGIN>"为钱箱或者外置音响指令
  287. //成对标签:
  288. //"<CB></CB>"为居中放大一倍,"<B></B>"为放大一倍,"<C></C>"为居中,<L></L>字体变高一倍
  289. //<W></W>字体变宽一倍,"<QR></QR>"为二维码,"<BOLD></BOLD>"为字体加粗,""为右对齐
  290. //拼凑订单内容时可参考如下格式
  291. //根据打印纸张的宽度,自行调整内容的格式,可参考下面的样例格式
  292. //region 以前的代码
  293. /*content = "<CB>测试打印</CB><BR>";
  294. content += "名称      单价 数量 金额<BR>";
  295. content += "--------------------------------<BR>";
  296. content += "饭       1.0 1 1.0<BR>";
  297. content += "炒饭      10.0 10 10.0<BR>";
  298. content += "蛋炒饭     10.0 10 100.0<BR>";
  299. content += "鸡蛋炒饭    100.0 1 100.0<BR>";
  300. content += "番茄蛋炒饭   1000.0 1 100.0<BR>";
  301. content += "西红柿蛋炒饭  1000.0 1 100.0<BR>";
  302. content += "西红柿鸡蛋炒饭 100.0 10 100.0<BR>";
  303. content += "备注:加辣<BR>";
  304. content += "--------------------------------<BR>";
  305. content += "合计:xx.0元<BR>";
  306. content += "送货地点:广州市南沙区xx路xx号<BR>";
  307. content += "联系电话:13888888888888<BR>";
  308. content += "订餐时间:2016-08-08 08:08:08<BR>";
  309. content += "<QR>http://www.dzist.com</QR>";*/
  310. //endregion
  311. //region 获取配置的打印模板信息 没设置的就正常不加粗
  312. GoodsShop shopData = goodsShopService.selectGoodDatas(tbOrder.getShopId());
  313. UserEntity parentUser = userService.selectUserById(tbOrder.getParentUserId());
  314. TbCoupon tbCoupon = tbCouponService.selectCoupon(tbOrder.getCouponId());
  315. ActivityOrderVO activity = tbOrder.getOrderId() == null ? null : activityService.selectByOrder(tbOrder.getOrderId());
  316. List<PrintModelResult> resultData = new ArrayList<>();
  317. List<PrintInfo> infos = printInfoService.printInfos();
  318. List<Integer> typeIds = infos.stream().map(PrintInfo::getTypeId).distinct().collect(Collectors.toList());
  319. PrintInfoShop pis = printInfoShopService.selectPrintInfoShopById(tbOrder.getShopId().intValue());
  320. if (pis == null) {
  321. for (Integer typeId : typeIds) {
  322. List<PrintInfo> nowInfos = infos.stream().filter(e -> e.getTypeId().intValue() == typeId.intValue()).collect(Collectors.toList());
  323. PrintModelResult data = new PrintModelResult();
  324. List<PrintModelDetailResult> details = new ArrayList<>();
  325. List<PrintModelTypeResult> types = new ArrayList<>();
  326. for (PrintInfo nowInfo : nowInfos) {
  327. PrintModelDetailResult detailData = new PrintModelDetailResult();
  328. detailData.setId(nowInfo.getId());
  329. detailData.setPrintField(nowInfo.getPrintField());
  330. detailData.setPrintName(nowInfo.getPrintName());
  331. detailData.setIsCheck(0);
  332. details.add(detailData);
  333. PrintModelTypeResult typeData = new PrintModelTypeResult();
  334. typeData.setId(nowInfo.getId());
  335. typeData.setIsBlod(0);
  336. typeData.setWordSize(0);
  337. typeData.setPrintField(nowInfo.getPrintField());
  338. typeData.setPrintName(nowInfo.getPrintName());
  339. types.add(typeData);
  340. }
  341. if (nowInfos != null && nowInfos.size() > 0) {
  342. data.setId(typeId);
  343. data.setName(nowInfos.get(0).getTypeName());
  344. }
  345. data.setDetails(details);
  346. data.setTypes(types);
  347. resultData.add(data);
  348. }
  349. } else {
  350. //要修改获取逻辑
  351. if (pis.getBaseInfo().isEmpty()) {
  352. PrintModelResult data = new PrintModelResult();
  353. List<PrintInfo> nowInfos = infos.stream().filter(e -> e.getTypeId().intValue() == ePrintStatu.Base.getValue()).collect(Collectors.toList());
  354. List<PrintModelDetailResult> details = new ArrayList<>();
  355. List<PrintModelTypeResult> types = new ArrayList<>();
  356. for (PrintInfo nowInfo : nowInfos) {
  357. PrintModelDetailResult detailData = new PrintModelDetailResult();
  358. detailData.setId(nowInfo.getId());
  359. detailData.setPrintField(nowInfo.getPrintField());
  360. detailData.setPrintName(nowInfo.getPrintName());
  361. detailData.setIsCheck(0);
  362. details.add(detailData);
  363. PrintModelTypeResult typeData = new PrintModelTypeResult();
  364. typeData.setId(nowInfo.getId());
  365. typeData.setIsBlod(0);
  366. typeData.setWordSize(0);
  367. typeData.setPrintField(nowInfo.getPrintField());
  368. typeData.setPrintName(nowInfo.getPrintName());
  369. types.add(typeData);
  370. }
  371. data.setId(ePrintStatu.Base.getValue());
  372. data.setName(nowInfos.get(0).getTypeName());
  373. data.setDetails(details);
  374. data.setTypes(types);
  375. resultData.add(data);
  376. } else {
  377. ObjectMapper objectMapper = new ObjectMapper();
  378. PrintModelResult data = objectMapper.readValue(pis.getBaseInfo(), PrintModelResult.class);
  379. resultData.add(data);
  380. }
  381. if (pis.getGoodInfo().isEmpty()) {
  382. PrintModelResult data = new PrintModelResult();
  383. List<PrintInfo> nowInfos = infos.stream().filter(e -> e.getTypeId().intValue() == ePrintStatu.Good.getValue()).collect(Collectors.toList());
  384. List<PrintModelDetailResult> details = new ArrayList<>();
  385. List<PrintModelTypeResult> types = new ArrayList<>();
  386. for (PrintInfo nowInfo : nowInfos) {
  387. PrintModelDetailResult detailData = new PrintModelDetailResult();
  388. detailData.setId(nowInfo.getId());
  389. detailData.setPrintField(nowInfo.getPrintField());
  390. detailData.setPrintName(nowInfo.getPrintName());
  391. detailData.setIsCheck(0);
  392. details.add(detailData);
  393. PrintModelTypeResult typeData = new PrintModelTypeResult();
  394. typeData.setId(nowInfo.getId());
  395. typeData.setIsBlod(0);
  396. typeData.setWordSize(0);
  397. typeData.setPrintField(nowInfo.getPrintField());
  398. typeData.setPrintName(nowInfo.getPrintName());
  399. types.add(typeData);
  400. }
  401. data.setId(ePrintStatu.Good.getValue());
  402. data.setName(nowInfos.get(0).getTypeName());
  403. data.setDetails(details);
  404. data.setTypes(types);
  405. resultData.add(data);
  406. } else {
  407. ObjectMapper objectMapper = new ObjectMapper();
  408. PrintModelResult data = objectMapper.readValue(pis.getGoodInfo(), PrintModelResult.class);
  409. resultData.add(data);
  410. }
  411. if (pis.getPayInfo().isEmpty()) {
  412. PrintModelResult data = new PrintModelResult();
  413. List<PrintInfo> nowInfos = infos.stream().filter(e -> e.getTypeId().intValue() == ePrintStatu.Pay.getValue()).collect(Collectors.toList());
  414. List<PrintModelDetailResult> details = new ArrayList<>();
  415. List<PrintModelTypeResult> types = new ArrayList<>();
  416. for (PrintInfo nowInfo : nowInfos) {
  417. PrintModelDetailResult detailData = new PrintModelDetailResult();
  418. detailData.setId(nowInfo.getId());
  419. detailData.setPrintField(nowInfo.getPrintField());
  420. detailData.setPrintName(nowInfo.getPrintName());
  421. detailData.setIsCheck(0);
  422. details.add(detailData);
  423. PrintModelTypeResult typeData = new PrintModelTypeResult();
  424. typeData.setId(nowInfo.getId());
  425. typeData.setIsBlod(0);
  426. typeData.setWordSize(0);
  427. typeData.setPrintField(nowInfo.getPrintField());
  428. typeData.setPrintName(nowInfo.getPrintName());
  429. types.add(typeData);
  430. }
  431. data.setId(ePrintStatu.Pay.getValue());
  432. data.setName(nowInfos.get(0).getTypeName());
  433. data.setDetails(details);
  434. data.setTypes(types);
  435. resultData.add(data);
  436. } else {
  437. ObjectMapper objectMapper = new ObjectMapper();
  438. PrintModelResult data = objectMapper.readValue(pis.getPayInfo(), PrintModelResult.class);
  439. resultData.add(data);
  440. }
  441. if (pis.getUserInfo().isEmpty()) {
  442. PrintModelResult data = new PrintModelResult();
  443. List<PrintInfo> nowInfos = infos.stream().filter(e -> e.getTypeId().intValue() == ePrintStatu.User.getValue()).collect(Collectors.toList());
  444. List<PrintModelDetailResult> details = new ArrayList<>();
  445. List<PrintModelTypeResult> types = new ArrayList<>();
  446. for (PrintInfo nowInfo : nowInfos) {
  447. PrintModelDetailResult detailData = new PrintModelDetailResult();
  448. detailData.setId(nowInfo.getId());
  449. detailData.setPrintField(nowInfo.getPrintField());
  450. detailData.setPrintName(nowInfo.getPrintName());
  451. detailData.setIsCheck(0);
  452. details.add(detailData);
  453. PrintModelTypeResult typeData = new PrintModelTypeResult();
  454. typeData.setId(nowInfo.getId());
  455. typeData.setIsBlod(0);
  456. typeData.setWordSize(0);
  457. typeData.setPrintField(nowInfo.getPrintField());
  458. typeData.setPrintName(nowInfo.getPrintName());
  459. types.add(typeData);
  460. }
  461. data.setId(ePrintStatu.User.getValue());
  462. data.setName(nowInfos.get(0).getTypeName());
  463. data.setDetails(details);
  464. data.setTypes(types);
  465. resultData.add(data);
  466. } else {
  467. ObjectMapper objectMapper = new ObjectMapper();
  468. PrintModelResult data = objectMapper.readValue(pis.getUserInfo(), PrintModelResult.class);
  469. resultData.add(data);
  470. }
  471. }
  472. //endregion
  473. //<B></B> :放大一倍
  474. //<BOLD></BOLD> :字体加粗
  475. //<C></C> :居中
  476. //<CB></CB> :居中放大
  477. // :右对齐
  478. //PrintModelResult result
  479. //字体样式只针对内容
  480. List<OrderGoods> orderGoodsList = tbOrder.getOrderGoodsList();
  481. StringBuilder stringBuilder = new StringBuilder();
  482. int max = Integer.parseInt(commonInfoService.findOne(420).getValue());
  483. for (int i = 0; i < max; i++) {
  484. //region 基本信息:平台名称 店铺名 订单号 送达时间 订单类型
  485. Optional<PrintModelResult> oBaseInfo = resultData.stream().filter(e -> e.getId().intValue() == ePrintStatu.Base.getValue()).findFirst();
  486. List<PrintModelTypeResult> baseTypes = oBaseInfo.get().getTypes();
  487. //region 平台名称
  488. String bplatformNameType_start = null;
  489. String bplatformNameType_end = null;
  490. //endregion
  491. //region 商铺名
  492. String bshopName_start = null;
  493. String bshopName_end = null;
  494. //endregion
  495. //region 订单类型
  496. String borderTypeExtra_start = null;
  497. String borderTypeExtra_end = null;
  498. //endregion
  499. //region 订单号
  500. String borderNumber_start = null;
  501. String borderNumber_end = null;
  502. //endregion
  503. StringBuilder baseStringBuilder = new StringBuilder();
  504. for (PrintModelTypeResult baseType : baseTypes) {
  505. StringBuilder start = new StringBuilder();
  506. StringBuilder end = new StringBuilder();
  507. if (baseType.getWordSize().intValue() == eWordSize.Amplify.getValue()) {
  508. start.append("<B>");
  509. end.append("</B>");
  510. }
  511. if (baseType.getWordSize().intValue() == eWordSize.Amplify.getValue()) {
  512. start.append("<BOLD>");
  513. end.append("</BOLD>");
  514. }
  515. switch (baseType.getPrintField()) {
  516. case "platform_name":
  517. bplatformNameType_start = "<C>" + start.toString();
  518. bplatformNameType_end = end.toString()+"</C>";
  519. break;
  520. case "shop_name":
  521. bshopName_start = "<C>" + start.toString();
  522. bshopName_end = end.toString()+"</C>";
  523. break;
  524. case "order_type_extra":
  525. borderTypeExtra_start = start.toString();
  526. borderTypeExtra_end = end.toString();
  527. break;
  528. case "order_number":
  529. borderNumber_start = start.toString();
  530. borderNumber_end = end.toString();
  531. break;
  532. case "user_nick_name":
  533. baseStringBuilder.append(start.toString()).append(baseType.getPrintName()+":").append(tbOrder.getUserNickName()).append(end.toString()).append("<BR>");
  534. break;
  535. case "shop_phone":
  536. baseStringBuilder.append(start.toString()).append(baseType.getPrintName()+":").append(tbOrder.getShopPhone()).append(end.toString()).append("<BR>");
  537. break;
  538. case "distribution_distance_start":
  539. baseStringBuilder.append(start.toString()).append(baseType.getPrintName()+":").append(tbOrder.getDistributionDistance()).append(end.toString()).append("<BR>");
  540. break;
  541. case "detailed_address":
  542. baseStringBuilder.append(start.toString()).append(baseType.getPrintName()+":").append(tbOrder.getDetailedAddress()).append(end.toString()).append("<BR>");
  543. break;
  544. case "remark":
  545. baseStringBuilder.append(start.toString()).append(baseType.getPrintName()+":").append(tbOrder.getRemark()).append(end.toString()).append("<BR>");
  546. break;
  547. case "shop_receiving_time":
  548. baseStringBuilder.append(start.toString()).append(baseType.getPrintName()+":").append(tbOrder.getShopReceivingTime()).append(end.toString()).append("<BR>");
  549. break;
  550. case "shop_id":
  551. baseStringBuilder.append(start.toString()).append(baseType.getPrintName()+":").append(shopData != null ? shopData.getBusinessHours()+"-"+shopData.getLockHours() : "").append(end.toString()).append("<BR>");
  552. break;
  553. case "parent_user_id":
  554. baseStringBuilder.append(start.toString()).append(baseType.getPrintName()+":").append(parentUser == null ? "" : parentUser.getNickName()).append(end.toString()).append("<BR>");
  555. break;
  556. case "pay_money":
  557. baseStringBuilder.append(start.toString()).append(baseType.getPrintName()+":").append(tbOrder.getPayMoney()).append("元").append(end.toString()).append("<BR>");
  558. break;
  559. case "auto_send_order":
  560. baseStringBuilder.append(start.toString()).append(baseType.getPrintName()+":").append(tbOrder.getAutoSendOrder().intValue() == 0 ? "开启" : "关闭").append(end.toString()).append("<BR>");
  561. break;
  562. case "errand_money":
  563. baseStringBuilder.append(start.toString()).append(baseType.getPrintName()+":").append(tbOrder.getErrandMoney()).append("元").append(end.toString()).append("<BR>");
  564. break;
  565. case "pack_money":
  566. baseStringBuilder.append(start.toString()).append(baseType.getPrintName()+":").append(tbOrder.getPackMoney()).append("元").append(end.toString()).append("<BR>");
  567. break;
  568. case "order_code":
  569. baseStringBuilder.append(start.toString()).append(baseType.getPrintName()+":").append(tbOrder.getOrderCode()).append(end.toString()).append("<BR>");
  570. break;
  571. case "pay_type":
  572. baseStringBuilder.append(start.toString()).append(baseType.getPrintName()+":").append(tbOrder.getPayType().intValue() == 1 ? "微信支付" : (tbOrder.getPayType().intValue() == 2 ? "余额支付" : "支付宝支付")).append(end.toString()).append("<BR>");
  573. break;
  574. case "pay_time":
  575. baseStringBuilder.append(start.toString()).append(baseType.getPrintName()+":").append(tbOrder.getPayTime()).append(end.toString()).append("<BR>");
  576. break;
  577. case "create_time":
  578. baseStringBuilder.append(start.toString()).append(baseType.getPrintName()+":").append(tbOrder.getCreateTime()).append(end.toString()).append("<BR>");
  579. break;
  580. case "address":
  581. if (StringUtils.isNotEmpty(tbOrder.getAddress())) {
  582. JSONObject jsonObject = JSONObject.parseObject(tbOrder.getAddress());
  583. String addressDetail = jsonObject.getString("addressDetail");
  584. String city = jsonObject.getString("city");
  585. String district = jsonObject.getString("district");
  586. String province = jsonObject.getString("province");
  587. baseStringBuilder.append(start.toString()).append(province).append(city).append(district).append(addressDetail).append(end.toString()).append("<BR>");
  588. }
  589. break;
  590. case "phone":
  591. if (StringUtils.isNotEmpty(tbOrder.getAddress())) {
  592. JSONObject jsonObject = JSONObject.parseObject(tbOrder.getAddress());
  593. String phone = jsonObject.getString("userPhone");
  594. baseStringBuilder.append(start.toString()).append(phone).append(end.toString()).append("<BR>");
  595. }
  596. break;
  597. case "user_name":
  598. if (StringUtils.isNotEmpty(tbOrder.getAddress())) {
  599. JSONObject jsonObject = JSONObject.parseObject(tbOrder.getAddress());
  600. String userName = jsonObject.getString("userName");
  601. baseStringBuilder.append(start.toString()).append(userName).append(end.toString()).append("<BR>");
  602. }
  603. break;
  604. case "user_id"://显示isVip和vipExpirationTime
  605. UserEntity user = null;
  606. if(tbOrder.getUserId() != null){
  607. user = userService.selectUserById(tbOrder.getUserId());
  608. }
  609. baseStringBuilder.append(start.toString()).append("是否会员:").append(user == null ? "" : (user.getIsVip().intValue() == 1 ? "是" : "否")).append(end.toString()).append("<BR>");
  610. baseStringBuilder.append(start.toString()).append("会员有效期:").append(user == null ? "" : user.getVipExpirationTime()).append(end.toString()).append("<BR>");
  611. break;
  612. case "coupon_id"://拿优惠券表中的优惠券名称
  613. baseStringBuilder.append(start.toString()).append(baseType.getPrintName()+":").append(tbCoupon == null ? "" : tbCoupon.getCouponName()).append(end.toString()).append("<BR>");
  614. break;
  615. case "expect_delivery_time":
  616. baseStringBuilder.append(start.toString()).append(baseType.getPrintName()+":").append(tbOrder.getExpectDeliveryTime()).append(end.toString()).append("<BR>");
  617. break;
  618. case "activity_title"://通过订单ID去activity_part_record表中查到activity_id后再去activity表中获取活动标题
  619. baseStringBuilder.append(start.toString()).append(baseType.getPrintName()+":").append(activity == null ? "" : activity.getTitle()).append(end.toString()).append("<BR>");
  620. break;
  621. default:
  622. break;
  623. }
  624. }
  625. if (bplatformNameType_start != null) {
  626. stringBuilder.append(bplatformNameType_start).append("#").append(tbOrder.getCountOrder()).append("-").append("墨轩云商").append(bplatformNameType_end);
  627. }
  628. if (bshopName_start != null) {
  629. stringBuilder.append(bshopName_start).append(tbOrder.getShopName()).append(bshopName_end);
  630. }
  631. if (borderTypeExtra_start != null) {
  632. stringBuilder.append(borderTypeExtra_start).append("类型:").append(eOrderTypeExtra.stringOf(tbOrder.getOrderTypeExtra())).append(borderTypeExtra_end).append("<BR>");
  633. }
  634. if (borderNumber_start != null) {
  635. stringBuilder.append(borderNumber_start).append("订单号:").append(tbOrder.getOrderNumber()).append(borderNumber_end).append("<BR>");
  636. }
  637. stringBuilder.append(baseStringBuilder.toString());
  638. //endregion
  639. stringBuilder.append("--------------------------------<BR>");
  640. //region 商品信息:商品名称 单价 数量 备注 金额
  641. stringBuilder.append("名称    单价  数量  金额 <BR>");
  642. stringBuilder.append("--------------------------------<BR>");
  643. BigDecimal packMoney = BigDecimal.ZERO;
  644. BigDecimal goodsMoney = BigDecimal.ZERO;
  645. //stringBuilder.append("<BR>");
  646. //region 字号样式
  647. Optional<PrintModelResult> oGoodInfo = resultData.stream().filter(e -> e.getId().intValue() == ePrintStatu.Good.getValue()).findFirst();
  648. List<PrintModelTypeResult> goodTypes = oGoodInfo.get().getTypes();
  649. //region 商品名称
  650. String goods_name_start = null;
  651. String goods_name_end = null;
  652. //endregion
  653. //region 单价
  654. String goods_price_start = null;
  655. String goods_price_end = null;
  656. //endregion
  657. //region 数量
  658. String goods_num_start = null;
  659. String goods_num_end = null;
  660. //endregion
  661. //region 备注
  662. String remark_start = null;
  663. String remark_end = null;
  664. //endregion
  665. //region 金额
  666. String sum_money_start = null;
  667. String sum_money_end = null;
  668. //endregion
  669. StringBuilder goodStringBuilder = new StringBuilder();
  670. for (PrintModelTypeResult type : goodTypes) {
  671. StringBuilder start = new StringBuilder();
  672. StringBuilder end = new StringBuilder();
  673. if (type.getWordSize().intValue() == eWordSize.Amplify.getValue()) {
  674. start.append("<B>");
  675. end.append("</B>");
  676. }
  677. if (type.getWordSize().intValue() == eWordSize.Amplify.getValue()) {
  678. start.append("<BOLD>");
  679. end.append("</BOLD>");
  680. }
  681. switch (type.getPrintField()) {
  682. case "goods_name":
  683. goods_name_start = start.toString();
  684. goods_name_end = end.toString();
  685. break;
  686. case "goods_price":
  687. goods_price_start = start.toString();
  688. goods_price_end = end.toString();
  689. break;
  690. case "goods_num":
  691. goods_num_start = start.toString();
  692. goods_num_end = end.toString();
  693. break;
  694. case "remark":
  695. remark_start = start.toString();
  696. remark_end = end.toString();
  697. break;
  698. case "sum_money":
  699. sum_money_start = start.toString();
  700. sum_money_end = end.toString();
  701. break;
  702. case "platform_name":
  703. goodStringBuilder.append(start.toString()).append("#").append(tbOrder.getCountOrder()).append("-").append("墨轩云商").append(end.toString());
  704. break;
  705. case "shop_name":
  706. goodStringBuilder.append(start.toString()).append(tbOrder.getShopName()).append(end.toString());
  707. break;
  708. case "order_type_extra":
  709. goodStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(eOrderTypeExtra.stringOf(tbOrder.getOrderTypeExtra())).append(end.toString()).append("<BR>");
  710. break;
  711. case "order_number":
  712. goodStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getOrderNumber()).append(end.toString()).append("<BR>");
  713. break;
  714. case "user_nick_name":
  715. goodStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getUserNickName()).append(end.toString()).append("<BR>");
  716. break;
  717. case "shop_phone":
  718. goodStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getShopPhone()).append(end.toString()).append("<BR>");
  719. break;
  720. case "distribution_distance_start":
  721. goodStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getDistributionDistance()).append(end.toString()).append("<BR>");
  722. break;
  723. case "detailed_address":
  724. goodStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getDetailedAddress()).append(end.toString()).append("<BR>");
  725. break;
  726. case "shop_receiving_time":
  727. goodStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getShopReceivingTime()).append(end.toString()).append("<BR>");
  728. break;
  729. case "shop_id":
  730. goodStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(shopData != null ? shopData.getBusinessHours()+"-"+shopData.getLockHours() : "").append(end.toString()).append("<BR>");
  731. break;
  732. case "parent_user_id":
  733. goodStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(parentUser == null ? "" : parentUser.getNickName()).append(end.toString()).append("<BR>");
  734. break;
  735. case "pay_money":
  736. goodStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getPayMoney()).append("元").append(end.toString()).append("<BR>");
  737. break;
  738. case "auto_send_order":
  739. goodStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getAutoSendOrder().intValue() == 0 ? "开启" : "关闭").append(end.toString()).append("<BR>");
  740. break;
  741. case "errand_money":
  742. goodStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getErrandMoney()).append("元").append(end.toString()).append("<BR>");
  743. break;
  744. case "pack_money":
  745. goodStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getPackMoney()).append("元").append(end.toString()).append("<BR>");
  746. break;
  747. case "order_code":
  748. goodStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getOrderCode()).append(end.toString()).append("<BR>");
  749. break;
  750. case "pay_type":
  751. goodStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getPayType().intValue() == 1 ? "微信支付" : (tbOrder.getPayType().intValue() == 2 ? "余额支付" : "支付宝支付")).append(end.toString()).append("<BR>");
  752. break;
  753. case "pay_time":
  754. goodStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getPayTime()).append(end.toString()).append("<BR>");
  755. break;
  756. case "create_time":
  757. goodStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getCreateTime()).append(end.toString()).append("<BR>");
  758. break;
  759. case "address":
  760. if (StringUtils.isNotEmpty(tbOrder.getAddress())) {
  761. JSONObject jsonObject = JSONObject.parseObject(tbOrder.getAddress());
  762. String addressDetail = jsonObject.getString("addressDetail");
  763. String city = jsonObject.getString("city");
  764. String district = jsonObject.getString("district");
  765. String province = jsonObject.getString("province");
  766. goodStringBuilder.append(start.toString()).append(province).append(city).append(district).append(addressDetail).append(end.toString()).append("<BR>");
  767. }
  768. break;
  769. case "phone":
  770. if (StringUtils.isNotEmpty(tbOrder.getAddress())) {
  771. JSONObject jsonObject = JSONObject.parseObject(tbOrder.getAddress());
  772. String phone = jsonObject.getString("userPhone");
  773. goodStringBuilder.append(start.toString()).append(phone).append(end.toString()).append("<BR>");
  774. }
  775. break;
  776. case "user_name":
  777. if (StringUtils.isNotEmpty(tbOrder.getAddress())) {
  778. JSONObject jsonObject = JSONObject.parseObject(tbOrder.getAddress());
  779. String userName = jsonObject.getString("userName");
  780. goodStringBuilder.append(start.toString()).append(userName).append(end.toString()).append("<BR>");
  781. }
  782. break;
  783. case "user_id"://显示isVip和vipExpirationTime
  784. UserEntity user = null;
  785. if(tbOrder.getUserId() != null){
  786. user = userService.selectUserById(tbOrder.getUserId());
  787. }
  788. goodStringBuilder.append(start.toString()).append("是否会员:").append(user == null ? "" : (user.getIsVip().intValue() == 1 ? "是" : "否")).append(end.toString()).append("<BR>");
  789. goodStringBuilder.append(start.toString()).append("会员有效期:").append(user == null ? "" : user.getVipExpirationTime()).append(end.toString()).append("<BR>");
  790. break;
  791. case "coupon_id"://拿优惠券表中的优惠券名称
  792. goodStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbCoupon == null ? "" : tbCoupon.getCouponName()).append(end.toString()).append("<BR>");
  793. break;
  794. case "expect_delivery_time":
  795. goodStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getExpectDeliveryTime()).append(end.toString()).append("<BR>");
  796. break;
  797. case "activity_title"://通过订单ID去activity_part_record表中查到activity_id后再去activity表中获取活动标题
  798. goodStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(activity == null ? "" : activity.getTitle()).append(end.toString()).append("<BR>");
  799. break;
  800. default:
  801. break;
  802. }
  803. }
  804. //endregion
  805. for (OrderGoods orderGoods : orderGoodsList) {
  806. BigDecimal sumMoney = orderGoods.getGoodsPrice().multiply(BigDecimal.valueOf(orderGoods.getGoodsNum()));
  807. BigDecimal sumPack = orderGoods.getGoodsPack().multiply(BigDecimal.valueOf(orderGoods.getGoodsNum()));
  808. packMoney = packMoney.add(sumPack);
  809. goodsMoney = goodsMoney.add(sumMoney);
  810. String name;
  811. if (StringUtils.isNotEmpty(orderGoods.getSkuMessage())) {
  812. name = orderGoods.getGoodsName() + "(" + orderGoods.getSkuMessage() + ")";
  813. } else {
  814. name = orderGoods.getGoodsName();
  815. }
  816. stringBuilder.append(goods_name_start).append(name).append(goods_name_end);
  817. stringBuilder.append("    ");
  818. stringBuilder.append(goods_price_start).append(orderGoods.getGoodsPrice()).append(goods_price_end);
  819. if (orderGoods.getGoodsPrice().doubleValue() < 10) {
  820. stringBuilder.append("  ");
  821. } else if (orderGoods.getGoodsPrice().doubleValue() < 100) {
  822. stringBuilder.append("  ");
  823. } else {
  824. stringBuilder.append("  ");
  825. }
  826. stringBuilder.append(goods_num_start).append(orderGoods.getGoodsNum()).append(goods_num_end);
  827. stringBuilder.append("  ");
  828. stringBuilder.append(sum_money_start).append(sumMoney).append(sum_money_end);
  829. stringBuilder.append("<BR>");
  830. //stringBuilder.append("<BR>");
  831. }
  832. stringBuilder.append("--------------------------------<BR>");
  833. String remark = tbOrder.getRemark();
  834. if (StringUtils.isEmpty(remark)) {
  835. remark = "";
  836. }
  837. stringBuilder.append(remark_start).append("备注:").append(remark).append(remark_end).append("<BR>");
  838. stringBuilder.append(goodStringBuilder.toString());
  839. //endregion
  840. stringBuilder.append("--------------------------------<BR>");
  841. //region 支付信息:打包费 商品费 优惠券 跑腿费 优惠活动 合计
  842. //region 字号样式
  843. Optional<PrintModelResult> oPayInfo = resultData.stream().filter(e -> e.getId().intValue() == ePrintStatu.Pay.getValue()).findFirst();
  844. List<PrintModelTypeResult> payTypes = oPayInfo.get().getTypes();
  845. //region 打包费
  846. String pack_money_start = null;
  847. String pack_money_end = null;
  848. //endregion
  849. //region 商品费
  850. String goods_money_start = null;
  851. String goods_money_end = null;
  852. //endregion
  853. //region 优惠券
  854. String coupon_money_start = null;
  855. String coupon_money_end = null;
  856. //endregion
  857. //region 跑腿费
  858. String errand_money_start = null;
  859. String errand_money_end = null;
  860. //endregion
  861. //region 优惠活动
  862. String activity_title_start = null;
  863. String activity_title_end = null;
  864. //endregion
  865. //region 合计
  866. String pay_money_start = null;
  867. String pay_money_end = null;
  868. //endregion
  869. StringBuilder payStringBuilder = new StringBuilder();
  870. for (PrintModelTypeResult type : payTypes) {
  871. StringBuilder start = new StringBuilder();
  872. StringBuilder end = new StringBuilder();
  873. if (type.getWordSize().intValue() == eWordSize.Amplify.getValue()) {
  874. start.append("<B>");
  875. end.append("</B>");
  876. }
  877. if (type.getWordSize().intValue() == eWordSize.Amplify.getValue()) {
  878. start.append("<BOLD>");
  879. end.append("</BOLD>");
  880. }
  881. switch (type.getPrintField()) {
  882. case "pack_money":
  883. pack_money_start = start.toString();
  884. pack_money_end = end.toString();
  885. break;
  886. case "goods_money":
  887. goods_money_start = start.toString();
  888. goods_money_end = end.toString();
  889. break;
  890. case "coupon_money":
  891. coupon_money_start = start.toString();
  892. coupon_money_end = end.toString();
  893. break;
  894. case "errand_money":
  895. errand_money_start = start.toString();
  896. errand_money_end = end.toString();
  897. break;
  898. case "activity_title":
  899. activity_title_start = start.toString();
  900. activity_title_end = end.toString();
  901. break;
  902. case "pay_money":
  903. pay_money_start = start.toString();
  904. pay_money_end = end.toString();
  905. break;
  906. case "platform_name":
  907. payStringBuilder.append(start.toString()).append("#").append(tbOrder.getCountOrder()).append("-").append("墨轩云商").append(end.toString());
  908. break;
  909. case "shop_name":
  910. payStringBuilder.append(start.toString()).append(tbOrder.getShopName()).append(end.toString());
  911. break;
  912. case "order_type_extra":
  913. payStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(eOrderTypeExtra.stringOf(tbOrder.getOrderTypeExtra())).append(end.toString()).append("<BR>");
  914. break;
  915. case "order_number":
  916. payStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getOrderNumber()).append(end.toString()).append("<BR>");
  917. break;
  918. case "user_nick_name":
  919. payStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getUserNickName()).append(end.toString()).append("<BR>");
  920. break;
  921. case "shop_phone":
  922. payStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getShopPhone()).append(end.toString()).append("<BR>");
  923. break;
  924. case "distribution_distance_start":
  925. payStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getDistributionDistance()).append(end.toString()).append("<BR>");
  926. break;
  927. case "detailed_address":
  928. payStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getDetailedAddress()).append(end.toString()).append("<BR>");
  929. break;
  930. case "remark":
  931. payStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getRemark()).append(end.toString()).append("<BR>");
  932. break;
  933. case "shop_receiving_time":
  934. payStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getShopReceivingTime()).append(end.toString()).append("<BR>");
  935. break;
  936. case "shop_id":
  937. payStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(shopData != null ? shopData.getBusinessHours()+"-"+shopData.getLockHours() : "").append(end.toString()).append("<BR>");
  938. break;
  939. case "parent_user_id":
  940. payStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(parentUser == null ? "" : parentUser.getNickName()).append(end.toString()).append("<BR>");
  941. break;
  942. case "auto_send_order":
  943. payStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getAutoSendOrder().intValue() == 0 ? "开启" : "关闭").append(end.toString()).append("<BR>");
  944. break;
  945. case "order_code":
  946. payStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getOrderCode()).append(end.toString()).append("<BR>");
  947. break;
  948. case "pay_type":
  949. payStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getPayType().intValue() == 1 ? "微信支付" : (tbOrder.getPayType().intValue() == 2 ? "余额支付" : "支付宝支付")).append(end.toString()).append("<BR>");
  950. break;
  951. case "pay_time":
  952. payStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getPayTime()).append(end.toString()).append("<BR>");
  953. break;
  954. case "create_time":
  955. payStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getCreateTime()).append(end.toString()).append("<BR>");
  956. break;
  957. case "address":
  958. if (StringUtils.isNotEmpty(tbOrder.getAddress())) {
  959. JSONObject jsonObject = JSONObject.parseObject(tbOrder.getAddress());
  960. String addressDetail = jsonObject.getString("addressDetail");
  961. String city = jsonObject.getString("city");
  962. String district = jsonObject.getString("district");
  963. String province = jsonObject.getString("province");
  964. payStringBuilder.append(start.toString()).append(province).append(city).append(district).append(addressDetail).append(end.toString()).append("<BR>");
  965. }
  966. break;
  967. case "phone":
  968. if (StringUtils.isNotEmpty(tbOrder.getAddress())) {
  969. JSONObject jsonObject = JSONObject.parseObject(tbOrder.getAddress());
  970. String phone = jsonObject.getString("userPhone");
  971. payStringBuilder.append(start.toString()).append(phone).append(end.toString()).append("<BR>");
  972. }
  973. break;
  974. case "user_name":
  975. if (StringUtils.isNotEmpty(tbOrder.getAddress())) {
  976. JSONObject jsonObject = JSONObject.parseObject(tbOrder.getAddress());
  977. String userName = jsonObject.getString("userName");
  978. payStringBuilder.append(start.toString()).append(userName).append(end.toString()).append("<BR>");
  979. }
  980. break;
  981. case "user_id"://显示isVip和vipExpirationTime
  982. UserEntity user = null;
  983. if(tbOrder.getUserId() != null){
  984. user = userService.selectUserById(tbOrder.getUserId());
  985. }
  986. payStringBuilder.append(start.toString()).append("是否会员:").append(user == null ? "" : (user.getIsVip().intValue() == 1 ? "是" : "否")).append(end.toString()).append("<BR>");
  987. payStringBuilder.append(start.toString()).append("会员有效期:").append(user == null ? "" : user.getVipExpirationTime()).append(end.toString()).append("<BR>");
  988. break;
  989. case "coupon_id"://拿优惠券表中的优惠券名称
  990. payStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbCoupon == null ? "" : tbCoupon.getCouponName()).append(end.toString()).append("<BR>");
  991. break;
  992. case "expect_delivery_time":
  993. payStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getExpectDeliveryTime()).append(end.toString()).append("<BR>");
  994. break;
  995. default:
  996. break;
  997. }
  998. }
  999. //endregion
  1000. stringBuilder.append(pack_money_start).append("打包费:").append(packMoney).append("元").append(pack_money_end + "<BR>");
  1001. stringBuilder.append(goods_money_start).append("商品费:").append(goodsMoney).append("元").append(goods_money_end + "<BR>");
  1002. if (tbOrder.getCouponMoney() != null) {
  1003. stringBuilder.append(coupon_money_start).append("优惠券:").append("-" + tbOrder.getCouponMoney()).append("元").append(coupon_money_end + "<BR>");
  1004. } else {
  1005. stringBuilder.append(coupon_money_start).append("优惠券:").append("-0").append("元").append(coupon_money_end + "<BR>");
  1006. }
  1007. BigDecimal errandMoney = tbOrder.getErrandMoney();
  1008. if (errandMoney == null) {
  1009. errandMoney = BigDecimal.ZERO;
  1010. }
  1011. stringBuilder.append(errand_money_start).append("跑腿费:").append(errandMoney).append("元").append(errand_money_end + "<BR>");
  1012. //从这个表里拿优惠活动 activity_part_record 活动参与记录表
  1013. stringBuilder.append(activity_title_start).append("优惠活动:").append(activity == null ? "" : activity.getTitle()).append(activity_title_end + "<BR>");
  1014. stringBuilder.append(pay_money_start).append("合计:").append(tbOrder.getPayMoney()).append("元").append(pay_money_end + "<BR>");
  1015. stringBuilder.append(payStringBuilder.toString());
  1016. //endregion
  1017. stringBuilder.append("--------------------------------<BR>");
  1018. //region 用户信息:姓名 电话号 地址 订餐时间 商家联系电话
  1019. //region 字号样式
  1020. Optional<PrintModelResult> oUserInfo = resultData.stream().filter(e -> e.getId().intValue() == ePrintStatu.User.getValue()).findFirst();
  1021. List<PrintModelTypeResult> userTypes = oUserInfo.get().getTypes();
  1022. //region 姓名
  1023. String user_name_start = null;
  1024. String user_name_end = null;
  1025. //endregion
  1026. //region 电话号
  1027. String phone_start = null;
  1028. String phone_end = null;
  1029. //endregion
  1030. //region 地址
  1031. String address_detail_start = null;
  1032. String address_detail_end = null;
  1033. //endregion
  1034. //region 订餐时间
  1035. String pay_time_start = null;
  1036. String pay_time_end = null;
  1037. //endregion
  1038. StringBuilder userStringBuilder = new StringBuilder();
  1039. for (PrintModelTypeResult type : userTypes) {
  1040. StringBuilder start = new StringBuilder();
  1041. StringBuilder end = new StringBuilder();
  1042. if (type.getWordSize().intValue() == eWordSize.Amplify.getValue()) {
  1043. start.append("<B>");
  1044. end.append("</B>");
  1045. }
  1046. if (type.getWordSize().intValue() == eWordSize.Amplify.getValue()) {
  1047. start.append("<BOLD>");
  1048. end.append("</BOLD>");
  1049. }
  1050. switch (type.getPrintField()) {
  1051. case "user_name":
  1052. user_name_start = start.toString();
  1053. user_name_end = end.toString();
  1054. break;
  1055. case "phone":
  1056. phone_start = start.toString();
  1057. phone_end = end.toString();
  1058. break;
  1059. case "address_detail":
  1060. address_detail_start = start.toString();
  1061. address_detail_end = end.toString();
  1062. break;
  1063. case "pay_time":
  1064. pay_time_start = start.toString();
  1065. pay_time_end = end.toString();
  1066. break;
  1067. case "platform_name":
  1068. userStringBuilder.append(start.toString()).append("#").append(tbOrder.getCountOrder()).append("-").append("墨轩云商").append(end.toString());
  1069. break;
  1070. case "shop_name":
  1071. userStringBuilder.append(start.toString()).append(tbOrder.getShopName()).append(end.toString());
  1072. break;
  1073. case "order_type_extra":
  1074. userStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(eOrderTypeExtra.stringOf(tbOrder.getOrderTypeExtra())).append(end.toString()).append("<BR>");
  1075. break;
  1076. case "order_number":
  1077. userStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getOrderNumber()).append(end.toString()).append("<BR>");
  1078. break;
  1079. case "user_nick_name":
  1080. userStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getUserNickName()).append(end.toString()).append("<BR>");
  1081. break;
  1082. case "shop_phone":
  1083. userStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getShopPhone()).append(end.toString()).append("<BR>");
  1084. break;
  1085. case "distribution_distance_start":
  1086. userStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getDistributionDistance()).append(end.toString()).append("<BR>");
  1087. break;
  1088. case "detailed_address":
  1089. userStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getDetailedAddress()).append(end.toString()).append("<BR>");
  1090. break;
  1091. case "remark":
  1092. userStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getRemark()).append(end.toString()).append("<BR>");
  1093. break;
  1094. case "shop_receiving_time":
  1095. userStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getShopReceivingTime()).append(end.toString()).append("<BR>");
  1096. break;
  1097. case "shop_id":
  1098. userStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(shopData != null ? shopData.getBusinessHours()+"-"+shopData.getLockHours() : "").append(end.toString()).append("<BR>");
  1099. break;
  1100. case "parent_user_id":
  1101. userStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(parentUser == null ? "" : parentUser.getNickName()).append(end.toString()).append("<BR>");
  1102. break;
  1103. case "pay_money":
  1104. userStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getPayMoney()).append("元").append(end.toString()).append("<BR>");
  1105. break;
  1106. case "auto_send_order":
  1107. userStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getAutoSendOrder().intValue() == 0 ? "开启" : "关闭").append(end.toString()).append("<BR>");
  1108. break;
  1109. case "errand_money":
  1110. userStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getErrandMoney()).append("元").append(end.toString()).append("<BR>");
  1111. break;
  1112. case "pack_money":
  1113. userStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getPackMoney()).append("元").append(end.toString()).append("<BR>");
  1114. break;
  1115. case "order_code":
  1116. userStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getOrderCode()).append(end.toString()).append("<BR>");
  1117. break;
  1118. case "pay_type":
  1119. userStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getPayType().intValue() == 1 ? "微信支付" : (tbOrder.getPayType().intValue() == 2 ? "余额支付" : "支付宝支付")).append(end.toString()).append("<BR>");
  1120. break;
  1121. case "create_time":
  1122. userStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getCreateTime()).append(end.toString()).append("<BR>");
  1123. break;
  1124. case "address":
  1125. if (StringUtils.isNotEmpty(tbOrder.getAddress())) {
  1126. JSONObject jsonObject = JSONObject.parseObject(tbOrder.getAddress());
  1127. String addressDetail = jsonObject.getString("addressDetail");
  1128. String city = jsonObject.getString("city");
  1129. String district = jsonObject.getString("district");
  1130. String province = jsonObject.getString("province");
  1131. userStringBuilder.append(start.toString()).append(province).append(city).append(district).append(addressDetail).append(end.toString()).append("<BR>");
  1132. }
  1133. break;
  1134. case "user_id"://显示isVip和vipExpirationTime
  1135. UserEntity user = null;
  1136. if(tbOrder.getUserId() != null){
  1137. user = userService.selectUserById(tbOrder.getUserId());
  1138. }
  1139. userStringBuilder.append(start.toString()).append("是否会员:").append(user == null ? "" : (user.getIsVip().intValue() == 1 ? "是" : "否")).append(end.toString()).append("<BR>");
  1140. userStringBuilder.append(start.toString()).append("会员有效期:").append(user == null ? "" : user.getVipExpirationTime()).append(end.toString()).append("<BR>");
  1141. break;
  1142. case "coupon_id"://拿优惠券表中的优惠券名称
  1143. userStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbCoupon == null ? "" : tbCoupon.getCouponName()).append(end.toString()).append("<BR>");
  1144. break;
  1145. case "expect_delivery_time":
  1146. userStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(tbOrder.getExpectDeliveryTime()).append(end.toString()).append("<BR>");
  1147. break;
  1148. case "activity_title"://通过订单ID去activity_part_record表中查到activity_id后再去activity表中获取活动标题
  1149. userStringBuilder.append(start.toString()).append(type.getPrintName()+":").append(activity == null ? "" : activity.getTitle()).append(end.toString()).append("<BR>");
  1150. break;
  1151. default:
  1152. break;
  1153. }
  1154. }
  1155. //endregion
  1156. if (StringUtils.isNotEmpty(tbOrder.getAddress())) {
  1157. JSONObject jsonObject = JSONObject.parseObject(tbOrder.getAddress());
  1158. String addressDetail = jsonObject.getString("addressDetail");
  1159. String city = jsonObject.getString("city");
  1160. String district = jsonObject.getString("district");
  1161. String province = jsonObject.getString("province");
  1162. String userName = jsonObject.getString("userName");
  1163. String phone = jsonObject.getString("userPhone");
  1164. stringBuilder.append(user_name_start).append(userName).append(user_name_end).append("<BR>");
  1165. stringBuilder.append(phone_start).append(phone).append(phone_end).append("<BR>");
  1166. stringBuilder.append(address_detail_start).append(province).append(city).append(district).append(addressDetail).append(address_detail_end).append("<BR>");
  1167. } else {
  1168. stringBuilder.append(phone_start).append(tbOrder.getPhone()).append(phone_end).append("<BR>");
  1169. }
  1170. stringBuilder.append(pay_time_start).append("订餐时间:").append(tbOrder.getPayTime()).append(pay_time_end).append("<BR>");
  1171. stringBuilder.append(userStringBuilder);
  1172. stringBuilder.append("<BR>").append("<BR>").append("<BR>");
  1173. stringBuilder.append("<CUT>");
  1174. //endregion
  1175. }
  1176. String content = stringBuilder.toString();
  1177. //通过POST请求,发送打印信息到服务器
  1178. RequestConfig requestConfig = RequestConfig.custom()
  1179. .setSocketTimeout(30000)//读取超时
  1180. .setConnectTimeout(30000)//连接超时
  1181. .build();
  1182. CloseableHttpClient httpClient = HttpClients.custom()
  1183. .setDefaultRequestConfig(requestConfig)
  1184. .build();
  1185. String URL = commonInfoService.findOne(325).getValue();
  1186. String USER = commonInfoService.findOne(326).getValue();
  1187. String UKEY = commonInfoService.findOne(327).getValue();
  1188. HttpPost post = new HttpPost(URL);
  1189. List<NameValuePair> nvps = new ArrayList<NameValuePair>();
  1190. nvps.add(new BasicNameValuePair("user", USER));
  1191. String STIME = String.valueOf(System.currentTimeMillis() / 1000);
  1192. nvps.add(new BasicNameValuePair("stime", STIME));
  1193. nvps.add(new BasicNameValuePair("sig", signature(USER, UKEY, STIME)));
  1194. nvps.add(new BasicNameValuePair("apiname", "Open_printMsg"));//固定值,不需要修改
  1195. nvps.add(new BasicNameValuePair("sn", sn));
  1196. nvps.add(new BasicNameValuePair("content", content));
  1197. nvps.add(new BasicNameValuePair("times", "1"));//打印联数
  1198. CloseableHttpResponse response = null;
  1199. String result = null;
  1200. try {
  1201. post.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
  1202. response = httpClient.execute(post);
  1203. int statecode = response.getStatusLine().getStatusCode();
  1204. if (statecode == 200) {
  1205. HttpEntity httpentity = response.getEntity();
  1206. if (httpentity != null) {
  1207. //服务器返回的JSON字符串,建议要当做日志记录起来
  1208. result = EntityUtils.toString(httpentity);
  1209. }
  1210. }
  1211. } catch (Exception e) {
  1212. e.printStackTrace();
  1213. } finally {
  1214. try {
  1215. if (response != null) {
  1216. response.close();
  1217. }
  1218. } catch (IOException e) {
  1219. e.printStackTrace();
  1220. }
  1221. try {
  1222. post.abort();
  1223. } catch (Exception e) {
  1224. e.printStackTrace();
  1225. }
  1226. try {
  1227. httpClient.close();
  1228. } catch (IOException e) {
  1229. e.printStackTrace();
  1230. }
  1231. }
  1232. log.error("打印返回值:" + result);
  1233. return result;
  1234. } catch (Exception e) {
  1235. e.printStackTrace();
  1236. log.error("打印异常:" + e.getMessage(), e);
  1237. }
  1238. return null;
  1239. }
  1240. /**
  1241. * 小票机打印订单接口
  1242. *
  1243. * @param sn 打印机编号
  1244. * @return 成功:{"msg":"ok","ret":0,"data":"xxxxxxx_xxxxxxxx_xxxxxxxx","serverExecutedTime":5}
  1245. * 失败:{"msg":"错误描述","ret":非0,"data":"null","serverExecutedTime":5}
  1246. */
  1247. public static String testPrint(String sn) {
  1248. try {
  1249. //标签说明:
  1250. //单标签:
  1251. //"<BR>"为换行,"<CUT>"为切刀指令(主动切纸,仅限切刀打印机使用才有效果)
  1252. //"<LOGO>"为打印LOGO指令(前提是预先在机器内置LOGO图片),"<PLUGIN>"为钱箱或者外置音响指令
  1253. //成对标签:
  1254. //"<CB></CB>"为居中放大一倍,"<B></B>"为放大一倍,"<C></C>"为居中,<L></L>字体变高一倍
  1255. //<W></W>字体变宽一倍,"<QR></QR>"为二维码,"<BOLD></BOLD>"为字体加粗,""为右对齐
  1256. //拼凑订单内容时可参考如下格式
  1257. //根据打印纸张的宽度,自行调整内容的格式,可参考下面的样例格式
  1258. //region 以前的代码
  1259. String content = "<CB>测试打印</CB><BR>";
  1260. content += "名称      单价 数量 金额<BR>";
  1261. content += "--------------------------------<BR>";
  1262. content += "<B>饭       1.0 1 1.0</B><BR>";
  1263. content += "<BOLD>炒饭      10.0 10 10.0</BOLD><BR>";
  1264. content += "<B><BOLD>蛋炒饭     10.0 10 100.0</B></BOLD><BR>";
  1265. content += "<B><BOLD>蛋炒饭     10.0 10 100.0</BOLD></B><BR>";
  1266. content += "鸡蛋炒饭    100.0 1 100.0<BR>";
  1267. content += "番茄蛋炒饭   1000.0 1 100.0<BR>";
  1268. content += "西红柿蛋炒饭  1000.0 1 100.0<BR>";
  1269. content += "西红柿鸡蛋炒饭 100.0 10 100.0<BR>";
  1270. content += "备注:加辣<BR>";
  1271. content += "--------------------------------<BR>";
  1272. content += "合计:xx.0元<BR>";
  1273. content += "送货地点:广州市南沙区xx路xx号<BR>";
  1274. content += "联系电话:13888888888888<BR>";
  1275. content += "订餐时间:2016-08-08 08:08:08<BR>";
  1276. content += "<QR>http://www.dzist.com</QR>";
  1277. //endregion
  1278. StringBuilder stringBuilder = new StringBuilder();
  1279. //region 以前的代码
  1280. /*stringBuilder.append("<BR>").append("<BR>").append("<BR>");
  1281. stringBuilder.append("<CB>").append(tbOrder.getShopName()).append("</CB><BR>");
  1282. if(tbOrder.getOrderType()==1){
  1283. stringBuilder.append("<BOLD>类型:").append("到店取餐</BOLD><BR>");
  1284. stringBuilder.append("<BOLD>取餐号:").append(tbOrder.getOrderCode()).append("</BOLD><BR>");
  1285. }else{
  1286. stringBuilder.append("<BOLD>类型:").append("外卖配送</BOLD><BR>");
  1287. }
  1288. stringBuilder.append("<BOLD>订单号:").append(tbOrder.getOrderNumber()).append("</BOLD><BR>");
  1289. stringBuilder.append("--------------------------------<BR>");
  1290. stringBuilder.append("名称    单价  数量  金额 <BR>");
  1291. stringBuilder.append("--------------------------------<BR>");
  1292. stringBuilder.append("<BR>");
  1293. for(OrderGoods orderGoods:orderGoodsList){
  1294. BigDecimal sumMoney = orderGoods.getGoodsPrice().multiply(BigDecimal.valueOf(orderGoods.getGoodsNum()));
  1295. String name;
  1296. if(StringUtils.isNotEmpty(orderGoods.getSkuMessage())){
  1297. name=orderGoods.getGoodsName()+"("+orderGoods.getSkuMessage()+")";
  1298. }else{
  1299. name=orderGoods.getGoodsName();
  1300. }
  1301. stringBuilder.append(name).append("<BR>");
  1302. stringBuilder.append("      ");
  1303. stringBuilder.append(orderGoods.getGoodsPrice());
  1304. if(orderGoods.getGoodsPrice().doubleValue()<10){
  1305. stringBuilder.append("  ");
  1306. }else if(orderGoods.getGoodsPrice().doubleValue()<100){
  1307. stringBuilder.append("  ");
  1308. }else{
  1309. stringBuilder.append("  ");
  1310. }
  1311. stringBuilder.append(orderGoods.getGoodsNum());
  1312. stringBuilder.append("  ");
  1313. stringBuilder.append(sumMoney).append("<BR>");
  1314. stringBuilder.append("<BR>");
  1315. }
  1316. stringBuilder.append("--------------------------------<BR>");
  1317. stringBuilder.append("<BOLD>备注:").append(remark).append("</BOLD><BR>");
  1318. stringBuilder.append("--------------------------------<BR>");
  1319. stringBuilder.append("打包费:").append(packMoney).append("元<BR>");
  1320. stringBuilder.append("商品费:").append(goodsMoney).append("元<BR>");
  1321. if(tbOrder.getCouponMoney()!=null){
  1322. stringBuilder.append("优惠券:-").append(tbOrder.getCouponMoney()).append("元<BR>");
  1323. }
  1324. stringBuilder.append("跑腿费:").append(errandMoney).append("元<BR>");
  1325. stringBuilder.append("合计:").append(tbOrder.getPayMoney()).append("元<BR>");
  1326. stringBuilder.append("--------------------------------<BR>");
  1327. if(StringUtils.isNotEmpty(tbOrder.getAddress())){
  1328. JSONObject jsonObject = JSONObject.parseObject(tbOrder.getAddress());
  1329. String addressDetail = jsonObject.getString("addressDetail");
  1330. String city = jsonObject.getString("city");
  1331. String district = jsonObject.getString("district");
  1332. String province = jsonObject.getString("province");
  1333. String userName = jsonObject.getString("userName");
  1334. String phone = jsonObject.getString("userPhone");
  1335. stringBuilder.append("姓名:").append(userName).append("<BR>");
  1336. stringBuilder.append("联系电话:").append(phone).append("<BR>");
  1337. stringBuilder.append("送货地点:").append(province).append(city).append(district).append(addressDetail).append("<BR>");
  1338. }else{
  1339. stringBuilder.append("联系电话:").append(tbOrder.getPhone()).append("<BR>");
  1340. }
  1341. stringBuilder.append("订餐时间:").append(tbOrder.getPayTime()).append("<BR>");*/
  1342. //endregion
  1343. //通过POST请求,发送打印信息到服务器
  1344. RequestConfig requestConfig = RequestConfig.custom()
  1345. .setSocketTimeout(30000)//读取超时
  1346. .setConnectTimeout(30000)//连接超时
  1347. .build();
  1348. CloseableHttpClient httpClient = HttpClients.custom()
  1349. .setDefaultRequestConfig(requestConfig)
  1350. .build();
  1351. String URL = commonInfoService.findOne(325).getValue();
  1352. String USER = commonInfoService.findOne(326).getValue();
  1353. String UKEY = commonInfoService.findOne(327).getValue();
  1354. HttpPost post = new HttpPost(URL);
  1355. List<NameValuePair> nvps = new ArrayList<NameValuePair>();
  1356. nvps.add(new BasicNameValuePair("user", USER));
  1357. String STIME = String.valueOf(System.currentTimeMillis() / 1000);
  1358. nvps.add(new BasicNameValuePair("stime", STIME));
  1359. nvps.add(new BasicNameValuePair("sig", signature(USER, UKEY, STIME)));
  1360. nvps.add(new BasicNameValuePair("apiname", "Open_printMsg"));//固定值,不需要修改
  1361. nvps.add(new BasicNameValuePair("sn", sn));
  1362. nvps.add(new BasicNameValuePair("content", content));
  1363. nvps.add(new BasicNameValuePair("times", "1"));//打印联数
  1364. CloseableHttpResponse response = null;
  1365. String result = null;
  1366. try {
  1367. post.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
  1368. response = httpClient.execute(post);
  1369. int statecode = response.getStatusLine().getStatusCode();
  1370. if (statecode == 200) {
  1371. HttpEntity httpentity = response.getEntity();
  1372. if (httpentity != null) {
  1373. //服务器返回的JSON字符串,建议要当做日志记录起来
  1374. result = EntityUtils.toString(httpentity);
  1375. }
  1376. }
  1377. } catch (Exception e) {
  1378. e.printStackTrace();
  1379. } finally {
  1380. try {
  1381. if (response != null) {
  1382. response.close();
  1383. }
  1384. } catch (IOException e) {
  1385. e.printStackTrace();
  1386. }
  1387. try {
  1388. post.abort();
  1389. } catch (Exception e) {
  1390. e.printStackTrace();
  1391. }
  1392. try {
  1393. httpClient.close();
  1394. } catch (IOException e) {
  1395. e.printStackTrace();
  1396. }
  1397. }
  1398. log.info("打印返回值:" + result);
  1399. return result;
  1400. } catch (Exception e) {
  1401. e.printStackTrace();
  1402. log.error("打印异常:" + e.getMessage(), e);
  1403. }
  1404. return null;
  1405. }
  1406. /**
  1407. * 标签机专用打印订单接口
  1408. *
  1409. * @param sn 打印机编号
  1410. * @return 成功:{"msg":"ok","ret":0,"data":"xxxxxxx_xxxxxxxx_xxxxxxxx","serverExecutedTime":5}
  1411. * 失败:{"msg":"错误描述","ret":非0,"data":"null","serverExecutedTime":5}
  1412. */
  1413. private static String printLabelMsg(String sn) {
  1414. String content;
  1415. content = "<DIRECTION>1</DIRECTION>";//设定打印时出纸和打印字体的方向,n 0 或 1,每次设备重启后都会初始化为 0 值设置,1:正向出纸,0:反向出纸,
  1416. content += "<TEXT x='9' y='10' font='12' w='1' h='2' r='0'>#001 五号桌 1/3</TEXT><TEXT x='80' y='80' font='12' w='2' h='2' r='0'>可乐鸡翅</TEXT><TEXT x='9' y='180' font='12' w='1' h='1' r='0'>张三先生 13800138000</TEXT>";//40mm宽度标签纸打印例子,打开注释调用标签打印接口打印
  1417. //通过POST请求,发送打印信息到服务器
  1418. RequestConfig requestConfig = RequestConfig.custom()
  1419. .setSocketTimeout(30000)//读取超时
  1420. .setConnectTimeout(30000)//连接超时
  1421. .build();
  1422. CloseableHttpClient httpClient = HttpClients.custom()
  1423. .setDefaultRequestConfig(requestConfig)
  1424. .build();
  1425. String URL = commonInfoService.findOne(325).getValue();
  1426. String USER = commonInfoService.findOne(326).getValue();
  1427. String UKEY = commonInfoService.findOne(327).getValue();
  1428. HttpPost post = new HttpPost(URL);
  1429. List<NameValuePair> nvps = new ArrayList<NameValuePair>();
  1430. nvps.add(new BasicNameValuePair("user", USER));
  1431. String STIME = String.valueOf(System.currentTimeMillis() / 1000);
  1432. nvps.add(new BasicNameValuePair("stime", STIME));
  1433. nvps.add(new BasicNameValuePair("sig", signature(USER, UKEY, STIME)));
  1434. nvps.add(new BasicNameValuePair("apiname", "Open_printLabelMsg"));//固定值,不需要修改
  1435. nvps.add(new BasicNameValuePair("sn", sn));
  1436. nvps.add(new BasicNameValuePair("content", content));
  1437. nvps.add(new BasicNameValuePair("times", "1"));//打印联数
  1438. CloseableHttpResponse response = null;
  1439. String result = null;
  1440. try {
  1441. post.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
  1442. response = httpClient.execute(post);
  1443. int statecode = response.getStatusLine().getStatusCode();
  1444. if (statecode == 200) {
  1445. HttpEntity httpentity = response.getEntity();
  1446. if (httpentity != null) {
  1447. //服务器返回的JSON字符串,建议要当做日志记录起来
  1448. result = EntityUtils.toString(httpentity);
  1449. }
  1450. }
  1451. } catch (Exception e) {
  1452. e.printStackTrace();
  1453. } finally {
  1454. try {
  1455. if (response != null) {
  1456. response.close();
  1457. }
  1458. } catch (IOException e) {
  1459. e.printStackTrace();
  1460. }
  1461. try {
  1462. post.abort();
  1463. } catch (Exception e) {
  1464. e.printStackTrace();
  1465. }
  1466. try {
  1467. httpClient.close();
  1468. } catch (IOException e) {
  1469. e.printStackTrace();
  1470. }
  1471. }
  1472. return result;
  1473. }
  1474. /**
  1475. * 查询某订单是否打印成功
  1476. *
  1477. * @param orderid 订单id
  1478. * @return 成功:{"msg":"ok","ret":0,"data":true,"serverExecutedTime":2}//data:true为已打印,false为未打印
  1479. * 失败:{"msg":"错误描述","ret":非0, "data":null,"serverExecutedTime":7}
  1480. */
  1481. private static String queryOrderState(String orderid) {
  1482. //通过POST请求,发送打印信息到服务器
  1483. RequestConfig requestConfig = RequestConfig.custom()
  1484. .setSocketTimeout(30000)//读取超时
  1485. .setConnectTimeout(30000)//连接超时
  1486. .build();
  1487. CloseableHttpClient httpClient = HttpClients.custom()
  1488. .setDefaultRequestConfig(requestConfig)
  1489. .build();
  1490. String URL = commonInfoService.findOne(325).getValue();
  1491. String USER = commonInfoService.findOne(326).getValue();
  1492. String UKEY = commonInfoService.findOne(327).getValue();
  1493. HttpPost post = new HttpPost(URL);
  1494. List<NameValuePair> nvps = new ArrayList<NameValuePair>();
  1495. nvps.add(new BasicNameValuePair("user", USER));
  1496. String STIME = String.valueOf(System.currentTimeMillis() / 1000);
  1497. nvps.add(new BasicNameValuePair("stime", STIME));
  1498. nvps.add(new BasicNameValuePair("sig", signature(USER, UKEY, STIME)));
  1499. nvps.add(new BasicNameValuePair("apiname", "Open_queryOrderState"));//固定值,不需要修改
  1500. nvps.add(new BasicNameValuePair("orderid", orderid));
  1501. CloseableHttpResponse response = null;
  1502. String result = null;
  1503. try {
  1504. post.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
  1505. response = httpClient.execute(post);
  1506. int statecode = response.getStatusLine().getStatusCode();
  1507. if (statecode == 200) {
  1508. HttpEntity httpentity = response.getEntity();
  1509. if (httpentity != null) {
  1510. //服务器返回
  1511. result = EntityUtils.toString(httpentity);
  1512. }
  1513. }
  1514. } catch (Exception e) {
  1515. e.printStackTrace();
  1516. } finally {
  1517. try {
  1518. if (response != null) {
  1519. response.close();
  1520. }
  1521. } catch (IOException e) {
  1522. e.printStackTrace();
  1523. }
  1524. try {
  1525. post.abort();
  1526. } catch (Exception e) {
  1527. e.printStackTrace();
  1528. }
  1529. try {
  1530. httpClient.close();
  1531. } catch (IOException e) {
  1532. e.printStackTrace();
  1533. }
  1534. }
  1535. return result;
  1536. }
  1537. /**
  1538. * 查询指定打印机某天的订单详情
  1539. *
  1540. * @param sn 打印机编号
  1541. * @param strdate 时间 "yyyy-MM-dd"
  1542. * @return 成功:{"msg":"ok","ret":0,"data":{"print":6,"waiting":1},"serverExecutedTime":9}//print已打印,waiting为打印
  1543. * 失败:{"msg":"错误描述","ret":非0,"data":"null","serverExecutedTime":5}
  1544. */
  1545. private static String queryOrderInfoByDate(String sn, String strdate) {
  1546. //通过POST请求,发送打印信息到服务器
  1547. RequestConfig requestConfig = RequestConfig.custom()
  1548. .setSocketTimeout(30000)//读取超时
  1549. .setConnectTimeout(30000)//连接超时
  1550. .build();
  1551. CloseableHttpClient httpClient = HttpClients.custom()
  1552. .setDefaultRequestConfig(requestConfig)
  1553. .build();
  1554. String URL = commonInfoService.findOne(325).getValue();
  1555. String USER = commonInfoService.findOne(326).getValue();
  1556. String UKEY = commonInfoService.findOne(327).getValue();
  1557. HttpPost post = new HttpPost(URL);
  1558. List<NameValuePair> nvps = new ArrayList<NameValuePair>();
  1559. nvps.add(new BasicNameValuePair("user", USER));
  1560. String STIME = String.valueOf(System.currentTimeMillis() / 1000);
  1561. nvps.add(new BasicNameValuePair("stime", STIME));
  1562. nvps.add(new BasicNameValuePair("sig", signature(USER, UKEY, STIME)));
  1563. nvps.add(new BasicNameValuePair("apiname", "Open_queryOrderInfoByDate"));//固定值,不需要修改
  1564. nvps.add(new BasicNameValuePair("sn", sn));
  1565. nvps.add(new BasicNameValuePair("date", strdate));//yyyy-MM-dd格式
  1566. CloseableHttpResponse response = null;
  1567. String result = null;
  1568. try {
  1569. post.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
  1570. response = httpClient.execute(post);
  1571. int statecode = response.getStatusLine().getStatusCode();
  1572. if (statecode == 200) {
  1573. HttpEntity httpentity = response.getEntity();
  1574. if (httpentity != null) {
  1575. //服务器返回
  1576. result = EntityUtils.toString(httpentity);
  1577. }
  1578. }
  1579. } catch (Exception e) {
  1580. e.printStackTrace();
  1581. } finally {
  1582. try {
  1583. if (response != null) {
  1584. response.close();
  1585. }
  1586. } catch (IOException e) {
  1587. e.printStackTrace();
  1588. }
  1589. try {
  1590. post.abort();
  1591. } catch (Exception e) {
  1592. e.printStackTrace();
  1593. }
  1594. try {
  1595. httpClient.close();
  1596. } catch (IOException e) {
  1597. e.printStackTrace();
  1598. }
  1599. }
  1600. return result;
  1601. }
  1602. /**
  1603. * 查询打印机的状态
  1604. *
  1605. * @param sn 打印机编号
  1606. * @return 成功:{"msg":"ok","ret":0,"data":"状态","serverExecutedTime":4}
  1607. * 失败:{"msg":"错误描述","ret":非0,"data":"null","serverExecutedTime":5}
  1608. */
  1609. private static String queryPrinterStatus(String sn) {
  1610. //通过POST请求,发送打印信息到服务器
  1611. RequestConfig requestConfig = RequestConfig.custom()
  1612. .setSocketTimeout(30000)//读取超时
  1613. .setConnectTimeout(30000)//连接超时
  1614. .build();
  1615. CloseableHttpClient httpClient = HttpClients.custom()
  1616. .setDefaultRequestConfig(requestConfig)
  1617. .build();
  1618. String URL = commonInfoService.findOne(325).getValue();
  1619. String USER = commonInfoService.findOne(326).getValue();
  1620. String UKEY = commonInfoService.findOne(327).getValue();
  1621. HttpPost post = new HttpPost(URL);
  1622. List<NameValuePair> nvps = new ArrayList<NameValuePair>();
  1623. nvps.add(new BasicNameValuePair("user", USER));
  1624. String STIME = String.valueOf(System.currentTimeMillis() / 1000);
  1625. nvps.add(new BasicNameValuePair("stime", STIME));
  1626. nvps.add(new BasicNameValuePair("sig", signature(USER, UKEY, STIME)));
  1627. nvps.add(new BasicNameValuePair("apiname", "Open_queryPrinterStatus"));//固定值,不需要修改
  1628. nvps.add(new BasicNameValuePair("sn", sn));
  1629. CloseableHttpResponse response = null;
  1630. String result = null;
  1631. try {
  1632. post.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
  1633. response = httpClient.execute(post);
  1634. int statecode = response.getStatusLine().getStatusCode();
  1635. if (statecode == 200) {
  1636. HttpEntity httpentity = response.getEntity();
  1637. if (httpentity != null) {
  1638. //服务器返回
  1639. result = EntityUtils.toString(httpentity);
  1640. }
  1641. }
  1642. } catch (Exception e) {
  1643. e.printStackTrace();
  1644. } finally {
  1645. try {
  1646. if (response != null) {
  1647. response.close();
  1648. }
  1649. } catch (IOException e) {
  1650. e.printStackTrace();
  1651. }
  1652. try {
  1653. post.abort();
  1654. } catch (Exception e) {
  1655. e.printStackTrace();
  1656. }
  1657. try {
  1658. httpClient.close();
  1659. } catch (IOException e) {
  1660. e.printStackTrace();
  1661. }
  1662. }
  1663. return result;
  1664. }
  1665. //生成签名字符串
  1666. private static String signature(String USER, String UKEY, String STIME) {
  1667. String s = DigestUtils.sha1Hex(USER + UKEY + STIME);
  1668. return s;
  1669. }
  1670. }