|
|
@@ -6,6 +6,7 @@ import com.github.microservice.pay.client.model.ledger.GeneralLedgerModel;
|
|
|
import com.github.microservice.pay.client.model.ledger.GeneralLedgerTreeModel;
|
|
|
import com.github.microservice.pay.client.ret.ResultState;
|
|
|
import com.github.microservice.pay.client.service.ledger.GeneralLedgerService;
|
|
|
+import com.github.microservice.types.payment.PaymentChannelType;
|
|
|
import com.github.microservice.types.payment.PaymentType;
|
|
|
import com.zhongshu.card.client.model.pay.PayAccountParam;
|
|
|
import com.zhongshu.card.client.type.payAccount.PayAccountLevel;
|
|
|
@@ -20,6 +21,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Service
|
|
|
@@ -38,85 +40,184 @@ public class PayAccountService {
|
|
|
@Autowired
|
|
|
GeneralLedgerService generalLedgerService;
|
|
|
|
|
|
- @Transactional
|
|
|
- public PayAccount get(String userId, String projectOid, String oid, PayAccountLevel level, PaymentType paymentType){
|
|
|
- PayAccount payAccount = payAccountDao.findTopByProjectOidAndOidAndLevelAndPaymentChannelType(projectOid,
|
|
|
- oid, level, paymentType.getChannelType());
|
|
|
- if (payAccount == null){
|
|
|
- payAccount = upsert(userId, projectOid, oid, level, paymentType);
|
|
|
+ /**
|
|
|
+ * 获取项目总账户
|
|
|
+ */
|
|
|
+ public PayAccount getProjectParent(String projectOid){
|
|
|
+ PayAccount payAccount = payAccountDao.findTopByProjectOidAndLevel(projectOid, PayAccountLevel.projectParent);
|
|
|
+ if (payAccount==null){
|
|
|
+ payAccount = createProjectParentAccount(projectOid);
|
|
|
}
|
|
|
return payAccount;
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
- * 获取账户
|
|
|
+ * 获取项目子账户
|
|
|
*/
|
|
|
- @Transactional
|
|
|
- public PayAccount get(PayAccountParam param){
|
|
|
- //参数校验
|
|
|
- return get(param.getUserId(), param.getProjectOid(), param.getOid(), param.getLevel(), param.getPaymentType());
|
|
|
+ @SneakyThrows
|
|
|
+ public PayAccount getProjectChildren(String projectOid, PaymentChannelType channelType){
|
|
|
+ PayAccount parent = getProjectParent(projectOid);
|
|
|
+ if (parent==null){
|
|
|
+ throw new Exception("获取项目总账户失败");
|
|
|
+ }
|
|
|
+ PayAccount payAccount = payAccountDao.findTopByProjectOidAndLevelAndPaymentChannelType(projectOid, PayAccountLevel.projectChildren, channelType);
|
|
|
+ if (payAccount==null){
|
|
|
+ payAccount = createProjectChildrenAccount(projectOid, channelType, parent);
|
|
|
+ }
|
|
|
+ return payAccount;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 创建账户
|
|
|
+ * 获取用户总账户
|
|
|
+ */
|
|
|
+ public PayAccount getUserParent(String projectOid, String userId){
|
|
|
+ PayAccount payAccount = payAccountDao.findTopByProjectOidAndUserIdAndLevel(projectOid, userId, PayAccountLevel.UserParent);
|
|
|
+ if (payAccount==null){
|
|
|
+ payAccount = createUserParentAccount(projectOid, userId);
|
|
|
+ }
|
|
|
+ return payAccount;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户子账户
|
|
|
*/
|
|
|
- @Transactional
|
|
|
@SneakyThrows
|
|
|
- public PayAccount upsert(String userId, String projectOid, String oid, PayAccountLevel level, PaymentType paymentType){
|
|
|
- //处理userId
|
|
|
- if (StringUtils.isBlank(userId)){
|
|
|
- userId = authHelper.getCurrentUser().getUserId();
|
|
|
+ public PayAccount getUserChildren(String projectOid, String userId, PaymentChannelType channelType){
|
|
|
+ PayAccount payAccount = payAccountDao.findTopByProjectOidAndUserIdAndLevelAndPaymentChannelType(projectOid, userId, PayAccountLevel.UserChildren, channelType);
|
|
|
+ if (payAccount==null){
|
|
|
+ PayAccount parent = getUserParent(projectOid, userId);
|
|
|
+ if (parent==null){
|
|
|
+ throw new Exception("获取用户总账户失败");
|
|
|
+ }
|
|
|
+ payAccount = createUserChildrenAccount(projectOid, userId, channelType, parent);
|
|
|
}
|
|
|
- PayAccount parent = payAccountDao.findTopByProjectOidAndOidAndLevel(projectOid,
|
|
|
- oid, PayAccountLevel.Parent);
|
|
|
- //创建父账户
|
|
|
- if (parent==null){
|
|
|
- parent = createParentPayAccount(projectOid, oid, paymentType, userId);
|
|
|
+ return payAccount;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取机构总账户
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public PayAccount getOrgParent(String projectOid, String oid){
|
|
|
+ PayAccount payAccount = payAccountDao.findTopByProjectOidAndOidAndLevel(projectOid, oid, PayAccountLevel.OrgParent);
|
|
|
+ if (payAccount==null){
|
|
|
+ payAccount = createOrgParentAccount(projectOid, oid);
|
|
|
}
|
|
|
- if (level.equals(PayAccountLevel.Parent)){
|
|
|
- return parent;
|
|
|
- }else {
|
|
|
- PayAccount children = payAccountDao.findTopByProjectOidAndOidAndLevelAndPaymentChannelType(projectOid,
|
|
|
- oid, PayAccountLevel.Parent, paymentType.getChannelType());
|
|
|
- //创建子账户
|
|
|
- if (children==null){
|
|
|
- createChildrenPayAccount(projectOid, oid, paymentType, userId, parent);
|
|
|
+ return payAccount;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取机构子账户
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @SneakyThrows
|
|
|
+ public PayAccount getOrgChildren(String projectOid, String oid, PaymentChannelType channelType){
|
|
|
+ PayAccount payAccount = payAccountDao.findTopByProjectOidAndOidAndLevelAndPaymentChannelType(projectOid, oid,PayAccountLevel.OrgChildren, channelType);
|
|
|
+ if (payAccount==null){
|
|
|
+ PayAccount parent = getOrgParent(projectOid, oid);
|
|
|
+ if (parent==null){
|
|
|
+ throw new Exception("获取机构总账户失败");
|
|
|
}
|
|
|
- return children;
|
|
|
+ payAccount = createOrgChildrenAccount(projectOid, oid, channelType, parent);
|
|
|
+ }
|
|
|
+ return payAccount;
|
|
|
+ }
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ private PayAccount createOrgChildrenAccount(String projectOid, String oid, PaymentChannelType channelType, PayAccount parent) {
|
|
|
+ PayAccount payAccount = new PayAccount();
|
|
|
+ payAccount.setProjectOid(projectOid);
|
|
|
+ payAccount.setParent(parent);
|
|
|
+ payAccount.setOid(oid);
|
|
|
+ payAccount.setPaymentChannelType(channelType);
|
|
|
+ payAccount.setLevel(PayAccountLevel.OrgChildren);
|
|
|
+ String ledgerId = createLedger("机构子账-" + channelType.getRemark(),
|
|
|
+ parent.getLedgerId(),
|
|
|
+ Map.of("projectOid", projectOid, "paymentChannelType", channelType, "level", PayAccountLevel.OrgParent));
|
|
|
+ if (StringUtils.isBlank(ledgerId)){
|
|
|
+ throw new Exception("支付中心创建用户子账失败");
|
|
|
+ }
|
|
|
+ payAccount.setLedgerId(ledgerId);
|
|
|
+ payAccountDao.save(payAccount);
|
|
|
+ return payAccount;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ private PayAccount createOrgParentAccount(String projectOid, String oid) {
|
|
|
+ PayAccount payAccount = new PayAccount();
|
|
|
+ payAccount.setProjectOid(projectOid);
|
|
|
+ payAccount.setOid(oid);
|
|
|
+ payAccount.setLevel(PayAccountLevel.projectParent);
|
|
|
+ String ledgerId = createLedger("机构总账", null, Map.of("projectOid", projectOid, "oid", oid, "level", PayAccountLevel.OrgParent));
|
|
|
+ if (StringUtils.isBlank(ledgerId)){
|
|
|
+ throw new Exception("支付中心创建机构总账失败");
|
|
|
+ }
|
|
|
+ payAccountDao.save(payAccount);
|
|
|
+ return payAccount;
|
|
|
+ }
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ private PayAccount createUserChildrenAccount(String projectOid, String userId, PaymentChannelType channelType, PayAccount parent) {
|
|
|
+ PayAccount payAccount = new PayAccount();
|
|
|
+ payAccount.setProjectOid(projectOid);
|
|
|
+ payAccount.setParent(parent);
|
|
|
+ payAccount.setUserId(userId);
|
|
|
+ payAccount.setPaymentChannelType(channelType);
|
|
|
+ payAccount.setLevel(PayAccountLevel.UserChildren);
|
|
|
+ String ledgerId = createLedger("用户子账-" + channelType.getRemark(), parent.getLedgerId(), Map.of("projectOid", projectOid, "paymentChannelType", channelType));
|
|
|
+ if (StringUtils.isBlank(ledgerId)){
|
|
|
+ throw new Exception("支付中心创建用户子账失败");
|
|
|
}
|
|
|
+ payAccount.setLedgerId(ledgerId);
|
|
|
+ payAccountDao.save(payAccount);
|
|
|
+ return payAccount;
|
|
|
}
|
|
|
|
|
|
- private void createChildrenPayAccount(String projectOid, String oid, PaymentType paymentType, String userId, PayAccount parent) throws Exception {
|
|
|
- PayAccount children;
|
|
|
- children = new PayAccount();
|
|
|
- children.setParent(parent);
|
|
|
- children.setProjectOid(projectOid);
|
|
|
- children.setOid(oid);
|
|
|
- children.setUserId(userId);
|
|
|
- children.setLevel(PayAccountLevel.Children);
|
|
|
-// children.setPaymentType(paymentType);
|
|
|
- children.setPaymentChannelType(paymentType.getChannelType());
|
|
|
- String ledgerId = createLedger(paymentType.getChannelType().getRemark(), parent.getLedgerId(), null);
|
|
|
+ @SneakyThrows
|
|
|
+ private PayAccount createUserParentAccount(String projectOid, String userId) {
|
|
|
+ PayAccount payAccount = new PayAccount();
|
|
|
+ payAccount.setProjectOid(projectOid);
|
|
|
+ payAccount.setUserId(userId);
|
|
|
+ payAccount.setLevel(PayAccountLevel.UserParent);
|
|
|
+ String ledgerId = createLedger("用户总账", null, Map.of("projectOid", projectOid, "userId", userId));
|
|
|
if (StringUtils.isBlank(ledgerId)){
|
|
|
- throw new Exception("支付中心创建子账本失败");
|
|
|
+ throw new Exception("支付中心创建用户总账失败");
|
|
|
}
|
|
|
- payAccountDao.save(children);
|
|
|
+ payAccountDao.save(payAccount);
|
|
|
+ return payAccount;
|
|
|
}
|
|
|
|
|
|
- @NotNull
|
|
|
- private PayAccount createParentPayAccount(String projectOid, String oid, PaymentType paymentType, String userId) throws Exception {
|
|
|
- PayAccount parent;
|
|
|
- parent = new PayAccount();
|
|
|
- parent.setProjectOid(projectOid);
|
|
|
- parent.setOid(oid);
|
|
|
- parent.setUserId(userId);
|
|
|
- parent.setLevel(PayAccountLevel.Parent);
|
|
|
- String ledgerId = createLedger(paymentType.getChannelType().getRemark(), null, null);
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ private PayAccount createProjectChildrenAccount(String projectOid, PaymentChannelType channelType, PayAccount parent) {
|
|
|
+ PayAccount payAccount = new PayAccount();
|
|
|
+ payAccount.setProjectOid(projectOid);
|
|
|
+ payAccount.setParent(parent);
|
|
|
+ payAccount.setPaymentChannelType(channelType);
|
|
|
+ payAccount.setLevel(PayAccountLevel.projectChildren);
|
|
|
+ String ledgerId = createLedger("项目子账-" + channelType.getRemark(), parent.getLedgerId(), Map.of("projectOid", projectOid, "paymentChannelType", channelType));
|
|
|
if (StringUtils.isBlank(ledgerId)){
|
|
|
- throw new Exception("支付中心创建父账本失败");
|
|
|
+ throw new Exception("支付中心创建项目子账失败");
|
|
|
}
|
|
|
- parent.setLedgerId(ledgerId);
|
|
|
- payAccountDao.save(parent);
|
|
|
- return parent;
|
|
|
+ payAccount.setLedgerId(ledgerId);
|
|
|
+ payAccountDao.save(payAccount);
|
|
|
+ return payAccount;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ private PayAccount createProjectParentAccount(String projectOid){
|
|
|
+ PayAccount payAccount = new PayAccount();
|
|
|
+ payAccount.setProjectOid(projectOid);
|
|
|
+ payAccount.setLevel(PayAccountLevel.projectParent);
|
|
|
+ String ledgerId = createLedger("项目总账", null, Map.of("projectOid", projectOid));
|
|
|
+ if (StringUtils.isBlank(ledgerId)){
|
|
|
+ throw new Exception("支付中心创建项目总账失败");
|
|
|
+ }
|
|
|
+ payAccountDao.save(payAccount);
|
|
|
+ return payAccount;
|
|
|
}
|
|
|
|
|
|
private String createLedger(String name, String parentLedgerId, Map<String, Object> meta){
|