whycxzp
2021-05-20 f2e25328e074c47fcad8aca252805380ddcee143
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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 com.whyc.pojo.Experiment;
import com.whyc.pojo.ExperimentBaseDataKZ;
import com.whyc.pojo.ExperimentPoint;
import com.whyc.pojo.ExperimentPrecondition;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
 
@Service
public class ExperimentService {
 
    @Resource
    private ExperimentMapper mapper;
    @Resource
    private ExperimentBaseDataKZMapper kzMapper;
    @Resource
    private ExperimentPointMapper pointMapper;
    @Resource
    private ExperimentPreconditionMapper preconditionMapper;
    @Resource
    private CommonMapper commonMapper;
 
    /**
     * 绕组:rz,
     * 空载:kz,
     * 负载:fz,
     * 升温:sw,
     * 超速:cs,
     * 空载反电动势:kzfdds,
     * 振动:zd,
     * 耐压:ny,
     * 转动惯量:zdgl
     * @param type
     * @return
     */
    public Response getExperimentId(String type) {
        String id = null;
        //查询当前试验类型的最大编号
        QueryWrapper<Experiment> wrapper = Wrappers.query();
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String nowFormat = dateFormat.format(now);
        String idLike = type.toUpperCase()+"_"+nowFormat;
        wrapper.select("id").likeRight("id",idLike).orderByDesc("id").last(" limit 1");
        Experiment experiment = mapper.selectOne(wrapper);
        if(experiment!=null) {
            int idPrefixLength = idLike.length();
            String idSuffixStr = experiment.getId().substring(idPrefixLength);
            Integer idSuffix = Integer.parseInt(idSuffixStr)+1;
            //前面填充0的位数
            int zeroCount = idSuffixStr.length()-idSuffix.toString().length();
            StringBuilder zeroStr = new StringBuilder();
            for (int i = 0; i < zeroCount; i++) {
                zeroStr.append("0");
            }
            id = idLike+zeroStr+idSuffix;
        }else{
            id = idLike+"001";
        }
        return new Response().set(1,id);
 
    }
 
    @Transactional
    public Response addKZ(Experiment experiment) {
        //插入experiment数据
        experiment.setCreateTime(new Date());
        //新增初始化状态为1,正在进行
        experiment.setStatus(1);
        mapper.insert(experiment);
 
        //插入experiment_base_data数据
        kzMapper.insert((ExperimentBaseDataKZ) experiment.getBaseData());
 
        //插入experiment_point数据
        ExperimentPoint point = (ExperimentPoint) experiment.getPoint();
        point.setStatus(1);
        pointMapper.insert(point);
 
        //TODO 插入紧急停止数据
 
        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);
        //2种方式
        //m1
        QueryWrapper<ExperimentPrecondition> wrapper = Wrappers.query();
        wrapper.eq("type",type);
        List<ExperimentPrecondition> preconditions = preconditionMapper.selectList(wrapper);
        preconditions.stream().forEach(precondition->{
            //获取对应的值
            /*ExperimentPrecondition preconditionStatus = commonMapper.getPreconditionStatus(precondition);
            precondition.setActualValue(preconditionStatus.getActualValue());
            precondition.setStatus(preconditionStatus.getStatus());*/
            Object preconditionActualValue = commonMapper.getPreconditionStatus(precondition);
            String preconditionActualValueStr = preconditionActualValue.toString();
            if(precondition.getValue().equals(preconditionActualValueStr)){
                precondition.setStatus(1);
            }else{
                precondition.setStatus(0);
                code.set(-1);
            }
            precondition.setActualValue(preconditionActualValueStr);
        });
        return new Response().set(code.get(),preconditions);
    }
}