| | |
| | | import cn.iocoder.yudao.module.mes.dal.mysql.energy.EnergyRecordMapper; |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.context.ApplicationEventPublisher; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder; |
| | | import java.util.Set; |
| | | import java.math.RoundingMode; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | |
| | | private static final ErrorCode ID_REQUIRED = new ErrorCode(1_040_950_001, "修改能耗记录必须提供编号"); |
| | | @Resource |
| | | private EnergyRecordMapper recordMapper; |
| | | @Resource private ApplicationEventPublisher applicationEventPublisher; |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long create(EnergyRecordSaveReqVO req) { |
| | | EnergyRecordDO record = toRecord(req); |
| | | record.setId(null); |
| | | recordMapper.insert(record); |
| | | applicationEventPublisher.publishEvent(new EnergyRecordChangedEvent(TenantContextHolder.getTenantId(), Set.of(record.getRecordDate()))); |
| | | return record.getId(); |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void update(EnergyRecordSaveReqVO req) { |
| | | if (req.getId() == null) { |
| | | throw exception(ID_REQUIRED); |
| | | } |
| | | get(req.getId()); |
| | | EnergyRecordDO old = get(req.getId()); |
| | | if (recordMapper.updateById(toRecord(req)) == 0) { |
| | | throw exception(NOT_EXISTS); |
| | | } |
| | | var dates = new java.util.HashSet<>(Set.of(old.getRecordDate())); |
| | | dates.add(req.getRecordDate()); |
| | | applicationEventPublisher.publishEvent(new EnergyRecordChangedEvent(TenantContextHolder.getTenantId(), Set.copyOf(dates))); |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void delete(Long id) { |
| | | get(id); |
| | | EnergyRecordDO old = get(id); |
| | | if (recordMapper.deleteById(id) == 0) { |
| | | throw exception(NOT_EXISTS); |
| | | } |
| | | applicationEventPublisher.publishEvent(new EnergyRecordChangedEvent(TenantContextHolder.getTenantId(), Set.of(old.getRecordDate()))); |
| | | } |
| | | |
| | | public EnergyRecordDO get(Long id) { |