diff --git a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/defect/service/impl/DefectCheckServiceImpl.java b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/defect/service/impl/DefectCheckServiceImpl.java index ebca68e..5114ee1 100644 --- a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/defect/service/impl/DefectCheckServiceImpl.java +++ b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/defect/service/impl/DefectCheckServiceImpl.java @@ -23,6 +23,8 @@ import com.hnac.hzims.operational.defect.service.IOperAppearanceService; import com.hnac.hzims.operational.defect.service.IOperDefectService; import com.hnac.hzims.operational.defect.service.IOperDefectStatisticsService; import com.hnac.hzims.operational.defect.vo.SolveHomeVO; +import com.hnac.hzims.operational.station.entity.StationEntity; +import com.hnac.hzims.operational.station.feign.IStationClient; import com.hnac.hzims.operational.util.TimeUtils; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -40,8 +42,11 @@ import org.springblade.flow.core.utils.FlowUtil; import org.springblade.system.cache.DictCache; import org.springblade.system.feign.ISysClient; import org.springblade.system.user.feign.IUserClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.Assert; import java.beans.BeanInfo; import java.beans.Introspector; @@ -49,8 +54,11 @@ import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.time.LocalDateTime; +import java.time.YearMonth; import java.time.format.DateTimeFormatter; +import java.time.temporal.TemporalAdjusters; import java.util.*; +import java.util.concurrent.TimeUnit; /** @@ -69,7 +77,9 @@ public class DefectCheckServiceImpl extends BaseServiceImpl(){{ - eq(OperPhenomenonEntity::getDefectCode,entity.getDefectCode()); - last("limit 1"); - }}); - this.startDefectCheckV2(operPhenomenon); - return entity; + //加锁 + Boolean lock = redisTemplate.opsForValue().setIfAbsent("SysUserLock" + AuthUtil.getDeptId(), AuthUtil.getDeptId(), 30, TimeUnit.SECONDS); + try { + if (lock) { + // 步骤1.保存现象 + if (!this.savePhenomenon(entity)) { + throw new ServiceException("保存现象失败!"); + } + // 步骤2.开启缺陷流程 + OperPhenomenonEntity operPhenomenon = this.getOne(new LambdaQueryWrapper() {{ + eq(OperPhenomenonEntity::getDefectCode, entity.getDefectCode()); + last("limit 1"); + }}); + this.startDefectCheckV2(operPhenomenon); + redisTemplate.delete("SysUserLock" + AuthUtil.getDeptId()); //删除key,释放锁 + } else { + // 加锁失败,抛出异常 + throw new ServiceException("开启现象流程失败!"); + } + return entity; + }catch (Exception e){ + e.printStackTrace(); + log.error("保存现象失败!"+e); + //抛出异常,以便回滚 + throw new ServiceException("保存现象失败!"); + }finally { + redisTemplate.delete("SysUserLock" + AuthUtil.getDeptId()); //删除key,释放锁 + } } /** @@ -104,10 +134,31 @@ public class DefectCheckServiceImpl extends BaseServiceImpl stationR = stationClient.getOne(station); + Assert.isTrue(stationR.isSuccess() && ObjectUtil.isNotEmpty(stationR.getData()) && Func.isNotEmpty(stationR.getData().getSignage()), () -> { + throw new ServiceException("该工作票站点下未获取到两票抬头!"); + }); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM"); + String yearMonth = YearMonth.now().format(formatter); + LocalDateTime date = LocalDateTime.now(); + LocalDateTime firstDay = date.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0); + LocalDateTime lastDay = date.with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59); + List ticketList = this.list( + Wrappers.lambdaQuery().eq(OperPhenomenonEntity::getCreateDept, entity.getCreateDept()) + .ge(OperPhenomenonEntity::getCreateTime, firstDay).le(OperPhenomenonEntity::getCreateTime, lastDay) + ); + String suffix = String.format(String.format("%03d", ticketList.size() + 1)); + String result = stationR.getData().getSignage() + yearMonth + suffix; + + // 缺陷编码 : 按项目+年+月+序号 + if (Func.isEmpty(result)) { + entity.setDefectCode(result); } // 现象发起人 : 当前用户Id if (Func.isEmpty(entity.getFinder())) {