AccountService.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. package com.dk.mdm.service.mac;
  2. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  3. import com.dk.common.exception.BaseBusinessException;
  4. import com.dk.common.infrastructure.annotaiton.Pagination;
  5. import com.dk.common.infrastructure.constant.Constant;
  6. import com.dk.common.infrastructure.enums.ErrorCodeEnum;
  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.mapper.ivt.InboundMapper;
  11. import com.dk.mdm.mapper.ivt.OutboundMapper;
  12. import com.dk.mdm.mapper.mac.AccountItemMapper;
  13. import com.dk.mdm.mapper.mst.MoneyAccountItemMapper;
  14. import com.dk.mdm.model.pojo.ivt.Inbound;
  15. import com.dk.mdm.model.pojo.ivt.Outbound;
  16. import com.dk.mdm.model.pojo.mac.Account;
  17. import com.dk.mdm.mapper.mac.AccountMapper;
  18. import com.dk.common.service.BaseService;
  19. import com.dk.common.mapper.BaseMapper;
  20. import com.dk.mdm.model.pojo.mac.AccountItem;
  21. import com.dk.mdm.model.pojo.mst.MoneyAccount;
  22. import com.dk.mdm.model.query.mac.AccountItemQuery;
  23. import com.dk.mdm.model.response.mac.AccountItemResponse;
  24. import com.dk.mdm.model.response.mac.AccountResponse;
  25. import com.dk.mdm.service.ivt.inbound.InboundService;
  26. import com.dk.mdm.service.ivt.outbound.OutboundService;
  27. import com.dk.mdm.service.mst.MoneyAccountService;
  28. import org.springframework.stereotype.Service;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.transaction.annotation.Transactional;
  31. import java.math.BigDecimal;
  32. import java.util.List;
  33. import java.util.Map;
  34. import java.util.UUID;
  35. @Service
  36. @Transactional
  37. public class AccountService extends BaseService<Account> {
  38. @Override
  39. public String getPrimaryKey() {
  40. return "object_id";
  41. }
  42. @Override
  43. public BaseMapper<Account> getRepository() {
  44. return accountMapper;
  45. }
  46. @Autowired
  47. private AccountMapper accountMapper;
  48. @Autowired
  49. private AccountItemMapper accountItemMapper;
  50. @Autowired
  51. private MoneyAccountService moneyAccountService;
  52. @Autowired
  53. private MoneyAccountItemMapper moneyAccountItemMapper;
  54. @Autowired
  55. private OutboundService outboundService;
  56. @Autowired
  57. private OutboundMapper outboundMapper;
  58. @Autowired
  59. private InboundService inboundService;
  60. @Autowired
  61. private InboundMapper inboundMapper;
  62. /**
  63. * @desc : 查看来源单据,总单加明细
  64. * @author : 姜永辉
  65. * @date : 2024/3/6 10:36
  66. */
  67. public ResponseResultVO selectById(String id) {
  68. //根据id查询
  69. AccountResponse accountResponse = accountMapper.selectById(id);
  70. List<AccountItemResponse> accountItemResponses = accountItemMapper.getReceivableAccountItem(new AccountItemQuery().setObjectId(id));
  71. accountResponse.setList(accountItemResponses);
  72. return ResponseResultUtil.success(accountResponse);
  73. }
  74. /**
  75. * @desc : 只查询总单, 不包含总单加明细
  76. * @author : 姜永辉
  77. * @date : 2024/3/6 10:36
  78. */
  79. public ResponseResultVO selectAccountById(String id) {
  80. //根据id查询
  81. AccountResponse accountResponse = accountMapper.selectById(id);
  82. return ResponseResultUtil.success(accountResponse);
  83. }
  84. /**
  85. * @desc : 查询明细的总数量
  86. * @author : 姜永辉
  87. * @date : 2024/3/6 10:36
  88. */
  89. public ResponseResultVO countByCond(AccountItemQuery accountItemQuery) {
  90. //根据id查询
  91. Long aLong = accountItemMapper.countByCond(accountItemQuery);
  92. return ResponseResultUtil.success(aLong);
  93. }
  94. /**
  95. * @desc : 查询应收账款明细
  96. * @author : 付斌
  97. * @date : 2024-02-28 13:25
  98. */
  99. @Pagination
  100. public ResponseResultVO<?> getReceivableAccountItem(AccountItemQuery accountItemQuery) {
  101. List<AccountItemResponse> accountItemResponse = accountItemMapper.getReceivableAccountItem(accountItemQuery);
  102. return ResponseResultUtil.success(accountItemResponse);
  103. }
  104. /**
  105. * @desc : 查询应付账款明细
  106. * @author : 付斌
  107. * @date : 2024-02-28 13:25
  108. */
  109. @Pagination
  110. public ResponseResultVO<?> getPayableAccountItem(AccountItemQuery accountItemQuery) {
  111. List<AccountItemResponse> accountItemResponse = accountItemMapper.getPayableAccountItem(accountItemQuery);
  112. return ResponseResultUtil.success(accountItemResponse);
  113. }
  114. /********************* 账款部分共通方法 begin **********************/
  115. /**
  116. * @desc : 获取账款总表,没有则新建(客户)
  117. * @author : 付斌
  118. * @date : 2024-03-23 16:32
  119. */
  120. public Account getCusAccountForUpdate(String objectId) {
  121. return getAccountForUpdate(objectId, Constant.ObjectType.CUS.getName());
  122. }
  123. /**
  124. * @desc : 获取账款总表,没有则新建(供应商)
  125. * @author : 付斌
  126. * @date : 2024-03-23 16:32
  127. */
  128. public Account getSupAccountForUpdate(String objectId) {
  129. return getAccountForUpdate(objectId, Constant.ObjectType.SUP.getName());
  130. }
  131. /**
  132. * @desc : 更新总帐上收款类字段
  133. * @author : 付斌
  134. * @date : 2024-03-22 11:08
  135. */
  136. public void updateReceipt(String objectId) {
  137. Account accountForUpdate = accountMapper.selectByIdForUpdate(objectId);
  138. Map<String, Object> mapSumAmtRec = accountItemMapper.getSumAmtRec(objectId);
  139. BigDecimal sumAmtRec = new BigDecimal(mapSumAmtRec.get("sumAmtRec").toString());
  140. // 可退金额 = 总收款额-应收应款额+优惠金额
  141. BigDecimal sumReceiptResidue = sumAmtRec.subtract(accountForUpdate.getReceivableHandle()).add(accountForUpdate.getReceivableWaive());
  142. // 如果可退金额小于0 ,则提示余额不足
  143. if (sumReceiptResidue.compareTo(BigDecimal.ZERO) == -1) {
  144. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
  145. }
  146. // 更新账款总表上的收款总额和可用金额
  147. Account accountUpdate = new Account();
  148. accountUpdate.setReceipt(sumAmtRec).setReceiptResidue(sumReceiptResidue).setObjectId(objectId);
  149. super.updateByUuid(accountUpdate);
  150. }
  151. /**
  152. * @desc : 更新资金账户余额
  153. * @author : 付斌
  154. * @date : 2024-03-22 11:08
  155. */
  156. public void updateMac(String macId) {
  157. // 查询当前账户流水合计
  158. Map<String, Object> mapSumAmtInflow = moneyAccountItemMapper.getSumAmtInflow(macId);
  159. BigDecimal sumAmtInflow = new BigDecimal(mapSumAmtInflow.get("sumAmtInflow").toString());
  160. // 如果余额小于0 ,则提示余额不足
  161. if (sumAmtInflow.compareTo(BigDecimal.ZERO) == -1) {
  162. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
  163. }
  164. // 更新账款总表上的收款总额和可用金额
  165. MoneyAccount moneyAccountUpdate = new MoneyAccount();
  166. moneyAccountUpdate.setBalance(sumAmtInflow).setMacId(macId);
  167. moneyAccountService.updateByUuid(moneyAccountUpdate);
  168. }
  169. /**
  170. * @desc : 应收记账
  171. * @author : 付斌
  172. * @date : 2024-03-22 11:08
  173. */
  174. public void accReceivable(String invoiceId, String biznisType) {
  175. // 账款明细
  176. AccountItem accountItemInsert = new AccountItem();
  177. // 账务对象Id
  178. String objectId = null;
  179. if ("t_psi_outbound".equals(biznisType)) {
  180. Outbound outbound = outboundMapper.selectByIdForUpdate(invoiceId);
  181. objectId = outbound.getCusId();
  182. // 当前单据已经记账
  183. if (outbound.getReceivableId() != null) {
  184. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISACC.getMessage());
  185. }
  186. // 插入账款明细
  187. accountItemInsert.setAccItemType(Constant.accItemType.YING_SHOU.getName())
  188. .setObjectId(objectId).setOrgId(outbound.getOrgId()).setStaffId(outbound.getStaffId())
  189. .setAccDate(outbound.getOutDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName())
  190. .setAmtShould(outbound.getOutAmt()).setAmtResidue(outbound.getOutAmt())
  191. .setBiznisType(biznisType).setBiznisId(outbound.getOutId()).setBiznisNo(outbound.getOutNo())
  192. .setMakeStaff(outbound.getMakeStaff()).setCpId(outbound.getCpId());
  193. accountItemMapper.insert(accountItemInsert);
  194. // 更新出库单上的账款明细Id
  195. Outbound outboundUpdate = new Outbound();
  196. outboundUpdate.setReceivableId(accountItemInsert.getItemId()).setOutId(invoiceId);
  197. outboundService.updateByUuid(outboundUpdate);
  198. }
  199. // 更新账款总表上的总应收账款和总剩余应收
  200. Account accountForUpdate = getCusAccountForUpdate(objectId);
  201. Account accountUpdate = new Account();
  202. accountUpdate.setReceivable(accountForUpdate.getReceivable().add(accountItemInsert.getAmtShould()))
  203. .setReceivableResidue(accountForUpdate.getReceivableResidue().add(accountItemInsert.getAmtShould()))
  204. .setObjectId(objectId);
  205. super.updateByUuid(accountUpdate);
  206. }
  207. /**
  208. * @desc : 应收反记账
  209. * @author : 付斌
  210. * @date : 2024-03-22 11:08
  211. */
  212. public void reverseReceivable(String invoiceId, String biznisType) {
  213. // 更新账款明细应收收款
  214. AccountItem accountItemForUpdate = null;
  215. // 账务对象Id
  216. String objectId = null;
  217. if ("t_psi_outbound".equals(biznisType)) {
  218. Outbound outbound = outboundMapper.selectByIdForUpdate(invoiceId);
  219. objectId = outbound.getCusId();
  220. // 当前单据已经记账
  221. if (outbound.getReceivableId() == null) {
  222. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISREVERSE.getMessage());
  223. }
  224. accountItemForUpdate = accountItemMapper.selectByIdForUpdate(outbound.getReceivableId());
  225. // 当前单据已核销
  226. if (accountItemForUpdate.getAmtShould().compareTo(accountItemForUpdate.getAmtResidue()) != 0) {
  227. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISHANDLE.getMessage());
  228. }
  229. // 将出库单上的账款明细Id更为null
  230. LambdaUpdateWrapper<Outbound> updateWrapper = new LambdaUpdateWrapper<>();
  231. updateWrapper.set(Outbound::getReceivableId, null).eq(Outbound::getOutId, UUID.fromString(invoiceId));
  232. outboundMapper.update(null, updateWrapper);
  233. }
  234. // 删除账款明细
  235. accountItemMapper.deleteById(accountItemForUpdate.getItemId());
  236. // 更新账款总表上的总应收账款和总剩余应收
  237. Account accountForUpdate = getCusAccountForUpdate(objectId);
  238. Account accountUpdate = new Account();
  239. accountUpdate.setReceivable(accountForUpdate.getReceivable().subtract(accountItemForUpdate.getAmtShould()))
  240. .setReceivableResidue(accountForUpdate.getReceivableResidue().subtract(accountItemForUpdate.getAmtShould()))
  241. .setObjectId(objectId);
  242. super.updateByUuid(accountUpdate);
  243. }
  244. /**
  245. * @desc : 应付记账
  246. * @author : 付斌
  247. * @date : 2024-03-22 11:08
  248. */
  249. public void accPayable(String invoiceId, String biznisType) {
  250. // 账款明细
  251. AccountItem accountItemInsert = new AccountItem();
  252. // 账务对象Id
  253. String objectId = null;
  254. if ("t_psi_inbound".equals(biznisType)) {
  255. Inbound inbound = inboundMapper.selectByIdForUpdate(invoiceId);
  256. objectId = inbound.getSupId();
  257. // 当前单据已经记账
  258. if (inbound.getReceivableId() != null) {
  259. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISACC.getMessage());
  260. }
  261. // 插入账款明细
  262. accountItemInsert.setAccItemType(Constant.accItemType.YING_SHOU.getName())
  263. .setObjectId(objectId).setOrgId(inbound.getOrgId()).setStaffId(inbound.getStaffId())
  264. .setAccDate(inbound.getIntoDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName())
  265. .setAmtShould(inbound.getIntoAmt()).setAmtResidue(inbound.getIntoAmt())
  266. .setBiznisType(biznisType).setBiznisId(inbound.getIntoId()).setBiznisNo(inbound.getIntoNo())
  267. .setMakeStaff(inbound.getMakeStaff()).setCpId(inbound.getCpId());
  268. accountItemMapper.insert(accountItemInsert);
  269. // 更新出库单上的账款明细Id
  270. Inbound inboundUpdate = new Inbound();
  271. inboundUpdate.setReceivableId(accountItemInsert.getItemId()).setIntoId(invoiceId);
  272. inboundService.updateByUuid(inboundUpdate);
  273. }
  274. // 更新账款总表上的总应收账款和总剩余应收
  275. Account accountForUpdate = getSupAccountForUpdate(objectId);
  276. Account accountUpdate = new Account();
  277. accountUpdate.setPayable(accountForUpdate.getPayable().add(accountItemInsert.getAmtShould()))
  278. .setPayableResidue(accountForUpdate.getPayableResidue().add(accountItemInsert.getAmtShould()))
  279. .setObjectId(objectId);
  280. super.updateByUuid(accountUpdate);
  281. }
  282. /**
  283. * @desc : 应付反记账
  284. * @author : 付斌
  285. * @date : 2024-03-22 11:08
  286. */
  287. public void reversePayable(String invoiceId, String biznisType) {
  288. // 更新账款明细应收收款
  289. AccountItem accountItemForUpdate = null;
  290. // 账务对象Id
  291. String objectId = null;
  292. if ("t_psi_inbound".equals(biznisType)) {
  293. Inbound inbound = inboundMapper.selectByIdForUpdate(invoiceId);
  294. objectId = inbound.getSupId();
  295. // 当前单据已经记账
  296. if (inbound.getReceivableId() == null) {
  297. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISREVERSE.getMessage());
  298. }
  299. accountItemForUpdate = accountItemMapper.selectByIdForUpdate(inbound.getReceivableId());
  300. // 当前单据已核销
  301. if (accountItemForUpdate.getAmtShould().compareTo(accountItemForUpdate.getAmtResidue()) != 0) {
  302. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISHANDLE.getMessage());
  303. }
  304. // 将出库单上的账款明细Id更为null
  305. LambdaUpdateWrapper<Inbound> updateWrapper = new LambdaUpdateWrapper<>();
  306. updateWrapper.set(Inbound::getReceivableId, null).eq(Inbound::getIntoId, UUID.fromString(invoiceId));
  307. inboundMapper.update(null, updateWrapper);
  308. }
  309. // 删除账款明细
  310. accountItemMapper.deleteById(accountItemForUpdate.getItemId());
  311. // 更新账款总表上的总应收账款和总剩余应收
  312. Account accountForUpdate = getSupAccountForUpdate(objectId);
  313. Account accountUpdate = new Account();
  314. accountUpdate.setPayable(accountForUpdate.getPayable().subtract(accountItemForUpdate.getAmtShould()))
  315. .setPayableResidue(accountForUpdate.getPayableResidue().subtract(accountItemForUpdate.getAmtShould()))
  316. .setObjectId(objectId);
  317. super.updateByUuid(accountUpdate);
  318. }
  319. /********************* 账款部分共通方法 end **********************/
  320. /********************* 账款部分私有方法 begin **********************/
  321. /**
  322. * @desc : 获取账款总表,没有则新建(私有方法)
  323. * @author : 付斌
  324. * @date : 2024-03-23 16:32
  325. */
  326. private Account getAccountForUpdate(String objectId, String objectType) {
  327. // 查询账款总表
  328. Account accountForUpdate = accountMapper.selectByIdForUpdate(objectId);
  329. // 没有账款对象,需要新建
  330. if (accountForUpdate == null) {
  331. accountForUpdate = new Account();
  332. accountForUpdate.setObjectId(objectId).setObjectType(objectType);
  333. accountMapper.insert(accountForUpdate);
  334. accountForUpdate = accountMapper.selectByIdForUpdate(objectId);
  335. }
  336. return accountForUpdate;
  337. }
  338. /********************* 账款部分私有方法 end **********************/
  339. }