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;
|
}
|
}
|