package com.xy.alipay; import cn.hutool.json.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.xy.collections.list.JArrayList; import com.xy.collections.map.JMap; import com.xy.dto.CommandMqtt; import com.xy.dto.DeviceChangeStatusDTO; import com.xy.dto.MercMiniDeviceDto; import com.xy.entity.DeviceInfo; import com.xy.service.AlipayDeviceService; import com.xy.service.DeviceInfoServiceImpl; import com.xy.utils.AuthorizeUtils; import com.xy.utils.R; import com.xy.utils.enums.DictsSonEnum; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; @Slf4j @Aspect @Component @AllArgsConstructor public class AliPayAspet { private DeviceInfoServiceImpl deviceInfoService; private AlipayDeviceService alipayDeviceService; /** * 支付宝设备 * * @param joinPoint * @param aliPay * @return * @throws Throwable */ @Around("@annotation(aliPay)") public Object timer(ProceedingJoinPoint joinPoint, AliPay aliPay) throws Throwable { String type = aliPay.value().getType(); Object[] args = joinPoint.getArgs(); Object result = R.ok(); try { boolean fal = before(type, args); if (fal) { result = joinPoint.proceed(args); } } catch (Exception e) { throw e; } return result; } public boolean before(String type, Object[] args) { boolean fal = true; //重启设备 if (type.equals(AliPay.Type.RESTART.getType())) { List restart = restart(args); if (restart.size() > 0) { args[0] = restart; } else { fal = false; } } //修改运营状态 if (type.equals(AliPay.Type.BUSY_STATE.getType())) { fal = busyState(args); } return fal; } /** * 重启设备 * * @param args */ private List restart(Object[] args) { List result = new ArrayList<>(); List commandMqtts = (List) args[0]; //查询支付宝设备信息 List deviceInfos = deviceInfoService.list(new LambdaQueryWrapper() .in(DeviceInfo::getDeviceId, new JArrayList<>(commandMqtts).comparing(CommandMqtt::getDeviceId).getProperty(CommandMqtt::getDeviceId)) .eq(DeviceInfo::getDeviceType, DictsSonEnum.DEVICE_TYPE_5.getKey()) ); if (deviceInfos.size() == 0) { return commandMqtts; } JMap deviceInfosJMaps = new JArrayList<>(deviceInfos).toMap(DeviceInfo::getDeviceId).cover(); commandMqtts.forEach(commandMqtt -> { JSONObject templet = commandMqtt.getTemplet(); JSONObject data = templet.getJSONObject("data"); DeviceInfo deviceInfo = deviceInfosJMaps.get(commandMqtt.getDeviceId()); if (deviceInfo == null) { result.add(commandMqtt); return; } //发送支付宝设备重启请求 if (data.getStr("type").equals("app") && data.getStr("task").equals("restart")) { alipayDeviceService.deviceReboot(deviceInfo.getDeviceId().toString()); } }); return result; } /** * 修改运营状态 * * @param args * @return */ private boolean busyState(Object[] args) { MercMiniDeviceDto.BusySate busySate = (MercMiniDeviceDto.BusySate) args[0]; //查询支付宝设备信息 DeviceInfo deviceInfo = deviceInfoService.getOne(new LambdaQueryWrapper() .eq(DeviceInfo::getDeviceId, busySate.getDeviceId()) .eq(DeviceInfo::getDeviceType, DictsSonEnum.DEVICE_TYPE_5.getKey()) ); if (deviceInfo == null) { return true; } //发送支付宝设备修改运营状态请求 DeviceChangeStatusDTO deviceChangeStatusDTO = new DeviceChangeStatusDTO() .setTerminalId(String.valueOf(busySate.getDeviceId())) .setOperatorId(AuthorizeUtils.getLoginId(String.class)) .setStatus(busySate.getBusyState()); alipayDeviceService.changeStatus(deviceChangeStatusDTO); return false; } }