hejianhao
2026-09-01 287fa25ac03c4747472086232150723edb2db82f
tcm-training-admin-backend/src/main/java/com/tcm/training/admin/agent/service/ProvincialAgentAppService.java
@@ -17,12 +17,14 @@
import com.tcm.training.admin.app.register.entity.SysRegion;
import com.tcm.training.admin.app.register.mapper.SysRegionMapper;
import com.tcm.training.admin.common.PageResponse;
import com.tcm.training.admin.common.PhoneMasker;
import com.tcm.training.admin.live.entity.Clinic;
import com.tcm.training.admin.live.mapper.ClinicMapper;
import com.tcm.training.admin.utils.VzanHttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.tcm.training.admin.common.enums.EnabledStatusEnum;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@@ -30,6 +32,8 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.LinkedHashMap;
@Slf4j
@Service
@@ -39,6 +43,7 @@
    private final SysRegionMapper sysRegionMapper;
    private final ClinicMapper clinicMapper;
    private final VzanHttpUtil vzanHttpUtil;
    private volatile Map<String, SysRegion> regionCache;
    public ProvincialAgentAppService(ProvincialAgentMapper agentMapper, AgentRegionMapper regionMapper,
                                     SysRegionMapper sysRegionMapper, ClinicMapper clinicMapper, VzanHttpUtil vzanHttpUtil) {
@@ -79,7 +84,9 @@
        }
        IPage<ProvincialAgent> result = agentMapper.selectPage(new Page<ProvincialAgent>(page, pageSize), query);
        List<ProvincialAgentListItemVO> list = new ArrayList<ProvincialAgentListItemVO>();
        for (ProvincialAgent item : result.getRecords()) list.add(toList(item));
        for (ProvincialAgent item : result.getRecords()) {
            list.add(toList(item));
        }
        return PageResponse.of(list, result.getTotal(), result.getCurrent(), result.getSize());
    }
@@ -91,7 +98,7 @@
        return toDetail(require(id));
    }
    @Transactional
    @Transactional(rollbackFor = Exception.class)
    public ProvincialAgentDetailVO create(ProvincialAgentSaveRequest request) {
        boolean firstAgent = agentMapper.selectCount(new LambdaQueryWrapper<ProvincialAgent>()
                .eq(ProvincialAgent::getDeleted, 0)) == 0;
@@ -104,20 +111,21 @@
            clinicMapper.update(null, new LambdaUpdateWrapper<Clinic>().set(Clinic::getAgentId, agent.getId()));
        }
        log.info("调用微赞创建代理,入参 agentMobile={}, agentName={}, levelCode={}, parentMobile={}, storeName={}",
                request.getContactInfo(), request.getAgentName(), 2, null, request.getAgentName());
                request.getContactInfo(), request.getAgentName(), 2, null, null);
        try {
            JsonNode vzanResponse = vzanHttpUtil.createAgent(
                    request.getContactInfo(), request.getAgentName(), 2, null, request.getAgentName());
            log.info("微赞创建代理成功,出参={}", JSONUtil.toJsonStr(vzanResponse));
                    request.getContactInfo(), request.getAgentName(), 2, null, null);
            log.info("微赞创建代理成功,出参={}", vzanResponse);
        } catch (RuntimeException ex) {
            log.error("微赞创建代理失败,本地事务将回滚,入参 agentMobile={}, agentName={}, levelCode={}, parentMobile={}, storeName={}",
                    request.getContactInfo(), request.getAgentName(), 1, null, request.getAgentName(), ex);
            throw ex;
//            throw ex;
            throw new IllegalStateException("微赞创建代理失败", ex);
        }
        return detail(agent.getId());
    }
    @Transactional
    @Transactional(rollbackFor = Exception.class)
    public ProvincialAgentDetailVO update(Long id, ProvincialAgentSaveRequest request) {
        ProvincialAgent agent = require(id);
        fill(agent, request);
@@ -127,7 +135,7 @@
        return detail(id);
    }
    @Transactional
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        require(id);
        agentMapper.deleteById(id);
@@ -141,12 +149,13 @@
        agent.setAccountHolder(trim(request.getAccountHolder()));
        agent.setBranchName(trim(request.getBranchName()));
        agent.setContactInfo(trim(request.getContactInfo()));
        agent.setStatus(request.getStatus() == null ? 1 : request.getStatus());
        agent.setStatus(request.getStatus() == null ? EnabledStatusEnum.ENABLED.getValue() : request.getStatus());
        agent.setAddress(trim(request.getAddress()));
    }
    private void saveRegions(ProvincialAgent agent, ProvincialAgentSaveRequest request) {
        if (request.getRegions() == null) return;
        List<AgentRegion> regions = new ArrayList<AgentRegion>();
        for (ProvincialAgentSaveRequest.RegionItem item : request.getRegions()) {
            if (item == null || trim(item.getRegionCode()) == null) continue;
            RegionInfo info = resolveRegion(item.getRegionCode());
@@ -155,7 +164,10 @@
            region.setRegionCode(trim(item.getRegionCode()));
            region.setRegionName(info.name);
            region.setRegionLevel(info.path.size());
            regionMapper.insert(region);
            regions.add(region);
        }
        if (!regions.isEmpty()) {
            regionMapper.insertBatch(regions);
        }
    }
@@ -170,7 +182,7 @@
        ProvincialAgentListItemVO vo = new ProvincialAgentListItemVO();
        vo.setId(agent.getId());
        vo.setAgentName(agent.getAgentName());
        vo.setContactInfo(agent.getContactInfo());
        vo.setContactInfo(PhoneMasker.mask(agent.getContactInfo()));
        vo.setAddress(agent.getAddress());
        vo.setCreatedAt(agent.getCreatedAt());
        vo.setStatus(agent.getStatus());
@@ -224,11 +236,11 @@
    private RegionInfo resolveRegion(String code) {
        List<String> path = new ArrayList<String>();
        List<String> names = new ArrayList<String>();
        SysRegion current = sysRegionMapper.selectByCode(code);
        SysRegion current = getRegionCache().get(code);
        while (current != null) {
            path.add(0, current.getCode());
            names.add(0, current.getName());
            current = current.getParentId() == null || current.getParentId() == 0 ? null : sysRegionMapper.selectById(current.getParentId());
            current = current.getParentId() == null || current.getParentId() == 0 ? null : getRegionById(current.getParentId());
        }
        if (path.isEmpty()) {
            path.add(code);
@@ -237,6 +249,28 @@
        return new RegionInfo(path, String.join("", names));
    }
    private Map<String, SysRegion> getRegionCache() {
        Map<String, SysRegion> cache = regionCache;
        if (cache == null) {
            synchronized (this) {
                cache = regionCache;
                if (cache == null) {
                    cache = new LinkedHashMap<String, SysRegion>();
                    for (SysRegion region : sysRegionMapper.selectAllOrdered()) cache.put(region.getCode(), region);
                    regionCache = cache;
                }
            }
        }
        return cache;
    }
    private SysRegion getRegionById(Long id) {
        for (SysRegion region : getRegionCache().values()) if (id.equals(region.getId())) {
            return region;
        }
        return null;
    }
    private String trim(String value) {
        if (value == null) return null;
        String trimmed = value.trim();