houzhongjian
2024-07-23 a6de490948278991e47952e90671ddba4555e9a2
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package com.iailab.module.device.controller;
 
import com.iailab.common.annotation.LogOperation;
import com.iailab.common.constant.BusinessConstant;
import com.iailab.framework.common.constant.Constant;
import com.iailab.framework.common.page.PageData;
import com.iailab.framework.common.pojo.CommonResult;
import com.iailab.framework.common.util.validation.ValidationUtils;
import com.iailab.framework.common.validation.group.AddGroup;
import com.iailab.framework.common.validation.group.DefaultGroup;
import com.iailab.framework.common.validation.group.UpdateGroup;
import com.iailab.module.device.dto.DeviceIndexDTO;
import com.iailab.module.device.dto.DeviceInfoDTO;
import com.iailab.module.device.entity.DeviceIndexEntity;
import com.iailab.module.device.entity.DeviceInfoEntity;
import com.iailab.module.device.entity.DeviceStatNewnessEntity;
import com.iailab.module.device.entity.DeviceStatUsageEntity;
import com.iailab.module.device.service.DeviceIndexService;
import com.iailab.module.device.service.DeviceInfoService;
import com.iailab.module.device.service.DeviceStatNewnessService;
import com.iailab.module.device.service.DeviceStatUsageService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import javax.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
 
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
 
/**
 * 中选设备信息表
 *
 * @author lirm ${email}
 * @since 1.0.0 2024-05-28
 */
