Commit e2bb6b23 by 吕明尚

更改Redis自动任务

parent c55716fa
......@@ -2,7 +2,6 @@ package share.web.controller.system;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.injector.methods.Insert;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
......@@ -12,8 +11,11 @@ import org.springframework.web.bind.annotation.RestController;
import share.common.core.domain.R;
import share.common.enums.ReceiptRdeisEnum;
import share.system.domain.vo.SessionVo;
import share.system.service.QPService;
import java.text.SimpleDateFormat;
@RestController
@RequestMapping("/chessCards")
......@@ -30,11 +32,8 @@ public class QPController {
}
@GetMapping("/queryKey")
public R<String> queryKey() {
JSONObject o = (JSONObject) redisTemplate.opsForValue().get(ReceiptRdeisEnum.MT_SESSION.getValue());
Long expiresIn = (Long) o.get("expires_in");
Integer remainRefreshCount = (Integer) o.get("remain_refresh_count");
return R.ok(new JSONObject().set("expires_in", expiresIn).set("remain_refresh_count", remainRefreshCount).toString());
public R<SessionVo> queryKey() {
return R.ok(qpService.queryKey());
}
}
\ No newline at end of file
......@@ -181,9 +181,5 @@ public class SStoreController extends BaseController
return toAjax(sStoreService.insertStoreConsumers(storeId, consumerIds));
}
/**
* 数据同步
*/
}
......@@ -3,8 +3,8 @@ package share.common.enums;
public enum ReceiptRdeisEnum {
//1:验劵准备 2:session换取
PREPARE(1, "TUANGOU.RECEIPT.PREPARE."),
SESSION(2, "MT_SESSION_KEY"),
MT_SESSION(3, "MT_SESSION_OBJECT_KEY");
MT_SESSION_KEY(2, "MT_SESSION_KEY"),
MT_SESSION_OBJECT_KEY(3, "MT_SESSION_OBJECT_KEY");
private Integer code;
......
......@@ -43,13 +43,13 @@ public class RedisTask {
}
public void AutomaticMtSessionKey() {
Boolean b = redisTemplate.hasKey(ReceiptRdeisEnum.SESSION.getValue());
Boolean b = redisTemplate.hasKey(ReceiptRdeisEnum.MT_SESSION_KEY.getValue());
if (!b) {
JSONObject o = (JSONObject) redisTemplate.opsForValue().get(ReceiptRdeisEnum.MT_SESSION.getValue());
JSONObject o = (JSONObject) redisTemplate.opsForValue().get(ReceiptRdeisEnum.MT_SESSION_OBJECT_KEY.getValue());
String refreshToken = (String) o.get("refresh_token");
CustomerRefreshTokenResponse customerRefreshTokenResponse = qpService.refreshToken(refreshToken);
redisTemplate.opsForValue().set(ReceiptRdeisEnum.MT_SESSION.getValue(), new JSONObject(customerRefreshTokenResponse));
redisTemplate.opsForValue().set(ReceiptRdeisEnum.SESSION.getValue(), new JSONObject(customerRefreshTokenResponse), customerRefreshTokenResponse.getExpires_in() - 60 * 30);
redisTemplate.opsForValue().set(ReceiptRdeisEnum.MT_SESSION_OBJECT_KEY.getValue(), new JSONObject(customerRefreshTokenResponse));
redisTemplate.opsForValue().set(ReceiptRdeisEnum.MT_SESSION_KEY.getValue(), new JSONObject(customerRefreshTokenResponse), customerRefreshTokenResponse.getExpires_in() - 60 * 30);
}
}
}
package share.system.domain.vo;
import lombok.Data;
@Data
public class SessionVo {
private long expiresIn;
private Integer remainRefreshCount;
private String expirationTime;
}
......@@ -14,14 +14,14 @@ public interface DeviceOpService {
* 小程序订单开门
* @param deviceParam
*/
void openDoor(DeviceParamVo deviceParam, String userName);
void openDoor(DeviceParamVo deviceParam);
/**
* 小程序订单开门
* @param roomId 房间id
* @param phone 操作用户
*/
void openDoor(Long roomId, String phone, String userName);
void openDoor(Long roomId, String phone);
/**
* 清除网关锁id列表
......
......@@ -95,4 +95,6 @@ public interface ISStoreService extends IService<SStore>
int insertStoreConsumers(Long storeId, Long[] consumerIds);
List<SStore> listVoByCleaner();
int dataSync();
}
package share.system.service;
import com.dianping.openapi.sdk.api.customerauth.session.entity.CustomerKeyShopScopeResponseEntity;
import com.dianping.openapi.sdk.api.oauth.entity.CustomerRefreshTokenResponse;
import com.dianping.openapi.sdk.api.tuangou.entity.*;
import share.system.domain.vo.SessionVo;
import share.system.domain.vo.TuangouReceiptPrepareResponseEntityVo;
import java.util.List;
......@@ -24,4 +26,8 @@ public interface QPService {
List<TuangouDealQueryShopDealResponseEntity> queryshopdeal(String openShopUuid);
CustomerRefreshTokenResponse refreshToken(String newToken);
SessionVo queryKey();
List<CustomerKeyShopScopeResponseEntity> scope();
}
......@@ -40,7 +40,7 @@ public class DeviceOpServiceImpl implements DeviceOpService {
* 小程序订单开门
* @param deviceParam
*/
public void openDoor(DeviceParamVo deviceParam, String userName) {
public void openDoor(DeviceParamVo deviceParam) {
SRoom room = roomMapper.selectSRoomById(deviceParam.getRoomId());
if (room != null) {
Device param = new Device();
......@@ -49,11 +49,11 @@ public class DeviceOpServiceImpl implements DeviceOpService {
for (Device device : list) {
if (DeviceType.DEVICE_CCEE.getCode().equals(device.getDevType())) {
// 门锁
this.deviceOpInit(device.getDevId(), deviceParam.getPhone(), "10", userName);
this.deviceOpInit(device.getDevId(), deviceParam.getPhone(), "10");
}
if (DeviceType.DEVICE_0001.getCode().equals(device.getDevType())) {
// 取电开关
this.deviceOpInit(device.getDevId(), deviceParam.getPhone(), "20", userName);
this.deviceOpInit(device.getDevId(), deviceParam.getPhone(), "20");
}
}
......@@ -65,7 +65,7 @@ public class DeviceOpServiceImpl implements DeviceOpService {
* @param roomId 房间id
* @param phone 操作用户
*/
public void openDoor(Long roomId, String phone, String userName) {
public void openDoor(Long roomId, String phone) {
SRoom room = roomMapper.selectSRoomById(roomId);
if (room != null) {
Device param = new Device();
......@@ -74,24 +74,24 @@ public class DeviceOpServiceImpl implements DeviceOpService {
for (Device device : list) {
if (DeviceType.DEVICE_CCEE.getCode().equals(device.getDevType())) {
// 门锁
this.deviceOpInit(device.getDevId(), phone, "10", userName);
this.deviceOpInit(device.getDevId(), phone, "10");
}
if (DeviceType.DEVICE_0001.getCode().equals(device.getDevType())) {
// 取电开关
this.deviceOpInit(device.getDevId(), phone, "20", userName);
this.deviceOpInit(device.getDevId(), phone, "20");
}
}
}
}
private void deviceOpInit(String devId, String phone, String opType, String userName) {
private void deviceOpInit(String devId, String phone, String opType) {
// 获取mqtt的topic、payload
MqttxVo mqttxVo = mqttxService.openOrCloseDevice(devId, phone, opType);
// 发送mqtt消息
mqttGatewayComponent.sendToMqtt(mqttxVo.getTopic(), 0, mqttxVo.getPayload());
// 写日志记录
deviceLogService.addDeviceLog(mqttxVo, userName);
deviceLogService.addDeviceLog(mqttxVo, phone);
}
/**
......
......@@ -3,6 +3,10 @@ package share.system.service.impl;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dianping.openapi.sdk.api.customerauth.session.CustomerKeyShopScopeQuery;
import com.dianping.openapi.sdk.api.customerauth.session.entity.CustomerKeyShopScopeRequest;
import com.dianping.openapi.sdk.api.customerauth.session.entity.CustomerKeyShopScopeResponse;
import com.dianping.openapi.sdk.api.customerauth.session.entity.CustomerKeyShopScopeResponseEntity;
import com.dianping.openapi.sdk.api.oauth.CustomerRefreshToken;
import com.dianping.openapi.sdk.api.oauth.DynamicToken;
import com.dianping.openapi.sdk.api.oauth.entity.CustomerRefreshTokenResponse;
......@@ -20,20 +24,18 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import share.common.core.redis.RedisUtil;
import share.common.enums.*;
import share.common.utils.DateUtils;
import share.system.domain.Device;
import share.system.domain.SConsumer;
import share.system.domain.SConsumerCoupon;
import share.system.domain.SCoupon;
import share.system.domain.vo.FrontTokenComponent;
import share.system.domain.vo.SessionVo;
import share.system.domain.vo.TuangouReceiptPrepareResponseEntityVo;
import share.system.service.ISConsumerCouponService;
import share.system.service.ISCouponService;
import share.system.service.QPService;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
......@@ -60,43 +62,25 @@ public class QPServiceImpl implements QPService {
public TuangouReceiptPrepareResponseEntityVo consumeByUser(String code, String openShopUuid) {
//验券准备
TuangouReceiptPrepareResponseEntity prepare = prepare(code, openShopUuid);
//根据优惠卷名称查询优惠劵配置
SCoupon sCoupon = isCouponService.selectSCouponByName(prepare.getDeal_title());
if (sCoupon == null) {
throw new RuntimeException("未找到对应的优惠券");
}
//获取用户信息
SConsumer user = FrontTokenComponent.getWxSConsumerEntry();
TuangouReceiptPrepareResponseEntityVo response = new TuangouReceiptPrepareResponseEntityVo();
//查询领取记录表
LambdaQueryWrapper<SConsumerCoupon> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SConsumerCoupon::getDealId, prepare.getDeal_id());
queryWrapper.eq(SConsumerCoupon::getCouponCode, code);
queryWrapper.eq(SConsumerCoupon::getConsumerId, user.getId());
SConsumerCoupon one = isConsumerCouponService.getOne(queryWrapper);
//判断是否领取过
if (ObjectUtils.isEmpty(one)) {
if (ObjectUtils.isNotEmpty(one)) {
//领取删除原来的
isConsumerCouponService.deleteSConsumerCouponById(one.getId());
//判断redis键是否存在
if (Boolean.TRUE.equals(redisTemplate.hasKey(ReceiptRdeisEnum.PREPARE.getValue() + prepare.getReceipt_code()))) {
//获取redis中的值
JSONObject jsonObject = (JSONObject) redisTemplate.opsForValue().get(prepare.getReceipt_code());
//获取过期时间
Date expirationTime = jsonObject.getDate("expirationTime");
//判断是否过期
if (expirationTime.getTime() < new Date().getTime()) {
throw new RuntimeException("券码已过期");
}
//获取redis中的券码id
Long consumerCouponId = jsonObject.getLong("consumerCouponId");
//删除redis中的值
redisTemplate.delete(ReceiptRdeisEnum.PREPARE.getValue() + prepare.getReceipt_code());
//删除数据库中的值
isConsumerCouponService.deleteSConsumerCouponById(consumerCouponId);
}
//删除redis
redisTemplate.delete(ReceiptRdeisEnum.PREPARE.getValue() + prepare.getReceipt_code());
}
if (ObjectUtils.isNotEmpty(sCoupon)) {
//根据优惠卷名称查询优惠劵配置
SCoupon sCoupon = isCouponService.selectSCouponByName(prepare.getDeal_title());
TuangouReceiptPrepareResponseEntityVo response = new TuangouReceiptPrepareResponseEntityVo();
if (ObjectUtils.isEmpty(sCoupon)) {
SConsumerCoupon sConsumerCoupon = new SConsumerCoupon();
sConsumerCoupon.setDealId(prepare.getDeal_id());
sConsumerCoupon.setCouponCode(code);
......@@ -188,7 +172,7 @@ public class QPServiceImpl implements QPService {
*/
@Override
public TuangouReceiptPrepareResponseEntity prepare(String code, String openShopUuid) {
JSONObject sessionKey = (JSONObject) redisTemplate.opsForValue().get(ReceiptRdeisEnum.MT_SESSION.getValue());
JSONObject sessionKey = (JSONObject) redisTemplate.opsForValue().get(ReceiptRdeisEnum.MT_SESSION_KEY.getValue());
String session = (String) sessionKey.get("access_token");
TuangouReceiptPrepareRequest request = new TuangouReceiptPrepareRequest(APP_KEY, APP_SECRET, session, code, "", openShopUuid);
TuangouReceiptPrepare tuangouReceiptPrepare = new TuangouReceiptPrepare(request);
......@@ -206,7 +190,7 @@ public class QPServiceImpl implements QPService {
*/
@Override
public List<TuangouReceiptConsumeResponseEntity> consume(String code, int count, String openShopUuid) {
JSONObject sessionKey = (JSONObject) redisTemplate.opsForValue().get(ReceiptRdeisEnum.MT_SESSION.getValue());
JSONObject sessionKey = (JSONObject) redisTemplate.opsForValue().get(ReceiptRdeisEnum.MT_SESSION_KEY.getValue());
String session = (String) sessionKey.get("access_token");
SConsumer user = FrontTokenComponent.getWxSConsumerEntry();
String requestid = UUID.randomUUID().toString();
......@@ -228,7 +212,7 @@ public class QPServiceImpl implements QPService {
*/
@Override
public List<TuangouReceiptReverseConsumeResponseEntity> reverseconsume(String code, Long appDealId, String openShopUuid) {
JSONObject sessionKey = (JSONObject) redisTemplate.opsForValue().get(ReceiptRdeisEnum.MT_SESSION.getValue());
JSONObject sessionKey = (JSONObject) redisTemplate.opsForValue().get(ReceiptRdeisEnum.MT_SESSION_KEY.getValue());
String session = (String) sessionKey.get("access_token");
SConsumer user = FrontTokenComponent.getWxSConsumerEntry();
TuangouReceiptReverseConsumeRequest tuangouReceiptReverseConsumeRequest = new TuangouReceiptReverseConsumeRequest(APP_KEY, APP_SECRET, session,
......@@ -249,7 +233,7 @@ public class QPServiceImpl implements QPService {
*/
@Override
public TuangouReceiptGetConsumedReponseEntity getconsumed(String code, String openShopUuid) {
JSONObject sessionKey = (JSONObject) redisTemplate.opsForValue().get(ReceiptRdeisEnum.MT_SESSION.getValue());
JSONObject sessionKey = (JSONObject) redisTemplate.opsForValue().get(ReceiptRdeisEnum.MT_SESSION_KEY.getValue());
String session = (String) sessionKey.get("access_token");
DefaultOpenAPIClient openAPIClient = new DefaultOpenAPIClient();
TuangouReceiptGetConsumedRequest request = new TuangouReceiptGetConsumedRequest(APP_KEY, APP_SECRET,
......@@ -269,8 +253,8 @@ public class QPServiceImpl implements QPService {
APP_SECRET, GrantType.AUTHORIZATION_CODE.getValue(), authCode);
DynamicToken dynamicToken = new DynamicToken(request);
DynamicTokenResponse response = openAPIClient.invoke(dynamicToken);
redisTemplate.opsForValue().set(ReceiptRdeisEnum.MT_SESSION.getValue(), new JSONObject(response));
redisTemplate.opsForValue().set(ReceiptRdeisEnum.SESSION.getValue(), new JSONObject(response), response.getExpires_in() - 60 * 30);
redisTemplate.opsForValue().set(ReceiptRdeisEnum.MT_SESSION_OBJECT_KEY.getValue(), new JSONObject(response));
redisTemplate.opsForValue().set(ReceiptRdeisEnum.MT_SESSION_KEY.getValue(), new JSONObject(response), response.getExpires_in() - 60 * 30);
return response.getAccess_token();
}
......@@ -283,7 +267,7 @@ public class QPServiceImpl implements QPService {
* @param app_shop_id
*/
DefaultOpenAPIClient openAPIClient = new DefaultOpenAPIClient();
JSONObject sessionKey = (JSONObject) redisTemplate.opsForValue().get(ReceiptRdeisEnum.MT_SESSION.getValue());
JSONObject sessionKey = (JSONObject) redisTemplate.opsForValue().get(ReceiptRdeisEnum.MT_SESSION_KEY.getValue());
String session = (String) sessionKey.get("access_token");
TuangouDealQueryShopDealRequest request = new TuangouDealQueryShopDealRequest(APP_KEY, APP_SECRET, session, "", 1, 100, openShopUuid);
TuangouDealQueryShopDeal tuangouDealQueryShopDeal = new TuangouDealQueryShopDeal(request);
......@@ -302,4 +286,34 @@ public class QPServiceImpl implements QPService {
return response;
}
@Override
public SessionVo queryKey() {
JSONObject o = (JSONObject) redisTemplate.opsForValue().get(ReceiptRdeisEnum.MT_SESSION_OBJECT_KEY.getValue());
Long expiresIn = (Long) o.get("expires_in");
Integer remainRefreshCount = (Integer) o.get("remain_refresh_count");
SessionVo sessionVo = new SessionVo();
sessionVo.setExpiresIn(expiresIn);
sessionVo.setRemainRefreshCount(remainRefreshCount);
Date date = DateUtils.addSeconds(new Date(), expiresIn.intValue() - 60 * 30);
SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String output1 = format2.format(date);
TuangouReceiptPrepareResponseEntityVo vo = new TuangouReceiptPrepareResponseEntityVo();
vo.setExpirationTime(output1);
return sessionVo;
}
@Override
public List<CustomerKeyShopScopeResponseEntity> scope() {
DefaultOpenAPIClient openAPIClient = new DefaultOpenAPIClient();
JSONObject sessionKey = (JSONObject) redisTemplate.opsForValue().get(ReceiptRdeisEnum.MT_SESSION_KEY.getValue());
String session = (String) sessionKey.get("access_token");
String bid = (String) sessionKey.get("bid");
// CustomerKeyShopScopeRequest request = new CustomerKeyShopScopeRequest(APP_KEY, APP_SECRET, "13ff8a38219075fafbbdeea6839450ed6bea1b9b","f1fd23c1c413862137b895bdcfc10ef8");
CustomerKeyShopScopeRequest request = new CustomerKeyShopScopeRequest(APP_KEY, APP_SECRET, session, bid);
CustomerKeyShopScopeQuery shopScopeQuery = new CustomerKeyShopScopeQuery(request);
CustomerKeyShopScopeResponse response = openAPIClient.invoke(shopScopeQuery);
return response.getData();
}
}
......@@ -526,7 +526,7 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper,SOrder> implemen
}
}
SConsumer user = FrontTokenComponent.getWxSConsumerEntry();
deviceOpService.openDoor(sRoomVo.getId(), user.getPhone(), user.getNickName());
deviceOpService.openDoor(sRoomVo.getId(), user.getPhone());
return "开锁成功";
}
......
......@@ -234,7 +234,7 @@ public class SRoomServiceImpl extends ServiceImpl<SRoomMapper, SRoom> implements
if (Objects.isNull(one)) {
throw new BaseException("设备不存在!");
}
deviceOpService.openDoor(id, user.getPhone(), user.getNickName());
deviceOpService.openDoor(id, user.getPhone());
return "开锁成功";
}
......
......@@ -4,6 +4,8 @@ import java.util.*;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dianping.openapi.sdk.api.customerauth.session.entity.CustomerKeyShopScopeResponseEntity;
import lombok.var;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.record.DVALRecord;
......@@ -29,6 +31,7 @@ import share.system.request.SStoreRequest;
import share.system.service.IPackService;
import share.system.service.ISRoomService;
import share.system.service.ISStoreService;
import share.system.service.QPService;
/**
* 门店Service业务层处理
......@@ -48,6 +51,8 @@ public class SStoreServiceImpl extends ServiceImpl<SStoreMapper, SStore> impleme
private RoomPackMapper roomPackMapper;
@Autowired
private IPackService packService;
@Autowired
private QPService qpService;
/**
* 查询门店
......@@ -280,6 +285,31 @@ public class SStoreServiceImpl extends ServiceImpl<SStoreMapper, SStore> impleme
return storeList;
}
@Override
public int dataSync() {
List<SStore> sStores = sStoreMapper.selectSStoreList(new SStore());
//查询美团接口查看改门店是否存在
List<CustomerKeyShopScopeResponseEntity> scope = qpService.scope();
if (CollectionUtils.isEmpty(scope)) {
return 0; //美团接口查不到门店信息,直接返回0,不同步门店信息。
}
//获取差集
List<SStore> collect = sStores.stream().filter(item -> {
boolean flag = true;
for (CustomerKeyShopScopeResponseEntity entity : scope) {
if (item.getName().equals(entity.getShopname() + "(" + entity.getBranchname() + ")")) {
flag = false;
break;
}
}
return flag;
}).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(collect)) {
return collect.size();
}
return 0;
}
private List<Map> convertOptionList(List<SStore> storeList) {
List<Map> result = new ArrayList<>();
if (CollectionUtils.isNotEmpty(storeList)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment