RecPayService.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. package com.dk.mdm.service.mac;
  2. import com.dk.common.exception.BaseBusinessException;
  3. import com.dk.common.infrastructure.annotaiton.Pagination;
  4. import com.dk.common.infrastructure.constant.Constant;
  5. import com.dk.common.infrastructure.enums.ErrorCodeEnum;
  6. import com.dk.common.model.pojo.PageList;
  7. import com.dk.common.response.ResponseCodeEnum;
  8. import com.dk.common.response.ResponseResultUtil;
  9. import com.dk.common.response.ResponseResultVO;
  10. import com.dk.mdm.infrastructure.convert.mac.RecPayConvert;
  11. import com.dk.mdm.infrastructure.convert.mac.RecPayHandleItemConvert;
  12. import com.dk.mdm.infrastructure.convert.mac.RecPayItemConvert;
  13. import com.dk.mdm.mapper.mac.*;
  14. import com.dk.mdm.mapper.mst.MoneyAccountItemMapper;
  15. import com.dk.mdm.mapper.mst.MoneyAccountMapper;
  16. import com.dk.mdm.model.pojo.mac.*;
  17. import com.dk.common.service.BaseService;
  18. import com.dk.common.mapper.BaseMapper;
  19. import com.dk.mdm.model.pojo.mst.MoneyAccount;
  20. import com.dk.mdm.model.pojo.mst.MoneyAccountItem;
  21. import com.dk.mdm.model.query.mac.RecPayItemQuery;
  22. import com.dk.mdm.model.query.mac.RecPayQuery;
  23. import com.dk.mdm.model.response.mac.RecPayItemResponse;
  24. import com.dk.mdm.model.response.mac.RecPayResponse;
  25. import com.dk.mdm.model.vo.mac.RecPayHandleItemVO;
  26. import com.dk.mdm.model.vo.mac.RecPayItemVO;
  27. import com.dk.mdm.model.vo.mac.RecPayVO;
  28. import com.dk.mdm.service.common.CommonService;
  29. import com.dk.mdm.service.mst.MoneyAccountService;
  30. import org.springframework.stereotype.Service;
  31. import org.springframework.beans.factory.annotation.Autowired;
  32. import org.springframework.transaction.annotation.Transactional;
  33. import java.math.BigDecimal;
  34. import java.util.*;
  35. @Service
  36. @Transactional
  37. public class RecPayService extends BaseService<RecPay> {
  38. @Override
  39. public String getPrimaryKey() {
  40. return "rp_id";
  41. }
  42. @Override
  43. public BaseMapper<RecPay> getRepository() {
  44. return recPayMapper;
  45. }
  46. @Autowired
  47. private RecPayMapper recPayMapper;
  48. @Autowired
  49. private RecPayItemService recPayItemService;
  50. @Autowired
  51. private RecPayItemMapper recPayItemMapper;
  52. @Autowired
  53. private AccountService accountService;
  54. @Autowired
  55. private AccountMapper accountMapper;
  56. @Autowired
  57. private AccountItemService accountItemService;
  58. @Autowired
  59. private MoneyAccountMapper moneyAccountMapper;
  60. @Autowired
  61. private AccountItemMapper accountItemMapper;
  62. @Autowired
  63. private MoneyAccountService moneyAccountService;
  64. @Autowired
  65. private MoneyAccountItemMapper moneyAccountItemMapper;
  66. @Autowired
  67. private RecPayHandleItemMapper recPayHandleItemMapper;
  68. @Autowired
  69. private CommonService commonService;
  70. @Autowired
  71. private RecPayConvert recPayConvert;
  72. @Autowired
  73. private RecPayItemConvert recPayItemConvert;
  74. @Autowired
  75. private RecPayHandleItemConvert recPayHandleItemConvert;
  76. /**
  77. * @desc : 条件查询
  78. * @author : 付斌
  79. * @date : 2023/1/9 10:40
  80. */
  81. @Pagination
  82. public ResponseResultVO<PageList<RecPayResponse>> selectByCond(RecPayQuery recPayQuery) {
  83. return super.mergeListWithCount(recPayQuery, recPayMapper.selectByCond(recPayQuery),
  84. recPayMapper.countByCond(recPayQuery));
  85. }
  86. /**
  87. * @desc : 查询收款明细
  88. * @author : 付斌
  89. * @date : 2024-02-28 13:25
  90. */
  91. @Pagination
  92. public ResponseResultVO<Map<String, Object>> selectRpInfoById(String id) {
  93. Map<String, Object> result = new HashMap<>();
  94. // 收款明细
  95. List<RecPayItemResponse> recPayItem = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
  96. result.put("recPayItem", recPayItem);
  97. // 附件
  98. return ResponseResultUtil.success(result);
  99. }
  100. /**
  101. * @desc : 新建客户收款
  102. * @author : 付斌
  103. * @date : 2023/1/9 10:49
  104. */
  105. @Transactional(
  106. rollbackFor = {Exception.class}
  107. )
  108. public ResponseResultVO<?> insertReceipt(RecPayVO recPayVO) {
  109. // 获取单号
  110. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
  111. recPayVO.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString());
  112. // 转化实体
  113. RecPay recPay = recPayConvert.convertToPo(recPayVO);
  114. // 总单保存
  115. super.insert(recPay);
  116. // 明细保存
  117. if (recPayVO.getItemList() != null && recPayVO.getItemList().size() > 0) {
  118. for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
  119. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  120. recPayItem.setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
  121. recPayItemMapper.insert(recPayItem);
  122. // 插入账款明细
  123. AccountItem accountItem = new AccountItem();
  124. accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
  125. .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
  126. .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtRec(recPayItem.getAmtRec())
  127. .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
  128. .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
  129. accountItemMapper.insert(accountItem);
  130. // 更新收款单上的账款明细Id
  131. RecPayItem recPayItemUpdate = new RecPayItem();
  132. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setRpId(recPayVO.getRpId());
  133. recPayItemService.updateByUuid(recPayItemUpdate);
  134. // 插入资金流水
  135. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  136. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  137. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtRec()).setAccDate(recPayVO.getAccDate())
  138. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  139. moneyAccountItemMapper.insert(moneyAccountItem);
  140. // 更新资金账户
  141. MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
  142. MoneyAccount moneyAccountUpdate = new MoneyAccount();
  143. moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtRec()))
  144. .setMacId(moneyAccountForUpdate.getMacId());
  145. moneyAccountService.updateByUuid(moneyAccountUpdate);
  146. }
  147. }
  148. // 插入账款总表
  149. Account accountForUpdate = accountService.getCusAccountForUpdate(recPayVO.getObjectId());
  150. // 更新账款总表上收款的相关字段
  151. Account accountUpdate = new Account();
  152. accountUpdate.setReceipt(accountForUpdate.getReceipt().add(recPayVO.getSumAmtRec()))// 总收款金额
  153. .setReceiptResidue(accountForUpdate.getReceiptResidue().add(recPayVO.getSumAmtRec()))// 可退金额
  154. .setObjectId(accountForUpdate.getObjectId());
  155. accountService.updateByUuid(accountUpdate);
  156. return ResponseResultUtil.success();
  157. }
  158. /**
  159. * @desc : 新建客户退款
  160. * @author : 付斌
  161. * @date : 2023/1/9 10:49
  162. */
  163. @Transactional(
  164. rollbackFor = {Exception.class}
  165. )
  166. public ResponseResultVO<?> insertRefund(RecPayVO recPayVO) {
  167. // 查总账,看可退金额是否满足
  168. Account accountForUpdate = accountMapper.selectByIdForUpdate(recPayVO.getObjectId());
  169. if (accountForUpdate == null || accountForUpdate.getReceiptResidue().compareTo(recPayVO.getSumAmtRec().abs()) == -1) {
  170. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
  171. }
  172. // 获取单号
  173. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
  174. recPayVO.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString());
  175. // 转化实体
  176. RecPay recPay = recPayConvert.convertToPo(recPayVO);
  177. // 总单保存
  178. super.insert(recPay);
  179. // 明细保存
  180. if (recPayVO.getItemList() != null && recPayVO.getItemList().size() > 0) {
  181. for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
  182. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  183. recPayItem.setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
  184. recPayItemMapper.insert(recPayItem);
  185. // 插入账款明细
  186. AccountItem accountItem = new AccountItem();
  187. accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
  188. .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
  189. .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtRec(recPayItem.getAmtRec())
  190. .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
  191. .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
  192. accountItemMapper.insert(accountItem);
  193. // 更新收款单上的账款明细Id
  194. RecPayItem recPayItemUpdate = new RecPayItem();
  195. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setRpId(recPayVO.getRpId());
  196. recPayItemService.updateByUuid(recPayItemUpdate);
  197. // 插入资金流水
  198. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  199. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  200. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtRec()).setAccDate(recPayVO.getAccDate())
  201. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  202. moneyAccountItemMapper.insert(moneyAccountItem);
  203. // 更新资金账户
  204. MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
  205. // 如果转账之后账户余额小于0,则提示资金账户余额不足,不能退款
  206. if ((moneyAccountForUpdate.getBalance().add(recPayItem.getAmtRec())).compareTo(BigDecimal.ZERO) == -1) {
  207. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
  208. }
  209. MoneyAccount moneyAccountUpdate = new MoneyAccount();
  210. moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtRec()))
  211. .setMacId(moneyAccountForUpdate.getMacId());
  212. moneyAccountService.updateByUuid(moneyAccountUpdate);
  213. }
  214. }
  215. // 更新账款总表上收款的相关字段
  216. Account accountUpdate = new Account();
  217. accountUpdate.setReceipt(accountForUpdate.getReceipt().add(recPayVO.getSumAmtRec()))// 总收款金额
  218. .setReceiptResidue(accountForUpdate.getReceiptResidue().add(recPayVO.getSumAmtRec()))// 可退金额
  219. .setObjectId(accountForUpdate.getObjectId());
  220. accountService.updateByUuid(accountUpdate);
  221. return ResponseResultUtil.success();
  222. }
  223. /**
  224. * @desc : 编辑客户收款/退款
  225. * @author : 付斌
  226. * @date : 2023/1/9 10:49
  227. */
  228. @Transactional(
  229. rollbackFor = {Exception.class}
  230. )
  231. public ResponseResultVO<?> update(RecPayVO recPayVO) {
  232. // 将之前的明细全部删除
  233. List<RecPayItem> recPayItemOriginalList = recPayItemMapper.selectByZIdForUpdate(recPayVO.getRpId());
  234. // 需要重新计算的资金账户
  235. List<String> macList = new ArrayList<>();
  236. for (RecPayItem recPayItem : recPayItemOriginalList) {
  237. // 删除收付款明细
  238. recPayItemMapper.deleteById(recPayItem.getItemId());
  239. // 删除账款明细
  240. accountItemMapper.deleteById(recPayItem.getAccItemId());
  241. // 删除账户流水
  242. moneyAccountItemMapper.deleteByInvoiceId(recPayItem.getItemId());
  243. if (!macList.contains(recPayItem.getMacId())) {
  244. macList.add(recPayItem.getMacId());
  245. }
  246. }
  247. // 新增明细
  248. for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
  249. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  250. recPayItem.setRpId(recPayVO.getRpId()).setCpId(recPayVO.getCpId());
  251. recPayItemMapper.insert(recPayItem);
  252. // 插入账款明细
  253. AccountItem accountItem = new AccountItem();
  254. accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
  255. .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
  256. .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtRec(recPayItem.getAmtRec())
  257. .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
  258. .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
  259. accountItemMapper.insert(accountItem);
  260. // 更新收款单上的账款明细Id
  261. RecPayItem recPayItemUpdate = new RecPayItem();
  262. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setRpId(recPayVO.getRpId());
  263. recPayItemService.updateByUuid(recPayItemUpdate);
  264. // 插入资金流水
  265. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  266. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  267. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtRec()).setAccDate(recPayVO.getAccDate())
  268. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  269. moneyAccountItemMapper.insert(moneyAccountItem);
  270. if (!macList.contains(recPayItem.getMacId())) {
  271. macList.add(recPayItem.getMacId());
  272. }
  273. }
  274. // 更新总账表的总收款额和可用额
  275. accountService.updateReceipt(recPayVO.getObjectId());
  276. // 更新账户余额
  277. for (String macId : macList) {
  278. accountService.updateMac(macId);
  279. }
  280. // 更新收款单总表
  281. RecPay recPay = recPayConvert.convertToPo(recPayVO);
  282. return ResponseResultUtil.success(super.updateByUuid(recPay));
  283. }
  284. /**
  285. * @desc : 获取订单信息(编辑用)
  286. * @author : 付斌
  287. * @date : 2024-03-02 17:27
  288. */
  289. public ResponseResultVO<?> getRpForUpdate(String id) {
  290. Map<String, Object> dataInfo = new HashMap<>();
  291. RecPayResponse recPayResponse = recPayMapper.selectById(id);
  292. dataInfo.put("data", recPayResponse);
  293. // 收款明细
  294. List<RecPayItemResponse> recPayItemResponse = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
  295. dataInfo.put("dataItem", recPayItemResponse);
  296. return ResponseResultUtil.success(dataInfo);
  297. }
  298. /**
  299. * @desc : 新建应收收款(收款+冲应收)
  300. * @author : 付斌
  301. * @date : 2023/1/9 10:49
  302. */
  303. @Transactional(
  304. rollbackFor = {Exception.class}
  305. )
  306. public ResponseResultVO<?> insertReceivableReceipt(RecPayVO recPayVO) {
  307. /********************* 收款的处理 begin **********************/
  308. // 获取单号
  309. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
  310. recPayVO.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString());
  311. // 转化实体
  312. RecPay recPay = recPayConvert.convertToPo(recPayVO);
  313. // 总单保存
  314. super.insert(recPay);
  315. // 明细保存
  316. if (recPayVO.getItemList() != null && recPayVO.getItemList().size() > 0) {
  317. for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
  318. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  319. recPayItem.setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
  320. recPayItemMapper.insert(recPayItem);
  321. // 插入账款明细
  322. AccountItem accountItem = new AccountItem();
  323. accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
  324. .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
  325. .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtRec(recPayItem.getAmtRec())
  326. .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
  327. .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
  328. accountItemMapper.insert(accountItem);
  329. // 更新收款单上的账款明细Id
  330. RecPayItem recPayItemUpdate = new RecPayItem();
  331. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setRpId(recPayVO.getRpId());
  332. recPayItemService.updateByUuid(recPayItemUpdate);
  333. // 插入资金流水
  334. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  335. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  336. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtRec()).setAccDate(recPayVO.getAccDate())
  337. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  338. moneyAccountItemMapper.insert(moneyAccountItem);
  339. // 更新资金账户
  340. MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
  341. MoneyAccount moneyAccountUpdate = new MoneyAccount();
  342. moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtRec()))
  343. .setMacId(moneyAccountForUpdate.getMacId());
  344. moneyAccountService.updateByUuid(moneyAccountUpdate);
  345. }
  346. }
  347. // 插入账款总表
  348. Account accountForUpdate = accountService.getCusAccountForUpdate(recPayVO.getObjectId());
  349. // 更新账款总表上收款的相关字段
  350. Account accountUpdate = new Account();
  351. accountUpdate.setReceipt(accountForUpdate.getReceipt().add(recPayVO.getSumAmtRec()))// 总收款金额
  352. .setReceiptResidue(accountForUpdate.getReceiptResidue().add(recPayVO.getSumAmtRec()))// 可退金额
  353. .setObjectId(accountForUpdate.getObjectId());
  354. accountService.updateByUuid(accountUpdate);
  355. /********************* 收款的处理 end **********************/
  356. /********************* 应收收款的处理 begin **********************/
  357. // 应收收款的处理
  358. if (recPayVO.getReceivableList() != null && recPayVO.getReceivableList().size() > 0) {
  359. for (RecPayHandleItemVO recPayHandleItemVO : recPayVO.getReceivableList()) {
  360. RecPayHandleItem recPayHandleItem = recPayHandleItemConvert.convertToPo(recPayHandleItemVO);
  361. recPayHandleItem.setItemId(null).setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setAccDate(recPay.getAccDate());
  362. recPayHandleItemMapper.insert(recPayHandleItem);
  363. // 账款明细的核销金额和优惠金额
  364. AccountItem accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItem.getAccItemId());
  365. AccountItem accountItemUpdate = new AccountItem();
  366. // 核销金额加优惠金额,超出应收金额
  367. if (accountItemForUpdate.getAmtResidue().compareTo(recPayHandleItem.getAmtReceivableHandle().add(recPayHandleItem.getAmtWaive())) == -1) {
  368. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  369. }
  370. accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().add(recPayHandleItem.getAmtReceivableHandle()))
  371. .setAmtWaive(accountItemForUpdate.getAmtWaive().add(recPayHandleItem.getAmtWaive()))
  372. .setItemId(recPayHandleItem.getAccItemId());
  373. // 剩余金额 = 应收金额-应收收款金额-优惠金额
  374. accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()).subtract(accountItemUpdate.getAmtWaive()));
  375. accountItemService.updateByUuid(accountItemUpdate);
  376. }
  377. // 计算明细的核销金额,优惠金额合计
  378. RecPayHandleItemVO recPayHandleItemVO = recPayVO.getReceivableList().stream().reduce((x, y) -> {
  379. RecPayHandleItemVO item = new RecPayHandleItemVO();
  380. item.setAmtReceivableHandle(x.getAmtReceivableHandle().add(y.getAmtReceivableHandle()));
  381. item.setAmtWaive(x.getAmtWaive().add(y.getAmtWaive()));
  382. return item;
  383. }).get();
  384. // 更新总账上
  385. accountForUpdate = accountMapper.selectByIdForUpdate(recPayVO.getObjectId());
  386. accountUpdate = new Account();
  387. accountUpdate.setReceivableHandle(accountForUpdate.getReceivableHandle().add(recPayHandleItemVO.getAmtReceivableHandle()))// 总应收收款金额
  388. .setReceivableWaive(accountForUpdate.getReceivableWaive().add(recPayHandleItemVO.getAmtWaive()))// 总应收优惠金额
  389. .setObjectId(accountForUpdate.getObjectId());
  390. // 剩余应收 = 总应收账款-总应收收款金额-总应收优惠金额
  391. accountUpdate.setReceivableResidue(accountForUpdate.getReceivable().subtract(accountUpdate.getReceivableHandle()).subtract(accountUpdate.getReceivableWaive()));
  392. // 可退金额 = 总收款金额-总应收收款金额+总应收优惠金额
  393. accountUpdate.setReceiptResidue(accountForUpdate.getReceipt().subtract(accountUpdate.getReceivableHandle()).add(accountUpdate.getReceivableWaive()));
  394. // 更新前的最后校验
  395. // 剩余应收为负数,则不能保存
  396. if (accountUpdate.getReceivableResidue().compareTo(BigDecimal.ZERO) == -1) {
  397. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  398. }
  399. // 可用金额为负数,则不能保存
  400. if (accountUpdate.getReceiptResidue().compareTo(BigDecimal.ZERO) == -1) {
  401. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
  402. }
  403. accountService.updateByUuid(accountUpdate);
  404. }
  405. /********************* 应收收款的处理 end **********************/
  406. return ResponseResultUtil.success();
  407. }
  408. }