| | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.aspose.cad.internal.I.S; |
| | | import com.baomidou.dynamic.datasource.annotation.DS; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | |
| | | import com.example.erp.tools.JacksonUtil; |
| | | import com.fasterxml.jackson.core.JsonProcessingException; |
| | | import com.fasterxml.jackson.core.type.TypeReference; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.github.yulichang.wrapper.MPJLambdaWrapper; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.data.redis.core.RedisCallback; |
| | | import org.springframework.data.redis.core.StringRedisTemplate; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | |
| | | import java.util.*; |
| | | import java.util.concurrent.CompletableFuture; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | | @DS("pp") |
| | |
| | | //判断报工表是否存在,不存在则新增报工主副表数据,存在则修改副表数据 |
| | | if(ReportingWorks.isEmpty()){ |
| | | //获取当前最大报工单号生成报工单号 |
| | | String formattedNumber = String.format("%04d", reportingWorkMapper.selectMaxReportingWorkId() + 1); |
| | | //格式化当前日期 |
| | | Date currentDate = new Date(); |
| | | SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMdd"); |
| | | String formattedDate = dateFormat.format(currentDate); |
| | | String reportingWorkId = "BG" + formattedDate + formattedNumber; |
| | | String reportingWorkId = this.getTodayMaxReportingId(); |
| | | |
| | | reportingWork.setReportingWorkId(reportingWorkId); |
| | | reportingWorkDetail.setReportingWorkId(reportingWorkId); |
| | |
| | | |
| | | |
| | | //@Transactional(rollbackFor = Exception.class , noRollbackFor = ServiceException.class) |
| | | public Result mesReportingWorkSv1(Map<String, Object> reportingWorkMap) throws JsonProcessingException { |
| | | public Result mesReportingWorkSvToRedis(Map<String, Object> reportingWorkMap) throws JsonProcessingException, InterruptedException { |
| | | |
| | | //设置回滚点 |
| | | //Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint(); |
| | |
| | | String detailJson = JacksonUtil.writeValueAsString(reportingWorkMap.get("detail")); |
| | | ReportingWork reportingWork = JacksonUtil.readValue(titleJson, ReportingWork.class); |
| | | ReportingWorkDetail reportingWorkDetail = JacksonUtil.readValue(detailJson, ReportingWorkDetail.class); |
| | | String Base_KEY = reportingWork.getProcessId()+":"+reportingWork.getThisProcess()+":"+reportingWork.getDeviceName(); |
| | | String Base_KEY = "reportingWork:"+reportingWork.getProcessId()+"/"+reportingWorkDetail.getTechnologyNumber()+":"+reportingWork.getThisProcess()+":"+reportingWork.getDeviceName(); |
| | | String reportingWork_KEY = Base_KEY+":title"; |
| | | String reportingWorkDetail_KEY = Base_KEY+":detail:"+reportingWorkDetail.getOrderNumber()+":"+reportingWorkDetail.getTechnologyNumber(); |
| | | String reportingWorkDetail_KEY = Base_KEY+":detail:"+reportingWorkDetail.getOrderNumber(); |
| | | |
| | | String process_KEY = reportingWork.getProcessId()+":process:"+reportingWorkDetail.getOrderNumber()+":"+reportingWorkDetail.getTechnologyNumber(); |
| | | String process_KEY = "reportingWork:"+reportingWork.getProcessId()+"/"+reportingWorkDetail.getTechnologyNumber()+":process"; |
| | | |
| | | //判断锁是否存在,存在等待5秒 |
| | | if(Boolean.TRUE.equals(stringRedisTemplate.hasKey("lock:reportingWork"))){ |
| | | Thread.sleep(5*1000); |
| | | if(Boolean.TRUE.equals(stringRedisTemplate.hasKey("lock:reportingWork"))){ |
| | | throw new ServiceException(Constants.Code_600, "当前服务器正在处理报工数据,请稍后重试!"); |
| | | } |
| | | } |
| | | |
| | | |
| | | //判断工序是否存在redis中 |
| | | if(!Boolean.TRUE.equals(stringRedisTemplate.hasKey(process_KEY+":"+reportingWork.getThisProcess()))){ |
| | |
| | | JacksonUtil.readValueObjectToString(reportingWork,new TypeReference<Map<String, String>>() {}) |
| | | ); |
| | | } |
| | | //获取可报数量 |
| | | |
| | | //判断是否redis此报工明细中key是否存在 |
| | | if(Boolean.TRUE.equals(stringRedisTemplate.hasKey(reportingWorkDetail_KEY))){ |
| | | stringRedisTemplate.opsForHash().increment( |
| | |
| | | } |
| | | return Result.success("提交成功"); |
| | | } |
| | | |
| | | public void redisToMysqlReportingWork() throws InterruptedException { |
| | | if(Boolean.TRUE.equals(stringRedisTemplate.hasKey("lock:reportingWork"))){ |
| | | throw new ServiceException(Constants.Code_600, "定时任务:当前服务器正在处理报工数据,请稍后重试!"); |
| | | } |
| | | //获取所有key |
| | | String pattern = "reportingWork:*"; |
| | | Set<String> keys = stringRedisTemplate.keys(pattern); |
| | | |
| | | //判断redis中是否存在报工数据,存在则添加锁 并且修改key转移到上传mysql的key中 |
| | | if (keys != null && !keys.isEmpty()) { |
| | | //添加锁 |
| | | stringRedisTemplate.opsForValue().set("lock:reportingWork","1",60, TimeUnit.SECONDS); |
| | | |
| | | if(Boolean.FALSE.equals(stringRedisTemplate.hasKey("upload:*" ))){ |
| | | keys.forEach(key -> { |
| | | stringRedisTemplate.rename(key, "upload:" + key); |
| | | }); |
| | | } |
| | | } |
| | | //添加暂停时间,方式获取map时,redis中数据还在变化 |
| | | Thread.sleep(200); |
| | | |
| | | |
| | | // 保存日志,把redis中的数据保存到日志中 |
| | | Map<String, Map<Object, Object>> map = this.getAllHashesByPattern("upload:reportingWork:*"); |
| | | Log log = new Log(); |
| | | log.setContent(JacksonUtil.writeValueAsString(map)); |
| | | log.setFunction("reportingWorkRedisToMysql"); |
| | | log.setOperator("自动定时报工"); |
| | | logService.saveLog(log); |
| | | //删除锁,让接口继续接收报工 |
| | | stringRedisTemplate.delete("lock:reportingWork"); |
| | | |
| | | //获取到所有参数后清空redis库 |
| | | // stringRedisTemplate.getConnectionFactory().getConnection().flushDb(); |
| | | |
| | | //从redis中获取所有报工主表数据 |
| | | String titleKeyString = "upload:reportingWork:*:title"; |
| | | Set<String> titleKeys = stringRedisTemplate.keys(titleKeyString); |
| | | if (titleKeys != null) { |
| | | titleKeys.forEach(titleKey -> { |
| | | ReportingWork reportingWork = JSON.parseObject( |
| | | JSONObject.toJSONString(stringRedisTemplate.opsForHash().entries(titleKey)) |
| | | , ReportingWork.class |
| | | ); |
| | | //获取当天一共有多少个报工编号 |
| | | String reportingWorkId = this.getTodayMaxReportingId(); |
| | | //添加报工编号 |
| | | reportingWork.setReportingWorkId(reportingWorkId); |
| | | //往主表插入报工主数据 |
| | | reportingWorkMapper.insert(reportingWork); |
| | | |
| | | String detailKeyString = titleKey.replace(":title", ":detail:*"); |
| | | Set<String> detailKeys = stringRedisTemplate.keys(detailKeyString); |
| | | if (detailKeys != null) { |
| | | detailKeys.forEach(detailKey -> { |
| | | |
| | | ReportingWorkDetail reportingWorkDetail = JSON.parseObject( |
| | | JSONObject.toJSONString(stringRedisTemplate.opsForHash().entries(detailKey)) |
| | | , ReportingWorkDetail.class |
| | | ); |
| | | reportingWorkDetail.setReportingWorkId(reportingWorkId); |
| | | reportingWorkDetailMapper.insert(reportingWorkDetail); |
| | | |
| | | //根据订单id,订单序号,以及小片序号 更新小片流程的完工数量以及刺破数量 |
| | | LambdaUpdateWrapper<OrderProcessDetail> updateWrapper = new LambdaUpdateWrapper<>(); |
| | | updateWrapper.eq(OrderProcessDetail::getOrderNumber, reportingWorkDetail.getOrderNumber()) |
| | | .eq(OrderProcessDetail::getProcessId, reportingWork.getProcessId()) |
| | | .eq(OrderProcessDetail::getOrderId, reportingWork.getOrderId()) |
| | | .eq(OrderProcessDetail::getProcess, reportingWork.getThisProcess()) |
| | | .eq(OrderProcessDetail::getTechnologyNumber, reportingWorkDetail.getTechnologyNumber()) |
| | | .setSql("reporting_work_num_count = reporting_work_num_count +" + reportingWorkDetail.getCompletedQuantity()) |
| | | .setSql("reporting_work_num =reporting_work_num +" + reportingWorkDetail.getCompletedQuantity()); |
| | | |
| | | orderProcessDetailMapper.update(null, updateWrapper); |
| | | }); |
| | | } |
| | | |
| | | }); |
| | | } |
| | | Set<String> uploadKeys = stringRedisTemplate.keys("upload:reportingWork:*"); |
| | | if (uploadKeys != null) { |
| | | stringRedisTemplate.delete(uploadKeys); |
| | | } |
| | | |
| | | } |
| | | |
| | | public String getTodayMaxReportingId() { |
| | | String formattedNumber = String.format("%04d", reportingWorkMapper.selectMaxReportingWorkId() + 1); |
| | | //格式化当前日期 |
| | | Date currentDate = new Date(); |
| | | SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMdd"); |
| | | String formattedDate = dateFormat.format(currentDate); |
| | | return "BG" + formattedDate + formattedNumber; |
| | | |
| | | } |
| | | |
| | | |
| | | /*获取所有的参数转*/ |
| | | public Map<String, Map<Object, Object>> getAllHashesByPattern(String pattern) { |
| | | Set<String> keys = stringRedisTemplate.keys(pattern); |
| | | Map<String, Map<Object, Object>> result = new HashMap<>(); |
| | | |
| | | if (keys != null) { |
| | | for (String key : keys) { |
| | | Map<Object, Object> hashData = stringRedisTemplate.opsForHash().entries(key); |
| | | result.put(key, hashData); |
| | | } |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | } |