hejianhao
2026-09-01 f4e8dfd7f8f11966f4b5dc8c6661cbc3feccc4cf
tcm-training-admin-backend/src/main/java/com/tcm/training/admin/agent/service/ProvincialAgentAppService.java
@@ -1,5 +1,8 @@
package com.tcm.training.admin.agent.service;
import cn.hutool.json.JSONUtil;
import cn.idev.excel.FastExcel;
import com.fasterxml.jackson.databind.JsonNode;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -14,53 +17,85 @@
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;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Slf4j
@Service
public class ProvincialAgentAppService {
    private final ProvincialAgentMapper agentMapper;
    private final AgentRegionMapper regionMapper;
    private final SysRegionMapper sysRegionMapper;
    private final ClinicMapper clinicMapper;
    private final VzanHttpUtil vzanHttpUtil;
    public ProvincialAgentAppService(ProvincialAgentMapper agentMapper, AgentRegionMapper regionMapper,
                                     SysRegionMapper sysRegionMapper, ClinicMapper clinicMapper) {
                                     SysRegionMapper sysRegionMapper, ClinicMapper clinicMapper, VzanHttpUtil vzanHttpUtil) {
        this.agentMapper = agentMapper;
        this.regionMapper = regionMapper;
        this.sysRegionMapper = sysRegionMapper;
        this.clinicMapper = clinicMapper;
        this.vzanHttpUtil = vzanHttpUtil;
    }
    public PageResponse<ProvincialAgentListItemVO> page(int page, int pageSize, String agentName, String regionCode) {
    public PageResponse<ProvincialAgentListItemVO> page(int page, int pageSize, String regionCode,
                                                        String clinicName, String contactInfo, Integer status) {
        LambdaQueryWrapper<ProvincialAgent> query = new LambdaQueryWrapper<ProvincialAgent>()
                .eq(ProvincialAgent::getDeleted, 0)
                .like(trim(agentName) != null, ProvincialAgent::getAgentName, trim(agentName))
                .orderByAsc(ProvincialAgent::getCreatedAt).orderByDesc(ProvincialAgent::getId);
                .like(trim(contactInfo) != null, ProvincialAgent::getContactInfo, trim(contactInfo))
                .eq(status != null, ProvincialAgent::getStatus, status)
                .orderByAsc(ProvincialAgent::getCreatedAt).orderByAsc(ProvincialAgent::getId);
        if (trim(regionCode) != null) {
            List<Long> ids = new ArrayList<Long>();
            for (AgentRegion region : regionMapper.selectList(new LambdaQueryWrapper<AgentRegion>()
                    .eq(AgentRegion::getRegionCode, trim(regionCode))))
                ids.add(region.getAgentId());
            if (ids.isEmpty()) return PageResponse.of(new ArrayList<ProvincialAgentListItemVO>(), 0, page, pageSize);
            if (ids.isEmpty()) {
                return emptyPage(page, pageSize);
            }
            query.in(ProvincialAgent::getId, ids);
        }
        if (trim(clinicName) != null) {
            List<Long> ids = new ArrayList<Long>();
            for (Clinic clinic : clinicMapper.selectList(new LambdaQueryWrapper<Clinic>()
                    .like(Clinic::getName, trim(clinicName)))) {
                if (clinic.getAgentId() != null && !ids.contains(clinic.getAgentId())) ids.add(clinic.getAgentId());
            }
            if (ids.isEmpty()) {
                return emptyPage(page, pageSize);
            }
            query.in(ProvincialAgent::getId, ids);
        }
        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());
    }
    private PageResponse<ProvincialAgentListItemVO> emptyPage(int page, int pageSize) {
        return PageResponse.of(new ArrayList<ProvincialAgentListItemVO>(), 0, page, pageSize);
    }
    public ProvincialAgentDetailVO detail(Long id) {
        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;
@@ -72,10 +107,22 @@
        if (firstAgent) {
            clinicMapper.update(null, new LambdaUpdateWrapper<Clinic>().set(Clinic::getAgentId, agent.getId()));
        }
        log.info("调用微赞创建代理,入参 agentMobile={}, agentName={}, levelCode={}, parentMobile={}, storeName={}",
                request.getContactInfo(), request.getAgentName(), 2, null, null);
        try {
            JsonNode vzanResponse = vzanHttpUtil.createAgent(
                    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 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);
@@ -85,7 +132,7 @@
        return detail(id);
    }
    @Transactional
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        require(id);
        agentMapper.deleteById(id);
@@ -99,6 +146,7 @@
        agent.setAccountHolder(trim(request.getAccountHolder()));
        agent.setBranchName(trim(request.getBranchName()));
        agent.setContactInfo(trim(request.getContactInfo()));
        agent.setStatus(request.getStatus() == null ? EnabledStatusEnum.ENABLED.getValue() : request.getStatus());
        agent.setAddress(trim(request.getAddress()));
    }
@@ -127,13 +175,17 @@
        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());
        vo.setClinicCount(clinicMapper.selectCount(new LambdaQueryWrapper<Clinic>().eq(Clinic::getAgentId, agent.getId())));
        List<AgentRegion> regions = regions(agent.getId());
        vo.setRegionCount(regions.size());
        List<String> names = new ArrayList<String>();
        for (AgentRegion region : regions) names.add(resolveRegion(region.getRegionCode()).name);
        for (AgentRegion region : regions) {
            names.add(resolveRegion(region.getRegionCode()).name);
        }
        vo.setRegionNames(names);
        return vo;
    }
@@ -148,6 +200,8 @@
        vo.setRegionCount(base.getRegionCount());
        vo.setRegionNames(base.getRegionNames());
        vo.setCreatedAt(base.getCreatedAt());
        vo.setStatus(base.getStatus());
        vo.setClinicCount(base.getClinicCount());
        vo.setBankName(agent.getBankName());
        vo.setAccountNumber(agent.getAccountNumber());
        vo.setAccountHolder(agent.getAccountHolder());
@@ -203,4 +257,4 @@
            this.name = name;
        }
    }
}
}