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> page(@RequestParam Map params){ PageData page = deviceInfoService.page(params); if (!CollectionUtils.isEmpty(page.getList())) { page.getList().forEach(item -> { item.setChildren(deviceInfoService.getchildrenList(item.getId())); }); } return new CommonResult>().setData(page); } @GetMapping("{id}") @Operation(summary = "信息") public CommonResult get(@PathVariable("id") String id){ DeviceInfoDTO data = deviceInfoService.get(id); List tIndexList= deviceIndexService.getByDeviceId(id); data.setDeviceIndexList(tIndexList); return new CommonResult().setData(data); } @GetMapping("list") @Operation(summary = "列表") public CommonResult> list(@RequestParam Map params){ List data = deviceInfoService.list(params); return new CommonResult>().setData(data); } @PostMapping @Operation(summary = "保存") @LogOperation("保存") @Transactional public CommonResult save(@RequestBody DeviceInfoDTO dto) { //效验数据 ValidationUtils.validate(dto, AddGroup.class, DefaultGroup.class); List 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 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(); } }