|
|
@@ -5,14 +5,26 @@ import com.github.microservice.models.gateDoor.use.GateDoorUseParam;
|
|
|
import com.github.microservice.models.gateDoor.use.GateDoorUseResult;
|
|
|
import com.github.microservice.net.ResultContent;
|
|
|
import com.zhongshu.card.client.model.devices.DeviceUseRecordModel;
|
|
|
+import com.zhongshu.card.client.type.DataState;
|
|
|
+import com.zhongshu.card.client.type.UserState;
|
|
|
+import com.zhongshu.card.client.type.school.CardState;
|
|
|
+import com.zhongshu.card.client.utils.DateUtils;
|
|
|
+import com.zhongshu.card.server.core.dao.devices.DeviceInfoDao;
|
|
|
+import com.zhongshu.card.server.core.dao.devices.DevicePermissDao;
|
|
|
import com.zhongshu.card.server.core.dao.devices.DeviceUseRecordDao;
|
|
|
+import com.zhongshu.card.server.core.dao.org.UserCountDao;
|
|
|
+import com.zhongshu.card.server.core.dao.school.CardInfoDao;
|
|
|
+import com.zhongshu.card.server.core.domain.devices.DeviceInfo;
|
|
|
+import com.zhongshu.card.server.core.domain.devices.DevicePermiss;
|
|
|
import com.zhongshu.card.server.core.domain.devices.DeviceUseRecord;
|
|
|
-import com.zhongshu.card.server.core.domain.payment.ExpenseFlow;
|
|
|
+import com.zhongshu.card.server.core.domain.org.UserAccount;
|
|
|
+import com.zhongshu.card.server.core.domain.school.CardInfo;
|
|
|
import com.zhongshu.card.server.core.service.base.SuperService;
|
|
|
import com.zhongshu.card.server.core.util.BeanUtils;
|
|
|
import com.zhongshu.card.server.core.util.TokenUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
@@ -27,6 +39,18 @@ public class DeviceUseRecordService extends SuperService {
|
|
|
@Autowired
|
|
|
private DeviceUseRecordDao deviceUseRecordDao;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ protected DeviceInfoDao deviceInfoDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CardInfoDao cardInfoDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DevicePermissDao devicePermissDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserCountDao userCountDao;
|
|
|
+
|
|
|
/**
|
|
|
* 设备使用日志 (物联网来的数据)
|
|
|
*
|
|
|
@@ -36,21 +60,164 @@ public class DeviceUseRecordService extends SuperService {
|
|
|
public ResultContent saveDeviceLogs(GateDoorUseParam param) {
|
|
|
// 返回数据
|
|
|
GateDoorUseResult result = new GateDoorUseResult();
|
|
|
- // 是否是离线的日志(离线的话已经操作了,不能验证数据)
|
|
|
- Boolean isOffLine = param.getIsOffLine();
|
|
|
- if (isOffLine != null && isOffLine) {
|
|
|
- // 离线的数据
|
|
|
- }
|
|
|
- String deviceId = param.getDeviceId();
|
|
|
+ String mqttDataId = param.getMqttDataId();
|
|
|
+ if (StringUtils.isNotEmpty(mqttDataId)) {
|
|
|
+ DeviceUseRecord deviceUseRecord = init(mqttDataId);
|
|
|
+ if (ObjectUtils.isNotEmpty(deviceUseRecord)) {
|
|
|
+ BeanUtils.copyProperties(param, deviceUseRecord);
|
|
|
+ String deviceId = param.getDeviceId();
|
|
|
+ String cardNo = param.getCardNo();
|
|
|
+ String userId = param.getUserId();
|
|
|
+ String projectOid = "";
|
|
|
+ DeviceInfo deviceInfo = null;
|
|
|
+ if (StringUtils.isNotEmpty(deviceId)) {
|
|
|
+ deviceInfo = deviceInfoDao.findTopByDeviceId(deviceId);
|
|
|
+ if (ObjectUtils.isNotEmpty(deviceInfo)) {
|
|
|
+ projectOid = deviceInfo.getProjectOid();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ deviceUseRecord.setDeviceId(deviceId);
|
|
|
+ if (ObjectUtils.isNotEmpty(deviceInfo)) {
|
|
|
+ // 设备信息
|
|
|
+ deviceUseRecord.setDeviceInfo(deviceInfo);
|
|
|
+ deviceUseRecord.setDeviceType(deviceInfo.getDeviceType());
|
|
|
+ deviceUseRecord.setGateWayId(deviceInfo.getGateWayId());
|
|
|
|
|
|
+ deviceUseRecord.setBeLongOrganization(deviceInfo.getOrganization());
|
|
|
+ deviceUseRecord.setBeLongOid(deviceInfo.getBeLongOid());
|
|
|
+ deviceUseRecord.setBeLongName(deviceInfo.getBeLongName());
|
|
|
+ }
|
|
|
|
|
|
- result.setMqttDataId(param.getMqttDataId());
|
|
|
+ // 卡片信息
|
|
|
+ CardInfo cardInfo = null;
|
|
|
+ if (StringUtils.isNotEmpty(cardNo)) {
|
|
|
+ cardInfo = cardInfoDao.findByCode(cardNo);
|
|
|
+ if (ObjectUtils.isNotEmpty(cardInfo)) {
|
|
|
+ userId = cardInfo.getUserId();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ deviceUseRecord.setCardNo(cardNo);
|
|
|
+ if (ObjectUtils.isNotEmpty(cardInfo)) {
|
|
|
+ // 卡片信息
|
|
|
+ deviceUseRecord.setCardInfo(cardInfo);
|
|
|
+ }
|
|
|
+ UserAccount userAccount = null;
|
|
|
+ if (StringUtils.isNotEmpty(userId)) {
|
|
|
+ userAccount = userCountDao.findTopByUserId(userId);
|
|
|
+ }
|
|
|
+ deviceUseRecord.setUserId(userId);
|
|
|
+ deviceUseRecord.setUserAccount(userAccount);
|
|
|
+
|
|
|
+ if (StringUtils.isNotEmpty(param.getTime())) {
|
|
|
+ deviceUseRecord.setTimestamp(DateUtils.timeToLong(param.getTime(), DateUtils.FORMAT_LONG));
|
|
|
+ }
|
|
|
+ deviceUseRecord.setProjectOid(projectOid);
|
|
|
+ // 是否是离线的日志(离线的话已经操作了,不能验证数据)
|
|
|
+ Boolean isOffLine = param.getIsOffLine();
|
|
|
+ if (isOffLine == null) {
|
|
|
+ isOffLine = Boolean.FALSE;
|
|
|
+ }
|
|
|
+ deviceUseRecord.setIsOffLine(isOffLine);
|
|
|
+ if (isOffLine != null && isOffLine) {
|
|
|
+ Boolean isPassed = param.getIsPassed();
|
|
|
+ if (isPassed == null) {
|
|
|
+ isPassed = Boolean.FALSE;
|
|
|
+ }
|
|
|
+ // 离线的数据
|
|
|
+ deviceUseRecord.setIsPassed(isPassed);
|
|
|
+ if (isPassed) {
|
|
|
+ deviceUseRecord.setMsg("操作成功");
|
|
|
+ } else {
|
|
|
+ deviceUseRecord.setMsg("验证失败");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 在线验证
|
|
|
+ checkUseDeviceData(deviceUseRecord);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ result.setFailed("mqttDataId为空");
|
|
|
+ }
|
|
|
+ result.setMqttDataId(mqttDataId);
|
|
|
return ResultContent.buildSuccess(JSONUtil.toJsonStr(result));
|
|
|
}
|
|
|
|
|
|
public DeviceUseRecord init(String mqttDataId) {
|
|
|
- DeviceUseRecord expenseFlow = deviceUseRecordDao.init(mqttDataId, TokenUtil.create());
|
|
|
- return expenseFlow;
|
|
|
+ DeviceUseRecord entity = deviceUseRecordDao.init(mqttDataId, TokenUtil.create());
|
|
|
+ entity.setCreateTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
|
|
|
+ return entity;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void checkUseDeviceData(DeviceUseRecord entity) {
|
|
|
+ // 主要是从设备、卡片等的基础信息验证
|
|
|
+ if (ObjectUtils.isNotEmpty(entity)) {
|
|
|
+ // 1. 检查设备
|
|
|
+ if (ObjectUtils.isEmpty(entity.getDeviceInfo())) {
|
|
|
+ entity.setIsPassed(Boolean.FALSE);
|
|
|
+ entity.setMsg("设备信息未找到");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ DeviceInfo deviceInfo = entity.getDeviceInfo();
|
|
|
+ if (deviceInfo.getState() == null || deviceInfo.getState() != DataState.Enable) {
|
|
|
+ entity.setIsPassed(Boolean.FALSE);
|
|
|
+ entity.setMsg("设备未启用");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //2. 验证用户信息
|
|
|
+ UserAccount userAccount = entity.getUserAccount();
|
|
|
+ if (ObjectUtils.isEmpty(userAccount)) {
|
|
|
+ entity.setIsPassed(Boolean.FALSE);
|
|
|
+ entity.setMsg("用户信息未确定");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (userAccount.getState() == null || userAccount.getState() != UserState.Normal) {
|
|
|
+ entity.setIsPassed(Boolean.FALSE);
|
|
|
+ entity.setMsg(String.format("用户已禁用"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 验证用户是否有设备申请权限
|
|
|
+ if (deviceInfo.getIsOpenUse() == null || !deviceInfo.getIsOpenUse()) {
|
|
|
+ DevicePermiss devicePermiss = devicePermissDao.findTopByUserIdAndDeviceIdAndProjectOid(entity.getUserId(), entity.getDeviceId(), entity.getProjectOid());
|
|
|
+ if (ObjectUtils.isEmpty(devicePermiss)) {
|
|
|
+ entity.setIsPassed(Boolean.FALSE);
|
|
|
+ entity.setMsg("用户没有设备权限");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (devicePermiss.getDataState() == null || devicePermiss.getDataState() != DataState.Enable) {
|
|
|
+ entity.setIsPassed(Boolean.FALSE);
|
|
|
+ entity.setMsg("用户权限未启用");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Integer mode = entity.getMode();
|
|
|
+ if (mode == null) {
|
|
|
+ entity.setIsPassed(Boolean.FALSE);
|
|
|
+ entity.setMsg("使用模式未确定");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (mode == 0) {
|
|
|
+ // 刷卡
|
|
|
+ if (ObjectUtils.isEmpty(entity.getCardInfo())) {
|
|
|
+ entity.setIsPassed(Boolean.FALSE);
|
|
|
+ entity.setMsg("卡片未找到");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ CardInfo cardInfo = entity.getCardInfo();
|
|
|
+ if (cardInfo.getIsCanceled() != null && cardInfo.getIsCanceled()) {
|
|
|
+ entity.setIsPassed(Boolean.FALSE);
|
|
|
+ entity.setMsg("卡片已作废");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (cardInfo.getCardState() == null || cardInfo.getCardState() != CardState.Enable) {
|
|
|
+ entity.setIsPassed(Boolean.FALSE);
|
|
|
+ entity.setMsg(String.format("卡片%s", cardInfo.getCardState().getRemark()));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|