RecPayService.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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.RecPayItemConvert;
  12. import com.dk.mdm.mapper.mac.AccountItemMapper;
  13. import com.dk.mdm.mapper.mac.AccountMapper;
  14. import com.dk.mdm.mapper.mac.RecPayItemMapper;
  15. import com.dk.mdm.mapper.mst.MoneyAccountItemMapper;
  16. import com.dk.mdm.mapper.mst.MoneyAccountMapper;
  17. import com.dk.mdm.model.pojo.mac.Account;
  18. import com.dk.mdm.model.pojo.mac.AccountItem;
  19. import com.dk.mdm.model.pojo.mac.RecPay;
  20. import com.dk.mdm.mapper.mac.RecPayMapper;
  21. import com.dk.common.service.BaseService;
  22. import com.dk.common.mapper.BaseMapper;
  23. import com.dk.mdm.model.pojo.mac.RecPayItem;
  24. import com.dk.mdm.model.pojo.mst.MoneyAccount;
  25. import com.dk.mdm.model.pojo.mst.MoneyAccountItem;
  26. import com.dk.mdm.model.query.mac.RecPayItemQuery;
  27. import com.dk.mdm.model.query.mac.RecPayQuery;
  28. import com.dk.mdm.model.response.mac.RecPayItemResponse;
  29. import com.dk.mdm.model.response.mac.RecPayResponse;
  30. import com.dk.mdm.model.vo.mac.RecPayItemVO;
  31. import com.dk.mdm.model.vo.mac.RecPayVO;
  32. import com.dk.mdm.service.common.CommonService;
  33. import com.dk.mdm.service.mst.MoneyAccountService;
  34. import org.springframework.stereotype.Service;
  35. import org.springframework.beans.factory.annotation.Autowired;
  36. import org.springframework.transaction.annotation.Transactional;
  37. import java.math.BigDecimal;
  38. import java.util.*;
  39. @Service
  40. @Transactional
  41. public class RecPayService extends BaseService<RecPay> {
  42. @Override
  43. public String getPrimaryKey() {
  44. return "rp_id";
  45. }
  46. @Override
  47. public BaseMapper<RecPay> getRepository() {
  48. return recPayMapper;
  49. }
  50. @Autowired
  51. private RecPayMapper recPayMapper;
  52. @Autowired
  53. private RecPayItemService recPayItemService;
  54. @Autowired
  55. private RecPayItemMapper recPayItemMapper;
  56. @Autowired
  57. private AccountService accountService;
  58. @Autowired
  59. private AccountMapper accountMapper;
  60. @Autowired
  61. private AccountItemService accountItemService;
  62. @Autowired
  63. private MoneyAccountMapper moneyAccountMapper;
  64. @Autowired
  65. private AccountItemMapper accountItemMapper;
  66. @Autowired
  67. private MoneyAccountService moneyAccountService;
  68. @Autowired
  69. private MoneyAccountItemMapper moneyAccountItemMapper;
  70. @Autowired
  71. private CommonService commonService;
  72. @Autowired
  73. private RecPayConvert recPayConvert;
  74. @Autowired
  75. private RecPayItemConvert recPayItemConvert;
  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 = accountMapper.selectByIdForUpdate(recPayVO.getObjectId());
  150. // 没有账款对象,需要新建
  151. if (accountForUpdate == null) {
  152. accountForUpdate = new Account();
  153. accountForUpdate.setObjectId(recPayVO.getObjectId()).setObjectType(Constant.ObjectType.CUS.getName());
  154. accountMapper.insert(accountForUpdate);
  155. accountForUpdate = accountMapper.selectByIdForUpdate(recPayVO.getObjectId());
  156. }
  157. // 更新账款总表上收款的相关字段
  158. Account accountUpdate = new Account();
  159. accountUpdate.setReceipt(accountForUpdate.getReceipt().add(recPayVO.getSumAmtRec()))// 总收款金额
  160. .setReceiptResidue(accountForUpdate.getReceiptResidue().add(recPayVO.getSumAmtRec()))// 可退金额
  161. .setObjectId(accountForUpdate.getObjectId());
  162. accountService.updateByUuid(accountUpdate);
  163. return ResponseResultUtil.success();
  164. }
  165. /**
  166. * @desc : 新建退款
  167. * @author : 付斌
  168. * @date : 2023/1/9 10:49
  169. */
  170. @Transactional(
  171. rollbackFor = {Exception.class}
  172. )
  173. public ResponseResultVO<?> insertRefund(RecPayVO recPayVO) {
  174. // 查总账,看可退金额是否满足
  175. Account accountForUpdate = accountMapper.selectByIdForUpdate(recPayVO.getObjectId());
  176. if (accountForUpdate == null || accountForUpdate.getReceiptResidue().compareTo(recPayVO.getSumAmtRec().abs()) == -1) {
  177. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
  178. }
  179. // 获取单号
  180. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
  181. recPayVO.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString());
  182. // 转化实体
  183. RecPay recPay = recPayConvert.convertToPo(recPayVO);
  184. // 总单保存
  185. super.insert(recPay);
  186. // 明细保存
  187. if (recPayVO.getItemList() != null && recPayVO.getItemList().size() > 0) {
  188. for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
  189. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  190. recPayItem.setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
  191. recPayItemMapper.insert(recPayItem);
  192. // 插入账款明细
  193. AccountItem accountItem = new AccountItem();
  194. accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
  195. .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
  196. .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtRec(recPayItem.getAmtRec())
  197. .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
  198. .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
  199. accountItemMapper.insert(accountItem);
  200. // 更新收款单上的账款明细Id
  201. RecPayItem recPayItemUpdate = new RecPayItem();
  202. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setRpId(recPayVO.getRpId());
  203. recPayItemService.updateByUuid(recPayItemUpdate);
  204. // 插入资金流水
  205. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  206. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  207. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtRec()).setAccDate(recPayVO.getAccDate())
  208. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  209. moneyAccountItemMapper.insert(moneyAccountItem);
  210. // 更新资金账户
  211. MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
  212. // 如果转账之后账户余额小于0,则提示资金账户余额不足,不能退款
  213. if ((moneyAccountForUpdate.getBalance().add(recPayItem.getAmtRec())).compareTo(BigDecimal.ZERO) == -1) {
  214. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
  215. }
  216. MoneyAccount moneyAccountUpdate = new MoneyAccount();
  217. moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtRec()))
  218. .setMacId(moneyAccountForUpdate.getMacId());
  219. moneyAccountService.updateByUuid(moneyAccountUpdate);
  220. }
  221. }
  222. // 更新账款总表上收款的相关字段
  223. Account accountUpdate = new Account();
  224. accountUpdate.setReceipt(accountForUpdate.getReceipt().add(recPayVO.getSumAmtRec()))// 总收款金额
  225. .setReceiptResidue(accountForUpdate.getReceiptResidue().add(recPayVO.getSumAmtRec()))// 可退金额
  226. .setObjectId(accountForUpdate.getObjectId());
  227. accountService.updateByUuid(accountUpdate);
  228. return ResponseResultUtil.success();
  229. }
  230. /**
  231. * @desc : 编辑方法
  232. * @author : 付斌
  233. * @date : 2023/1/9 10:49
  234. */
  235. @Transactional(
  236. rollbackFor = {Exception.class}
  237. )
  238. public ResponseResultVO<?> update(RecPayVO recPayVO) {
  239. // 将之前的明细全部删除
  240. List<RecPayItem> recPayItemOriginalList = recPayItemMapper.selectByZIdForUpdate(recPayVO.getRpId());
  241. // 需要重新计算的资金账户
  242. List<String> macList = new ArrayList<>();
  243. for (RecPayItem recPayItem : recPayItemOriginalList) {
  244. // 删除收付款明细
  245. recPayItemMapper.deleteById(recPayItem.getItemId());
  246. // 删除账款明细
  247. accountItemMapper.deleteById(recPayItem.getAccItemId());
  248. // 删除账户流水
  249. moneyAccountItemMapper.deleteByInvoiceId(recPayItem.getItemId());
  250. if (!macList.contains(recPayItem.getMacId())) {
  251. macList.add(recPayItem.getMacId());
  252. }
  253. }
  254. // 新增明细
  255. for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
  256. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  257. recPayItem.setRpId(recPayVO.getRpId()).setCpId(recPayVO.getCpId());
  258. recPayItemMapper.insert(recPayItem);
  259. // 插入账款明细
  260. AccountItem accountItem = new AccountItem();
  261. accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
  262. .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
  263. .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtRec(recPayItem.getAmtRec())
  264. .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
  265. .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
  266. accountItemMapper.insert(accountItem);
  267. // 更新收款单上的账款明细Id
  268. RecPayItem recPayItemUpdate = new RecPayItem();
  269. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setRpId(recPayVO.getRpId());
  270. recPayItemService.updateByUuid(recPayItemUpdate);
  271. // 插入资金流水
  272. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  273. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  274. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtRec()).setAccDate(recPayVO.getAccDate())
  275. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  276. moneyAccountItemMapper.insert(moneyAccountItem);
  277. if (!macList.contains(recPayItem.getMacId())) {
  278. macList.add(recPayItem.getMacId());
  279. }
  280. }
  281. // 更新总账表的总收款额和可用额
  282. accountService.updateReceipt(recPayVO.getObjectId());
  283. // 更新账户余额
  284. for (String macId : macList) {
  285. accountService.updateMac(macId);
  286. }
  287. // 更新收款单总表
  288. RecPay recPay = recPayConvert.convertToPo(recPayVO);
  289. return ResponseResultUtil.success(super.updateByUuid(recPay));
  290. }
  291. /**
  292. * @desc : 获取订单信息(编辑用)
  293. * @author : 付斌
  294. * @date : 2024-03-02 17:27
  295. */
  296. public ResponseResultVO<?> getRpForUpdate(String id) {
  297. Map<String, Object> dataInfo = new HashMap<>();
  298. RecPayResponse recPayResponse = recPayMapper.selectById(id);
  299. dataInfo.put("data", recPayResponse);
  300. // 收款明细
  301. List<RecPayItemResponse> recPayItemResponse = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
  302. dataInfo.put("dataItem", recPayItemResponse);
  303. return ResponseResultUtil.success(dataInfo);
  304. }
  305. }