|
|
@@ -0,0 +1,158 @@
|
|
|
+package com.xunmei.host.work.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.xunmei.common.core.domain.work.domain.IotWorkRule;
|
|
|
+import com.xunmei.common.core.domain.work.domain.IotWorkRuleItem;
|
|
|
+import com.xunmei.common.core.domain.work.dto.WorkRuleReq;
|
|
|
+import com.xunmei.common.core.utils.JacksonUtils;
|
|
|
+import com.xunmei.host.websocket.constant.WebSocketConstants;
|
|
|
+import com.xunmei.host.websocket.dto.WebsocketExecuteReq;
|
|
|
+import com.xunmei.host.websocket.enums.ProductEnums;
|
|
|
+import com.xunmei.host.websocket.service.RouterService;
|
|
|
+import com.xunmei.host.work.mapper.IotWorkRuleMapper;
|
|
|
+import com.xunmei.host.work.service.IotWorkRuleItemService;
|
|
|
+import com.xunmei.host.work.service.IotWorkRuleService;
|
|
|
+import com.xunmei.system.api.util.LogUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.StringJoiner;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class IotWorkRuleServiceImpl extends ServiceImpl<IotWorkRuleMapper, IotWorkRule> implements IotWorkRuleService, RouterService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private IotWorkRuleItemService ruleItemService;
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteWorkRule(IotWorkRule iotWorkRule) {
|
|
|
+ if (iotWorkRule != null) {
|
|
|
+ Long ruleId = iotWorkRule.getId();
|
|
|
+ //删除上下班规则
|
|
|
+ super.removeById(ruleId);
|
|
|
+ //删除上下班规则与子系统关联关系
|
|
|
+ deleteItemByRuleId(ruleId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ProductEnums product() {
|
|
|
+ return ProductEnums.DETECTION_HOST;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String routerKey() {
|
|
|
+ StringJoiner sj = new StringJoiner(",");
|
|
|
+ //上下班规则全量数据
|
|
|
+ sj.add(WebSocketConstants.WORK_RULE);
|
|
|
+ //上下班规则增量数据
|
|
|
+ sj.add(WebSocketConstants.INCREMENT_WORK_RULE);
|
|
|
+ //删除上下班规则
|
|
|
+ sj.add(WebSocketConstants.DELETE_WORK_RULE);
|
|
|
+
|
|
|
+ return sj.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Object execute(WebsocketExecuteReq req) {
|
|
|
+ LogUtils.SOCKET_WORK_RULE_DATA.info("收到消息:{}", JacksonUtils.toJSONString(req));
|
|
|
+ if (WebSocketConstants.WORK_RULE.equals(req.getEvent())) {
|
|
|
+ saveBatchWorkRule(req);
|
|
|
+ } else if (WebSocketConstants.INCREMENT_WORK_RULE.equals(req.getEvent())) {
|
|
|
+ saveOrUpdateWorkRule(req);
|
|
|
+ }else if (WebSocketConstants.DELETE_WORK_RULE.equals(req.getEvent())) {
|
|
|
+ deleteData(req);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void saveBatchWorkRule(WebsocketExecuteReq req){
|
|
|
+ Object data = req.getData();
|
|
|
+ JSONArray dataArray = (JSONArray) data;
|
|
|
+ List<WorkRuleReq> dataList = dataArray.toJavaList(WorkRuleReq.class);
|
|
|
+ String iotToken = req.getServerInfo().getIotCode();
|
|
|
+ //查询所有数据
|
|
|
+ QueryWrapper<IotWorkRule> wrapper = new QueryWrapper<>();
|
|
|
+ wrapper.lambda().eq(IotWorkRule::getIotToken, iotToken);
|
|
|
+ List<IotWorkRule> ruleList = super.list();
|
|
|
+
|
|
|
+ //删除主机不存在的规则
|
|
|
+ if (ruleList != null && ruleList.size() > 0){
|
|
|
+ Iterator<IotWorkRule> iterator = ruleList.iterator();
|
|
|
+ while (iterator.hasNext()){
|
|
|
+ IotWorkRule next = iterator.next();
|
|
|
+ for (WorkRuleReq workRuleReq : dataList) {
|
|
|
+ boolean b = ObjectUtil.equal(next.getWorkRuleType(), workRuleReq.getWorkRuleType()) && ObjectUtil.equal(next.getRuleObjStatus(), workRuleReq.getRuleObjStatus())
|
|
|
+ && ObjectUtil.equal(next.getWorkType(), workRuleReq.getWorkType());
|
|
|
+ if (b){
|
|
|
+ iterator.remove();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (ruleList.size() > 0){
|
|
|
+ for (IotWorkRule workRule : ruleList) {
|
|
|
+ deleteWorkRule(workRule);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //新增或修改主机上送上来的规则
|
|
|
+ for (WorkRuleReq workRuleReq : dataList) {
|
|
|
+ saveData(workRuleReq,iotToken);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void saveOrUpdateWorkRule(WebsocketExecuteReq req){
|
|
|
+ Object data = req.getData();
|
|
|
+ WorkRuleReq workRuleReq = (WorkRuleReq) data;
|
|
|
+ saveData(workRuleReq,req.getServerInfo().getIotCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void deleteData(WebsocketExecuteReq req){
|
|
|
+ Object data = req.getData();
|
|
|
+ WorkRuleReq workRuleReq = (WorkRuleReq) data;
|
|
|
+
|
|
|
+ IotWorkRule workRule = getWorkRule(workRuleReq, req.getServerInfo().getIotCode());
|
|
|
+ deleteWorkRule(workRule);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveData(WorkRuleReq workRuleReq,String iotToken){
|
|
|
+ IotWorkRule workRule = getWorkRule(workRuleReq, iotToken);
|
|
|
+ //修改
|
|
|
+ if (null != workRule){
|
|
|
+
|
|
|
+ }else {
|
|
|
+ //新增
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private IotWorkRule getWorkRule(WorkRuleReq workRuleReq,String iotToken){
|
|
|
+ QueryWrapper<IotWorkRule> wrapper = new QueryWrapper<>();
|
|
|
+ wrapper.lambda()
|
|
|
+ .eq(IotWorkRule::getWorkRuleType, workRuleReq.getWorkRuleType())
|
|
|
+ .eq(IotWorkRule::getRuleObjStatus, workRuleReq.getRuleObjStatus())
|
|
|
+ .eq(IotWorkRule::getIotToken, iotToken)
|
|
|
+ .eq(IotWorkRule::getWorkType, workRuleReq.getWorkType());
|
|
|
+
|
|
|
+ return super.getOne(wrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private void deleteItemByRuleId(Long ruleId){
|
|
|
+ QueryWrapper<IotWorkRuleItem> wrapper = new QueryWrapper<>();
|
|
|
+ wrapper.lambda().eq(IotWorkRuleItem::getRuleId, ruleId);
|
|
|
+ ruleItemService.remove(wrapper);
|
|
|
+ }
|
|
|
+}
|