123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625 |
- package com.ruoyi.business.service.impl;
- import com.ruoyi.business.domain.*;
- import com.ruoyi.business.mapper.*;
- import com.ruoyi.business.req.AssayReq;
- import com.ruoyi.business.req.DeviceReq;
- import com.ruoyi.business.res.*;
- import com.ruoyi.business.service.IBusinessService;
- import com.ruoyi.common.NumberUtils;
- import com.ruoyi.common.utils.DateUtils;
- import com.ruoyi.common.utils.StringUtils;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.util.CollectionUtils;
- import java.util.*;
- import java.util.concurrent.atomic.AtomicInteger;
- import java.util.stream.Collectors;
- import static com.ruoyi.common.constant.Constants.INT_2;
- import static com.ruoyi.common.constant.Constants.INT_4;
- @Slf4j
- @Service
- public class BusinessServiceImpl implements IBusinessService {
- @Autowired
- private BizDeviceMapper bizDeviceMapper;
- @Autowired
- private BizWaterWorkMapper bizWaterWorkMapper;
- @Autowired
- private ZAssayMapper zAssayMapper;
- @Autowired
- private ZAssayResultMapper zAssayResultMapper;
- @Autowired
- private ZQualityValueMapper zQualityValueMapper;
- /**
- * 2024年10月25日09:46:36 新增逻辑:type可以不传。
- * @param type
- * @return
- */
- @Override
- public HomeCountsRes homeCountsInfo(String type) {//type 1:化验室,2:连续检测
- HomeCountsRes build = HomeCountsRes.builder().build();
- //处理各个维度的统计信息
- //获取所有的正在使用的设备列表 大概几十个,所以直接取所有数据,下面再过滤
- //2024年10月25日09:51:12 type可以不传,直接查询对应的数据
- List<BizDevice> totalList = this.bizDeviceMapper.selectBizDeviceList(BizDevice.builder().deviceStatus("0").type(type).build());
- //一定有设备 并且mybatis返回的默认是空集合new出来的而不是null,所以这里不用判断
- // List<BizDevice> totalList = bizDevices.stream().filter(bizDevice -> String.valueOf(type).equals(bizDevice.getType())).collect(Collectors.toList());
- if (!CollectionUtils.isEmpty(totalList)) {
- build.setDeviceTotals((long) totalList.size());
- //化验中
- build.setOnDeviceCounts(totalList.stream().filter(bizDevice -> "2".equals(bizDevice.getAssayStatus())).count());
- //待机
- build.setStandbyDeviceCounts(totalList.stream().filter(bizDevice -> "0".equals(bizDevice.getAssayStatus())).count());
- //离线
- build.setOffDeviceCounts(totalList.stream().filter(bizDevice -> "1".equals(bizDevice.getAssayStatus())).count());
- }
- //统计今日的化验数据
- //先获取今日的所有的化验的质控样数据 再编写通用方法,判断是否合格,再获取合格的数量
- List<ZAssayResult> zAssayResults = this.zAssayResultMapper.selectZAssayResultList(ZAssayResult.builder().resultDate(DateUtils.getDate()).assayType("3").build());
- // List<ZAssayResult> zAssayResults = this.zAssayResultMapper.selectZAssayResultList(ZAssayResult.builder().resultDate("2024-08-13").assayType("3").build());
- if(!CollectionUtils.isEmpty(zAssayResults)){
- int checkTotals = zAssayResults.size();
- build.setAssayTotals(checkTotals);
- int passCheckTotals = this.getPassedCounts(zAssayResults);
- build.setPassAssayTotals(passCheckTotals);
- build.setPassAssayRates(NumberUtils.getDoublePercentAndNLength(INT_4, (double)passCheckTotals / (double)checkTotals, INT_2));//整数除以整数还是整数,所以这里要强转成小数
- }
- return build;
- }
- @Override
- public List<DeviceRes> devicePageList(DeviceReq deviceReq) {
- //处理一下化验日期,如果没有,默认是今天
- String assayDate = deviceReq.getAssayDate();
- if(StringUtils.isBlank(assayDate)){
- assayDate = DateUtils.getDate();
- // assayDate = "2024-08-13";
- }
- //先获取设备列表 直接写SQL了,不使用copy
- List<DeviceRes> list =this.bizDeviceMapper.devicePageList(deviceReq);
- //额外处理其他数据
- if(!CollectionUtils.isEmpty(list)){
- for (DeviceRes deviceRes : list) {
- this.addDeviceExtra(deviceRes, assayDate, deviceRes.getDeviceNo());
- }
- }
- return list;
- }
- @Override
- public List<AssayDetailRes> assayPageListByDeviceNoAndDate(AssayReq assayReq) {
- //通过设备编号和日期获取质控样的化验记录的明细化验时间和化验编号
- //2024年10月28日14:51:33 化验室设备还是连续监测设备是分开的,不可以同时出现
- if(StringUtils.isBlank(assayReq.getType()))
- throw new RuntimeException("请输入一种设备类型");
- String nowDate = DateUtils.getDate();
- if(StringUtils.isBlank(assayReq.getTimeBegin()))
- assayReq.setTimeBegin(nowDate);
- if(StringUtils.isBlank(assayReq.getTimeEnd()))
- assayReq.setTimeEnd(nowDate);
- List<AssayDetailRes> list = this.zAssayMapper.assayPageListByDeviceNoAndDate(assayReq);
- if(!CollectionUtils.isEmpty(list)){
- for (AssayDetailRes assayDetailRes : list) {
- String assayNo = assayDetailRes.getAssayNo();
- if(StringUtils.isNotBlank(assayNo)){
- this.addAssayExtra(assayNo, assayDetailRes);
- }
- }
- }
- return list;
- }
- @Override
- public List<AssayQualityDetailRes> assayCountListByDates(AssayReq assayReq) {
- //2024年10月21日10:56:31 只获取质控样的数据
- //2024年10月22日16:22:53 因为数据的原因,这里只获取化验室的数据 即biz_device表type=1
- // List<ZAssayResult> zAssayResults = this.zAssayResultMapper.selectZAssayResultList(ZAssayResult.builder().assayType("3").deviceNo(assayReq.getDeviceNo()).timeBegin(assayReq.getTimeBegin()).timeEnd(assayReq.getTimeEnd()).assayType("3").build());
- List<ZAssayResult> zAssayResults = this.zAssayResultMapper.assayCountListByDates(assayReq);
- if(CollectionUtils.isEmpty(zAssayResults)){
- return Collections.emptyList();
- }
- //有数据 处理统计信息
- return this.getCountInfoByAssayResults(zAssayResults);
- }
- @Override
- public Map<String, Map<String, List<ContinuousAssayResultRes>>> continuousAssayCountByDates(AssayReq assayReq) {
- //返回的最终对象
- Map<String, Map<String, List<ContinuousAssayResultRes>>> resultMap = new LinkedHashMap<>();
- //通过筛选条件获取最基本的数据列表
- List<ContinuousAssayBasic> list = this.zAssayResultMapper.continuousAssayCountByDates(assayReq);
- AtomicInteger totalCount = new AtomicInteger();//合计次数
- if(!CollectionUtils.isEmpty(list)){
- //有数据 再分组,再分别统计信息
- Map<String, List<ContinuousAssayBasic>> itemNameMap = list.stream().collect(Collectors.groupingBy(ContinuousAssayBasic::getItemName));
- itemNameMap.forEach((k, v) -> {
- //内存的map
- HashMap<String, List<ContinuousAssayResultRes>> innerMap = new HashMap<>();
- //还需要再根据化验指标再分一次组
- Map<String, List<ContinuousAssayBasic>> assayNameMap = v.stream().collect(Collectors.groupingBy(ContinuousAssayBasic::getAssayName));
- assayNameMap.forEach((assayName, assayBasicList) -> {
- List<ContinuousAssayResultRes> innerList = new ArrayList<>(assayBasicList.size());
- ContinuousAssayResultRes continuousAssayResultRes = new ContinuousAssayResultRes();
- //处理统计信息
- int assayCounts = assayBasicList.size();//实际检测数量
- totalCount.addAndGet(assayCounts);
- //调用通用的获取合格数量的方法
- List<ZAssayResult> zAssayResults = new ArrayList<>();
- for (ContinuousAssayBasic continuousAssayBasic : assayBasicList) {
- ZAssayResult zAssayResult = new ZAssayResult();
- zAssayResult.setAssayItem(continuousAssayBasic.getAssayName());
- zAssayResult.setDeviceNo(continuousAssayBasic.getDeviceNo());
- zAssayResult.setResultConcentration(continuousAssayBasic.getResultConcentration());
- zAssayResults.add(zAssayResult);
- }
- int passedCounts = this.getPassedCounts(zAssayResults);
- //封装到对象
- continuousAssayResultRes.setAssayName(assayName);
- continuousAssayResultRes.setAssayCounts(assayCounts);
- assayBasicList.stream().map(ContinuousAssayBasic::getResultConcentration).min(Double::compareTo).ifPresent(continuousAssayResultRes::setMinVal);
- assayBasicList.stream().map(ContinuousAssayBasic::getResultConcentration).max(Double::compareTo).ifPresent(continuousAssayResultRes::setMaxVal);
- double passedRates = (double) passedCounts / (double) assayCounts;
- continuousAssayResultRes.setPassedRates(NumberUtils.getDoublePercentAndNLength(INT_4, passedRates, INT_2));
- continuousAssayResultRes.setRatesDeviation(NumberUtils.getDoublePercentAndNLength(INT_4, passedRates - continuousAssayResultRes.getBxDouble(), INT_2));
- innerList.add(continuousAssayResultRes);
- innerMap.put(assayName, innerList);
- });
- resultMap.put(k, innerMap);
- });
- //2024年10月10日10:10:10 把 合计的结果也返回
- HashMap<String, List<ContinuousAssayResultRes>> extraMap = new HashMap<>();
- List<ContinuousAssayResultRes> extraList = new ArrayList<>();
- ContinuousAssayResultRes continuousAssayResultRes = new ContinuousAssayResultRes();
- continuousAssayResultRes.setAssayCounts(totalCount.get());
- //2024年10月10日19:16:39 因为有默认值,所以需要把他们清空
- continuousAssayResultRes.setBx(null);
- continuousAssayResultRes.setRatesDeviation(null);
- extraList.add(continuousAssayResultRes);
- extraMap.put("合计", extraList);
- resultMap.put("total", extraMap);
- //2024年10月21日14:14:53 最下面额外再添加新的维度的统计数据
- HashMap<String, List<ContinuousAssayResultRes>> calculateMap = new HashMap<>();
- List<ContinuousAssayResultRes> calculateList = new ArrayList<>();
- //2024年10月21日14:43:43 额外统计的指标汇总信息
- Map<String, List<ContinuousAssayBasic>> collect = list.stream().collect(Collectors.groupingBy(ContinuousAssayBasic::getAssayName));
- collect.forEach((key, value) -> {
- ContinuousAssayResultRes continuousAssayResultResCalculate = new ContinuousAssayResultRes();
- continuousAssayResultResCalculate.setAssayName(key);
- continuousAssayResultResCalculate.setAssayCounts(value.size());
- continuousAssayResultResCalculate.setBx(null);
- calculateList.add(continuousAssayResultResCalculate);
- });
- calculateMap.put("额外统计", calculateList);
- resultMap.put("calculate", calculateMap);
- }
- return resultMap;
- }
- @Override
- public Map<String, ContinuousAssayResultResNew> continuousAssayCountByDatesNew(AssayReq assayReq) {
- //返回的最终对象
- Map<String, ContinuousAssayResultResNew> resultMap = new LinkedHashMap<>();
- //通过筛选条件获取最基本的数据列表
- List<ContinuousAssayBasic> list = this.zAssayResultMapper.continuousAssayCountByDates(assayReq);
- Map<String, List<ContinuousAssayBasic>> map = list.stream().collect(Collectors.groupingBy(ContinuousAssayBasic::getAssayName));
- map.forEach((key, value) -> {
- //获取同检测指标的的质控样的统计数据
- List<ZAssayResult> zAssayResults = this.zAssayResultMapper.selectZAssayResultList(ZAssayResult.builder().assayType("3").assayItem(value.get(0).getAssayItem()).deviceNo(assayReq.getDeviceNo()).timeBegin(assayReq.getTimeBegin()).timeEnd(assayReq.getTimeEnd()).build());
- int zkCounts = zAssayResults.size();
- int passedCounts = this.getPassedCounts(zAssayResults);
- double val = (double) passedCounts / zkCounts;//质控样合格率
- //组装对应的数据
- ContinuousAssayResultResNew continuousAssayResultResNew = new ContinuousAssayResultResNew();
- continuousAssayResultResNew.setAssayName(key);
- continuousAssayResultResNew.setAssayCounts(value.size());
- continuousAssayResultResNew.setZkCounts(zkCounts);
- continuousAssayResultResNew.setPassedRates(NumberUtils.getDoublePercentAndNLength(INT_4, val, INT_2));
- double ratesDeviation = val - continuousAssayResultResNew.getBxDouble();
- double ratesDeviationNumber = ratesDeviation > 0 ? 0D : Math.abs(ratesDeviation);
- continuousAssayResultResNew.setRatesDeviation(NumberUtils.getDoublePercentAndNLength(INT_4, ratesDeviationNumber, INT_2));
- continuousAssayResultResNew.setRatesDeviationNumber(ratesDeviationNumber);
- resultMap.put(key, continuousAssayResultResNew);
- });
- return resultMap;
- }
- private List<AssayQualityDetailRes> getCountInfoByAssayResults(List<ZAssayResult> zAssayResults) {
- List<AssayQualityDetailRes> result = new ArrayList<>();
- if(!CollectionUtils.isEmpty(zAssayResults)){
- //先按照水厂进行分组
- Map<String, List<ZAssayResult>> map = zAssayResults.stream().collect(Collectors.groupingBy(ZAssayResult::getDeviceNo));
- map.forEach((deviceNo, value) -> {
- AssayQualityDetailRes assayQualityDetailRes = new AssayQualityDetailRes();
- log.info("水厂的编号为{}", deviceNo);
- //获取水厂的名称
- //先查询化验室,再获取水厂信息
- List<BizDevice> bizDevices = this.bizDeviceMapper.selectBizDeviceList(BizDevice.builder().deviceNo(deviceNo).build());
- if(!CollectionUtils.isEmpty(bizDevices)){
- //水厂
- /*log.info("化验对应的设备id为{}", bizDevices.get(0).getDeviceWorks());
- List<BizWaterWork> bizWaterWorks = this.bizWaterWorkMapper.selectBizWaterWorkList(BizWaterWork.builder().worksId(bizDevices.get(0).getDeviceWorks()).build());
- if(!CollectionUtils.isEmpty(bizWaterWorks)){
- assayQualityDetailRes.setWorksName(bizWaterWorks.get(0).getWorksName());
- }*/
- Long deviceWorks = bizDevices.get(0).getDeviceWorks();
- if(Objects.isNull(deviceWorks)){
- log.error("通过化验结果的化验设备编号查询设备记录时,没有查询到设备记录,导致无法获取水厂信息");
- }else{
- BizWaterWork bizWaterWork = this.bizWaterWorkMapper.selectBizWaterWorkByWorksId(deviceWorks);
- if(!Objects.isNull(bizWaterWork)){
- assayQualityDetailRes.setWorksName(bizWaterWork.getWorksName());
- }
- }
- }
- //处理统计信息
- //处理总的合格率
- int totalDeviationRateCounts = 0;
- int totalpassedDeviationRateCounts = 0;
- //高COD
- List<ZAssayResult> highCodList = value.stream().filter(zAssayResult -> "1".equals(zAssayResult.getAssayItem())).collect(Collectors.toList());
- Double highCodDeviationRate = null;//后续有个总的合格率,这里用null默认,后面计算分别做判断
- if(!CollectionUtils.isEmpty(highCodList)){
- int highCodArrayCounts = highCodList.size();
- assayQualityDetailRes.setHighCodArrayCounts(highCodArrayCounts);
- int highCodPassedCounts = this.getPassedCounts(highCodList);
- assayQualityDetailRes.setHighCodPassedCounts(highCodPassedCounts);
- highCodDeviationRate = (double) highCodPassedCounts / (double) highCodArrayCounts;
- totalDeviationRateCounts += highCodArrayCounts;
- totalpassedDeviationRateCounts += highCodPassedCounts;
- assayQualityDetailRes.setHighCodDeviationRate(NumberUtils.getDoublePercentAndNLength(INT_4, highCodDeviationRate, INT_2));
- }
- //低COD
- List<ZAssayResult> lowCodList = value.stream().filter(zAssayResult -> "2".equals(zAssayResult.getAssayItem())).collect(Collectors.toList());
- Double lowCodDeviationRate = null;//后续有个总的合格率,这里用null默认,后面计算分别做判断
- if(!CollectionUtils.isEmpty(lowCodList)){
- int lowCodArrayCounts = lowCodList.size();
- assayQualityDetailRes.setLowCodArrayCounts(lowCodArrayCounts);
- int lowCodPassedCounts = this.getPassedCounts(lowCodList);
- assayQualityDetailRes.setLowCodPassedCounts(lowCodPassedCounts);
- lowCodDeviationRate = (double) lowCodPassedCounts / (double) lowCodArrayCounts;
- totalDeviationRateCounts += lowCodArrayCounts;
- totalpassedDeviationRateCounts += lowCodPassedCounts;
- assayQualityDetailRes.setLowCodDeviationRate(NumberUtils.getDoublePercentAndNLength(INT_4, lowCodDeviationRate, INT_2));
- }
- //氨氮
- List<ZAssayResult> anDanList = value.stream().filter(zAssayResult -> "3".equals(zAssayResult.getAssayItem())).collect(Collectors.toList());
- Double anDanDeviationRate = null;//后续有个总的合格率,这里用null默认,后面计算分别做判断
- if(!CollectionUtils.isEmpty(anDanList)){
- int anDanArrayCounts = anDanList.size();
- assayQualityDetailRes.setAnDanArrayCounts(anDanArrayCounts);
- int anDanPassedCounts = this.getPassedCounts(anDanList);
- assayQualityDetailRes.setAnDanPassedCounts(anDanPassedCounts);
- anDanDeviationRate = (double) anDanPassedCounts / (double) anDanArrayCounts;
- totalDeviationRateCounts += anDanArrayCounts;
- totalpassedDeviationRateCounts += anDanPassedCounts;
- assayQualityDetailRes.setAnDanDeviationRate(NumberUtils.getDoublePercentAndNLength(INT_4, anDanDeviationRate, INT_2));
- }
- //总氮
- List<ZAssayResult> tnList = value.stream().filter(zAssayResult -> "4".equals(zAssayResult.getAssayItem())).collect(Collectors.toList());
- Double tnDeviationRate = null;//后续有个总的合格率,这里用null默认,后面计算分别做判断
- if(!CollectionUtils.isEmpty(tnList)){
- int tnArrayCounts = tnList.size();
- assayQualityDetailRes.setTnArrayCounts(tnArrayCounts);
- int tnPassedCounts = this.getPassedCounts(tnList);
- assayQualityDetailRes.setTnPassedCounts(tnPassedCounts);
- tnDeviationRate = (double) tnPassedCounts / (double) tnArrayCounts;
- totalDeviationRateCounts += tnArrayCounts;
- totalpassedDeviationRateCounts += tnPassedCounts;
- assayQualityDetailRes.setTnDeviationRate(NumberUtils.getDoublePercentAndNLength(INT_4, tnDeviationRate, INT_2));
- }
- //总磷
- List<ZAssayResult> tpList = value.stream().filter(zAssayResult -> "5".equals(zAssayResult.getAssayItem())).collect(Collectors.toList());
- Double tpDeviationRate = null;//后续有个总的合格率,这里用null默认,后面计算分别做判断
- if(!CollectionUtils.isEmpty(tpList)){
- int tpArrayCounts = tpList.size();
- assayQualityDetailRes.setTpArrayCounts(tpArrayCounts);
- int tpPassedCounts = this.getPassedCounts(tpList);
- assayQualityDetailRes.setTpPassedCounts(tpPassedCounts);
- tpDeviationRate = (double) tpPassedCounts / (double) tpArrayCounts;
- totalDeviationRateCounts += tpArrayCounts;
- totalpassedDeviationRateCounts += tpPassedCounts;
- assayQualityDetailRes.setTpDeviationRate(NumberUtils.getDoublePercentAndNLength(INT_4, tpDeviationRate, INT_2));
- }
- //正磷酸盐
- List<ZAssayResult> zlsyList = value.stream().filter(zAssayResult -> "6".equals(zAssayResult.getAssayItem())).collect(Collectors.toList());
- Double zlsyDeviationRate = null;//后续有个总的合格率,这里用null默认,后面计算分别做判断
- if(!CollectionUtils.isEmpty(zlsyList)){
- int zlsyArrayCounts = zlsyList.size();
- assayQualityDetailRes.setZlsyArrayCounts(zlsyArrayCounts);
- int zlsyPassedCounts = this.getPassedCounts(zlsyList);
- assayQualityDetailRes.setZlsyPassedCounts(zlsyPassedCounts);
- zlsyDeviationRate = (double) zlsyPassedCounts / (double) zlsyArrayCounts;
- totalDeviationRateCounts += zlsyArrayCounts;
- totalpassedDeviationRateCounts += zlsyPassedCounts;
- assayQualityDetailRes.setZlsyDeviationRate(NumberUtils.getDoublePercentAndNLength(INT_4, zlsyDeviationRate, INT_2));
- }
- //亚硝酸盐氮
- List<ZAssayResult> yxsydList = value.stream().filter(zAssayResult -> "7".equals(zAssayResult.getAssayItem())).collect(Collectors.toList());
- Double yxsydDeviationRate = null;//后续有个总的合格率,这里用null默认,后面计算分别做判断
- if(!CollectionUtils.isEmpty(yxsydList)){
- int yxsydArrayCounts = yxsydList.size();
- assayQualityDetailRes.setYxsyArrayCounts(yxsydArrayCounts);
- int ysxydPassedCounts = this.getPassedCounts(yxsydList);
- assayQualityDetailRes.setYxsyPassedCounts(ysxydPassedCounts);
- yxsydDeviationRate = (double) ysxydPassedCounts / (double) yxsydArrayCounts;
- totalDeviationRateCounts += yxsydArrayCounts;
- totalpassedDeviationRateCounts += ysxydPassedCounts;
- assayQualityDetailRes.setYxsyDeviationRate(NumberUtils.getDoublePercentAndNLength(INT_4, yxsydDeviationRate, INT_2));
- }
- //硝酸盐氮
- List<ZAssayResult> xsydList = value.stream().filter(zAssayResult -> "8".equals(zAssayResult.getAssayItem())).collect(Collectors.toList());
- Double xsydDeviationRate = null;//后续有个总的合格率,这里用null默认,后面计算分别做判断
- if(!CollectionUtils.isEmpty(xsydList)){
- int xsydArrayCounts = xsydList.size();
- assayQualityDetailRes.setXsydArrayCounts(xsydArrayCounts);
- int xsydPassedCounts = this.getPassedCounts(xsydList);
- assayQualityDetailRes.setXsydPassedCounts(xsydPassedCounts);
- xsydDeviationRate = (double) xsydPassedCounts / (double) xsydArrayCounts;
- totalDeviationRateCounts += xsydArrayCounts;
- totalpassedDeviationRateCounts += xsydPassedCounts;
- assayQualityDetailRes.setXsydDeviationRate(NumberUtils.getDoublePercentAndNLength(INT_4, xsydDeviationRate, INT_2));
- }
- assayQualityDetailRes.setTotalDeviationRate(NumberUtils.getDoublePercentAndNLength(INT_4, (double)totalpassedDeviationRateCounts / (double)totalDeviationRateCounts, INT_2));
- result.add(assayQualityDetailRes);
- });
- }
- return result;
- }
- private void addAssayExtra(String assayNo, AssayDetailRes assayDetailRes) {
- //分别处理化验设备的名称、水厂的名字字段(没有通过SQL关联查询,数据量大会很慢。。。)
- //化验室
- List<BizDevice> bizDevices = this.bizDeviceMapper.selectBizDeviceList(BizDevice.builder().deviceNo(assayDetailRes.getDeviceNo()).build());
- //水厂
- /*List<BizWaterWork> bizWaterWorks = this.bizWaterWorkMapper.selectBizWaterWorkList(BizWaterWork.builder().worksId(assayDetailRes.getDeviceWorks()).build());
- if(!CollectionUtils.isEmpty(bizWaterWorks)){
- assayDetailRes.setWorksName(bizWaterWorks.get(0).getWorksName());
- }*/
- if(CollectionUtils.isEmpty(bizDevices)){
- log.error("通过化验明细的设备号查询设备时,没有查询到对应的设备信息");
- }else{
- BizDevice bizDevice = bizDevices.get(0);
- assayDetailRes.setDeviceName(bizDevice.getDeviceName());
- assayDetailRes.setDeviceWorks(bizDevice.getDeviceWorks());
- Long deviceWorks = bizDevice.getDeviceWorks();
- if(Objects.isNull(deviceWorks)){
- log.error("通过化验结果的化验设备编号查询设备记录时,没有查询到设备记录id,导致无法获取水厂信息");
- }else{
- BizWaterWork bizWaterWork = this.bizWaterWorkMapper.selectBizWaterWorkByWorksId(deviceWorks);
- if(!Objects.isNull(bizWaterWork)){
- assayDetailRes.setWorksName(bizWaterWork.getWorksName());
- }
- }
- }
- //通过化验编号查询所有的化验明细,然后根据类型计算对应的
- List<ZAssayResult> zAssayResults = this.zAssayResultMapper.selectZAssayResultList(ZAssayResult.builder().assayNo(assayNo).assayType("3").build());
- calculateRate(assayDetailRes, zAssayResults);
- }
- private void calculateRate(AssayDetailRes assayDetailRes, List<ZAssayResult> zAssayResults) {
- if(!CollectionUtils.isEmpty(zAssayResults)){
- //高COD
- Optional<ZAssayResult> highCodOptional = zAssayResults.stream().filter(zAssayResult -> "1".equals(zAssayResult.getAssayItem())).findFirst();
- if(highCodOptional.isPresent()){
- ZAssayResult zAssayResult = highCodOptional.get();
- Double resultConcentration = NumberUtils.getDoubleNLength(INT_2, NumberUtils.getDoubleNLength(INT_2, zAssayResult.getResultConcentration()));
- if(!Objects.isNull(resultConcentration)){
- assayDetailRes.setHighCodResultConcentration(resultConcentration);
- Double highCodStandardVal = assayDetailRes.getHighCodStandardVal();
- Double val = (resultConcentration - highCodStandardVal) / highCodStandardVal;
- assayDetailRes.setHighCodDeviationRate(NumberUtils.getDoublePercentAndNLength(INT_4, val, INT_2));
- assayDetailRes.setHighCodDeviationRateNumber(NumberUtils.getDoubleNLength(INT_2, val));
- }
- }
- //低COD
- Optional<ZAssayResult> lowCodOptional = zAssayResults.stream().filter(zAssayResult -> "2".equals(zAssayResult.getAssayItem())).findFirst();
- if(lowCodOptional.isPresent()){
- ZAssayResult zAssayResult = lowCodOptional.get();
- Double resultConcentration = NumberUtils.getDoubleNLength(INT_2, zAssayResult.getResultConcentration());
- if(!Objects.isNull(resultConcentration)){
- assayDetailRes.setLowCodResultConcentration(resultConcentration);
- Double lowCodStandardVal = assayDetailRes.getLowCodStandardVal();
- Double val = (resultConcentration - lowCodStandardVal) / lowCodStandardVal;
- assayDetailRes.setLowCodDeviationRate(NumberUtils.getDoublePercentAndNLength(INT_4, val, INT_2));
- assayDetailRes.setLowCodDeviationRateNumber(NumberUtils.getDoubleNLength(INT_2, val));
- }
- }
- //氨氮
- Optional<ZAssayResult> andanOptional = zAssayResults.stream().filter(zAssayResult -> "3".equals(zAssayResult.getAssayItem())).findFirst();
- if(andanOptional.isPresent()){
- ZAssayResult zAssayResult = andanOptional.get();
- Double resultConcentration = NumberUtils.getDoubleNLength(INT_2, zAssayResult.getResultConcentration());
- if(!Objects.isNull(resultConcentration)){
- assayDetailRes.setAnDanResultConcentration(resultConcentration);
- Double anDanStandardVal = assayDetailRes.getAnDanStandardVal();
- Double val = (resultConcentration - anDanStandardVal) / anDanStandardVal;
- assayDetailRes.setAnDanDeviationRate(NumberUtils.getDoublePercentAndNLength(INT_4, val, INT_2));
- assayDetailRes.setAnDanDeviationRateNumber(NumberUtils.getDoubleNLength(INT_2, val));
- }
- }
- //总氮
- Optional<ZAssayResult> tnOptional = zAssayResults.stream().filter(zAssayResult -> "4".equals(zAssayResult.getAssayItem())).findFirst();
- if(tnOptional.isPresent()){
- ZAssayResult zAssayResult = tnOptional.get();
- Double resultConcentration = NumberUtils.getDoubleNLength(INT_2, zAssayResult.getResultConcentration());
- if(!Objects.isNull(resultConcentration)){
- assayDetailRes.setTnResultConcentration(resultConcentration);
- Double tnStandardVal = assayDetailRes.getTnStandardVal();
- Double val = (resultConcentration - tnStandardVal) / tnStandardVal;
- assayDetailRes.setTnDeviationRate(NumberUtils.getDoublePercentAndNLength(INT_4, val, INT_2));
- assayDetailRes.setTnDeviationRateNumber(NumberUtils.getDoubleNLength(INT_2, val));
- }
- }
- //总磷
- Optional<ZAssayResult> tpOptional = zAssayResults.stream().filter(zAssayResult -> "5".equals(zAssayResult.getAssayItem())).findFirst();
- if(tpOptional.isPresent()){
- ZAssayResult zAssayResult = tpOptional.get();
- Double resultConcentration = NumberUtils.getDoubleNLength(INT_2, zAssayResult.getResultConcentration());
- if(!Objects.isNull(resultConcentration)){
- assayDetailRes.setTpResultConcentration(resultConcentration);
- Double tpStandardVal = assayDetailRes.getTpStandardVal();
- Double val = (resultConcentration - tpStandardVal) / tpStandardVal;
- assayDetailRes.setTpDeviationRate(NumberUtils.getDoublePercentAndNLength(INT_4, val, INT_2));
- assayDetailRes.setTpDeviationRateNumber(NumberUtils.getDoubleNLength(INT_2, val));
- }
- }
- //正磷酸盐
- Optional<ZAssayResult> zlsyOptional = zAssayResults.stream().filter(zAssayResult -> "6".equals(zAssayResult.getAssayItem())).findFirst();
- if(zlsyOptional.isPresent()){
- ZAssayResult zAssayResult = zlsyOptional.get();
- Double resultConcentration = NumberUtils.getDoubleNLength(INT_2, zAssayResult.getResultConcentration());
- if(!Objects.isNull(resultConcentration)){
- assayDetailRes.setZlsyResultConcentration(resultConcentration);
- Double zlsyStandardVal = assayDetailRes.getZlsyStandardVal();
- Double val = (resultConcentration - zlsyStandardVal) / zlsyStandardVal;
- assayDetailRes.setZlsyDeviationRate(NumberUtils.getDoublePercentAndNLength(INT_4, val, INT_2));
- assayDetailRes.setZlsyDeviationRateNumber(NumberUtils.getDoubleNLength(INT_2, val));
- }
- }
- //亚硝酸盐氮
- Optional<ZAssayResult> yxsydOptional = zAssayResults.stream().filter(zAssayResult -> "7".equals(zAssayResult.getAssayItem())).findFirst();
- if(yxsydOptional.isPresent()){
- ZAssayResult zAssayResult = yxsydOptional.get();
- Double resultConcentration = NumberUtils.getDoubleNLength(INT_2, zAssayResult.getResultConcentration());
- if(!Objects.isNull(resultConcentration)){
- assayDetailRes.setYxsyResultConcentration(resultConcentration);
- Double yxsyStandardVal = assayDetailRes.getYxsyStandardVal();
- Double val = (resultConcentration - yxsyStandardVal) / yxsyStandardVal;
- assayDetailRes.setYxsyDeviationRate(NumberUtils.getDoublePercentAndNLength(INT_4, val, INT_2));
- assayDetailRes.setYxsyDeviationRateNumber(NumberUtils.getDoubleNLength(INT_2, val));
- }
- }
- //硝酸盐氮
- Optional<ZAssayResult> xsydOptional = zAssayResults.stream().filter(zAssayResult -> "8".equals(zAssayResult.getAssayItem())).findFirst();
- if(xsydOptional.isPresent()){
- ZAssayResult zAssayResult = xsydOptional.get();
- Double resultConcentration = NumberUtils.getDoubleNLength(INT_2, zAssayResult.getResultConcentration());
- if(!Objects.isNull(resultConcentration)){
- assayDetailRes.setXsydResultConcentration(resultConcentration);
- Double xsydStandardVal = assayDetailRes.getXsydStandardVal();
- Double val = (resultConcentration - xsydStandardVal) / xsydStandardVal;
- assayDetailRes.setXsydDeviationRate(NumberUtils.getDoublePercentAndNLength(INT_4, val, INT_2));
- assayDetailRes.setXsydDeviationRateNumber(NumberUtils.getDoubleNLength(INT_2, val));
- }
- }
- }
- }
- /**
- * 额外处理其他的字段 聚合,查询其他表,计算等
- *
- * @param deviceRes
- * @param assayDate
- * @param deviceNo
- */
- private void addDeviceExtra(DeviceRes deviceRes, String assayDate, String deviceNo) {
- //查询最近的一条化验记录明细 及 化验统计(结果)
- List<ZAssayResult> zAssayResults = this.zAssayResultMapper.selectZAssayResultList(ZAssayResult.builder().resultDate(assayDate).assayType("3").deviceNo(deviceNo).build());
- if(!CollectionUtils.isEmpty(zAssayResults)){
- int checkTotals = zAssayResults.size();
- int passCheckTotals = this.getPassedCounts(zAssayResults);
- deviceRes.setAssayTotals(passCheckTotals);
- deviceRes.setPassAssayRates(NumberUtils.getDoublePercentAndNLength(INT_4, (double)passCheckTotals / (double)checkTotals, INT_2));//整数除以整数还是整数,所以这里要强转成小数
- //获取最新的一条化验明细数据
- ZAssayResult zAssayResult = zAssayResults.get(0);
- //根据化验类型处理不同的数据
- String assayStatus = deviceRes.getAssayStatus();
- if("1".equals(assayStatus)){//离线
- deviceRes.setLastAssayTime(zAssayResult.getUpdateTime());
- }else if("2".equals(assayStatus)){//化验中
- deviceRes.setBeginAssayTime(zAssayResult.getUpdateTime());
- }
- deviceRes.setCurrentTemperature(zAssayResult.getResultWendu());
- // deviceRes.setCurrentHumidity(NumberUtils.getDoublePercentAndNLength(INT_4, zAssayResult.getResultShidu(), INT_2));
- //2024年10月14日16:55:06 原本就已经乘以100了,如果有值直接拼接%即可
- Double resultShidu = zAssayResult.getResultShidu();
- if(!Objects.isNull(resultShidu)){
- deviceRes.setCurrentHumidity(NumberUtils.getDoubleNLength(INT_2, resultShidu) + "%");
- }
- //获取最新的一条化验记录 处理化验进度的展示: 因为历史数据有总步进为0的,这种的默认进度就是0处理 只有化验中的才显示进度
- String assayNo = zAssayResult.getAssayNo();
- if("2".equals(deviceRes.getAssayStatus()) && StringUtils.isNotBlank(assayNo)){
- ZAssay zAssay = this.zAssayMapper.selectUniqueRecordByAssayNo(assayNo);
- if(!Objects.isNull(zAssay)){
- String stepNow = zAssay.getStepNow();
- String stepTotal = zAssay.getStepTotal();
- if(StringUtils.isBlank(stepNow) || StringUtils.isBlank(stepTotal) || "0".equals(stepNow) || "0".equals(stepTotal)){
- deviceRes.setAssayProgress(null);
- }else{
- deviceRes.setAssayProgress(NumberUtils.getDoublePercentAndNLength(INT_4, Double.parseDouble(stepNow)/Double.parseDouble(stepTotal), INT_2));
- }
- }
- }
- //todo 废液量暂时无法计算
- }
- //因为有重复数据,需要去重
- deviceRes.setAssayCounts(this.zAssayMapper.getDistinctCountsByDeviceNoAndDate(assayDate, deviceNo));
- }
- //判断每个质控样的数是否各个,然后获取汇总的数量
- private int getPassedCounts(List<ZAssayResult> zAssayResults) {
- boolean passed = false;
- int count = 0;
- for (ZAssayResult zAssayResult : zAssayResults) {
- passed = this.isPassed(zAssayResult);
- if (passed) {
- count++;//通过 数量加一
- }
- }
- return count;
- }
- //获取每个质控样的数据是否是合格的
- private boolean isPassed(ZAssayResult zAssayResult) {
- boolean isPassed = true;//默认是合格的,防止一些极端场景:没有配置范围 范围配置时间过期等等情况
- String deviceNo = zAssayResult.getDeviceNo();
- if(StringUtils.isBlank(deviceNo))
- return isPassed;//无法关联数据,默认合格
- //需要通过化验明细里的设备ID找到水厂ID,配置的表需要该参数
- List<BizDevice> bizDevices = this.bizDeviceMapper.selectBizDeviceList(BizDevice.builder().deviceNo(deviceNo).build());
- if(CollectionUtils.isEmpty(bizDevices)){
- return isPassed;//无法查询到水厂,默认合格
- }
- Date nowDate = DateUtils.getNowDate();
- // Date nowDate = DateUtils.parseDate("2024-08-13");
- Double resultConcentration = zAssayResult.getResultConcentration();//化验的真实值
- //去查询配置的标准值的范围(数据不确认,有可能是多个,获取最后一条数据):
- List<ZQualityValue> list = this.zQualityValueMapper.selectZQualityValueList(ZQualityValue.builder().beginTime(nowDate).endTime(nowDate).deviceWorks(bizDevices.get(0).getDeviceWorks()).assayItem(zAssayResult.getAssayItem()).build());
- //理论上是只有一个的
- if(!CollectionUtils.isEmpty(list)){
- ZQualityValue zQualityValue = list.get(0);//获取最新的一条,防止有多条数据的情况
- Double lowValue = zQualityValue.getLowValue();
- Double highValue = zQualityValue.getHighValue();
- if(!Objects.isNull(resultConcentration) && !Objects.isNull(lowValue) && !Objects.isNull(highValue)){
- isPassed = resultConcentration >= lowValue && resultConcentration <= highValue;
- }
- }
- return isPassed;
- }
- public static void main(String[] args) {
- List list = new ArrayList<>();
- list.add(1);
- list.add(1);
- System.out.println(list.stream().count());
- System.out.println(DateUtils.getDate());
- }
- }
|