@RestController
@RequestMapping("device/device-info")
@Tag(name = "中选设备信息表")
public class DeviceInfoController {
    @Resource
    private DeviceInfoService deviceInfoService;
    @Resource
    private DeviceIndexService deviceIndexService;
    @Resource
    private DeviceStatUsageService deviceStatUsageService;
    @Resource
    private DeviceStatNewnessService deviceStatNewnessService;
 
    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
        @Parameter(name = Constant.PAGE, description = "当前页码,从1开始", required = true) ,
        @Parameter(name = Constant.LIMIT, description = "每页显示记录数", required = true) ,
        @Parameter(name = Constant.ORDER_FIELD, description = "排序字段") ,
        @Parameter(name = Constant.ORDER, description = "排序方式,可选值(asc、desc)")
    })
    public CommonResult<PageData<DeviceInfoDTO>> page(@RequestParam Map<String, Object> params){
        PageData<DeviceInfoDTO> page = deviceInfoService.page(params);
        if (!CollectionUtils.isEmpty(page.getList())) {
            page.getList().forEach(item -> {
                item.setChildren(deviceInfoService.getchildrenList(item.getId()));
            });
        }
 
        return new CommonResult<PageData<DeviceInfoDTO>>().setData(page);
    }
 
    @GetMapping("{id}")
    @Operation(summary = "信息")
    public CommonResult<DeviceInfoDTO> get(@PathVariable("id") String id){
        DeviceInfoDTO data = deviceInfoService.get(id);
 
        List<DeviceIndexDTO> tIndexList= deviceIndexService.getByDeviceId(id);
        data.setDeviceIndexList(tIndexList);
 
        return new CommonResult<DeviceInfoDTO>().setData(data);
    }
 
    @GetMapping("list")
    @Operation(summary = "列表")
    public CommonResult<List<DeviceInfoDTO>> list(@RequestParam Map<String, Object> params){
        List<DeviceInfoDTO> data = deviceInfoService.list(params);
        return new CommonResult<List<DeviceInfoDTO>>().setData(data);
    }
 
    @PostMapping
    @Operation(summary = "保存")
    @LogOperation("保存")
    @Transactional
    public CommonResult save(@RequestBody DeviceInfoDTO dto) {
        //效验数据
        ValidationUtils.validate(dto, AddGroup.class, DefaultGroup.class);
 
        List<DeviceIndexDTO> tIndexList = dto.getDeviceIndexList();
        if (tIndexList != null) {
            for (int i = 0; i < tIndexList.size(); i++) {
                DeviceIndexDTO tIndexDTO = tIndexList.get(i);
                DeviceIndexEntity tIndexEntity = new DeviceIndexEntity();
                tIndexEntity.setModelId(BusinessConstant.MODEL_Health_Status_Evaluate);
                tIndexEntity.setDeviceId(tIndexDTO.getDeviceId());
                tIndexEntity.setIndexNo(tIndexDTO.getIndexNo());
                tIndexEntity.setIndexUpperLimit(tIndexDTO.getIndexUpperLimit());
                tIndexEntity.setIndexLowerLimit(tIndexDTO.getIndexLowerLimit());
                tIndexEntity.setIndexOrder(tIndexDTO.getIndexOrder());
                tIndexEntity.setDataLength(tIndexDTO.getDataLength());
                tIndexEntity.setIndexType(tIndexDTO.getIndexType());
                deviceIndexService.insert(tIndexEntity);
            }
        }
        return new CommonResult();
    }
 
    @PutMapping
    @Operation(summary = "修改")
    @LogOperation("修改")
    @Transactional
    public CommonResult update(@RequestBody DeviceInfoDTO dto) {
        //效验数据
        ValidationUtils.validate(dto, UpdateGroup.class, DefaultGroup.class);
 
        List<DeviceIndexDTO> tIndexList = dto.getDeviceIndexList();
        if (tIndexList != null) {
            deviceIndexService.deleteByDeviceId(dto.getId());
            for (int i = 0; i < tIndexList.size(); i++) {
                DeviceIndexDTO tIndexDTO = tIndexList.get(i);
                DeviceIndexEntity tIndexEntity = new DeviceIndexEntity();
                tIndexEntity.setModelId(BusinessConstant.MODEL_Health_Status_Evaluate);
                tIndexEntity.setDeviceId(dto.getId());
                tIndexEntity.setIndexNo(tIndexDTO.getIndexNo());
                tIndexEntity.setIndexUpperLimit(tIndexDTO.getIndexUpperLimit());
                tIndexEntity.setIndexLowerLimit(tIndexDTO.getIndexLowerLimit());
                tIndexEntity.setIndexOrder(tIndexDTO.getIndexOrder());
                tIndexEntity.setDataLength(tIndexDTO.getDataLength());
                tIndexEntity.setIndexType(tIndexDTO.getIndexType());
                deviceIndexService.insert(tIndexEntity);
            }
        }
 
        return new CommonResult();
    }
 
    @PostMapping("/updatePlanTime")
    @Operation(summary = "修改计划运行时间")
    @LogOperation("修改计划运行时间")
    @Transactional
    public CommonResult updatePlanTime(@RequestBody DeviceInfoDTO dto) {
 
        Integer planTime = dto.getPlanTime();
 
        // 重新计算使用率
        DeviceStatUsageEntity deviceStatUsage = deviceStatUsageService.getLastByNo(dto.getNo());
        BigDecimal usage = null;
        if (deviceStatUsage != null){
            Integer runTime = deviceStatUsage.getRunTime();
            BigDecimal d1 = new BigDecimal(runTime);
            BigDecimal d2 = new BigDecimal(planTime*60);
            usage = d1.divide(d2, 4, RoundingMode.HALF_UP);
 
            DeviceStatUsageEntity insertUsageEntry = new DeviceStatUsageEntity();
            insertUsageEntry.setId(deviceStatUsage.getId());
            insertUsageEntry.setUsageRate(usage);
            insertUsageEntry.setPlanTime(planTime*60);
            deviceStatUsageService.updateById(insertUsageEntry);
        }
 
        // 重新计算新度系数
        // 设备使用年限(分钟)
        long durableMinutes = dto.getDurableYears() * BusinessConstant.YEAR_MINUTE;
        // 设备剩余使用分钟
        long residueTime = dto.getResidueTime();
        BigDecimal d1 = new BigDecimal(residueTime);
        BigDecimal d2 = new BigDecimal(durableMinutes);
        BigDecimal newnessRate = d1.divide(d2, 4, RoundingMode.HALF_UP);
 
        //修改最新新度系数历史
        DeviceStatNewnessEntity deviceStatNewness = deviceStatNewnessService.getLastByNo(dto.getNo());
        if (deviceStatNewness != null){
 
            DeviceStatNewnessEntity insertNewnessEntry = new DeviceStatNewnessEntity();
            insertNewnessEntry.setId(deviceStatNewness.getId());
            insertNewnessEntry.setNewnessRate(newnessRate);
            insertNewnessEntry.setDurableTime(durableMinutes);
            insertNewnessEntry.setResidueTime(residueTime);
            deviceStatNewnessService.updateById(insertNewnessEntry);
        }
 
 
        DeviceInfoEntity deviceInfoEntity = new DeviceInfoEntity();
        deviceInfoEntity.setId(dto.getId());
        deviceInfoEntity.setPlanTime(planTime);
        deviceInfoEntity.setUsageRate(usage);
        deviceInfoEntity.setIsMain(dto.getIsMain());
        deviceInfoEntity.setDurableYears(dto.getDurableYears());
        deviceInfoEntity.setNewnessRate(newnessRate);
        deviceInfoEntity.setResidueTime(dto.getResidueTime());
        deviceInfoEntity.setUseTime(dto.getUseTime());
        deviceInfoEntity.setUpdateDate(new Date());
        deviceInfoService.updateById(deviceInfoEntity);
 
        return new CommonResult();
    }
}