package com.ydtech.modules.admin.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ydtech.modules.admin.dao.SysDeptMapper; import com.ydtech.modules.admin.dao.SysPcAddressMapper; import com.ydtech.modules.admin.model.SysDept; import com.ydtech.modules.admin.model.SysUser; import com.ydtech.modules.admin.model.dto.*; import com.ydtech.modules.admin.model.po.SysPcAddressHistory; import com.ydtech.modules.admin.model.po.SysPcAddress; import com.ydtech.modules.admin.model.vo.RegionInfoVo; import com.ydtech.modules.admin.model.vo.SysDeptTreeVo; import com.ydtech.modules.admin.service.GeoService; import com.ydtech.modules.admin.service.SysPcAddressHistoryService; import com.ydtech.modules.admin.service.SysPcAddressService; import com.ydtech.modules.admin.service.SysUserService; import com.ydtech.modules.order.entity.BasePageResult; import com.ydtech.utils.StringUtils; import lombok.RequiredArgsConstructor; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.*; import java.util.stream.Collectors; /** * @author Administrator * @description 针对表【sys_pc_address(pc获取ip地址)】的数据库操作Service实现 * @createDate 2024-09-02 09:40:08 */ @Service @RequiredArgsConstructor public class SysPcAddressServiceImpl extends ServiceImpl implements SysPcAddressService { private final SysPcAddressHistoryService sysPcAddressHistoryService; private final SysPcAddressMapper sysPcAddressMapper; private final GeoService geoService; private static final String PC_GEO_KEY = "PCLocations"; @Resource private SysDeptMapper sysDeptMapper; @Override @Transactional(rollbackFor = Exception.class) public void saveSysPcAddress(IpInfoDto ipInfoDto, SysUser user) { SysPcAddress sysPcAddress = getById(user.getId()); if (sysPcAddress == null) { sysPcAddress = new SysPcAddress(); } SysDept sysDept = sysDeptMapper.selectOne(new LambdaQueryWrapper() .eq(SysDept::getId, user.getDeptId()).eq(SysDept::getDelFlag, 0)); sysPcAddress.setUserId(user.getId()); sysPcAddress.setUserName(user.getName()); sysPcAddress.setIp(ipInfoDto.getIp()); sysPcAddress.setLatitude(ipInfoDto.getLat()); sysPcAddress.setLongitude(ipInfoDto.getLng()); sysPcAddress.setProvince(ipInfoDto.getProv()); sysPcAddress.setCity(ipInfoDto.getCity()); sysPcAddress.setDistrict(ipInfoDto.getDistrict()); sysPcAddress.setSource(sysDept.getManagementSource()); sysPcAddress.setAddress(ipInfoDto.getProv() + ipInfoDto.getCity() + ipInfoDto.getDistrict()); sysPcAddress.setOperationTime(new Date()); SysPcAddressHistory sysPcAddressHistory = new SysPcAddressHistory(); BeanUtils.copyProperties(sysPcAddress, sysPcAddressHistory); this.saveOrUpdate(sysPcAddress); sysPcAddressHistoryService.save(sysPcAddressHistory); } @Override public List getEveryone(SysPcAddressDto sysPcAddressDto) { List sysPcAddresses = sysPcAddressMapper.selectList(new LambdaQueryWrapper() .eq(StringUtils.isNotEmpty(sysPcAddressDto.getProvince()), SysPcAddress::getProvince, sysPcAddressDto.getProvince()) .eq(StringUtils.isNotEmpty(sysPcAddressDto.getCity()), SysPcAddress::getCity, sysPcAddressDto.getCity()) .eq(StringUtils.isNotEmpty(sysPcAddressDto.getDistrict()), SysPcAddress::getDistrict, sysPcAddressDto.getDistrict()) .eq(StringUtils.isNotEmpty(sysPcAddressDto.getSource()), SysPcAddress::getSource, sysPcAddressDto.getSource()) .orderByDesc(SysPcAddress::getOperationTime)); return sysPcAddresses; } @Override public List findLocationsWithinRadius(AdressRangeDto adressRangeDto) { geoService.addLocations(list(), PC_GEO_KEY); List nearbyIds = geoService.findNearbyIds(PC_GEO_KEY, adressRangeDto.getLongitude(), adressRangeDto.getLatitude(), adressRangeDto.getRadius()); return listByIds(nearbyIds); } @Override public SysPcAddressNumVo queryPcNum() { SysPcAddressNumVo sysPcAddressNumVo = new SysPcAddressNumVo(); Long all = sysPcAddressMapper.selectCount(new LambdaQueryWrapper()); Long channelNum = sysPcAddressMapper.selectCount(new LambdaQueryWrapper().eq(SysPcAddress::getSource,1)); Long proxyNum = sysPcAddressMapper.selectCount(new LambdaQueryWrapper().eq(SysPcAddress::getSource,2)); sysPcAddressNumVo.setAllNum(all); sysPcAddressNumVo.setProxyNum(proxyNum); sysPcAddressNumVo.setChannelNum(channelNum); return sysPcAddressNumVo; } @Override public List queryRegionInfo(Integer type) { List regionInfoVos = new ArrayList<>(); //全部:0 渠道:1 代理:2 if (type == 0){ RegionInfoVo regionInfoVo = new RegionInfoVo(); List list = list(); Map> map = list.stream().collect(Collectors.groupingBy(SysPcAddress::getDistrict)); // map.forEach(); // for (Map.Entry> entry : map.entrySet()) { // String key = entry.getKey(); // List list = entry.getValue(); // int listSize = list.size(); // System.out.println("Key: " + key + ", List size: " + listSize); // } }else if (type == 1) { List sysPcAddresses = sysPcAddressMapper.selectList(new LambdaQueryWrapper().eq(SysPcAddress::getSource, 1)); }else if (type == 2){ List sysPcAddresses = sysPcAddressMapper.selectList(new LambdaQueryWrapper().eq(SysPcAddress::getSource, 2)); } return regionInfoVos; } }