package com.tcm.training.admin.agent.service;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.tcm.training.admin.agent.dto.ProvincialAgentSaveRequest;
|
import com.tcm.training.admin.agent.entity.AgentRegion;
|
import com.tcm.training.admin.agent.entity.ProvincialAgent;
|
import com.tcm.training.admin.agent.mapper.AgentRegionMapper;
|
import com.tcm.training.admin.agent.mapper.ProvincialAgentMapper;
|
import com.tcm.training.admin.agent.vo.ProvincialAgentDetailVO;
|
import com.tcm.training.admin.agent.vo.ProvincialAgentListItemVO;
|
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.live.entity.Clinic;
|
import com.tcm.training.admin.live.mapper.ClinicMapper;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
@Service
|
public class ProvincialAgentAppService {
|
private final ProvincialAgentMapper agentMapper;
|
private final AgentRegionMapper regionMapper;
|
private final SysRegionMapper sysRegionMapper;
|
private final ClinicMapper clinicMapper;
|
|
public ProvincialAgentAppService(ProvincialAgentMapper agentMapper, AgentRegionMapper regionMapper,
|
SysRegionMapper sysRegionMapper, ClinicMapper clinicMapper) {
|
this.agentMapper = agentMapper;
|
this.regionMapper = regionMapper;
|
this.sysRegionMapper = sysRegionMapper;
|
this.clinicMapper = clinicMapper;
|
}
|
|
public PageResponse<ProvincialAgentListItemVO> page(int page, int pageSize, String agentName, String regionCode) {
|
LambdaQueryWrapper<ProvincialAgent> query = new LambdaQueryWrapper<ProvincialAgent>()
|
.eq(ProvincialAgent::getDeleted, 0)
|
.like(trim(agentName) != null, ProvincialAgent::getAgentName, trim(agentName))
|
.orderByDesc(ProvincialAgent::getCreatedAt).orderByDesc(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);
|
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));
|
return PageResponse.of(list, result.getTotal(), result.getCurrent(), result.getSize());
|
}
|
|
public ProvincialAgentDetailVO detail(Long id) {
|
return toDetail(require(id));
|
}
|
|
@Transactional
|
public ProvincialAgentDetailVO create(ProvincialAgentSaveRequest request) {
|
boolean firstAgent = agentMapper.selectCount(new LambdaQueryWrapper<ProvincialAgent>()
|
.eq(ProvincialAgent::getDeleted, 0)) == 0;
|
ProvincialAgent agent = new ProvincialAgent();
|
fill(agent, request);
|
agent.setDeleted(0);
|
agentMapper.insert(agent);
|
saveRegions(agent, request);
|
if (firstAgent) {
|
clinicMapper.update(null, new LambdaUpdateWrapper<Clinic>().set(Clinic::getAgentId, agent.getId()));
|
}
|
return detail(agent.getId());
|
}
|
|
@Transactional
|
public ProvincialAgentDetailVO update(Long id, ProvincialAgentSaveRequest request) {
|
ProvincialAgent agent = require(id);
|
fill(agent, request);
|
agentMapper.updateById(agent);
|
regionMapper.delete(new LambdaQueryWrapper<AgentRegion>().eq(AgentRegion::getAgentId, id));
|
saveRegions(agent, request);
|
return detail(id);
|
}
|
|
@Transactional
|
public void delete(Long id) {
|
require(id);
|
agentMapper.deleteById(id);
|
}
|
|
private void fill(ProvincialAgent agent, ProvincialAgentSaveRequest request) {
|
agent.setAgentName(trim(request.getAgentName()));
|
agent.setProvince("");
|
agent.setBankName(trim(request.getBankName()));
|
agent.setAccountNumber(trim(request.getAccountNumber()));
|
agent.setAccountHolder(trim(request.getAccountHolder()));
|
agent.setBranchName(trim(request.getBranchName()));
|
agent.setContactInfo(trim(request.getContactInfo()));
|
agent.setAddress(trim(request.getAddress()));
|
}
|
|
private void saveRegions(ProvincialAgent agent, ProvincialAgentSaveRequest request) {
|
if (request.getRegions() == null) return;
|
for (ProvincialAgentSaveRequest.RegionItem item : request.getRegions()) {
|
if (item == null || trim(item.getRegionCode()) == null) continue;
|
RegionInfo info = resolveRegion(item.getRegionCode());
|
AgentRegion region = new AgentRegion();
|
region.setAgentId(agent.getId());
|
region.setAgentName(agent.getAgentName());
|
region.setRegionCode(trim(item.getRegionCode()));
|
region.setRegionName(info.name);
|
region.setRegionLevel(info.path.size());
|
regionMapper.insert(region);
|
}
|
}
|
|
private ProvincialAgent require(Long id) {
|
ProvincialAgent agent = agentMapper.selectOne(new LambdaQueryWrapper<ProvincialAgent>()
|
.eq(ProvincialAgent::getId, id).eq(ProvincialAgent::getDeleted, 0).last("limit 1"));
|
if (agent == null) throw new IllegalArgumentException("省代理不存在");
|
return agent;
|
}
|
|
private ProvincialAgentListItemVO toList(ProvincialAgent agent) {
|
ProvincialAgentListItemVO vo = new ProvincialAgentListItemVO();
|
vo.setId(agent.getId());
|
vo.setAgentName(agent.getAgentName());
|
vo.setContactInfo(agent.getContactInfo());
|
vo.setAddress(agent.getAddress());
|
vo.setCreatedAt(agent.getCreatedAt());
|
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);
|
vo.setRegionNames(names);
|
return vo;
|
}
|
|
private ProvincialAgentDetailVO toDetail(ProvincialAgent agent) {
|
ProvincialAgentDetailVO vo = new ProvincialAgentDetailVO();
|
ProvincialAgentListItemVO base = toList(agent);
|
vo.setId(base.getId());
|
vo.setAgentName(base.getAgentName());
|
vo.setContactInfo(base.getContactInfo());
|
vo.setAddress(base.getAddress());
|
vo.setRegionCount(base.getRegionCount());
|
vo.setRegionNames(base.getRegionNames());
|
vo.setCreatedAt(base.getCreatedAt());
|
vo.setBankName(agent.getBankName());
|
vo.setAccountNumber(agent.getAccountNumber());
|
vo.setAccountHolder(agent.getAccountHolder());
|
vo.setBranchName(agent.getBranchName());
|
vo.setUpdatedAt(agent.getUpdatedAt());
|
List<ProvincialAgentDetailVO.RegionVO> values = new ArrayList<ProvincialAgentDetailVO.RegionVO>();
|
for (AgentRegion region : regions(agent.getId())) {
|
RegionInfo info = resolveRegion(region.getRegionCode());
|
ProvincialAgentDetailVO.RegionVO value = new ProvincialAgentDetailVO.RegionVO();
|
value.setRegionCode(region.getRegionCode());
|
value.setRegionName(info.name);
|
value.setRegionLevel(info.path.size());
|
value.setRegionPath(info.path);
|
values.add(value);
|
}
|
vo.setRegions(values);
|
return vo;
|
}
|
|
private List<AgentRegion> regions(Long id) {
|
return regionMapper.selectList(new LambdaQueryWrapper<AgentRegion>()
|
.eq(AgentRegion::getAgentId, id).orderByAsc(AgentRegion::getId));
|
}
|
|
private RegionInfo resolveRegion(String code) {
|
List<String> path = new ArrayList<String>();
|
List<String> names = new ArrayList<String>();
|
SysRegion current = sysRegionMapper.selectByCode(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());
|
}
|
if (path.isEmpty()) {
|
path.add(code);
|
names.add(code);
|
}
|
return new RegionInfo(path, String.join("", names));
|
}
|
|
private String trim(String value) {
|
if (value == null) return null;
|
String trimmed = value.trim();
|
return trimmed.isEmpty() ? null : trimmed;
|
}
|
|
private static class RegionInfo {
|
private final List<String> path;
|
private final String name;
|
|
private RegionInfo(List<String> path, String name) {
|
this.path = path;
|
this.name = name;
|
}
|
}
|
}
|