|
|
@@ -1,13 +1,35 @@
|
|
|
package com.dk.mdm.service.mac;
|
|
|
|
|
|
+import com.dk.common.exception.BaseBusinessException;
|
|
|
+import com.dk.common.infrastructure.annotaiton.Pagination;
|
|
|
+import com.dk.common.infrastructure.constant.Constant;
|
|
|
+import com.dk.common.infrastructure.enums.ErrorCodeEnum;
|
|
|
+import com.dk.common.model.pojo.PageList;
|
|
|
+import com.dk.common.response.ResponseCodeEnum;
|
|
|
+import com.dk.common.response.ResponseResultUtil;
|
|
|
+import com.dk.common.response.ResponseResultVO;
|
|
|
+import com.dk.mdm.infrastructure.convert.mac.MacTransferConvert;
|
|
|
+import com.dk.mdm.mapper.mst.MoneyAccountItemMapper;
|
|
|
+import com.dk.mdm.mapper.mst.MoneyAccountMapper;
|
|
|
import com.dk.mdm.model.pojo.mac.MacTransfer;
|
|
|
import com.dk.mdm.mapper.mac.MacTransferMapper;
|
|
|
import com.dk.common.service.BaseService;
|
|
|
import com.dk.common.mapper.BaseMapper;
|
|
|
+import com.dk.mdm.model.pojo.mst.MoneyAccount;
|
|
|
+import com.dk.mdm.model.pojo.mst.MoneyAccountItem;
|
|
|
+import com.dk.mdm.model.query.mac.MacTransferQuery;
|
|
|
+import com.dk.mdm.model.response.mac.MacTransferResponse;
|
|
|
+import com.dk.mdm.model.vo.mac.MacTransferVO;
|
|
|
+import com.dk.mdm.service.common.CommonService;
|
|
|
+import com.dk.mdm.service.mst.MoneyAccountService;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
@Service
|
|
|
@Transactional
|
|
|
public class MacTransferService extends BaseService<MacTransfer> {
|
|
|
@@ -25,4 +47,177 @@ public class MacTransferService extends BaseService<MacTransfer> {
|
|
|
@Autowired
|
|
|
private MacTransferMapper macTransferMapper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private MoneyAccountService moneyAccountService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MoneyAccountMapper moneyAccountMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MoneyAccountItemMapper moneyAccountItemMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AccountService accountService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CommonService commonService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MacTransferConvert macTransferConvert;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @desc : 条件查询
|
|
|
+ * @author : 付斌
|
|
|
+ * @date : 2023/1/9 10:40
|
|
|
+ */
|
|
|
+ @Pagination
|
|
|
+ public ResponseResultVO<PageList<MacTransferResponse>> selectByCond(MacTransferQuery macTransferQuery) {
|
|
|
+ return super.mergeListWithCount(macTransferQuery, macTransferMapper.selectByCond(macTransferQuery),
|
|
|
+ macTransferMapper.countByCond(macTransferQuery));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @desc : 条件查询
|
|
|
+ * @author : 付斌
|
|
|
+ * @date : 2023/2/29 10:40
|
|
|
+ */
|
|
|
+ @Pagination
|
|
|
+ public ResponseResultVO selectById(String id) {
|
|
|
+ MacTransferResponse macTransferResponse = macTransferMapper.selectById(id);
|
|
|
+ return ResponseResultUtil.success(macTransferResponse);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @desc : 新建
|
|
|
+ * @author : 付斌
|
|
|
+ * @date : 2023/2/29 10:49
|
|
|
+ */
|
|
|
+ @Transactional(
|
|
|
+ rollbackFor = {Exception.class}
|
|
|
+ )
|
|
|
+ public ResponseResultVO<?> insert(MacTransferVO macTransferVO) {
|
|
|
+ // 转化实体
|
|
|
+ MacTransfer macTransfer = macTransferConvert.convertToPo(macTransferVO);
|
|
|
+
|
|
|
+ // 获取编码和主键UuId
|
|
|
+ Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.MACTRANSFER.getName(),true);
|
|
|
+ macTransfer.setTrfId(codeMap.get("outId").toString()).setTrfNo(codeMap.get("outNote").toString());
|
|
|
+ macTransferMapper.insert(macTransfer);
|
|
|
+
|
|
|
+ // 更新资金账户
|
|
|
+ MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(macTransfer.getTrfOutMac());
|
|
|
+ // 如果账户不允许为负数
|
|
|
+ if(!moneyAccountForUpdate.getFlgNegative()){
|
|
|
+ // 如果账户余额比转出金额小,就不允许转出
|
|
|
+ if(moneyAccountForUpdate.getBalance().compareTo(macTransfer.getAmtTrf()) == -1){
|
|
|
+ throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 插入转出流水
|
|
|
+ MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
|
|
|
+ moneyAccountItem.setMacId(macTransfer.getTrfOutMac()).setFlowType(Constant.FlowType.ZHUAN_ZHANG.getName())
|
|
|
+ .setInvoiceId(macTransfer.getTrfId()).setAmtInflow(macTransfer.getAmtTrf().negate()).setAccDate(macTransfer.getAccDate())
|
|
|
+ .setMakeStaff(macTransferVO.getMakeStaff()).setCpId(macTransfer.getCpId());
|
|
|
+ moneyAccountItemMapper.insert(moneyAccountItem);
|
|
|
+
|
|
|
+ MoneyAccount moneyAccountUpdate = new MoneyAccount();
|
|
|
+ moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().subtract(macTransfer.getAmtTrf()))
|
|
|
+ .setMacId(moneyAccountForUpdate.getMacId());
|
|
|
+ moneyAccountService.updateByUuid(moneyAccountUpdate);
|
|
|
+
|
|
|
+ // 插入转入流水
|
|
|
+ moneyAccountItem = new MoneyAccountItem();
|
|
|
+ moneyAccountItem.setMacId(macTransfer.getTrfInMac()).setFlowType(Constant.FlowType.ZHUAN_ZHANG.getName())
|
|
|
+ .setInvoiceId(macTransfer.getTrfId()).setAmtInflow(macTransfer.getAmtTrf()).setAccDate(macTransfer.getAccDate())
|
|
|
+ .setMakeStaff(macTransferVO.getMakeStaff()).setCpId(macTransfer.getCpId());
|
|
|
+ moneyAccountItemMapper.insert(moneyAccountItem);
|
|
|
+
|
|
|
+ // 更新资金账户
|
|
|
+ moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(macTransfer.getTrfInMac());
|
|
|
+ moneyAccountUpdate = new MoneyAccount();
|
|
|
+ moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(macTransfer.getAmtTrf()))
|
|
|
+ .setMacId(moneyAccountForUpdate.getMacId());
|
|
|
+ moneyAccountService.updateByUuid(moneyAccountUpdate);
|
|
|
+
|
|
|
+ return ResponseResultUtil.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @desc : 更新
|
|
|
+ * @author : 付斌
|
|
|
+ * @date : 2023/2/29 10:49
|
|
|
+ */
|
|
|
+ @Transactional(
|
|
|
+ rollbackFor = {Exception.class}
|
|
|
+ )
|
|
|
+ public ResponseResultVO<?> update(MacTransferVO macTransferVO) {
|
|
|
+ MacTransfer macTransferForUpdate = macTransferMapper.selectByIdForUpdate(macTransferVO.getTrfId());
|
|
|
+ // 需要重新计算的资金账户
|
|
|
+ List<String> macList = new ArrayList<>();
|
|
|
+ macList.add(macTransferForUpdate.getTrfOutMac());
|
|
|
+ macList.add(macTransferForUpdate.getTrfInMac());
|
|
|
+
|
|
|
+ // 删除账户流水
|
|
|
+ moneyAccountItemMapper.deleteByInvoiceId(macTransferVO.getTrfId());
|
|
|
+
|
|
|
+ // 转化实体
|
|
|
+ MacTransfer macTransfer = macTransferConvert.convertToPo(macTransferVO);
|
|
|
+ super.updateByUuid(macTransfer);
|
|
|
+
|
|
|
+ // 插入转出流水
|
|
|
+ MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
|
|
|
+ moneyAccountItem.setMacId(macTransfer.getTrfOutMac()).setFlowType(Constant.FlowType.ZHUAN_ZHANG.getName())
|
|
|
+ .setInvoiceId(macTransfer.getTrfId()).setAmtInflow(macTransfer.getAmtTrf().negate()).setAccDate(macTransfer.getAccDate())
|
|
|
+ .setMakeStaff(macTransferVO.getMakeStaff()).setCpId(macTransfer.getCpId());
|
|
|
+ moneyAccountItemMapper.insert(moneyAccountItem);
|
|
|
+
|
|
|
+ if (!macList.contains(macTransfer.getTrfOutMac())) {
|
|
|
+ macList.add(macTransfer.getTrfOutMac());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 插入转入流水
|
|
|
+ moneyAccountItem = new MoneyAccountItem();
|
|
|
+ moneyAccountItem.setMacId(macTransfer.getTrfInMac()).setFlowType(Constant.FlowType.ZHUAN_ZHANG.getName())
|
|
|
+ .setInvoiceId(macTransfer.getTrfId()).setAmtInflow(macTransfer.getAmtTrf()).setAccDate(macTransfer.getAccDate())
|
|
|
+ .setMakeStaff(macTransferVO.getMakeStaff()).setCpId(macTransfer.getCpId());
|
|
|
+ moneyAccountItemMapper.insert(moneyAccountItem);
|
|
|
+
|
|
|
+ if (!macList.contains(macTransfer.getTrfInMac())) {
|
|
|
+ macList.add(macTransfer.getTrfInMac());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新账户余额
|
|
|
+ for (String macId : macList) {
|
|
|
+ accountService.updateMac(macId);
|
|
|
+ }
|
|
|
+
|
|
|
+ return ResponseResultUtil.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @desc : 作废
|
|
|
+ * @author : 付斌
|
|
|
+ * @date : 2024-03-08 16:38
|
|
|
+ */
|
|
|
+ public ResponseResultVO<?> invalid(String id) {
|
|
|
+ MacTransfer macTransferForUpdate = macTransferMapper.selectByIdForUpdate(id);
|
|
|
+ // 并发校验
|
|
|
+ if (!macTransferForUpdate.getFlgValid()) {
|
|
|
+ throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ISFLGVALID_FALSE.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除账户流水
|
|
|
+ moneyAccountItemMapper.deleteByInvoiceId(id);
|
|
|
+
|
|
|
+ // 需要重新计算的资金账户
|
|
|
+ accountService.updateMac(macTransferForUpdate.getTrfOutMac());
|
|
|
+ accountService.updateMac(macTransferForUpdate.getTrfInMac());
|
|
|
+
|
|
|
+ // 作废
|
|
|
+ MacTransfer macTransferUpdate = new MacTransfer();
|
|
|
+ macTransferUpdate.setFlgValid(false).setTrfId(id);
|
|
|
+ super.updateByUuid(macTransferUpdate);
|
|
|
+ return ResponseResultUtil.success();
|
|
|
+ }
|
|
|
}
|