package com.iailab.module.any.service.impl; import com.alibaba.fastjson.JSONArray; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.iailab.module.data.dto.ApiDataDTO; import com.iailab.module.data.dto.FeignQueryPointDTO; import com.iailab.framework.common.constant.Constant; import com.iailab.common.dto.echarts.BarLineDTO; import com.iailab.common.dto.echarts.SeriesItem; 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.common.utils.DateUtils; import com.iailab.module.data.dto.ApiDataPointDTO; import com.iailab.module.data.api.IFeignDataApi; import com.iailab.module.any.dao.AnyProcReportDao; import com.iailab.module.any.dto.AnyProcCardDTO; import com.iailab.module.any.dto.AnyProcParamDTO; import com.iailab.module.any.dto.AnyProcReportDTO; import com.iailab.module.any.dto.AnyProcReportDetDTO; import com.iailab.module.any.entity.AnyProcReportEntity; import com.iailab.module.any.service.AnyProcCardService; import com.iailab.module.any.service.AnyProcParamService; import com.iailab.module.any.service.AnyProcReportDetService; import com.iailab.module.any.service.AnyProcReportService; import org.apache.commons.lang3.StringUtils; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import java.math.BigDecimal; import java.util.*; import java.util.stream.Collectors; /** * @author PanZhibao * @Description * @createTime 2024年06月25日 */ @Service public class AnyProcReportServiceImpl extends BaseServiceImpl implements AnyProcReportService { @Resource private AnyProcCardService anyProcCardService; @Resource private AnyProcReportDetService anyProcReportDetService; @Resource private AnyProcParamService anyProcParamService; @Resource private IFeignDataApi feignDataApi; @Override public PageData page(Map params) { IPage page = baseDao.selectPage( getPage(params, Constant.CREATE_DATE, false), getWrapper(params) ); return getPageData(page, AnyProcReportDTO.class); } private QueryWrapper getWrapper(Map params) { String procCode = (String) params.get("procCode"); String procName = (String) params.get("procName"); String reportName = (String) params.get("reportName"); String startTime = (String) params.get("startTime"); String endTime = (String) params.get("endTime"); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq(StringUtils.isNotBlank(procCode), "proc_code", procCode) .like(StringUtils.isNotBlank(procName), "proc_name", procName) .like(StringUtils.isNotBlank(reportName), "report_name", reportName) .ge(StringUtils.isNotBlank(startTime), "start_time", startTime) .le(StringUtils.isNotBlank(endTime), "end_time", endTime); return wrapper; } @Override public AnyProcReportDTO get(String id) { AnyProcReportEntity entity = baseDao.selectById(id); AnyProcReportDTO result = ConvertUtils.sourceToTarget(entity, AnyProcReportDTO.class); List detList = anyProcReportDetService.listByReportId(id); List chartList = new ArrayList<>(); if (CollectionUtils.isEmpty(detList)) { result.setChartList(chartList); return result; } List ct = detList.stream().map(t -> { return t.getContentType(); }).distinct().collect(Collectors.toList()); Map> detGroup = detList.stream().collect(Collectors.groupingBy(AnyProcReportDetDTO::getContentType)); ct.forEach(k -> { List v = detGroup.get(k); BarLineDTO dto = new BarLineDTO(); dto.setValueName(k); List legend = new ArrayList<>(); List series = new ArrayList<>(); List categories = new ArrayList<>(); v.forEach(item -> { legend.add(item.getRemark()); SeriesItem seriesItem = new SeriesItem(); seriesItem.setName(item.getRemark()); List sData = new ArrayList<>(); if (StringUtils.isNotBlank(item.getContent())) { sData = JSONArray.parseArray(item.getContent(), Object[].class); } seriesItem.setData(sData); series.add(seriesItem); }); series.get(0).getData().forEach(item -> { categories.add(item[0].toString()); }); dto.setLegend(legend); dto.setSeries(series); dto.setCategories(categories); chartList.add(dto); }); result.setChartList(chartList); return result; } @Override @Transactional(rollbackFor = Exception.class) public void save(AnyProcReportDTO dto) { AnyProcReportEntity entity = ConvertUtils.sourceToTarget(dto, AnyProcReportEntity.class); entity.setId(UUID.randomUUID().toString()); entity.setCreateDate(new Date()); insert(entity); if (CollectionUtils.isEmpty(dto.getDetList())) { return; } for (int i = 0; i < dto.getDetList().size(); i++) { AnyProcReportDetDTO det = dto.getDetList().get(i); det.setReportId(entity.getId()); det.setSort(i); anyProcReportDetService.save(det); } } @Override public void update(AnyProcReportDTO dto) { AnyProcReportEntity entity = ConvertUtils.sourceToTarget(dto, AnyProcReportEntity.class); updateById(entity); } @Override @Transactional(rollbackFor = Exception.class) public void delete(Long[] ids) { baseDao.deleteBatchIds(Arrays.asList(ids)); } @Override public void saveReport(String cardCode, String reportName, String content, Date runTime) { AnyProcCardDTO card = anyProcCardService.getByCode(cardCode); AnyProcReportDTO reportDTO = new AnyProcReportDTO(); reportDTO.setProcCode(card.getCode()); reportDTO.setProcName(card.getName()); reportDTO.setReportName(reportName); reportDTO.setReportDate(runTime); reportDTO.setContent(content); List detList = new ArrayList<>(); List paramList = anyProcParamService.listByCardId(card.getId()); for (int i = 0; i < paramList.size(); i++) { AnyProcParamDTO paramItem = paramList.get(i); FeignQueryPointDTO queryPointDTO = new FeignQueryPointDTO(); Calendar calendar = Calendar.getInstance(); calendar.setTime(runTime); queryPointDTO.setEndTime(calendar.getTime()); calendar.add(Calendar.MINUTE, paramItem.getParamLength() * -1); queryPointDTO.setStartTime(calendar.getTime()); queryPointDTO.setPointCode(paramItem.getParamId()); List dataList = feignDataApi.queryPointValues(queryPointDTO); ApiDataPointDTO pointDTO = feignDataApi.getPoint(paramItem.getParamId()); if (CollectionUtils.isEmpty(dataList)) { continue; } AnyProcReportDetDTO det = new AnyProcReportDetDTO(); List dataCont = dataList.stream().map(t -> { return new Object[]{DateUtils.format(t.getTimeStamp(), DateUtils.DATE_TIME_PATTERN_MIN), new BigDecimal(t.getDataValue()).setScale(4, BigDecimal.ROUND_HALF_UP)}; }).collect(Collectors.toList()); det.setContent(JSONArray.toJSONString(dataCont)); det.setContentType(paramItem.getType()); det.setRemark(pointDTO.getPointName()); detList.add(det); } reportDTO.setDetList(detList); this.save(reportDTO); } @Override public void migrationProcReport(Map tMap) { List list = baseDao.selectList(getDateWrapper(tMap)); if (CollectionUtils.isEmpty(list)){ return; } baseDao.migrationProcReport(list); baseDao.delete(getDateWrapper(tMap)); } public QueryWrapper getDateWrapper(Map params) { String startDate = DateUtils.format(params.get("startdate"),DateUtils.DATE_TIME_PATTERN); String endDate = DateUtils.format(params.get("enddate"),DateUtils.DATE_TIME_PATTERN); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.ge(StringUtils.isNotBlank(startDate), "report_date", startDate); wrapper.le(StringUtils.isNotBlank(endDate), "report_date", endDate); return wrapper; } }