|
@@ -1,21 +1,26 @@
|
|
package com.huimv.wine.ws;
|
|
package com.huimv.wine.ws;
|
|
|
|
|
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
|
import com.huimv.wine.common.EventWsErrUtil;
|
|
import com.huimv.wine.common.EventWsErrUtil;
|
|
import com.huimv.wine.entity.Change;
|
|
import com.huimv.wine.entity.Change;
|
|
|
|
+import com.huimv.wine.entity.Device;
|
|
|
|
+import com.huimv.wine.entity.Operation;
|
|
import com.huimv.wine.entity.Worker;
|
|
import com.huimv.wine.entity.Worker;
|
|
-import com.huimv.wine.entity.vo.WorkerQueryParam;
|
|
|
|
-import com.huimv.wine.entity.vo.WorkerQueryResult;
|
|
|
|
-import com.huimv.wine.entity.vo.WsEvent;
|
|
|
|
|
|
+import com.huimv.wine.entity.vo.*;
|
|
import com.huimv.wine.mapper.ChangeMapper;
|
|
import com.huimv.wine.mapper.ChangeMapper;
|
|
|
|
+import com.huimv.wine.mapper.DeviceMapper;
|
|
|
|
+import com.huimv.wine.mapper.OperationMapper;
|
|
import com.huimv.wine.mapper.WorkerMapper;
|
|
import com.huimv.wine.mapper.WorkerMapper;
|
|
import com.huimv.wine.utils.*;
|
|
import com.huimv.wine.utils.*;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.web.socket.WebSocketMessage;
|
|
import org.springframework.web.socket.WebSocketMessage;
|
|
import org.springframework.web.socket.handler.TextWebSocketHandler;
|
|
import org.springframework.web.socket.handler.TextWebSocketHandler;
|
|
import sun.nio.cs.ext.MacArabic;
|
|
import sun.nio.cs.ext.MacArabic;
|
|
@@ -25,8 +30,8 @@ import javax.websocket.server.PathParam;
|
|
import javax.websocket.server.ServerEndpoint;
|
|
import javax.websocket.server.ServerEndpoint;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
import java.time.Duration;
|
|
import java.time.Duration;
|
|
-import java.util.HashMap;
|
|
|
|
-import java.util.Map;
|
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
/**
|
|
* websocket接口处理类
|
|
* websocket接口处理类
|
|
@@ -82,29 +87,31 @@ public class WorkerController extends TextWebSocketHandler {
|
|
JSONObject jsonObject = JSON.parseObject(message);
|
|
JSONObject jsonObject = JSON.parseObject(message);
|
|
String event = jsonObject.getString("event");
|
|
String event = jsonObject.getString("event");
|
|
JSONObject data = jsonObject.getJSONObject("data");
|
|
JSONObject data = jsonObject.getJSONObject("data");
|
|
-
|
|
|
|
if ("login".equals(event)){
|
|
if ("login".equals(event)){
|
|
login(workerMapper,session,token,data);
|
|
login(workerMapper,session,token,data);
|
|
}else if ("pin".equals(event)){
|
|
}else if ("pin".equals(event)){
|
|
keepAlive(session);
|
|
keepAlive(session);
|
|
}else {
|
|
}else {
|
|
- if (!redisTemplate.hasKey(token)) {
|
|
|
|
|
|
+ if (!redisTemplate.hasKey("WorkerToken_"+token)) {
|
|
WebsocketWorkerUtil.sendMessage(session,new WsEvent("tokenExpired",null));
|
|
WebsocketWorkerUtil.sendMessage(session,new WsEvent("tokenExpired",null));
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
- String worker = (String)redisTemplate.opsForValue().get(token);
|
|
|
|
|
|
+ String wId = (String)redisTemplate.opsForValue().get(token);
|
|
|
|
+ Worker worker = workerMapper.selectById(wId);
|
|
|
|
+ worker.setLast(LocalDateTime.now());
|
|
|
|
+ workerMapper.updateById(worker);
|
|
switch (event){
|
|
switch (event){
|
|
case "query":
|
|
case "query":
|
|
- query(workerMapper,session,worker, data);
|
|
|
|
|
|
+ query(workerMapper,session,wId, data);
|
|
break;
|
|
break;
|
|
case "detail":
|
|
case "detail":
|
|
- detail(workerMapper,session,data);
|
|
|
|
|
|
+ detail(changeMapper,session,jsonObject.getInteger("data"));
|
|
break;
|
|
break;
|
|
case "ready":
|
|
case "ready":
|
|
- ready(changeMapper,session, jsonObject.getInteger("data"));
|
|
|
|
|
|
+ ready(changeMapper,session,redisTemplate,wId,jsonObject.getInteger("data"));
|
|
break;
|
|
break;
|
|
case "finish":
|
|
case "finish":
|
|
- finish(changeMapper,session,jsonObject.getInteger("data"));
|
|
|
|
|
|
+ finish(changeMapper,workerMapper,session,worker,jsonObject.getInteger("data"));
|
|
break;
|
|
break;
|
|
default:
|
|
default:
|
|
WebsocketWorkerUtil.sendMessage(session, EventWsErrUtil.getWsErr("unrecognized event"));
|
|
WebsocketWorkerUtil.sendMessage(session, EventWsErrUtil.getWsErr("unrecognized event"));
|
|
@@ -113,29 +120,170 @@ public class WorkerController extends TextWebSocketHandler {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private void finish(ChangeMapper changeMapper, Session session, Integer id) {
|
|
|
|
|
|
+ @Transactional
|
|
|
|
+ public void finish(ChangeMapper changeMapper, WorkerMapper workerMapper, Session session, Worker worker, Integer id) {
|
|
|
|
+ DeviceMapper deviceMapper = SpringContextUtil.getBean(DeviceMapper.class);
|
|
|
|
+ OperationMapper operationMapper = SpringContextUtil.getBean(OperationMapper.class);
|
|
Change change = changeMapper.selectById(id);
|
|
Change change = changeMapper.selectById(id);
|
|
if (ObjectUtil.isEmpty(change)){
|
|
if (ObjectUtil.isEmpty(change)){
|
|
WebsocketWorkerUtil.sendMessage(session,new WsEvent("finishResult",Result.fail("订单不存在")));
|
|
WebsocketWorkerUtil.sendMessage(session,new WsEvent("finishResult",Result.fail("订单不存在")));
|
|
|
|
+ return;
|
|
}
|
|
}
|
|
if (change.getStatus() != 1){
|
|
if (change.getStatus() != 1){
|
|
WebsocketWorkerUtil.sendMessage(session,new WsEvent("finishResult",Result.fail("请先完成上酒动作")));
|
|
WebsocketWorkerUtil.sendMessage(session,new WsEvent("finishResult",Result.fail("请先完成上酒动作")));
|
|
|
|
+ return;
|
|
}
|
|
}
|
|
|
|
+ ChangeVo changeVo = new ChangeVo();
|
|
|
|
+ BeanUtil.copyProperties(change,changeVo);
|
|
|
|
+ List<WineWithIdRemainVo> old = new ArrayList<>();
|
|
|
|
+ List<WineWithIdRemainVo> now = new ArrayList<>();
|
|
|
|
+
|
|
|
|
+ old.add(createWineWithIdRemain(change.getWine1o(),change.getRemain1o()));
|
|
|
|
+ old.add(createWineWithIdRemain(change.getWine2o(),change.getRemain2o()));
|
|
|
|
+ old.add(createWineWithIdRemain(change.getWine3o(),change.getRemain3o()));
|
|
|
|
+ old.add(createWineWithIdRemain(change.getWine4o(),change.getRemain4o()));
|
|
|
|
+
|
|
|
|
+ now.add(createWineWithIdRemain(change.getWine1n(),change.getRemain1n()));
|
|
|
|
+ now.add(createWineWithIdRemain(change.getWine2n(),change.getRemain2n()));
|
|
|
|
+ now.add(createWineWithIdRemain(change.getWine3n(),change.getRemain3n()));
|
|
|
|
+ now.add(createWineWithIdRemain(change.getWine4n(),change.getRemain4n()));
|
|
|
|
+
|
|
|
|
+ changeVo.setOld(old);
|
|
|
|
+ changeVo.setNow(now);
|
|
|
|
+
|
|
|
|
+ Device device = deviceMapper.selectById(change.getId());
|
|
|
|
+ DeviceVo deviceVo = new DeviceVo();
|
|
|
|
+ BeanUtil.copyProperties(device,deviceVo);
|
|
|
|
+ List<WineWithIdRemainVppVo> wines = new ArrayList<>();
|
|
|
|
+ wines.add(createWineWithIdRemainVpp(changeVo.getNow().get(0).getId(),changeVo.getNow().get(0).getRemain(),device.getVpp1()));
|
|
|
|
+ wines.add(createWineWithIdRemainVpp(changeVo.getNow().get(1).getId(),changeVo.getNow().get(1).getRemain(),device.getVpp2()));
|
|
|
|
+ wines.add(createWineWithIdRemainVpp(changeVo.getNow().get(2).getId(),changeVo.getNow().get(2).getRemain(),device.getVpp3()));
|
|
|
|
+ wines.add(createWineWithIdRemainVpp(changeVo.getNow().get(3).getId(),changeVo.getNow().get(3).getRemain(),device.getVpp4()));
|
|
|
|
+ deviceVo.setWines(wines);
|
|
|
|
+
|
|
|
|
+ device.setLast(new Date());
|
|
|
|
+ device.setWine1(deviceVo.getWines().get(0).getId());
|
|
|
|
+ device.setRemain1(deviceVo.getWines().get(0).getRemain());
|
|
|
|
+ device.setVpp1(deviceVo.getWines().get(0).getVpp());
|
|
|
|
+
|
|
|
|
+ device.setWine2(deviceVo.getWines().get(1).getId());
|
|
|
|
+ device.setRemain2(deviceVo.getWines().get(1).getRemain());
|
|
|
|
+ device.setVpp2(deviceVo.getWines().get(1).getVpp());
|
|
|
|
+
|
|
|
|
+ device.setWine3(deviceVo.getWines().get(2).getId());
|
|
|
|
+ device.setRemain3(deviceVo.getWines().get(2).getRemain());
|
|
|
|
+ device.setVpp3(deviceVo.getWines().get(2).getVpp());
|
|
|
|
+
|
|
|
|
+ device.setWine4(deviceVo.getWines().get(3).getId());
|
|
|
|
+ device.setRemain4(deviceVo.getWines().get(3).getRemain());
|
|
|
|
+ device.setVpp4(deviceVo.getWines().get(3).getVpp());
|
|
|
|
+
|
|
|
|
+ deviceMapper.updateById(device);
|
|
|
|
+ LocalDateTime oldFinish = change.getFinish();
|
|
|
|
+ LocalDateTime nowFinish = LocalDateTime.now();
|
|
|
|
+ change.setStatus(2);
|
|
|
|
+ change.setFinish(nowFinish);
|
|
|
|
+ changeMapper.updateById(change);
|
|
|
|
+
|
|
|
|
+ Duration between = Duration.between(oldFinish, nowFinish);
|
|
|
|
+ long seconds = between.getSeconds();
|
|
|
|
+
|
|
|
|
+ int sum = (int)(worker.getAvgCost() * (worker.getCount()) + seconds);
|
|
|
|
+ worker.setCount(worker.getCount()+1);
|
|
|
|
+ worker.setCount(worker.getTodo()-1);
|
|
|
|
+ if (seconds > worker.getMaxCost()){
|
|
|
|
+ worker.setMaxCost((int) seconds);
|
|
|
|
+ }
|
|
|
|
+ worker.setAvgCost(sum/worker.getCount());
|
|
|
|
+ workerMapper.updateById(worker);
|
|
|
|
+
|
|
|
|
+ Operation operation = new Operation();
|
|
|
|
+ operation.setUserType(1);
|
|
|
|
+ operation.setUserId(worker.getId());
|
|
|
|
+ operation.setLevel(0);
|
|
|
|
+ operation.setDescribe("完成上酒订单");
|
|
|
|
+ operationMapper.insert(operation);
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- private void ready(ChangeMapper changeMapper, Session session, Integer id) {
|
|
|
|
|
|
+ private void ready(ChangeMapper changeMapper, Session session, RedisTemplate redisTemplate, String worker, Integer id) {
|
|
Change change = changeMapper.selectById(id);
|
|
Change change = changeMapper.selectById(id);
|
|
if (ObjectUtil.isEmpty(change)){
|
|
if (ObjectUtil.isEmpty(change)){
|
|
- WebsocketWorkerUtil.sendMessage(session,new WsEvent("finishResult",Result.fail("订单不存在")));
|
|
|
|
|
|
+ WebsocketWorkerUtil.sendMessage(session,new WsEvent("readyResult",Result.fail("订单不存在")));
|
|
|
|
+ return;
|
|
}
|
|
}
|
|
if (change.getStatus() != 0){
|
|
if (change.getStatus() != 0){
|
|
- WebsocketWorkerUtil.sendMessage(session,new WsEvent("finishResult",Result.fail("订单状态异常")));
|
|
|
|
|
|
+ WebsocketWorkerUtil.sendMessage(session,new WsEvent("readyResult",Result.fail("订单状态异常")));
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ Map<String, Session> sessionMap = WebsocketSellerUtil.getSession();
|
|
|
|
+ if (sessionMap.containsKey(change.getDevice())){
|
|
|
|
+ try {
|
|
|
|
+ redisTemplate.opsForValue().set("Change_"+change.getDevice()+"_"+worker,id,Duration.ofSeconds(300));
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ WsEvent wsEvent = new WsEvent("readyResult",Result.fail("授权码存储失败"));
|
|
|
|
+ WebsocketWorkerUtil.sendMessage(session,wsEvent);
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ Map map = new HashMap();
|
|
|
|
+ map.put("type","Changing");
|
|
|
|
+ map.put("user_type","worker");
|
|
|
|
+ map.put("user_id",worker);
|
|
|
|
+ WsEvent wsEvent = new WsEvent("readyResult",Result.success(map));
|
|
|
|
+ WebsocketWorkerUtil.sendMessage(session,wsEvent);
|
|
|
|
+ return;
|
|
}
|
|
}
|
|
|
|
+ WsEvent wsEvent = new WsEvent("readyResult",Result.fail("设备离线"));
|
|
|
|
+ WebsocketWorkerUtil.sendMessage(session,wsEvent);
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
- private void detail(WorkerMapper WorkerMapper, Session session, JSONObject data) {
|
|
|
|
|
|
+ private void detail(ChangeMapper changeMapper, Session session, Integer id) {
|
|
|
|
+ ChangeQueryResult changeQueryResult = changeMapper.changeDetail(id);
|
|
|
|
+ ChangeQueryResultVo changeQueryResultVo = new ChangeQueryResultVo();
|
|
|
|
+ BeanUtil.copyProperties(changeQueryResultVo,changeQueryResult);
|
|
|
|
+ List<WineWithIdNameRemainVo> old = new ArrayList<>();
|
|
|
|
+ List<WineWithIdNameRemainVo> now = new ArrayList<>();
|
|
|
|
+
|
|
|
|
+ old.add(createWineWithIdNameRemain(changeQueryResult.getW1id(),changeQueryResult.getW1name(),changeQueryResult.getW1remain()));
|
|
|
|
+ old.add(createWineWithIdNameRemain(changeQueryResult.getW2id(),changeQueryResult.getW2name(),changeQueryResult.getW2remain()));
|
|
|
|
+ old.add(createWineWithIdNameRemain(changeQueryResult.getW3id(),changeQueryResult.getW3name(),changeQueryResult.getW3remain()));
|
|
|
|
+ old.add(createWineWithIdNameRemain(changeQueryResult.getW4id(),changeQueryResult.getW4name(),changeQueryResult.getW4remain()));
|
|
|
|
|
|
|
|
+ now.add(createWineWithIdNameRemain(changeQueryResult.getW5id(),changeQueryResult.getW5name(),changeQueryResult.getW5remain()));
|
|
|
|
+ now.add(createWineWithIdNameRemain(changeQueryResult.getW6id(),changeQueryResult.getW6name(),changeQueryResult.getW6remain()));
|
|
|
|
+ now.add(createWineWithIdNameRemain(changeQueryResult.getW7id(),changeQueryResult.getW7name(),changeQueryResult.getW7remain()));
|
|
|
|
+ now.add(createWineWithIdNameRemain(changeQueryResult.getW8id(),changeQueryResult.getW8name(),changeQueryResult.getW8remain()));
|
|
|
|
+
|
|
|
|
+ changeQueryResultVo.setOld(old);
|
|
|
|
+ changeQueryResultVo.setNow(now);
|
|
|
|
+
|
|
|
|
+ WsEvent wsEvent = new WsEvent("detailResult",Result.success(changeQueryResultVo));
|
|
|
|
+ WebsocketWorkerUtil.sendMessage(session,wsEvent);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static WineWithIdRemainVppVo createWineWithIdRemainVpp(int id, int remain,int vpp) {
|
|
|
|
+ WineWithIdRemainVppVo wine = new WineWithIdRemainVppVo();
|
|
|
|
+ wine.setId(id);
|
|
|
|
+ wine.setVpp(vpp);
|
|
|
|
+ wine.setRemain(remain);
|
|
|
|
+ return wine;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static WineWithIdNameRemainVo createWineWithIdNameRemain(int id, String name, int remain) {
|
|
|
|
+ WineWithIdNameRemainVo wine = new WineWithIdNameRemainVo();
|
|
|
|
+ wine.setId(id);
|
|
|
|
+ wine.setName(name);
|
|
|
|
+ wine.setRemain(remain);
|
|
|
|
+ return wine;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static WineWithIdRemainVo createWineWithIdRemain(int id, int remain) {
|
|
|
|
+ WineWithIdRemainVo wine = new WineWithIdRemainVo();
|
|
|
|
+ wine.setId(id);
|
|
|
|
+ wine.setRemain(remain);
|
|
|
|
+ return wine;
|
|
}
|
|
}
|
|
|
|
|
|
private void query(WorkerMapper WorkerMapper, Session session,String worker, JSONObject data) {
|
|
private void query(WorkerMapper WorkerMapper, Session session,String worker, JSONObject data) {
|
|
@@ -159,7 +307,7 @@ public class WorkerController extends TextWebSocketHandler {
|
|
WsEvent wsEvent = new WsEvent("loginResult",Result.fail("用户名或密码错误"));
|
|
WsEvent wsEvent = new WsEvent("loginResult",Result.fail("用户名或密码错误"));
|
|
WebsocketWorkerUtil.sendMessage(session,wsEvent);
|
|
WebsocketWorkerUtil.sendMessage(session,wsEvent);
|
|
}else {
|
|
}else {
|
|
- redisTemplate.opsForValue().set(token,worker.getId(), Duration.ofDays(7));
|
|
|
|
|
|
+ redisTemplate.opsForValue().set("WorkerToken_"+token,worker.getId(), Duration.ofDays(7));
|
|
WsEvent wsEvent = new WsEvent("loginResult",Result.success(token));
|
|
WsEvent wsEvent = new WsEvent("loginResult",Result.success(token));
|
|
WebsocketWorkerUtil.sendMessage(session,wsEvent);
|
|
WebsocketWorkerUtil.sendMessage(session,wsEvent);
|
|
}
|
|
}
|
|
@@ -170,10 +318,6 @@ public class WorkerController extends TextWebSocketHandler {
|
|
WebsocketSellerUtil.sendMessage(session,new WsEvent("pon",null));
|
|
WebsocketSellerUtil.sendMessage(session,new WsEvent("pon",null));
|
|
}
|
|
}
|
|
|
|
|
|
- private void loginSocket(String message, Session session) {
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 处理用户活连接异常
|
|
* 处理用户活连接异常
|
|
*
|
|
*
|