package com.iailab.module.device.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.iailab.common.constant.BusinessConstant; import com.iailab.framework.common.page.PageData; import com.iailab.framework.common.service.impl.BaseServiceImpl; import com.iailab.framework.common.util.object.ConvertUtils; import com.iailab.module.device.dao.DeviceStatNewnessDao; import com.iailab.module.device.dto.DeviceInfoDTO; import com.iailab.module.device.dto.DeviceStatNewnessDTO; import com.iailab.module.device.entity.DeviceStatNewnessEntity; import com.iailab.module.device.service.DeviceInfoService; import com.iailab.module.device.service.DeviceStatNewnessService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.util.*; @Slf4j @Service public class DeviceStatNewnessServiceImpl extends BaseServiceImpl implements DeviceStatNewnessService { @Resource private DeviceInfoService deviceInfoService; @Override @Transactional(rollbackFor = Exception.class) public void statNewness(List list,Date statDate) { // 计划运行时间 Map ListParams = new HashMap<>(); ListParams.put("pid", "0"); List deviceInfoList = deviceInfoService.list(ListParams); Map deviceInfoMap = new HashMap<>(); deviceInfoList.forEach(item -> { deviceInfoMap.put(item.getNo(), item); }); List deviceStatNewnessList = new ArrayList<>(); for (DeviceStatNewnessEntity entity : list) { DeviceInfoDTO devInfo = deviceInfoMap.get(entity.getDevNo()); if (devInfo == null) { continue; } DeviceInfoDTO devInfoUpdate = new DeviceInfoDTO(); devInfoUpdate.setId(devInfo.getId()); entity.setId(UUID.randomUUID().toString()); entity.setDate(statDate); entity.setCreateDate(new Date()); // 使用年限 Integer durableYears = devInfo.getDurableYears() == null ? 0 : devInfo.getDurableYears(); BigDecimal durMins = new BigDecimal(durableYears.longValue() * BusinessConstant.YEAR_MINUTE); entity.setDurableTime(durMins.longValue()); // 已用时长 BigDecimal useTime = devInfo.getUseTime() == null ? BigDecimal.ZERO : new BigDecimal(devInfo.getUseTime()); BigDecimal useNew = useTime.add(new BigDecimal(entity.getLossTime())); devInfoUpdate.setUseTime(useNew.longValue()); // 剩余时长 BigDecimal residueNew = new BigDecimal(entity.getDurableTime() - useNew.longValue()); devInfoUpdate.setResidueTime(residueNew.longValue()); entity.setResidueTime(residueNew.longValue()); // 新度系数 BigDecimal newnessRate = residueNew.divide(durMins, 4, BigDecimal.ROUND_FLOOR); devInfoUpdate.setNewnessRate(newnessRate); entity.setNewnessRate(newnessRate); deviceStatNewnessList.add(entity); // 更新设备信息 deviceInfoService.updateById(devInfoUpdate); } // 插入明细 insertBatch(deviceStatNewnessList); } @Override public DeviceStatNewnessEntity getLastByNo(String no) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("dev_no",no); wrapper.orderByDesc("create_date"); wrapper.last("limit 1"); return baseDao.selectOne(wrapper); } @Override public List list(Map params) { List list = baseDao.selectList(getWrapper(params)); return ConvertUtils.sourceToTarget(list,DeviceStatNewnessDTO.class); } @Override public PageData page(Map params) { params.put("pid","0"); IPage page = baseDao.selectPage(getPage(params, "create_date", false), getWrapper(params)); return getPageData(page, DeviceStatNewnessDTO.class); } public QueryWrapper getWrapper(Map params) { String no = (String) params.get("no"); String startTime = (String) params.get("startTime"); String endTime = (String) params.get("endTime"); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq(StringUtils.isNotBlank(no), "dev_no", no); wrapper.ge(StringUtils.isNotBlank(startTime), "date", startTime); wrapper.le(StringUtils.isNotBlank(endTime), "date", endTime); wrapper.orderByDesc("create_date"); return wrapper; } }