1、注册统一接口类组件(ApplicationContextAware)
@Component
public class CalcServiceLocator implements ApplicationContextAware {
private List<ACalcBase> calcImpl;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Map<String, ACalcBase> calcImplMap = applicationContext.getBeansOfType(ACalcBase.class);
calcImpl = new ArrayList<>();
calcImplMap.forEach((k, v) -> calcImpl.add(v));
calcImpl.sort(Comparator.comparing(ACalcBase::sequence));
}
public List<ACalcBase> getCalcImpl() {
return calcImpl;
}
}
2、业务统一调用实现
@Service
public class CalReportServiceImpl extends BaseServiceImpl<CalReportRepository, CalReport, CalReportDTO> implements ICalReportService {
@Autowired
private CalcServiceLocator calcServiceLocator;
@Override
public CalReportDTO create(CalReportDTO dto) throws BaseException {
CalReport qResult = iBaseRepository.findTopByMonthAndFarmId(dto.getMonth(), dto.getFarmId());
if (qResult == null) {
CalReportDTO result = super.create(dto);
calcServiceLocator.getCalcImpl().forEach(v -> v.initCalc(result));
return result;
}
throw new SysException("该报表已经存在,不用再次创建!");
}
@Override
public PageDTO<CalReportDTO> getPage(PageDTO<CalReportDTO> pageDTO) {
Page<CalReportDTO> pageList = iBaseRepository.findPageable(pageDTO.getFilters(), CommonJpaPageUtil.getInstance().toPageRequest(pageDTO));
pageDTO.setTotal(pageList.getTotalElements());
pageDTO.setList(pageList.getContent());
return pageDTO;
}
@Override
public void beforeRemove(List<String> ids) {
super.beforeRemove(ids);
calcServiceLocator.getCalcImpl().forEach(v -> v.delCalc(ids));
}
@Override
public CalReportDTO update(CalReportDTO dto) throws BaseException {
dto.setModifiedDate(null);
dto.setModifiedBy(null);
CalReportDTO result = super.update(dto);
calcServiceLocator.getCalcImpl().forEach(v -> v.reCalc(dto));
return result;
}
}
3、业务统一接口
public abstract class ACalcBase {
public abstract void initCalc(CalReportDTO dto);
public abstract void delCalc(List<String> ids);
public void reCalc(CalReportDTO dto) {
System.out.println("★默认重算★-[" + this.getClass().getSimpleName() + "]");
delCalc(Arrays.asList(new String[]{dto.getId()}));
initCalc(dto);
}
public abstract int sequence();
}
4子类
@Service
public class BasicParametersServiceImpl extends ACalcBase {
@Autowired
private CalcCustomRepository calcCustomRepository;
@Autowired
private ICalBaseCapaService calBaseCapaService;
@Autowired
private ICalBaseEquipService calBaseEquipService;
@Override
public int sequence() {
return 1;
}
@Override
public void initCalc(CalReportDTO dto) {
System.out.println("计算--基础参数");
System.out.println("*********"+dto.getId());
this.initBaseCapaData(dto);
this.initEquip(dto);
}
@Override
public void delCalc(List<String> ids) {
System.out.println("删除--基础参数");
if (BaseUtils.isEmpty(ids)) return;
this.delBaseCapaData(ids);
this.delEquip(ids);
}
private void initBaseCapaData(CalReportDTO dto) {
List<CapaData> capaDataList = calcCustomRepository.getBaseCapaData(dto);
List<CalBaseCapaDTO> dtoList = capaDataList.stream().map(val -> new CalBaseCapaDTO().setReportId(dto.getId())
.setCapa(val.getCapa()).setPositiveImpSmall(val.getPositiveImpSmall()).setPositiveImpBig(val.getPositiveImpBig())
.setZeroImpSmall(val.getZeroImpSmall()).setZeroImpBig(val.getZeroImpBig()))
.collect(Collectors.toList());
if (BaseUtils.isEmpty(dtoList)) return;
calBaseCapaService.batchCreate(dtoList);
}
private void initEquip(CalReportDTO dto) {
List<Map> qResult = calcCustomRepository.getEquipmentConfigure(dto);
AtomicInteger index = new AtomicInteger(0);
List<CalBaseEquipDTO> dtoList = qResult.stream().map(val -> new CalBaseEquipDTO(val).setReportId(dto.getId()).setSequence(index.getAndIncrement())).collect(Collectors.toList());
if (BaseUtils.isEmpty(dtoList)) return;
calBaseEquipService.batchCreate(dtoList);
}
private void delBaseCapaData(List<String> ids) {
List<CalBaseCapaDTO> qList = calBaseCapaService.selectByReportIds(ids);
List<String> dtoIds = qList.stream().map(v -> v.getId()).collect(Collectors.toList());
if (BaseUtils.isEmpty(dtoIds)) return;
calBaseCapaService.removeByIds(dtoIds);
}
private void delEquip(List<String> ids) {
List<CalBaseEquipDTO> qList = calBaseEquipService.selectByReportIds(ids);
List<String> dtoIds = qList.stream().map(v -> v.getId()).collect(Collectors.toList());
if (BaseUtils.isEmpty(dtoIds)) return;
calBaseEquipService.removeByIds(dtoIds);
}
}