PaymentService.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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.mapper.BaseMapper;
  7. import com.dk.common.model.pojo.PageList;
  8. import com.dk.common.response.ResponseCodeEnum;
  9. import com.dk.common.response.ResponseResultUtil;
  10. import com.dk.common.response.ResponseResultVO;
  11. import com.dk.common.service.BaseService;
  12. import com.dk.mdm.infrastructure.convert.mac.RecPayConvert;
  13. import com.dk.mdm.infrastructure.convert.mac.RecPayHandleItemConvert;
  14. import com.dk.mdm.infrastructure.convert.mac.RecPayItemConvert;
  15. import com.dk.mdm.mapper.mac.*;
  16. import com.dk.mdm.mapper.mst.MoneyAccountItemMapper;
  17. import com.dk.mdm.mapper.mst.MoneyAccountMapper;
  18. import com.dk.mdm.model.pojo.mac.*;
  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.RecPayHandleItemQuery;
  22. import com.dk.mdm.model.query.mac.RecPayItemQuery;
  23. import com.dk.mdm.model.query.mac.RecPayQuery;
  24. import com.dk.mdm.model.response.mac.RecPayHandleItemResponse;
  25. import com.dk.mdm.model.response.mac.RecPayItemResponse;
  26. import com.dk.mdm.model.response.mac.RecPayResponse;
  27. import com.dk.mdm.model.vo.mac.RecPayHandleItemVO;
  28. import com.dk.mdm.model.vo.mac.RecPayItemVO;
  29. import com.dk.mdm.model.vo.mac.RecPayVO;
  30. import com.dk.mdm.service.common.CommonService;
  31. import com.dk.mdm.service.mst.MoneyAccountService;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.stereotype.Service;
  34. import org.springframework.transaction.annotation.Transactional;
  35. import java.math.BigDecimal;
  36. import java.util.ArrayList;
  37. import java.util.HashMap;
  38. import java.util.List;
  39. import java.util.Map;
  40. @Service
  41. @Transactional
  42. public class PaymentService extends BaseService<RecPay> {
  43. @Override
  44. public String getPrimaryKey() {
  45. return "rp_id";
  46. }
  47. @Override
  48. public BaseMapper<RecPay> getRepository() {
  49. return recPayMapper;
  50. }
  51. @Autowired
  52. private RecPayMapper recPayMapper;
  53. @Autowired
  54. private RecPayItemService recPayItemService;
  55. @Autowired
  56. private RecPayItemMapper recPayItemMapper;
  57. @Autowired
  58. private AccountService accountService;
  59. @Autowired
  60. private AccountMapper accountMapper;
  61. @Autowired
  62. private AccountItemService accountItemService;
  63. @Autowired
  64. private MoneyAccountMapper moneyAccountMapper;
  65. @Autowired
  66. private AccountItemMapper accountItemMapper;
  67. @Autowired
  68. private MoneyAccountService moneyAccountService;
  69. @Autowired
  70. private MoneyAccountItemMapper moneyAccountItemMapper;
  71. @Autowired
  72. private RecPayHandleItemService recPayHandleItemService;
  73. @Autowired
  74. private RecPayHandleItemMapper recPayHandleItemMapper;
  75. @Autowired
  76. private CommonService commonService;
  77. @Autowired
  78. private RecPayConvert recPayConvert;
  79. @Autowired
  80. private RecPayItemConvert recPayItemConvert;
  81. @Autowired
  82. private RecPayHandleItemConvert recPayHandleItemConvert;
  83. /**
  84. * @desc : 条件查询
  85. * @author : 付斌
  86. * @date : 2023/1/9 10:40
  87. */
  88. @Pagination
  89. public ResponseResultVO<PageList<RecPayResponse>> selectPaymentByCond(RecPayQuery recPayQuery) {
  90. return super.mergeListWithCount(recPayQuery, recPayMapper.selectPaymentByCond(recPayQuery),
  91. recPayMapper.countPaymentByCond(recPayQuery));
  92. }
  93. /**
  94. * @desc : 查询收款明细
  95. * @author : 付斌
  96. * @date : 2024-02-28 13:25
  97. */
  98. @Pagination
  99. public ResponseResultVO selectById(String id) {
  100. Map<String, Object> result = new HashMap<>();
  101. RecPayResponse recPayResponse = recPayMapper.selectById(id);
  102. result.put("data", recPayResponse);
  103. // 付款明细
  104. List<RecPayItemResponse> recPayItem = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
  105. result.put("recPayItem", recPayItem);
  106. // 应付核销明细
  107. List<RecPayHandleItemResponse> recPayHandleItem = recPayHandleItemMapper.selectByCond(new RecPayHandleItemQuery().setRpId(id));
  108. result.put("recPayHandleItem", recPayHandleItem);
  109. // 附件
  110. return ResponseResultUtil.success(result);
  111. }
  112. /**
  113. * @desc : 查询付款明细
  114. * @author : 付斌
  115. * @date : 2024-02-28 13:25
  116. */
  117. @Pagination
  118. public ResponseResultVO<Map<String, Object>> selectRpInfoById(String id) {
  119. Map<String, Object> result = new HashMap<>();
  120. // 付款明细
  121. List<RecPayItemResponse> recPayItem = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
  122. result.put("recPayItem", recPayItem);
  123. // 应付核销明细
  124. List<RecPayHandleItemResponse> recPayHandleItem = recPayHandleItemMapper.selectByCond(new RecPayHandleItemQuery().setRpId(id));
  125. result.put("recPayHandleItem", recPayHandleItem);
  126. // 附件
  127. return ResponseResultUtil.success(result);
  128. }
  129. /**
  130. * @desc : 新建应付核销(付款+核销应付)
  131. * @author : 付斌
  132. * @date : 2023/1/9 10:49
  133. */
  134. @Transactional(
  135. rollbackFor = {Exception.class}
  136. )
  137. public ResponseResultVO<?> insertPayablePayment(RecPayVO recPayVO) {
  138. /********************* 付款的处理 begin **********************/
  139. // 获取单号
  140. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
  141. recPayVO.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString());
  142. // 转化实体
  143. RecPay recPay = recPayConvert.convertToPo(recPayVO);
  144. // 总单保存
  145. super.insert(recPay);
  146. // 明细保存
  147. if (recPayVO.getItemList() != null && recPayVO.getItemList().size() > 0) {
  148. for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
  149. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  150. recPayItem.setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
  151. recPayItemMapper.insert(recPayItem);
  152. // 插入账款明细
  153. AccountItem accountItem = new AccountItem();
  154. accountItem.setAccItemType(Constant.accItemType.FU_KUAN.getName())
  155. .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
  156. .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtPay(recPayItem.getAmtPay())
  157. .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
  158. .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
  159. accountItemMapper.insert(accountItem);
  160. // 更新付款单上的账款明细Id
  161. RecPayItem recPayItemUpdate = new RecPayItem();
  162. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
  163. recPayItemService.updateByUuid(recPayItemUpdate);
  164. // 插入资金流水
  165. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  166. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  167. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtPay().negate()).setAccDate(recPayVO.getAccDate())
  168. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  169. moneyAccountItemMapper.insert(moneyAccountItem);
  170. // 更新资金账户
  171. MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
  172. MoneyAccount moneyAccountUpdate = new MoneyAccount();
  173. moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtPay().negate()))
  174. .setMacId(moneyAccountForUpdate.getMacId());
  175. // 如果账户不允许为负数
  176. if (!moneyAccountForUpdate.getFlgNegative()) {
  177. // 如果余额小于0 ,则提示余额不足
  178. if (moneyAccountUpdate.getBalance().compareTo(BigDecimal.ZERO) == -1) {
  179. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
  180. }
  181. }
  182. moneyAccountService.updateByUuid(moneyAccountUpdate);
  183. }
  184. }
  185. // 插入账款总表
  186. Account accountForUpdate = accountService.getCusAccountForUpdate(recPayVO.getObjectId());
  187. // 更新账款总表上付款的相关字段
  188. Account accountUpdate = new Account();
  189. accountUpdate.setPayment(accountForUpdate.getPayment().add(recPayVO.getSumAmtPay()))// 总付款金额
  190. .setPaymentResidue(accountForUpdate.getPaymentResidue().add(recPayVO.getSumAmtPay()))// 可退金额
  191. .setObjectId(accountForUpdate.getObjectId());
  192. accountService.updateByUuid(accountUpdate);
  193. /********************* 付款的处理 end **********************/
  194. /********************* 应付付款的处理 begin **********************/
  195. // 应付付款的处理
  196. if (recPayVO.getPayableList() != null && recPayVO.getPayableList().size() > 0) {
  197. for (RecPayHandleItemVO recPayHandleItemVO : recPayVO.getPayableList()) {
  198. RecPayHandleItem recPayHandleItem = recPayHandleItemConvert.convertToPo(recPayHandleItemVO);
  199. recPayHandleItem.setItemId(null).setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setAccDate(recPay.getAccDate());
  200. recPayHandleItemMapper.insert(recPayHandleItem);
  201. // 账款明细的核销金额和优惠金额
  202. AccountItem accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItem.getAccItemId());
  203. AccountItem accountItemUpdate = new AccountItem();
  204. // 核销金额,超出剩余应付金额
  205. if (accountItemForUpdate.getAmtResidue().compareTo(recPayHandleItem.getAmtPayableHandle()) == -1) {
  206. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  207. }
  208. accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().add(recPayHandleItem.getAmtPayableHandle()))
  209. .setAmtWaive(accountItemForUpdate.getAmtWaive().add(recPayHandleItem.getAmtWaive()))
  210. .setItemId(recPayHandleItem.getAccItemId());
  211. // 剩余金额 = 应付金额-应付付款金额
  212. accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()));
  213. accountItemService.updateByUuid(accountItemUpdate);
  214. }
  215. // 计算明细的核销金额,优惠金额合计
  216. RecPayHandleItemVO recPayHandleItemVO = recPayVO.getPayableList().stream().reduce((x, y) -> {
  217. RecPayHandleItemVO item = new RecPayHandleItemVO();
  218. item.setAmtPayableHandle(x.getAmtPayableHandle().add(y.getAmtPayableHandle()));
  219. item.setAmtWaive(x.getAmtWaive().add(y.getAmtWaive()));
  220. return item;
  221. }).get();
  222. // 更新总账上
  223. accountForUpdate = accountMapper.selectByIdForUpdate(recPayVO.getObjectId());
  224. accountUpdate = new Account();
  225. accountUpdate.setPayableHandle(accountForUpdate.getPayableHandle().add(recPayHandleItemVO.getAmtPayableHandle()))// 总应付付款金额
  226. .setPayableWaive(accountForUpdate.getPayableWaive().add(recPayHandleItemVO.getAmtWaive()))// 总应付优惠金额
  227. .setObjectId(accountForUpdate.getObjectId());
  228. // 剩余应付 = 总应付账款-总应付付款金额
  229. accountUpdate.setPayableResidue(accountForUpdate.getPayable().subtract(accountUpdate.getPayableHandle()));
  230. // 可退金额 = 总付款金额-总应付付款金额+总应付优惠金额
  231. accountUpdate.setPaymentResidue(accountForUpdate.getPayment().subtract(accountUpdate.getPayableHandle()).add(accountUpdate.getPayableWaive()));
  232. // 更新前的最后校验
  233. // 剩余应付为负数,则不能保存
  234. // if (accountUpdate.getPayableResidue().compareTo(BigDecimal.ZERO) == -1) {
  235. // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  236. // }
  237. // 可用金额为负数,则不能保存
  238. if (accountUpdate.getPaymentResidue().compareTo(BigDecimal.ZERO) == -1) {
  239. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
  240. }
  241. accountService.updateByUuid(accountUpdate);
  242. }
  243. /********************* 应付付款的处理 end **********************/
  244. return ResponseResultUtil.success();
  245. }
  246. /**
  247. * @desc : 更新应收核销(小编辑)
  248. * @author : 付斌
  249. * @date : 2023/1/9 10:49
  250. */
  251. @Transactional(
  252. rollbackFor = {Exception.class}
  253. )
  254. public ResponseResultVO<?> updatePayablePayment(RecPayVO recPayVO) {
  255. // 先只改备注和附件
  256. RecPay recPay = new RecPay();
  257. recPay.setRemarks(recPayVO.getRemarks()).setAnnexPaths(recPayVO.getAnnexPaths()).setRpId(recPayVO.getRpId());
  258. super.updateByUuid(recPay);
  259. return ResponseResultUtil.success();
  260. }
  261. /**
  262. * @desc : 作废
  263. * @author : 付斌
  264. * @date : 2024-03-08 16:38
  265. */
  266. public ResponseResultVO<?> invalid(String id) {
  267. RecPay recPayForUpdate = recPayMapper.selectByIdForUpdate(id);
  268. // 并发校验
  269. if (!recPayForUpdate.getFlgValid()) {
  270. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ISFLGVALID_FALSE.getMessage());
  271. }
  272. // 240624 问完付斌后
  273. // if(recPayForUpdate.getBiznisId() != null){
  274. // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVOICE_FORBID_EDIT.getMessage());
  275. // }
  276. // 如果所在月份已结账,则不能作废 todo
  277. // 查出并锁定所有应付核销明细
  278. AccountItem accountItemForUpdate;
  279. List<RecPayHandleItem> recPayHandleItemForUpdateList = recPayHandleItemMapper.selectByZIdForUpdate(id);
  280. for (RecPayHandleItem recPayHandleItemForUpdate : recPayHandleItemForUpdateList) {
  281. // 更新账款明细应付付款
  282. accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItemForUpdate.getAccItemId());
  283. AccountItem accountItemUpdate = new AccountItem();
  284. accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().subtract(recPayHandleItemForUpdate.getAmtPayableHandle()))
  285. .setAmtWaive(accountItemForUpdate.getAmtWaive().subtract(recPayHandleItemForUpdate.getAmtWaive()))
  286. .setItemId(recPayHandleItemForUpdate.getAccItemId());
  287. accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()).subtract(accountItemUpdate.getAmtWaive()));
  288. accountItemService.updateByUuid(accountItemUpdate);
  289. // 将核销明细有效标识置为false
  290. RecPayHandleItem recPayHandleItemUpdate = new RecPayHandleItem();
  291. recPayHandleItemUpdate.setFlgValid(false).setItemId(recPayHandleItemForUpdate.getItemId());
  292. recPayHandleItemService.updateByUuid(recPayHandleItemUpdate);
  293. }
  294. // 把总帐上的钱加回来
  295. Account accountForUpdate = accountMapper.selectByIdForUpdate(recPayForUpdate.getObjectId());
  296. Account accountUpdate = new Account();
  297. accountUpdate.setPayableHandle(accountForUpdate.getPayableHandle().subtract(recPayForUpdate.getSumAmtPayableHandle()))// 总应付付款金额
  298. .setPayableWaive(accountForUpdate.getPayableWaive().subtract(recPayForUpdate.getSumWaiveAmt()))// 总应付优惠金额
  299. .setObjectId(accountForUpdate.getObjectId());
  300. // 剩余应付 = 总应付账款-总应付付款金额
  301. accountUpdate.setPayableResidue(accountForUpdate.getPayable().subtract(accountUpdate.getPayableHandle()));
  302. // 可退金额 = 总付款金额-总应付付款金额+总应付优惠金额
  303. accountUpdate.setPaymentResidue(accountForUpdate.getPayment().subtract(accountUpdate.getPayableHandle()).add(accountUpdate.getPayableWaive()));
  304. // 更新前的最后校验
  305. // 剩余应付为负数,则不能保存
  306. // if (accountUpdate.getPayableResidue().compareTo(BigDecimal.ZERO) == -1) {
  307. // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  308. // }
  309. // 可用金额为负数,则不能保存
  310. if (accountUpdate.getPaymentResidue().compareTo(BigDecimal.ZERO) == -1) {
  311. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
  312. }
  313. accountService.updateByUuid(accountUpdate);
  314. // 将之前的明细全部删除
  315. List<RecPayItem> recPayItemOriginalList = recPayItemMapper.selectByZIdForUpdate(id);
  316. // 需要重新计算的资金账户
  317. List<String> macList = new ArrayList<>();
  318. for (RecPayItem recPayItem : recPayItemOriginalList) {
  319. // 删除收付款明细
  320. recPayItemMapper.deleteById(recPayItem.getItemId());
  321. // 删除账款明细
  322. accountItemMapper.deleteById(recPayItem.getAccItemId());
  323. // 删除账户流水
  324. moneyAccountItemMapper.deleteByInvoiceId(recPayItem.getItemId());
  325. if (!macList.contains(recPayItem.getMacId())) {
  326. macList.add(recPayItem.getMacId());
  327. }
  328. }
  329. // 更新总账表的总付款额和可用额
  330. accountService.updatePayment(recPayForUpdate.getObjectId());
  331. // 更新账户余额
  332. for (String macId : macList) {
  333. accountService.updateMac(macId);
  334. }
  335. // 作废
  336. RecPay recPayUpdate = new RecPay();
  337. recPayUpdate.setFlgValid(false).setRpId(id);
  338. super.updateByUuid(recPayUpdate);
  339. return ResponseResultUtil.success();
  340. }
  341. /**
  342. * @desc : 获取应付付款(编辑用)
  343. * @author : 付斌
  344. * @date : 2024-02-28 13:25
  345. */
  346. @Pagination
  347. public ResponseResultVO getPayablePaymentForUpdate(String id) {
  348. Map<String, Object> result = new HashMap<>();
  349. RecPayResponse recPayResponse = recPayMapper.selectById(id);
  350. result.put("data", recPayResponse);
  351. // 付款明细
  352. List<RecPayItemResponse> recPayItem = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
  353. result.put("dataItem", recPayItem);
  354. // 应付核销明细
  355. List<RecPayHandleItemResponse> recPayHandleItem = recPayHandleItemMapper.selectByCond(new RecPayHandleItemQuery().setRpId(id));
  356. result.put("recPayHandleItem", recPayHandleItem);
  357. // 附件
  358. return ResponseResultUtil.success(result);
  359. }
  360. }