Newer
Older
huludao / src / main / java / com / newfiber / api / pc / service / impl / BlackRiverServiceImpl.java
package com.newfiber.api.pc.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.newfiber.api.config.FileVisitProperty;
import com.newfiber.api.core.exception.RRException;
import com.newfiber.api.pc.dao.RiverManage.BlackRiverMapper;
import com.newfiber.api.pc.dao.RiverManage.RiverInfoDao;
import com.newfiber.api.pc.dao.RiverManage.SiteWaterLevelDao;
import com.newfiber.api.pc.model.River.*;
import com.newfiber.api.pc.service.BlackRiverImageService;
import com.newfiber.api.pc.service.BlackRiverService;
import com.newfiber.api.pc.service.ProblemLongTermErrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName BlackRiverServiceImpl
 * @Description TODO
 * @Author 张鸿志
 * @Date sj2 14:23
 * Version 1.0
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class BlackRiverServiceImpl extends ServiceImpl<BlackRiverMapper, BlackRiver> implements BlackRiverService {

    @Autowired
    private BlackRiverMapper blackRiverMapper;

    @Autowired
    private BlackRiverImageService blackRiverImageService;


    @Autowired
    private SiteWaterLevelDao siteWaterLevelDao;

    @Autowired
    private FileVisitProperty fileVisitProperty;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private ProblemLongTermErrService problemLongTermErrService;

    @Autowired
    private RiverInfoDao riverInfoDao;

    @Override
    public void updateBlackRiver(BlackRiver blackRiver) {
        if(StringUtils.isEmpty(blackRiver)){
            throw new RRException("参数不能为空");
        }
        BlackRiver blackRiver1 = this.selectById(blackRiver.getBlackId());
        if(StringUtils.isEmpty(blackRiver1)){
            throw new RRException("没有该黑臭水体");
        }
        this.updateById(blackRiver);
        EntityWrapper<BlackRiverImageUrl> wrapper = new EntityWrapper<>();
        wrapper.eq("black_inner_id",blackRiver.getBlackId());
        blackRiverImageService.delete(wrapper);
        if(blackRiver != null && blackRiver.getImageUrls().size() > 0){
            blackRiver.getImageUrls().forEach(s -> s.setBlackInnerId(blackRiver.getBlackId()));
            blackRiverImageService.insertBatch(blackRiver.getImageUrls());
        }
    }

    @Override
    public void addBlackRiver(BlackRiver blackRiver) {
        if(StringUtils.isEmpty(blackRiver.getBlackId())){
            throw new RRException("黑臭水体编号不能为空!");
        }
        BlackRiver vertifyObject = this.selectById(blackRiver.getBlackId());
        if(StringUtils.isEmpty(vertifyObject)){
            vertify(blackRiver.getGovernanceState());
            this.insert(blackRiver);
            List<BlackRiverImageUrl> imageUrls = blackRiver.getImageUrls();
            if(imageUrls!=null && imageUrls.size()>0){
                for (BlackRiverImageUrl imageUrl : imageUrls) {
                    imageUrl.setBlackInnerId(blackRiver.getBlackId());
                }
                blackRiverImageService.insertBatch(imageUrls);
            }
        }else{
            throw  new RRException("该黑臭水体编号已经存在!",500);
        }


    }

    private void vertify(String governanceState){
        ArrayList<String> states = new ArrayList<>(Arrays.asList(new String[]{"完成治理","治理中","方案制定"}));
        if(StringUtils.isEmpty(governanceState)){
            throw new RRException("治理状态不能为空");
        }
       if(!states.contains(governanceState)){
            throw new RRException("状态填写错误");
       }
    }

    @Override
    public List<BlackRiver> selectAll() {
        List<BlackRiver> blackRivers = blackRiverMapper.selectAll();
        return blackRivers;
    }

    @Override
    public List<BlackRiver> queryPage(Object riverName, Object blackType, Integer pageNo, Integer pageSize) {
        List<BlackRiver> blackRivers = blackRiverMapper.queryPage(riverName, blackType, pageNo, pageSize);
        for (BlackRiver blackRiver : blackRivers) {
            if(!StringUtils.isEmpty(blackRiver.getImageUrls())) {
                blackRiver.getImageUrls().forEach(s -> s.setImageUrl(fileVisitProperty.getWebPic() + s.getImageUrl()));
            }
        }
        return blackRivers;
    }

    @Override
    public int queryCount(Object riverName, Object blackType) {
        return blackRiverMapper.queryCount(riverName,blackType);
    }

    @Override
    public List<RiverInfoData> selectListData() {
        //查询黑臭水体
        List<RiverInfoData> data = blackRiverMapper.selectListData();
        List<SiteWaterLevel> siteWaterLevels = siteWaterLevelDao.listAll(2021);
        Map<String, List<SiteWaterLevel>> map = siteWaterLevels.stream().collect(Collectors.groupingBy(SiteWaterLevel::getStCode));
        if(data!=null){
            //组装水质数据
            for (RiverInfoData datum : data) {
                if(map.get(datum.getWaterQualitySiteNo()) != null){
                    datum.setWaterQualityLevel(map.get(datum.getWaterQualitySiteNo()).get(0).getNowLevel());
                }
            }
            //组装水位数据
            for (RiverInfoData datum : data) {
                if(datum.getWaterSiteNo()!=null){
                    Query query = new Query();
                    Criteria criteria = Criteria.where("");
                    criteria.andOperator(
                            //大于开始时间
                            Criteria.where("tt").gte(getDayBegin()),
                            //小于结束时间
                            Criteria.where("tt").lte(new Date())
                    );
                    query.addCriteria(criteria);
                    List<Sl6512014> list = mongoTemplate.find(query, Sl6512014.class, datum.getWaterSiteNo());
                    if(!list.isEmpty()){
                        datum.setWaterLevel(list.get(0).getZ());
                        if(list.get(0)!=null && list.get(0).getZ()!=null &&
                                list.get(1)!=null && list.get(1).getZ()!=null){
                            BigDecimal zt1 = list.get(0).getZ();
                            BigDecimal zt2 = list.get(1).getZ();
                            if(zt1.compareTo(zt2)>0){
                                datum.setWaterTrend(1);
                            }else if(zt1.compareTo(zt2)==0){
                                datum.setWaterTrend(0);
                            }else{
                                datum.setWaterTrend(2);
                            }
                        }else{
                            datum.setWaterTrend(0);
                        }
                    }
                }

            }
        }

        return data;
    }

    @Override
    public BlackRiver selecById(String blackId) {
        BlackRiver blackRiver = blackRiverMapper.selectById(blackId);
        if(!StringUtils.isEmpty(blackRiver.getImageUrls())){
            blackRiver.getImageUrls().forEach(s -> s.setImageUrl(fileVisitProperty.getWebPic() + s.getImageUrl()));
        }
        return blackRiver;
    }

    /**
     * 获取当天开始时间
     * @return
     */
    public static Date getDayBegin() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    @Override
    public OneMapBlackRiver oneMapSelectAll() {
        List<BlackRiver> blackRivers = this.selectList(null);
        if(blackRivers.size() == 0){
            return null;
        }
        List<BlackRiver> riverList = blackRivers.stream().filter(s -> "河流".equals(s.getBlackType())).collect(Collectors.toList());
        OneMapBlackRiver oneMapBlackRiver = new OneMapBlackRiver();
        oneMapBlackRiver.setTotal(blackRivers.size());
        oneMapBlackRiver.setRiverCount(riverList.size());
        oneMapBlackRiver.setRiverList(riverList);
        oneMapBlackRiver.setLakeCount(blackRivers.size() - riverList.size());
        blackRivers.removeAll(riverList);
        oneMapBlackRiver.setLakeList(blackRivers);
        RiverCount riverCount = problemLongTermErrService.riverCout();
        oneMapBlackRiver.setPassRate(riverCount.getPassRate());
        return oneMapBlackRiver;
    }


    @Override
    public OneMapGisResult oneMapBlackRiverInfo(String type) {
        OneMapGisResult oneMapGisResult = new OneMapGisResult();
        oneMapGisResult.setLabel("blackRiver");
        oneMapGisResult.setLableTitle("河 湖 水 系 一 张 图");
        List<OneMapInteraction> interactions = blackRiverMapper.oneMapBlackRiverInfo();
        if(type.compareTo("-1") == 0){
            oneMapGisResult.setOneMapInteraction(interactions);
            return oneMapGisResult;
        }

        oneMapGisResult.setOneMapInteraction(interactions.stream().filter(s -> s.getStatus().compareTo(Integer.valueOf(type)) == 0).collect(Collectors.toList()));
        return oneMapGisResult;
    }

    @Override
    public List<RiverInfoData> selectNewListData() {
        List<RiverInfoData> result=new ArrayList<>();
       List<RiverInfo> rivers= riverInfoDao.selectAllRiver();
       if(CollectionUtil.isNotEmpty(rivers)){
           rivers.forEach(r->{
               RiverInfoData river=new RiverInfoData();
               String stCode=r.getRiverCode();
               Query query = new Query();
               query.with(Sort.by(Sort.Direction.DESC,"tt"));
               Sl6512014 dto = mongoTemplate.findOne(query, Sl6512014.class,stCode);
               river.setBlackId(stCode);
               river.setBlackRiverName(r.getRiverName());
               river.setWaterLevel(dto.getZ());
               river.setWaterQualityLevel(r.getWaterQualityLevel());
               river.setWaterTrend(0);
               river.setWaterWarnLevel(r.getWaterWarnLevel());
               result.add(river);
           });
       }
        return result;
    }
}