liujie
2026-08-04 e9e42b621afdf6ac94f5327ee843b32b10a401b4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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";
    }
}