admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.yeshi.fanli.service.impl.pdd;
 
import com.yeshi.fanli.dao.pdd.UserPDDAuthRecordDao;
import com.yeshi.fanli.dao.user.UserGoodsChannelConfigDao;
import com.yeshi.fanli.entity.goods.UserGoodsChannelConfig;
import com.yeshi.fanli.entity.pdd.UserPDDAuthRecord;
import com.yeshi.fanli.service.inter.pdd.PDDAuthService;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.pinduoduo.PinDuoDuoApiUtil;
import com.yeshi.fanli.util.pinduoduo.PinDuoDuoUtil;
import com.yeshi.fanli.vo.pdd.PDDConvertLinkResultVO;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
@Service
public class PDDAuthServiceImpl implements PDDAuthService {
 
    @Resource
    private UserPDDAuthRecordDao userPDDAuthRecordDao;
 
    @Resource
    private UserGoodsChannelConfigDao userGoodsChannelConfigDao;
 
 
    @Override
    public String createPDDAuthLink(Long uid, String pid) throws Exception {
        String customParams = PinDuoDuoUtil.getCustomParams(uid);
        PDDConvertLinkResultVO link = PinDuoDuoApiUtil.getAuthLink(pid, customParams);
        if (link == null) {
            throw new Exception("生成授权链接失败");
        }
        UserPDDAuthRecord record = new UserPDDAuthRecord();
        record.setUid(uid);
        record.setCreateTime(new Date());
        record.setPid(pid);
        record.setCustomParams(customParams);
        record.setId(UserPDDAuthRecord.createId(pid, customParams));
        record.setSuccess(false);
 
        if (userPDDAuthRecordDao.get(record.getId()) == null) {
            userPDDAuthRecordDao.save(record);
        }
        return link.getMobile_url();
    }
 
    @Override
    public UserPDDAuthRecord selectByPidAndCustomParams(String pid, String customParams) {
        return userPDDAuthRecordDao.selectByPidAndCustomParams(pid, customParams);
    }
 
    @Override
    public List<UserPDDAuthRecord> listNeedValidRecord(Date minCreateTime, int page, int pageSize) {
        UserPDDAuthRecordDao.DaoQuery query = new UserPDDAuthRecordDao.DaoQuery();
        query.success = false;
        query.minCreateTime = new Date(System.currentTimeMillis() - 1000 * 60 * 60L * 24 * 1);
        query.start = (page - 1) * pageSize;
        query.count = pageSize;
        return userPDDAuthRecordDao.list(query);
    }
 
    @Override
    public long countNeedValidRecord(Date minCreateTime) {
        UserPDDAuthRecordDao.DaoQuery query = new UserPDDAuthRecordDao.DaoQuery();
        query.success = false;
        query.minCreateTime = new Date(System.currentTimeMillis() - 1000 * 60 * 60L * 24 * 1);
        return userPDDAuthRecordDao.count(query);
    }
 
    @Override
    public void authSuccess(String id) {
        UserPDDAuthRecord record = userPDDAuthRecordDao.get(id);
        if (record == null) {
            return;
        }
        UserPDDAuthRecord update = new UserPDDAuthRecord();
        update.setId(record.getId());
        update.setSuccess(true);
        update.setSuccessTime(new Date());
        userPDDAuthRecordDao.updateSelectiveByPrimaryKey(update);
 
        UserGoodsChannelConfig config = userGoodsChannelConfigDao.get(record.getUid());
        if (config == null) {
            config = new UserGoodsChannelConfig();
            config.setUid(record.getUid());
            config.setPddFanliCustomerParams(record.getCustomParams());
            config.setPddFanliPid(record.getPid());
            config.setCreateTime(new Date());
            userGoodsChannelConfigDao.save(config);
        } else {
            UserGoodsChannelConfig updateConfig = new UserGoodsChannelConfig();
            updateConfig.setUid(record.getUid());
            updateConfig.setPddFanliCustomerParams(record.getCustomParams());
            updateConfig.setPddFanliPid(record.getPid());
            userGoodsChannelConfigDao.updateSelective(updateConfig);
        }
 
    }
 
    @Override
    public String getFanliCustomParams(Long uid) {
        UserGoodsChannelConfig config = userGoodsChannelConfigDao.get(uid);
        if (config == null || StringUtil.isNullOrEmpty(config.getPddFanliCustomerParams())) {
            return uid + "";
        }
        return config.getPddFanliCustomerParams();
    }
 
    @Override
    public UserPDDAuthRecord getLatestRecordByUid(Long uid) {
        UserPDDAuthRecordDao.DaoQuery query = new UserPDDAuthRecordDao.DaoQuery();
        query.uid = uid;
        List<Sort.Order> orderList = new ArrayList<>();
        orderList.add(new Sort.Order(Sort.Direction.DESC, "createTime"));
        query.sortList = orderList;
        query.count = 1;
        List<UserPDDAuthRecord> list = userPDDAuthRecordDao.list(query);
        return list != null && list.size() > 0 ? list.get(0) : null;
    }
}