| | |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.github.pagehelper.PageHelper; |
| | | import com.github.pagehelper.PageInfo; |
| | | import com.whyc.constant.ExperimentType; |
| | | import com.whyc.dto.ExperimentConditionDTO; |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.mapper.*; |
| | |
| | | import javax.annotation.Resource; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.concurrent.atomic.AtomicBoolean; |
| | | import java.util.concurrent.atomic.AtomicReference; |
| | | |
| | | import static java.lang.Thread.sleep; |
| | | |
| | | @Service |
| | | public class ExperimentService { |
| | |
| | | private ExperimentPreconditionMapper preconditionMapper; |
| | | @Resource |
| | | private CommonMapper commonMapper; |
| | | |
| | | public Response exist() { |
| | | QueryWrapper<Experiment> wrapper = Wrappers.query(); |
| | | wrapper.eq("status",1); |
| | | List<Experiment> experiments = mapper.selectList(wrapper); |
| | | //不存在试验 |
| | | if (experiments.isEmpty()){ |
| | | return new Response().set(-1,"暂无试验"); |
| | | } |
| | | return new Response().set(1,experiments); |
| | | } |
| | | |
| | | /** |
| | | * 绕组:rz, |
| | |
| | | public Response addKZ(Experiment experiment) { |
| | | //插入experiment数据 |
| | | experiment.setCreateTime(new Date()); |
| | | //新增初始化状态为1,正在进行 |
| | | experiment.setStatus(1); |
| | | //新增初始化状态为0未开始,正在进行 |
| | | experiment.setStatus(0); |
| | | mapper.insert(experiment); |
| | | |
| | | //插入experiment_base_data数据 |
| | |
| | | return new Response().setMsg(1,"新增成功"); |
| | | } |
| | | |
| | | /*======History======*/ |
| | | |
| | | public Response<PageInfo<Experiment>> getPage(Integer pageNum, Integer pageSize, ExperimentConditionDTO condition) { |
| | | PageHelper.startPage(pageNum,pageSize); |
| | | List<Experiment> experimentList = mapper.getPage(condition); |
| | | //查询结果的duration是分钟,转换为 x时x分格式 |
| | | experimentList.stream().forEach(experiment -> { |
| | | int duration = Integer.parseInt(experiment.getDuration()); |
| | | //获取时 |
| | | int hour = duration/60; |
| | | //获取分 |
| | | int minute = duration%60; |
| | | experiment.setDuration(hour+"小时"+minute+"分"); |
| | | }); |
| | | |
| | | PageInfo<Experiment> pageInfo = new PageInfo<>(experimentList); |
| | | return new Response<PageInfo<Experiment>>().set(1,pageInfo); |
| | | } |
| | | |
| | | /** |
| | | * 检查前置条件 |
| | | * @param type |
| | | * @return |
| | | */ |
| | | public Response checkPrecondition(String type) { |
| | | AtomicReference<Integer> code = new AtomicReference<>(1); |
| | | AtomicReference<Integer> code = new AtomicReference<>(1); |
| | | //2种方式 |
| | | //m1 |
| | | QueryWrapper<ExperimentPrecondition> wrapper = Wrappers.query(); |
| | |
| | | }); |
| | | return new Response().set(code.get(),preconditions); |
| | | } |
| | | |
| | | /** |
| | | * 检查中置条件-当前功率和当前定子温度,如果符合标准,返回当前值 |
| | | */ |
| | | public Response checkPreconditionStep1(String type){ |
| | | QueryWrapper<ExperimentPrecondition> wrapper = Wrappers.query(); |
| | | wrapper.eq("type",type+"_1"); |
| | | List<ExperimentPrecondition> preconditions = preconditionMapper.selectList(wrapper); |
| | | |
| | | //获取到条件数量 |
| | | int conditionNum = preconditions.size(); |
| | | AtomicReference<Integer> passNum = new AtomicReference(0); |
| | | while (conditionNum!=passNum.get()) { |
| | | //重置为0 |
| | | passNum.set(0); |
| | | preconditions.stream().forEach(precondition -> { |
| | | //获取对应的值 |
| | | Object preconditionActualValue = commonMapper.getPreconditionStatus(precondition); |
| | | //采用浮点进行数值对比 |
| | | Float actualValue = (Float) preconditionActualValue; |
| | | Float defaultValue = Float.parseFloat(precondition.getValue()); |
| | | |
| | | if (defaultValue.equals(actualValue)) { |
| | | //条件达成,达成数上+1 |
| | | passNum.set(passNum.get()+1); |
| | | //记录当前值 |
| | | precondition.setActualValue(actualValue.toString()); |
| | | } |
| | | }); |
| | | } |
| | | return new Response().set(1,preconditions); |
| | | } |
| | | |
| | | /** |
| | | * 开始试验,更新 试验状态和启动试验时间 |
| | | */ |
| | | public Response updateStatus(String experimentId){ |
| | | Experiment<Object, Object> experiment = new Experiment<>(); |
| | | experiment.setId(experimentId); |
| | | experiment.setStatus(1); |
| | | experiment.setStartTime(new Date()); |
| | | mapper.updateById(experiment); |
| | | return new Response().set(1,"状态更新成功"); |
| | | } |
| | | |
| | | /** |
| | | * 获取测试点 |
| | | */ |
| | | public Response<List<ExperimentPoint>> getPoint(String experimentId){ |
| | | QueryWrapper<ExperimentPoint> wrapper = Wrappers.query(); |
| | | wrapper.eq("experiment_id",experimentId); |
| | | List<ExperimentPoint> experimentPoints = pointMapper.selectList(wrapper); |
| | | return new Response<List<ExperimentPoint>>().set(1,experimentPoints); |
| | | } |
| | | |
| | | /** |
| | | * 更新测试点的开始时间和状态 |
| | | */ |
| | | public Response updatePointStatus(ExperimentPoint experimentPoint){ |
| | | pointMapper.update(experimentPoint,null); |
| | | return new Response().set(1,"状态更新成功"); |
| | | } |
| | | |
| | | /** |
| | | * 启动测试点,并在测试时长过后,返回测试结果 |
| | | * @return |
| | | */ |
| | | public Response startExperimentPoint(ExperimentPoint point){ |
| | | //更新测试时间和测试点状态 |
| | | point.setStartTime(new Date()); |
| | | point.setStatus(1); |
| | | pointMapper.update(point,null); |
| | | //等待duration时间 |
| | | Integer duration = point.getDuration(); |
| | | Integer durationSecond = duration*60; |
| | | try { |
| | | sleep(durationSecond); |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | pointMapper.updateEndTime(point.getId(),new Date()); |
| | | //返回这段时间的测试结果 |
| | | Map<String,String> resMap = new HashMap<>(); |
| | | int value = (int) (100 + Math.random() * 10); |
| | | resMap.put("平均功率",value+"kW"); |
| | | return new Response().set(1,resMap); |
| | | } |
| | | |
| | | /*======History======*/ |
| | | |
| | | public Response<PageInfo<Experiment>> getPage(Integer pageNum, Integer pageSize, ExperimentConditionDTO condition) { |
| | | PageHelper.startPage(pageNum,pageSize); |
| | | List<Experiment> experimentList = mapper.getPage(condition); |
| | | //查询结果的duration是分钟,转换为 x时x分格式 |
| | | experimentList.stream().forEach(experiment -> { |
| | | int duration = Integer.parseInt(experiment.getDuration()); |
| | | //获取时 |
| | | int hour = duration/60; |
| | | //获取分 |
| | | int minute = duration%60; |
| | | experiment.setDuration(hour+"小时"+minute+"分"); |
| | | }); |
| | | |
| | | PageInfo<Experiment> pageInfo = new PageInfo<>(experimentList); |
| | | return new Response<PageInfo<Experiment>>().set(1,pageInfo); |
| | | } |
| | | |
| | | } |