PrinterServiceImpl.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. package com.fuint.common.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  4. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  5. import com.fuint.common.dto.GoodsSpecValueDto;
  6. import com.fuint.common.dto.OrderGoodsDto;
  7. import com.fuint.common.dto.UserOrderDto;
  8. import com.fuint.common.enums.*;
  9. import com.fuint.common.service.SettingService;
  10. import com.fuint.common.util.HashSignUtil;
  11. import com.fuint.common.util.NoteFormatter;
  12. import com.fuint.common.util.PrinterUtil;
  13. import com.fuint.common.vo.printer.*;
  14. import com.fuint.framework.annoation.OperationServiceLog;
  15. import com.fuint.framework.exception.BusinessCheckException;
  16. import com.fuint.framework.pagination.PaginationRequest;
  17. import com.fuint.framework.pagination.PaginationResponse;
  18. import com.fuint.repository.model.MtPrinter;
  19. import com.fuint.common.service.PrinterService;
  20. import com.fuint.repository.mapper.MtPrinterMapper;
  21. import com.fuint.repository.model.MtSetting;
  22. import com.fuint.repository.model.MtStore;
  23. import com.fuint.utils.StringUtil;
  24. import com.github.pagehelper.PageHelper;
  25. import lombok.AllArgsConstructor;
  26. import org.apache.commons.lang.StringUtils;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;
  29. import com.github.pagehelper.Page;
  30. import org.springframework.beans.BeanUtils;
  31. import org.springframework.core.env.Environment;
  32. import org.springframework.data.domain.PageImpl;
  33. import org.springframework.data.domain.PageRequest;
  34. import org.springframework.stereotype.Service;
  35. import org.springframework.transaction.annotation.Transactional;
  36. import java.util.*;
  37. /**
  38. * 打印机服务接口
  39. *
  40. * Created by FSQ
  41. * CopyRight https://www.fuint.cn
  42. */
  43. @Service
  44. @AllArgsConstructor
  45. public class PrinterServiceImpl extends ServiceImpl<MtPrinterMapper, MtPrinter> implements PrinterService {
  46. private static final Logger logger = LoggerFactory.getLogger(PrinterServiceImpl.class);
  47. private MtPrinterMapper mtPrinterMapper;
  48. /**
  49. * 系统配置服务接口
  50. * */
  51. private SettingService settingService;
  52. /**
  53. * 环境变量
  54. * */
  55. private Environment env;
  56. /**
  57. * 分页查询数据列表
  58. *
  59. * @param paginationRequest
  60. * @return
  61. */
  62. @Override
  63. public PaginationResponse<MtPrinter> queryPrinterListByPagination(PaginationRequest paginationRequest) {
  64. Page<MtPrinter> pageHelper = PageHelper.startPage(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
  65. LambdaQueryWrapper<MtPrinter> lambdaQueryWrapper = Wrappers.lambdaQuery();
  66. lambdaQueryWrapper.ne(MtPrinter::getStatus, StatusEnum.DISABLE.getKey());
  67. String status = paginationRequest.getSearchParams().get("status") == null ? "" : paginationRequest.getSearchParams().get("status").toString();
  68. if (StringUtils.isNotBlank(status)) {
  69. lambdaQueryWrapper.eq(MtPrinter::getStatus, status);
  70. }
  71. String merchantId = paginationRequest.getSearchParams().get("merchantId") == null ? "" : paginationRequest.getSearchParams().get("merchantId").toString();
  72. if (StringUtils.isNotBlank(merchantId)) {
  73. lambdaQueryWrapper.eq(MtPrinter::getMerchantId, merchantId);
  74. }
  75. String storeId = paginationRequest.getSearchParams().get("storeId") == null ? "" : paginationRequest.getSearchParams().get("storeId").toString();
  76. if (StringUtils.isNotBlank(storeId)) {
  77. lambdaQueryWrapper.and(wq -> wq
  78. .eq(MtPrinter::getStoreId, 0)
  79. .or()
  80. .eq(MtPrinter::getStoreId, storeId));
  81. }
  82. String sn = paginationRequest.getSearchParams().get("sn") == null ? "" : paginationRequest.getSearchParams().get("sn").toString();
  83. if (StringUtils.isNotBlank(sn)) {
  84. lambdaQueryWrapper.eq(MtPrinter::getSn, sn);
  85. }
  86. String name = paginationRequest.getSearchParams().get("name") == null ? "" : paginationRequest.getSearchParams().get("name").toString();
  87. if (StringUtils.isNotBlank(name)) {
  88. lambdaQueryWrapper.eq(MtPrinter::getName, name);
  89. }
  90. String autoPrint = paginationRequest.getSearchParams().get("autoPrint") == null ? "" : paginationRequest.getSearchParams().get("autoPrint").toString();
  91. if (StringUtils.isNotBlank(autoPrint)) {
  92. lambdaQueryWrapper.eq(MtPrinter::getAutoPrint, autoPrint);
  93. }
  94. lambdaQueryWrapper.orderByAsc(MtPrinter::getId);
  95. List<MtPrinter> dataList = mtPrinterMapper.selectList(lambdaQueryWrapper);
  96. PageRequest pageRequest = PageRequest.of(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
  97. PageImpl pageImpl = new PageImpl(dataList, pageRequest, pageHelper.getTotal());
  98. PaginationResponse<MtPrinter> paginationResponse = new PaginationResponse(pageImpl, MtPrinter.class);
  99. paginationResponse.setTotalPages(pageHelper.getPages());
  100. paginationResponse.setTotalElements(pageHelper.getTotal());
  101. paginationResponse.setContent(dataList);
  102. return paginationResponse;
  103. }
  104. /**
  105. * 添加打印机
  106. *
  107. * @param mtPrinter 打印机信息
  108. * @return
  109. */
  110. @Override
  111. @Transactional(rollbackFor = Exception.class)
  112. @OperationServiceLog(description = "新增打印机")
  113. public MtPrinter addPrinter(MtPrinter mtPrinter) throws BusinessCheckException {
  114. mtPrinter.setStatus(StatusEnum.ENABLED.getKey());
  115. mtPrinter.setUpdateTime(new Date());
  116. mtPrinter.setCreateTime(new Date());
  117. if (mtPrinter.getMerchantId() == null || mtPrinter.getMerchantId() < 1) {
  118. throw new BusinessCheckException("平台方帐号无法执行该操作,请使用商户帐号操作");
  119. }
  120. Integer printerId = mtPrinterMapper.insert(mtPrinter);
  121. if (printerId > 0) {
  122. // 添加云打印机
  123. if (mtPrinter.getSn() != null && mtPrinter.getName() != null) {
  124. AddPrinterRequest restRequest = new AddPrinterRequest();
  125. createRequestHeader(mtPrinter.getMerchantId(), restRequest);
  126. AddPrinterRequestItem item = new AddPrinterRequestItem();
  127. item.setName(mtPrinter.getName());
  128. item.setSn(mtPrinter.getSn());
  129. AddPrinterRequestItem[] items = { item };
  130. restRequest.setItems(items);
  131. PrinterUtil.addPrinters(restRequest);
  132. }
  133. return mtPrinter;
  134. } else {
  135. logger.error("新增打印机数据失败.");
  136. throw new BusinessCheckException("新增打印机数据失败");
  137. }
  138. }
  139. /**
  140. * 打印订单
  141. *
  142. * @param orderInfo 订单信息
  143. * @param autoPrint 自动打印
  144. * @return
  145. * */
  146. @Override
  147. public Boolean printOrder(UserOrderDto orderInfo, boolean autoPrint) throws Exception {
  148. PrintRequest printRequest = new PrintRequest();
  149. createRequestHeader(orderInfo.getMerchantId(), printRequest);
  150. if (orderInfo.getStoreInfo() == null) {
  151. throw new BusinessCheckException("打印失败:订单所属店铺信息为空!");
  152. }
  153. // 获取打印机列表
  154. Map<String, Object> params = new HashMap<>();
  155. params.put("storeId", orderInfo.getStoreInfo().getId());
  156. params.put("status", StatusEnum.ENABLED.getKey());
  157. if (autoPrint) {
  158. params.put("autoPrint", YesOrNoEnum.YES.getKey());
  159. }
  160. List<MtPrinter> printers = queryPrinterListByParams(params);
  161. if (printers == null || printers.size() < 1) {
  162. throw new BusinessCheckException("打印失败:该店铺还没有添加云打印机!");
  163. }
  164. MtStore storeInfo = orderInfo.getStoreInfo();
  165. for (MtPrinter mtPrinter : printers) {
  166. printRequest.setSn(mtPrinter.getSn());
  167. StringBuilder printContent = new StringBuilder();
  168. printContent.append("<C>").append("<B>" + storeInfo.getName() + "</B>").append("<BR></C>");
  169. printContent.append("<BR>");
  170. // 分割线
  171. printContent.append(org.apache.commons.lang3.StringUtils.repeat("-", 32)).append("<BR>");
  172. // 订单号
  173. printContent.append("<L>订单号:").append(orderInfo.getOrderSn()).append("</L>");
  174. // 分割线
  175. printContent.append(org.apache.commons.lang3.StringUtils.repeat("-", 32)).append("<BR>");
  176. printContent.append("品名").append(org.apache.commons.lang3.StringUtils.repeat(" ", 16))
  177. .append("数量").append(org.apache.commons.lang3.StringUtils.repeat(" ", 2))
  178. .append("单价").append(org.apache.commons.lang3.StringUtils.repeat(" ", 2))
  179. .append("<BR>");
  180. // 分割线
  181. printContent.append(org.apache.commons.lang3.StringUtils.repeat("-", 32)).append("<BR>");
  182. // 商品列表
  183. if (orderInfo.getGoods() != null && orderInfo.getGoods().size() > 0) {
  184. for (OrderGoodsDto goodsDto : orderInfo.getGoods()) {
  185. List<GoodsSpecValueDto> specList = goodsDto.getSpecList();
  186. String name = goodsDto.getName();
  187. List<String> specValue = new ArrayList<>();
  188. if (specList != null && specList.size() > 0) {
  189. for (GoodsSpecValueDto spec : specList) {
  190. if (StringUtil.isNotEmpty(spec.getSpecValue())) {
  191. specValue.add(spec.getSpecValue());
  192. }
  193. }
  194. if (specValue.size() > 0) {
  195. name = name + "(" + String.join(",", specValue) + ")";
  196. }
  197. }
  198. printContent.append(NoteFormatter.formatPrintOrderItemForNewLine80(name, goodsDto.getNum(), Double.parseDouble(goodsDto.getPrice())));
  199. }
  200. }
  201. // 配送订单,打印配送信息
  202. if (orderInfo.getOrderMode().equals(OrderModeEnum.EXPRESS.getKey())) {
  203. // 分割线
  204. printContent.append(org.apache.commons.lang3.StringUtils.repeat("-", 32)).append("<BR>");
  205. printContent.append("<L>")
  206. .append("配送姓名:").append(orderInfo.getAddress().getName()).append("<BR>")
  207. .append("联系电话:").append(orderInfo.getAddress().getMobile()).append("<BR>")
  208. .append("详细地址:").append(orderInfo.getAddress().getProvinceName() + orderInfo.getAddress().getCityName() + orderInfo.getAddress().getRegionName() + orderInfo.getAddress().getDetail()).append("<BR>");
  209. }
  210. // 分割线
  211. printContent.append(org.apache.commons.lang3.StringUtils.repeat("-", 32)).append("<BR>");
  212. printContent.append("<R>").append("合计:").append(orderInfo.getPayAmount()).append("元").append("<BR></R>");
  213. printContent.append("<BR>");
  214. printContent.append("<L>")
  215. .append("店铺地址:").append((orderInfo.getStoreInfo().getAddress() == null) ? "无" : orderInfo.getStoreInfo().getAddress()).append("<BR>")
  216. .append("联系电话:").append((orderInfo.getStoreInfo().getPhone() == null) ? "无" : orderInfo.getStoreInfo().getPhone()).append("<BR>")
  217. .append("下单时间:").append(orderInfo.getCreateTime()).append("<BR>")
  218. .append("订单备注:").append(StringUtil.isEmpty(orderInfo.getRemark()) ? "无" : orderInfo.getRemark()).append("<BR>");
  219. // 网站二维码
  220. String webSite = env.getProperty("website.url");
  221. if (StringUtil.isNotEmpty(webSite)) {
  222. printContent.append("<C>").append("<QR>" + webSite + "</QR>").append("</C>");
  223. }
  224. printRequest.setContent(printContent.toString());
  225. printRequest.setCopies(1);
  226. printRequest.setVoice(2);
  227. printRequest.setMode(0);
  228. ObjectRestResponse<String> result = PrinterUtil.print(printRequest);
  229. if (result != null && result.getCode() != 0) {
  230. throw new BusinessCheckException("打印失败:" + result.getMsg());
  231. }
  232. }
  233. return true;
  234. }
  235. /**
  236. * 根据ID获打印机取息
  237. *
  238. * @param id 打印机ID
  239. * @return
  240. */
  241. @Override
  242. public MtPrinter queryPrinterById(Integer id) {
  243. return mtPrinterMapper.selectById(id);
  244. }
  245. /**
  246. * 根据ID删除打印机
  247. *
  248. * @param id 打印机ID
  249. * @param operator 操作人
  250. * @return
  251. */
  252. @Override
  253. @Transactional(rollbackFor = Exception.class)
  254. @OperationServiceLog(description = "删除打印机")
  255. public void deletePrinter(Integer id, String operator) throws BusinessCheckException {
  256. MtPrinter mtPrinter = queryPrinterById(id);
  257. if (null == mtPrinter) {
  258. return;
  259. }
  260. // 删除云打印机
  261. if (StringUtil.isNotEmpty(mtPrinter.getSn())) {
  262. DelPrinterRequest restRequest = new DelPrinterRequest();
  263. createRequestHeader(mtPrinter.getMerchantId(), restRequest);
  264. String[] snList = { mtPrinter.getSn() };
  265. restRequest.setSnlist(snList);
  266. PrinterUtil.delPrinters(restRequest);
  267. }
  268. mtPrinter.setStatus(StatusEnum.DISABLE.getKey());
  269. mtPrinter.setUpdateTime(new Date());
  270. mtPrinterMapper.updateById(mtPrinter);
  271. }
  272. /**
  273. * 修改打印机数据
  274. *
  275. * @param mtPrinter 打印机参数
  276. * @throws BusinessCheckException
  277. * @return
  278. */
  279. @Override
  280. @Transactional(rollbackFor = Exception.class)
  281. @OperationServiceLog(description = "更新打印机")
  282. public MtPrinter updatePrinter(MtPrinter mtPrinter) throws BusinessCheckException {
  283. MtPrinter printer = queryPrinterById(mtPrinter.getId());
  284. BeanUtils.copyProperties(mtPrinter, printer);
  285. if (mtPrinter == null) {
  286. throw new BusinessCheckException("该打印机状态异常");
  287. }
  288. if (printer.getMerchantId() == null || printer.getMerchantId() < 1) {
  289. throw new BusinessCheckException("平台方帐号无法执行该操作,请使用商户帐号操作");
  290. }
  291. if (mtPrinter.getSn() != null && mtPrinter.getName() != null && !mtPrinter.getStatus().equals(StatusEnum.DISABLE.getKey())) {
  292. UpdPrinterRequest restRequest = new UpdPrinterRequest();
  293. createRequestHeader(mtPrinter.getMerchantId(), restRequest);
  294. restRequest.setName(mtPrinter.getName());
  295. restRequest.setSn(mtPrinter.getSn());
  296. PrinterUtil.updPrinter(restRequest);
  297. }
  298. if (mtPrinter.getStatus().equals(StatusEnum.DISABLE.getKey())) {
  299. deletePrinter(mtPrinter.getId(), mtPrinter.getOperator());
  300. }
  301. mtPrinter.setUpdateTime(new Date());
  302. mtPrinterMapper.updateById(printer);
  303. return printer;
  304. }
  305. /**
  306. * 根据条件搜索打印机
  307. *
  308. * @param params 查询参数
  309. * @throws BusinessCheckException
  310. * @return
  311. * */
  312. @Override
  313. public List<MtPrinter> queryPrinterListByParams(Map<String, Object> params) {
  314. String status = params.get("status") == null ? StatusEnum.ENABLED.getKey(): params.get("status").toString();
  315. String storeId = params.get("storeId") == null ? "" : params.get("storeId").toString();
  316. String merchantId = params.get("merchantId") == null ? "" : params.get("merchantId").toString();
  317. String sn = params.get("sn") == null ? "" : params.get("sn").toString();
  318. String name = params.get("name") == null ? "" : params.get("name").toString();
  319. String autoPrint = params.get("autoPrint") == null ? "" : params.get("autoPrint").toString();
  320. LambdaQueryWrapper<MtPrinter> lambdaQueryWrapper = Wrappers.lambdaQuery();
  321. lambdaQueryWrapper.ne(MtPrinter::getStatus, StatusEnum.DISABLE.getKey());
  322. if (StringUtils.isNotBlank(status)) {
  323. lambdaQueryWrapper.eq(MtPrinter::getStatus, status);
  324. }
  325. if (StringUtils.isNotBlank(merchantId)) {
  326. lambdaQueryWrapper.eq(MtPrinter::getMerchantId, merchantId);
  327. }
  328. if (StringUtils.isNotBlank(sn)) {
  329. lambdaQueryWrapper.eq(MtPrinter::getSn, sn);
  330. }
  331. if (StringUtils.isNotBlank(name)) {
  332. lambdaQueryWrapper.eq(MtPrinter::getName, name);
  333. }
  334. if (StringUtils.isNotBlank(storeId)) {
  335. lambdaQueryWrapper.and(wq -> wq
  336. .eq(MtPrinter::getStoreId, 0)
  337. .or()
  338. .eq(MtPrinter::getStoreId, storeId));
  339. }
  340. if (StringUtils.isNotBlank(autoPrint)) {
  341. lambdaQueryWrapper.eq(MtPrinter::getAutoPrint, autoPrint);
  342. }
  343. lambdaQueryWrapper.orderByAsc(MtPrinter::getId);
  344. return mtPrinterMapper.selectList(lambdaQueryWrapper);
  345. }
  346. /**
  347. * 创建接口请求header
  348. *
  349. * @param merchantId 商户ID
  350. * @param request RestRequest
  351. * @return
  352. * */
  353. public void createRequestHeader(Integer merchantId, RestRequest request) throws BusinessCheckException {
  354. List<MtSetting> settings = settingService.getSettingList(merchantId, SettingTypeEnum.PRINTER.getKey());
  355. if (settings != null && settings.size() > 0) {
  356. String userName = "";
  357. String userKey = "";
  358. for (MtSetting mtSetting : settings) {
  359. if (mtSetting.getName().equals(PrinterSettingEnum.USER_NAME.getKey())) {
  360. userName = mtSetting.getValue();
  361. }
  362. if (mtSetting.getName().equals(PrinterSettingEnum.USER_KEY.getKey())) {
  363. userKey = mtSetting.getValue();
  364. }
  365. }
  366. if (StringUtil.isNotEmpty(userName) && StringUtil.isNotEmpty(userKey)) {
  367. request.setUser(userName);
  368. request.setTimestamp(System.currentTimeMillis() + "");
  369. request.setSign(HashSignUtil.sign(request.getUser() + userKey + request.getTimestamp()));
  370. request.setDebug("0");
  371. } else {
  372. throw new BusinessCheckException("请先设置芯烨云打印账号!");
  373. }
  374. } else {
  375. throw new BusinessCheckException("请先设置芯烨云打印账号!");
  376. }
  377. }
  378. }