package com.tourguide.modules.user.service;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.tourguide.integration.unipush.UniPushClient;
|
import com.tourguide.modules.user.entity.GuidePushDevice;
|
import com.tourguide.modules.user.mapper.GuidePushDeviceMapper;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.util.StringUtils;
|
|
import java.time.LocalDateTime;
|
|
@Service
|
public class GuidePushDeviceService {
|
private final GuidePushDeviceMapper deviceMapper;
|
private final UniPushClient uniPushClient;
|
|
public GuidePushDeviceService(GuidePushDeviceMapper deviceMapper, UniPushClient uniPushClient) {
|
this.deviceMapper = deviceMapper;
|
this.uniPushClient = uniPushClient;
|
}
|
|
@Transactional
|
public void bind(Long guideId, String cid, String platform) {
|
String normalizedCid = cid.trim();
|
deviceMapper.delete(new LambdaQueryWrapper<GuidePushDevice>()
|
.eq(GuidePushDevice::getCid, normalizedCid)
|
.ne(GuidePushDevice::getGuideId, guideId));
|
GuidePushDevice device = deviceMapper.selectById(guideId);
|
if (device == null) {
|
device = new GuidePushDevice();
|
device.setGuideId(guideId);
|
device.setCid(normalizedCid);
|
device.setPlatform(normalizePlatform(platform));
|
device.setCreatedAt(LocalDateTime.now());
|
deviceMapper.insert(device);
|
return;
|
}
|
device.setCid(normalizedCid);
|
device.setPlatform(normalizePlatform(platform));
|
device.setUpdatedAt(LocalDateTime.now());
|
deviceMapper.updateById(device);
|
}
|
|
public boolean push(Long guideId, String title, String content, String targetUrl) {
|
if (guideId == null) return false;
|
GuidePushDevice device = deviceMapper.selectById(guideId);
|
return device != null && uniPushClient.pushToCid(device.getCid(), title, content, targetUrl);
|
}
|
|
public void unbind(Long guideId) {
|
if (guideId != null) deviceMapper.deleteById(guideId);
|
}
|
|
private String normalizePlatform(String platform) {
|
return StringUtils.hasText(platform) ? platform.trim().toUpperCase() : "UNKNOWN";
|
}
|
}
|