OrderInvoiceService.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. package com.ruoyi.app.order;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson2.JSONObject;
  5. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6. import com.ruoyi.app.utils.ezPay.EzPay;
  7. import com.ruoyi.app.utils.ezPay.EzPayConfig;
  8. import com.ruoyi.common.exception.ServiceException;
  9. import com.ruoyi.common.utils.SecurityUtils;
  10. import com.ruoyi.system.domain.PosOrder;
  11. import com.ruoyi.system.domain.PosOrderInvoice;
  12. import com.ruoyi.system.domain.PosStore;
  13. import com.ruoyi.system.domain.PosStoreEzpay;
  14. import com.ruoyi.system.domain.InfoUser;
  15. import com.ruoyi.system.domain.dto.ApplyInvoiceDto;
  16. import com.ruoyi.system.domain.vo.PosOrderInvoiceVo;
  17. import com.ruoyi.system.mapper.PosOrderInvoiceMapper;
  18. import com.ruoyi.system.mapper.PosOrderMapper;
  19. import com.ruoyi.system.mapper.PosStoreEzpayMapper;
  20. import com.ruoyi.system.mapper.PosStoreMapper;
  21. import com.ruoyi.system.service.IInfoUserService;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.beans.factory.annotation.Value;
  24. import org.springframework.dao.DuplicateKeyException;
  25. import org.springframework.stereotype.Service;
  26. import java.util.Date;
  27. import java.util.LinkedHashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.Objects;
  31. /**
  32. * 订单电子发票 开票 / 作废 / 查询 业务(放 admin,因需调用 {@link EzPay})。
  33. *
  34. * <p>开票链路({@link #applyInvoice}):校验订单归属/状态/支付 → 校验门店可开票(009 凭证 + 免用发票)
  35. * → 金额拆分(不含运费)→ 组装 ezPay issue 参数 + 逐商品明细 → 调 {@link EzPay#issueInvoice}
  36. * → 判读回应 → 落库。作废走 {@link EzPay#doPost} + invoice_invalid。
  37. *
  38. * @author ruoyi
  39. * @date 2026-06-16
  40. */
  41. @Service
  42. public class OrderInvoiceService {
  43. @Autowired
  44. private EzPay ezPay;
  45. @Autowired
  46. private PosOrderMapper posOrderMapper;
  47. @Autowired
  48. private PosStoreMapper posStoreMapper;
  49. @Autowired
  50. private PosStoreEzpayMapper posStoreEzpayMapper;
  51. @Autowired
  52. private PosOrderInvoiceMapper posOrderInvoiceMapper;
  53. @Autowired
  54. private IInfoUserService infoUserService;
  55. /** ezPay 接口根地址(从 application.yml 的 ezpay.base-url 注入,测试/正式在此切换) */
  56. @Value("${ezpay.base-url}")
  57. private String ezpayBaseUrl;
  58. /** 发票状态:0未开/1已开/2失败/3作废 */
  59. private static final int STATUS_NOT_ISSUED = 0;
  60. private static final int STATUS_ISSUED = 1;
  61. private static final int STATUS_FAILED = 2;
  62. private static final int STATUS_INVALID = 3;
  63. /** 订单完成态、已支付 */
  64. private static final long ORDER_STATE_DONE = 3L;
  65. private static final long ORDER_PAID = 1L;
  66. // ==================== US1/US2/US3:客户开票 ====================
  67. /**
  68. * 客户申请开票。校验 → 金额拆分 → 调 ezPay 即时开立 → 落库。
  69. * B2C/B2B/载具由 {@link #buildIssueData} 按 {@code category}/{@code carrierType} 分支处理。
  70. */
  71. public PosOrderInvoiceVo applyInvoice(ApplyInvoiceDto dto, Long currentUserId) {
  72. Long orderId = dto.getOrderId();
  73. PosOrder order = posOrderMapper.selectOne(new LambdaQueryWrapper<PosOrder>().eq(PosOrder::getDdId,orderId.toString()));
  74. if (order == null) {
  75. throw new ServiceException("订单不存在");
  76. }
  77. if (!Objects.equals(order.getUserId(), currentUserId)) {
  78. throw new ServiceException("无权操作该订单");
  79. }
  80. if (order.getState() == null || order.getState() != ORDER_STATE_DONE) {
  81. throw new ServiceException("订单未完成,暂不可开票");
  82. }
  83. if (order.getPayStatus() == null || order.getPayStatus() != ORDER_PAID) {
  84. throw new ServiceException("订单未支付,暂不可开票");
  85. }
  86. Long storeId = order.getMdId();
  87. PosStoreEzpay ez = assertInvoiceable(storeId);
  88. // 场景入参校验(B2B 统编 / B2C 邮箱 / 载具号码)
  89. validateInvoiceInput(dto);
  90. // 防重复开票
  91. PosOrderInvoice row = posOrderInvoiceMapper.selectOne(
  92. new LambdaQueryWrapper<PosOrderInvoice>().eq(PosOrderInvoice::getOrderId, orderId));
  93. if (row != null && Integer.valueOf(STATUS_ISSUED).equals(row.getInvoiceStatus())) {
  94. throw new ServiceException("该订单已开票,不可重复开票");
  95. }
  96. // 金额拆分(不含运费:invoiceTotal = amount - freight)
  97. int amount = order.getAmount() == null ? 0 : order.getAmount();
  98. int freight = (int) Math.round(order.getFreight() == null ? 0d : order.getFreight());
  99. int invoiceTotal = amount - freight;
  100. if (invoiceTotal <= 0) {
  101. throw new ServiceException("订单可开票金额异常,无法开票");
  102. }
  103. int sales = (int) Math.round(invoiceTotal / 1.05);
  104. int tax = invoiceTotal - sales;
  105. // 组装 ezPay issue 参数 + 调用
  106. Map<String, Object> inv = buildIssueData(dto, order, invoiceTotal, sales, tax);
  107. EzPayConfig cfg = new EzPayConfig(ez.getMerchantId(), ez.getHashKey(), ez.getHashIv());
  108. int newStatus;
  109. String invoiceNumber = null, randomNum = null, invoiceTransNo = null,
  110. invoiceBarCode = null, invoiceQrcodeL = null, invoiceQrcodeR = null, failReason = null;
  111. try {
  112. JSONObject resp = ezPay.issueInvoice(ezpayBaseUrl, cfg, inv); String status = resp == null ? "" : resp.getString("Status");
  113. JSONObject result = resp == null ? null : resp.getJSONObject("Result");
  114. if ("SUCCESS".equals(status) && result != null && StrUtil.isNotBlank(result.getString("InvoiceNumber"))) {
  115. invoiceNumber = result.getString("InvoiceNumber");
  116. randomNum = result.getString("RandomNum");
  117. invoiceTransNo = result.getString("InvoiceTransNo");
  118. invoiceBarCode = result.getString("BarCode");
  119. invoiceQrcodeL = result.getString("QRcodeL");
  120. invoiceQrcodeR = result.getString("QRcodeR");
  121. newStatus = STATUS_ISSUED;
  122. } else {
  123. failReason = resp == null ? "ezPay 无回应" : (status + ":" + resp.getString("Message"));
  124. newStatus = STATUS_FAILED;
  125. }
  126. } catch (Exception e) {
  127. failReason = "ezPay 服务暂不可用:" + (e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage());
  128. newStatus = STATUS_FAILED;
  129. }
  130. // 落库(uk_order_id 兜底防并发重复)
  131. Date now = new Date();
  132. PosOrderInvoice saveRow = row == null ? new PosOrderInvoice() : row;
  133. saveRow.setOrderId(orderId);
  134. saveRow.setOrderNo(order.getDdId());
  135. saveRow.setStoreId(storeId);
  136. saveRow.setInvoiceCategory(dto.getCategory());
  137. saveRow.setBuyerName(dto.getBuyerName());
  138. saveRow.setBuyerUbn(dto.getBuyerUbn());
  139. saveRow.setBuyerEmail(dto.getBuyerEmail());
  140. saveRow.setCarrierType(dto.getCarrierType());
  141. saveRow.setCarrierNum(dto.getCarrierNum());
  142. saveRow.setTotalAmt(invoiceTotal);
  143. saveRow.setSalesAmt(sales);
  144. saveRow.setTaxAmt(tax);
  145. saveRow.setApplyTime(now);
  146. if (newStatus == STATUS_ISSUED) {
  147. saveRow.setInvoiceNumber(invoiceNumber);
  148. saveRow.setRandomNum(randomNum);
  149. saveRow.setInvoiceTransNo(invoiceTransNo);
  150. saveRow.setInvoiceBarCode(invoiceBarCode);
  151. saveRow.setInvoiceQrcodeL(invoiceQrcodeL);
  152. saveRow.setInvoiceQrcodeR(invoiceQrcodeR);
  153. saveRow.setIssueTime(now);
  154. saveRow.setFailReason(null);
  155. } else {
  156. saveRow.setFailReason(StrUtil.sub(failReason, 0, 480));
  157. }
  158. saveRow.setInvoiceStatus(newStatus);
  159. try {
  160. saveOrUpdate(saveRow);
  161. } catch (DuplicateKeyException dup) {
  162. throw new ServiceException("请勿重复提交开票申请");
  163. }
  164. return posOrderInvoiceMapper.selectInvoiceDetail(orderId);
  165. }
  166. /** 客户查询订单发票;门店不可开票时 invoiceStatus=null(客户端据此隐藏入口)。 */
  167. public PosOrderInvoiceVo getInvoice(Long orderId, Long currentUserId) {
  168. PosOrder order = posOrderMapper.selectPosOrderById(orderId);
  169. if (order == null || !Objects.equals(order.getUserId(), currentUserId)) {
  170. throw new ServiceException("订单不存在");
  171. }
  172. boolean can = canInvoice(order.getMdId());
  173. PosOrderInvoiceVo vo = posOrderInvoiceMapper.selectInvoiceDetail(orderId);
  174. if (vo == null) {
  175. vo = new PosOrderInvoiceVo();
  176. vo.setOrderId(orderId);
  177. vo.setOrderNo(order.getDdId());
  178. vo.setInvoiceStatus(can ? STATUS_NOT_ISSUED : null);
  179. } else if (!can) {
  180. vo.setInvoiceStatus(null);
  181. }
  182. return vo;
  183. }
  184. /** 商家端查询订单发票(校验商家为该订单门店归属人)。 */
  185. public PosOrderInvoiceVo getInvoiceForMerchant(Long orderId, Long merchantUserId) {
  186. requireOwnedOrder(orderId, merchantUserId);
  187. return posOrderInvoiceMapper.selectInvoiceDetail(orderId);
  188. }
  189. /** 商家端作废发票(校验归属后复用作废逻辑)。 */
  190. public int invalidForMerchant(Long orderId, String reason, Long merchantUserId) {
  191. requireOwnedOrder(orderId, merchantUserId);
  192. return invalid(orderId, reason);
  193. }
  194. /** 校验订单存在且属于该商家门店,返回订单;否则抛异常。 */
  195. private PosOrder requireOwnedOrder(Long orderId, Long merchantUserId) {
  196. PosOrder order = posOrderMapper.selectPosOrderById(orderId);
  197. if (order == null) {
  198. throw new ServiceException("订单不存在");
  199. }
  200. InfoUser user = infoUserService.getOne(
  201. new LambdaQueryWrapper<InfoUser>().eq(InfoUser::getUserId, merchantUserId));
  202. if (user == null) {
  203. throw new ServiceException("用户不存在");
  204. }
  205. String ut = user.getUserType();
  206. boolean own;
  207. if ("1".equals(ut) || "3".equals(ut)) {
  208. // 普通/夜市商家:订单 shId 即商家 userId
  209. own = merchantUserId.equals(order.getShId());
  210. } else {
  211. // 摊位商家:订单门店 mdId 即商家 storeId
  212. own = user.getStoreId() != null && user.getStoreId().equals(order.getMdId());
  213. }
  214. if (!own) {
  215. throw new ServiceException("无权操作该订单发票");
  216. }
  217. return order;
  218. }
  219. // ==================== US4/US5:管理端 ====================
  220. /** 管理端:订单发票列表(配合 PageHelper 分页)。 */
  221. public List<PosOrderInvoiceVo> list(PosOrderInvoiceVo query) {
  222. if (query != null && query.getDateRange() != null && query.getDateRange().length == 2) {
  223. query.setCretimStart(query.getDateRange()[0]);
  224. query.setCretimEnd(query.getDateRange()[1]);
  225. }
  226. return posOrderInvoiceMapper.selectInvoiceList(query);
  227. }
  228. /** 管理端:订单发票详情。 */
  229. public PosOrderInvoiceVo detail(Long orderId) {
  230. return posOrderInvoiceMapper.selectInvoiceDetail(orderId);
  231. }
  232. /** 管理端:重新开票(仅 失败/作废 单;运营身份不校验客户归属)。 */
  233. public int retry(Long orderId) {
  234. PosOrderInvoice row = posOrderInvoiceMapper.selectOne(
  235. new LambdaQueryWrapper<PosOrderInvoice>().eq(PosOrderInvoice::getOrderId, orderId));
  236. if (row == null) {
  237. throw new ServiceException("发票记录不存在");
  238. }
  239. int st = row.getInvoiceStatus() == null ? STATUS_NOT_ISSUED : row.getInvoiceStatus();
  240. if (st != STATUS_FAILED && st != STATUS_INVALID) {
  241. throw new ServiceException("仅失败或作废的发票可重新开票");
  242. }
  243. // 重置为未开,复用客户开票参数重开(运营身份 currentUserId 传 null 跳过归属校验)
  244. PosOrder order = posOrderMapper.selectOne(new LambdaQueryWrapper<PosOrder>().eq(PosOrder::getDdId,orderId.toString()));
  245. if (order == null) {
  246. throw new ServiceException("订单不存在");
  247. }
  248. PosStoreEzpay ez = assertInvoiceable(order.getMdId());
  249. ApplyInvoiceDto dto = new ApplyInvoiceDto();
  250. dto.setOrderId(orderId);
  251. dto.setCategory(row.getInvoiceCategory());
  252. dto.setBuyerName(row.getBuyerName());
  253. dto.setBuyerUbn(row.getBuyerUbn());
  254. dto.setBuyerEmail(row.getBuyerEmail());
  255. dto.setCarrierType(row.getCarrierType());
  256. dto.setCarrierNum(row.getCarrierNum());
  257. // 复用核心开票(不含归属校验):直接走 issue + 落库
  258. return reIssueAndSave(order, row, dto, ez);
  259. }
  260. /** 管理端:作废发票(仅 已开 单,调 ezPay invoice_invalid)。 */
  261. public int invalid(Long orderId, String reason) {
  262. PosOrderInvoice row = posOrderInvoiceMapper.selectOne(
  263. new LambdaQueryWrapper<PosOrderInvoice>().eq(PosOrderInvoice::getOrderId, orderId));
  264. if (row == null || !Integer.valueOf(STATUS_ISSUED).equals(row.getInvoiceStatus())) {
  265. throw new ServiceException("仅已开发票可作废");
  266. }
  267. PosOrder order = posOrderMapper.selectPosOrderById(orderId);
  268. PosStoreEzpay ez = posStoreEzpayMapper.selectOne(
  269. new LambdaQueryWrapper<PosStoreEzpay>().eq(PosStoreEzpay::getStoreId, order.getMdId()));
  270. if (ez == null) {
  271. throw new ServiceException("门店 ezPay 凭证缺失,无法作废");
  272. }
  273. Map<String, Object> postData = new LinkedHashMap<>();
  274. postData.put("RespondType", "JSON");
  275. postData.put("Version", "1.0");
  276. postData.put("InvoiceNumber", row.getInvoiceNumber());
  277. postData.put("InvalidReason", StrUtil.isBlank(reason) ? "商家作废" : reason);
  278. try {
  279. EzPayConfig cfg = new EzPayConfig(ez.getMerchantId(), ez.getHashKey(), ez.getHashIv());
  280. JSONObject resp = ezPay.doPost(ezpayBaseUrl + EzPay.URL_INVALID, cfg, postData); String status = resp == null ? "" : resp.getString("Status");
  281. if ("SUCCESS".equals(status)) {
  282. row.setInvoiceStatus(STATUS_INVALID);
  283. row.setInvalidTime(new Date());
  284. saveOrUpdate(row);
  285. return 1;
  286. }
  287. throw new ServiceException("作废失败:" + (resp == null ? "ezPay 无回应" : resp.getString("Message")));
  288. } catch (ServiceException se) {
  289. throw se;
  290. } catch (Exception e) {
  291. throw new ServiceException("ezPay 服务暂不可用:" + (e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage()));
  292. }
  293. }
  294. // ==================== 内部辅助 ====================
  295. /** 复用开票核心(运营重开,无归属校验)。 */
  296. private int reIssueAndSave(PosOrder order, PosOrderInvoice row, ApplyInvoiceDto dto, PosStoreEzpay ez) {
  297. int amount = order.getAmount() == null ? 0 : order.getAmount();
  298. int freight = (int) Math.round(order.getFreight() == null ? 0d : order.getFreight());
  299. int invoiceTotal = amount - freight;
  300. int sales = (int) Math.round(invoiceTotal / 1.05);
  301. int tax = invoiceTotal - sales;
  302. Map<String, Object> inv = buildIssueData(dto, order, invoiceTotal, sales, tax);
  303. EzPayConfig cfg = new EzPayConfig(ez.getMerchantId(), ez.getHashKey(), ez.getHashIv());
  304. int newStatus;
  305. String invoiceNumber = null, randomNum = null, invoiceTransNo = null,
  306. invoiceBarCode = null, invoiceQrcodeL = null, invoiceQrcodeR = null, failReason = null;
  307. try {
  308. JSONObject resp = ezPay.issueInvoice(ezpayBaseUrl, cfg, inv);
  309. String status = resp == null ? "" : resp.getString("Status");
  310. JSONObject result = resp == null ? null : resp.getJSONObject("Result");
  311. if ("SUCCESS".equals(status) && result != null && StrUtil.isNotBlank(result.getString("InvoiceNumber"))) {
  312. invoiceNumber = result.getString("InvoiceNumber");
  313. randomNum = result.getString("RandomNum");
  314. invoiceTransNo = result.getString("InvoiceTransNo");
  315. invoiceBarCode = result.getString("BarCode");
  316. invoiceQrcodeL = result.getString("QRcodeL");
  317. invoiceQrcodeR = result.getString("QRcodeR");
  318. newStatus = STATUS_ISSUED;
  319. } else {
  320. failReason = resp == null ? "ezPay 无回应" : (status + ":" + resp.getString("Message"));
  321. newStatus = STATUS_FAILED;
  322. }
  323. } catch (Exception e) {
  324. failReason = "ezPay 服务暂不可用:" + (e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage());
  325. newStatus = STATUS_FAILED;
  326. }
  327. Date now = new Date();
  328. if (newStatus == STATUS_ISSUED) {
  329. row.setInvoiceNumber(invoiceNumber);
  330. row.setRandomNum(randomNum);
  331. row.setInvoiceTransNo(invoiceTransNo);
  332. row.setInvoiceBarCode(invoiceBarCode);
  333. row.setInvoiceQrcodeL(invoiceQrcodeL);
  334. row.setInvoiceQrcodeR(invoiceQrcodeR);
  335. row.setIssueTime(now);
  336. row.setFailReason(null);
  337. } else {
  338. row.setFailReason(StrUtil.sub(failReason, 0, 480));
  339. }
  340. row.setInvoiceStatus(newStatus);
  341. saveOrUpdate(row);
  342. return newStatus == STATUS_ISSUED ? 1 : 0;
  343. }
  344. /** 校验门店可开票并返回凭证;不可开票抛异常。 */
  345. private PosStoreEzpay assertInvoiceable(Long storeId) {
  346. if (storeId == null) {
  347. throw new ServiceException("订单未关联门店,暂不可开票");
  348. }
  349. PosStore store = posStoreMapper.selectPosStoreById(storeId);
  350. if (store != null && Integer.valueOf(1).equals(store.getInvoiceExempt())) {
  351. throw new ServiceException("该门店免用发票,无需开票");
  352. }
  353. PosStoreEzpay ez = posStoreEzpayMapper.selectOne(
  354. new LambdaQueryWrapper<PosStoreEzpay>().eq(PosStoreEzpay::getStoreId, storeId));
  355. if (ez == null || !Integer.valueOf(2).equals(ez.getEzpayStatus())
  356. || !Integer.valueOf(1).equals(ez.getIsEnabled())) {
  357. throw new ServiceException("该门店暂不支持电子发票");
  358. }
  359. return ez;
  360. }
  361. /** 门店是否可开票(不抛异常)。 */
  362. private boolean canInvoice(Long storeId) {
  363. if (storeId == null) {
  364. return false;
  365. }
  366. PosStore store = posStoreMapper.selectPosStoreById(storeId);
  367. if (store != null && Integer.valueOf(1).equals(store.getInvoiceExempt())) {
  368. return false;
  369. }
  370. PosStoreEzpay ez = posStoreEzpayMapper.selectOne(
  371. new LambdaQueryWrapper<PosStoreEzpay>().eq(PosStoreEzpay::getStoreId, storeId));
  372. return ez != null && Integer.valueOf(2).equals(ez.getEzpayStatus())
  373. && Integer.valueOf(1).equals(ez.getIsEnabled());
  374. }
  375. /**
  376. * 场景入参校验(service 内强校验,拦截非法请求不调 ezPay)。对齐 ezPay 开票模型:
  377. * B2B:统编(8位)+邮箱必填、无载具;B2C:载具(0/1/2)+号码必填,ezPay会员载具(2)时邮箱必填。
  378. */
  379. private void validateInvoiceInput(ApplyInvoiceDto dto) {
  380. if ("B2B".equals(dto.getCategory())) {
  381. if (StrUtil.isBlank(dto.getBuyerUbn()) || !dto.getBuyerUbn().matches("\\d{8}")) {
  382. throw new ServiceException("统编格式不正确,须为 8 位数字");
  383. }
  384. if (StrUtil.isBlank(dto.getBuyerEmail())) {
  385. throw new ServiceException("B2B 发票须填写买方邮箱");
  386. }
  387. if (StrUtil.isNotBlank(dto.getCarrierType()) || StrUtil.isNotBlank(dto.getCarrierNum())) {
  388. throw new ServiceException("B2B 发票不支持载具");
  389. }
  390. } else if ("B2C".equals(dto.getCategory())) {
  391. String ct = dto.getCarrierType();
  392. if (!"0".equals(ct) && !"1".equals(ct) && !"2".equals(ct)) {
  393. throw new ServiceException("B2C 发票须选择载具类型(0手机条码/1自然人凭证/2ezPay会员)");
  394. }
  395. if (StrUtil.isBlank(dto.getCarrierNum())) {
  396. throw new ServiceException("载具号码不能为空");
  397. }
  398. if ("2".equals(ct) && StrUtil.isBlank(dto.getBuyerEmail())) {
  399. throw new ServiceException("ezPay 会员载具须填写买方邮箱");
  400. }
  401. }
  402. }
  403. /** 组装 ezPay invoice_issue 业务参数(Category/PrintFlag/Buyer/Amt/明细)。 */
  404. private Map<String, Object> buildIssueData(ApplyInvoiceDto dto, PosOrder order,
  405. int invoiceTotal, int sales, int tax) {
  406. Map<String, Object> inv = new LinkedHashMap<>();
  407. inv.put("MerchantOrderNo", order.getDdId());
  408. inv.put("Category", dto.getCategory());
  409. inv.put("BuyerName", dto.getBuyerName());
  410. if ("B2B".equals(dto.getCategory())) {
  411. inv.put("BuyerUBN", dto.getBuyerUbn());
  412. inv.put("BuyerEmail", dto.getBuyerEmail()); // B2B 邮箱必填:ezPay 发开立通知给买方,凭此查看
  413. inv.put("PrintFlag", "Y");
  414. } else {
  415. // B2C 载具必填(validateInvoiceInput 已校验 carrierType∈0/1/2、号码非空),PrintFlag=N
  416. inv.put("CarrierType", dto.getCarrierType());
  417. inv.put("CarrierNum", dto.getCarrierNum());
  418. inv.put("PrintFlag", "N");
  419. // ezPay 会员载具(2)需邮箱;手机条码(0)/自然人凭证(1)票进载具,邮箱留空
  420. if ("2".equals(dto.getCarrierType()) && StrUtil.isNotBlank(dto.getBuyerEmail())) {
  421. inv.put("BuyerEmail", dto.getBuyerEmail());
  422. }
  423. }
  424. inv.put("TaxType", "1");
  425. inv.put("TaxRate", "5");
  426. inv.put("Amt", String.valueOf(sales));
  427. inv.put("TaxAmt", String.valueOf(tax));
  428. inv.put("TotalAmt", String.valueOf(invoiceTotal));
  429. appendItems(inv, order.getFood(), invoiceTotal);
  430. return inv;
  431. }
  432. /**
  433. * 逐商品明细(research D2/D3):从 food JSON 解析,优惠按比例分摊到单价(方案 B)。
  434. * 每行满足 ItemCount × ItemPrice = ItemAmt(单价决定金额,ezPay 逐商品校验通过)。
  435. */
  436. private void appendItems(Map<String, Object> inv, String foodJson, int invoiceTotal) {
  437. JSONArray arr = StrUtil.isBlank(foodJson) ? null : com.alibaba.fastjson.JSON.parseArray(foodJson);
  438. if (arr == null || arr.isEmpty()) {
  439. inv.put("ItemName", "餐点");
  440. inv.put("ItemCount", "1");
  441. inv.put("ItemUnit", "份");
  442. inv.put("ItemPrice", String.valueOf(invoiceTotal));
  443. inv.put("ItemAmt", String.valueOf(invoiceTotal));
  444. return;
  445. }
  446. int foodTotal = foodOriginalTotal(arr);
  447. double ratio = foodTotal > 0 ? (double) invoiceTotal / foodTotal : 1.0;
  448. StringBuilder name = new StringBuilder(), count = new StringBuilder(),
  449. unit = new StringBuilder(), price = new StringBuilder(), amt = new StringBuilder();
  450. for (int i = 0; i < arr.size(); i++) {
  451. com.alibaba.fastjson.JSONObject it = arr.getJSONObject(i);
  452. int p = it.getIntValue("price") + it.getIntValue("otherPrice");
  453. int c = it.getIntValue("number");
  454. if (c <= 0) {
  455. c = 1;
  456. }
  457. int itemPrice = (int) Math.round(p * ratio);
  458. int itemAmt = itemPrice * c;
  459. if (i > 0) {
  460. name.append("|"); count.append("|"); unit.append("|"); price.append("|"); amt.append("|");
  461. }
  462. String nm = it.getString("name");
  463. name.append(StrUtil.isBlank(nm) ? "餐点" : nm);
  464. count.append(c);
  465. unit.append("個");
  466. price.append(itemPrice);
  467. amt.append(itemAmt);
  468. }
  469. inv.put("ItemName", name.toString());
  470. inv.put("ItemCount", count.toString());
  471. inv.put("ItemUnit", unit.toString());
  472. inv.put("ItemPrice", price.toString());
  473. inv.put("ItemAmt", amt.toString());
  474. }
  475. /** food 商品原价总额(含税)= Σ(price + otherPrice) × number。 */
  476. private int foodOriginalTotal(JSONArray arr) {
  477. int sum = 0;
  478. for (int i = 0; i < arr.size(); i++) {
  479. com.alibaba.fastjson.JSONObject it = arr.getJSONObject(i);
  480. int p = it.getIntValue("price") + it.getIntValue("otherPrice");
  481. int c = it.getIntValue("number");
  482. sum += p * (c <= 0 ? 1 : c);
  483. }
  484. return sum;
  485. }
  486. /** 新增或更新(显式设置时间与人)。 */
  487. private void saveOrUpdate(PosOrderInvoice row) {
  488. Date now = new Date();
  489. String user = currentUser();
  490. if (row.getId() == null) {
  491. row.setCreateTime(now);
  492. row.setUpdateTime(now);
  493. row.setCreateBy(user);
  494. row.setUpdateBy(user);
  495. posOrderInvoiceMapper.insert(row);
  496. } else {
  497. row.setUpdateTime(now);
  498. row.setUpdateBy(user);
  499. posOrderInvoiceMapper.updateById(row);
  500. }
  501. }
  502. private String currentUser() {
  503. try {
  504. return SecurityUtils.getUsername();
  505. } catch (Exception e) {
  506. return "system";
  507. }
  508. }
  509. }