PaymentService.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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<Map<String, Object>> selectRpInfoById(String id) {
  100. Map<String, Object> result = new HashMap<>();
  101. // 付款明细
  102. List<RecPayItemResponse> recPayItem = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
  103. result.put("recPayItem", recPayItem);
  104. // 应付核销明细
  105. List<RecPayHandleItemResponse> recPayHandleItem = recPayHandleItemMapper.selectByCond(new RecPayHandleItemQuery().setRpId(id));
  106. result.put("recPayHandleItem", recPayHandleItem);
  107. // 附件
  108. return ResponseResultUtil.success(result);
  109. }
  110. /**
  111. * @desc : 新建应付核销(付款+核销应付)
  112. * @author : 付斌
  113. * @date : 2023/1/9 10:49
  114. */
  115. @Transactional(
  116. rollbackFor = {Exception.class}
  117. )
  118. public ResponseResultVO<?> insertPayablePayment(RecPayVO recPayVO) {
  119. /********************* 付款的处理 begin **********************/
  120. // 获取单号
  121. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
  122. recPayVO.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString());
  123. // 转化实体
  124. RecPay recPay = recPayConvert.convertToPo(recPayVO);
  125. // 总单保存
  126. super.insert(recPay);
  127. // 明细保存
  128. if (recPayVO.getItemList() != null && recPayVO.getItemList().size() > 0) {
  129. for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
  130. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  131. recPayItem.setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
  132. recPayItemMapper.insert(recPayItem);
  133. // 插入账款明细
  134. AccountItem accountItem = new AccountItem();
  135. accountItem.setAccItemType(Constant.accItemType.FU_KUAN.getName())
  136. .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
  137. .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtPay(recPayItem.getAmtPay())
  138. .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
  139. .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
  140. accountItemMapper.insert(accountItem);
  141. // 更新付款单上的账款明细Id
  142. RecPayItem recPayItemUpdate = new RecPayItem();
  143. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
  144. recPayItemService.updateByUuid(recPayItemUpdate);
  145. // 插入资金流水
  146. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  147. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  148. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtPay().negate()).setAccDate(recPayVO.getAccDate())
  149. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  150. moneyAccountItemMapper.insert(moneyAccountItem);
  151. // 更新资金账户
  152. MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
  153. MoneyAccount moneyAccountUpdate = new MoneyAccount();
  154. moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtPay().negate()))
  155. .setMacId(moneyAccountForUpdate.getMacId());
  156. moneyAccountService.updateByUuid(moneyAccountUpdate);
  157. }
  158. }
  159. // 插入账款总表
  160. Account accountForUpdate = accountService.getCusAccountForUpdate(recPayVO.getObjectId());
  161. // 更新账款总表上付款的相关字段
  162. Account accountUpdate = new Account();
  163. accountUpdate.setPayment(accountForUpdate.getPayment().add(recPayVO.getSumAmtPay()))// 总付款金额
  164. .setPaymentResidue(accountForUpdate.getPaymentResidue().add(recPayVO.getSumAmtPay()))// 可退金额
  165. .setObjectId(accountForUpdate.getObjectId());
  166. accountService.updateByUuid(accountUpdate);
  167. /********************* 付款的处理 end **********************/
  168. /********************* 应付付款的处理 begin **********************/
  169. // 应付付款的处理
  170. if (recPayVO.getPayableList() != null && recPayVO.getPayableList().size() > 0) {
  171. for (RecPayHandleItemVO recPayHandleItemVO : recPayVO.getPayableList()) {
  172. RecPayHandleItem recPayHandleItem = recPayHandleItemConvert.convertToPo(recPayHandleItemVO);
  173. recPayHandleItem.setItemId(null).setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setAccDate(recPay.getAccDate());
  174. recPayHandleItemMapper.insert(recPayHandleItem);
  175. // 账款明细的核销金额和优惠金额
  176. AccountItem accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItem.getAccItemId());
  177. AccountItem accountItemUpdate = new AccountItem();
  178. // 核销金额,超出剩余应付金额
  179. if (accountItemForUpdate.getAmtResidue().compareTo(recPayHandleItem.getAmtPayableHandle()) == -1) {
  180. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  181. }
  182. accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().add(recPayHandleItem.getAmtPayableHandle()))
  183. .setAmtWaive(accountItemForUpdate.getAmtWaive().add(recPayHandleItem.getAmtWaive()))
  184. .setItemId(recPayHandleItem.getAccItemId());
  185. // 剩余金额 = 应付金额-应付付款金额
  186. accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()));
  187. accountItemService.updateByUuid(accountItemUpdate);
  188. }
  189. // 计算明细的核销金额,优惠金额合计
  190. RecPayHandleItemVO recPayHandleItemVO = recPayVO.getPayableList().stream().reduce((x, y) -> {
  191. RecPayHandleItemVO item = new RecPayHandleItemVO();
  192. item.setAmtPayableHandle(x.getAmtPayableHandle().add(y.getAmtPayableHandle()));
  193. item.setAmtWaive(x.getAmtWaive().add(y.getAmtWaive()));
  194. return item;
  195. }).get();
  196. // 更新总账上
  197. accountForUpdate = accountMapper.selectByIdForUpdate(recPayVO.getObjectId());
  198. accountUpdate = new Account();
  199. accountUpdate.setPayableHandle(accountForUpdate.getPayableHandle().add(recPayHandleItemVO.getAmtPayableHandle()))// 总应付付款金额
  200. .setPayableWaive(accountForUpdate.getPayableWaive().add(recPayHandleItemVO.getAmtWaive()))// 总应付优惠金额
  201. .setObjectId(accountForUpdate.getObjectId());
  202. // 剩余应付 = 总应付账款-总应付付款金额
  203. accountUpdate.setPayableResidue(accountForUpdate.getPayable().subtract(accountUpdate.getPayableHandle()));
  204. // 可退金额 = 总付款金额-总应付付款金额+总应付优惠金额
  205. accountUpdate.setPaymentResidue(accountForUpdate.getPayment().subtract(accountUpdate.getPayableHandle()).add(accountUpdate.getPayableWaive()));
  206. // 更新前的最后校验
  207. // 剩余应付为负数,则不能保存
  208. if (accountUpdate.getPayableResidue().compareTo(BigDecimal.ZERO) == -1) {
  209. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  210. }
  211. // 可用金额为负数,则不能保存
  212. if (accountUpdate.getPaymentResidue().compareTo(BigDecimal.ZERO) == -1) {
  213. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
  214. }
  215. accountService.updateByUuid(accountUpdate);
  216. }
  217. /********************* 应付付款的处理 end **********************/
  218. return ResponseResultUtil.success();
  219. }
  220. /**
  221. * @desc : 作废
  222. * @author : 付斌
  223. * @date : 2024-03-08 16:38
  224. */
  225. public ResponseResultVO<?> invalid(String id) {
  226. RecPay recPayForUpdate = recPayMapper.selectByIdForUpdate(id);
  227. // 并发校验
  228. if (!recPayForUpdate.getFlgValid()) {
  229. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ISFLGVALID_FALSE.getMessage());
  230. }
  231. // 如果所在月份已结账,则不能作废 todo
  232. // 查出并锁定所有应付核销明细
  233. AccountItem accountItemForUpdate;
  234. List<RecPayHandleItem> recPayHandleItemForUpdateList = recPayHandleItemMapper.selectByZIdForUpdate(id);
  235. for (RecPayHandleItem recPayHandleItemForUpdate : recPayHandleItemForUpdateList) {
  236. // 更新账款明细应付付款
  237. accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItemForUpdate.getAccItemId());
  238. AccountItem accountItemUpdate = new AccountItem();
  239. accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().subtract(recPayHandleItemForUpdate.getAmtPayableHandle()))
  240. .setAmtWaive(accountItemForUpdate.getAmtWaive().subtract(recPayHandleItemForUpdate.getAmtWaive()))
  241. .setItemId(recPayHandleItemForUpdate.getAccItemId());
  242. accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()).subtract(accountItemUpdate.getAmtWaive()));
  243. accountItemService.updateByUuid(accountItemUpdate);
  244. // 将核销明细有效标识置为false
  245. RecPayHandleItem recPayHandleItemUpdate = new RecPayHandleItem();
  246. recPayHandleItemUpdate.setFlgValid(false).setItemId(recPayHandleItemForUpdate.getItemId());
  247. recPayHandleItemService.updateByUuid(recPayHandleItemUpdate);
  248. }
  249. // 把总帐上的钱加回来
  250. Account accountForUpdate = accountMapper.selectByIdForUpdate(recPayForUpdate.getObjectId());
  251. Account accountUpdate = new Account();
  252. accountUpdate.setPayableHandle(accountForUpdate.getPayableHandle().subtract(recPayForUpdate.getSumAmtPayableHandle()))// 总应付付款金额
  253. .setPayableWaive(accountForUpdate.getPayableWaive().subtract(recPayForUpdate.getSumWaiveAmt()))// 总应付优惠金额
  254. .setObjectId(accountForUpdate.getObjectId());
  255. // 剩余应付 = 总应付账款-总应付付款金额
  256. accountUpdate.setPayableResidue(accountForUpdate.getPayable().subtract(accountUpdate.getPayableHandle()));
  257. // 可退金额 = 总付款金额-总应付付款金额+总应付优惠金额
  258. accountUpdate.setPaymentResidue(accountForUpdate.getPayment().subtract(accountUpdate.getPayableHandle()).add(accountUpdate.getPayableWaive()));
  259. // 更新前的最后校验
  260. // 剩余应付为负数,则不能保存
  261. if (accountUpdate.getPayableResidue().compareTo(BigDecimal.ZERO) == -1) {
  262. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  263. }
  264. // 可用金额为负数,则不能保存
  265. if (accountUpdate.getPaymentResidue().compareTo(BigDecimal.ZERO) == -1) {
  266. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
  267. }
  268. accountService.updateByUuid(accountUpdate);
  269. // 将之前的明细全部删除
  270. List<RecPayItem> recPayItemOriginalList = recPayItemMapper.selectByZIdForUpdate(id);
  271. // 需要重新计算的资金账户
  272. List<String> macList = new ArrayList<>();
  273. for (RecPayItem recPayItem : recPayItemOriginalList) {
  274. // 删除收付款明细
  275. recPayItemMapper.deleteById(recPayItem.getItemId());
  276. // 删除账款明细
  277. accountItemMapper.deleteById(recPayItem.getAccItemId());
  278. // 删除账户流水
  279. moneyAccountItemMapper.deleteByInvoiceId(recPayItem.getItemId());
  280. if (!macList.contains(recPayItem.getMacId())) {
  281. macList.add(recPayItem.getMacId());
  282. }
  283. }
  284. // 更新总账表的总付款额和可用额
  285. accountService.updatePayment(recPayForUpdate.getObjectId());
  286. // 更新账户余额
  287. for (String macId : macList) {
  288. accountService.updateMac(macId);
  289. }
  290. // 作废
  291. RecPay recPayUpdate = new RecPay();
  292. recPayUpdate.setFlgValid(false).setRpId(id);
  293. super.updateByUuid(recPayUpdate);
  294. return ResponseResultUtil.success();
  295. }
  296. }