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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.newfiber.api.core.commons.CustomException;
import com.newfiber.api.core.commons.ResultCode;
import com.newfiber.api.pc.controller.newmonitoring.PackRealTimeDataController;
import com.newfiber.api.pc.dao.BlackRiverRelevanceMapper;
import com.newfiber.api.pc.dto.BlackRiverRelevanceDTO;
import com.newfiber.api.pc.model.entity.BlackRiverRelevance;
import com.newfiber.api.pc.service.BlackRiverRelevanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName BlackRiverRelevanceServiceImpl
 * @Description TODO
 * @Author 张鸿志
 * @Date 2021年4月7日09:38:23 9:38
 * Version 1.0
 **/
@Service
public class BlackRiverRelevanceServiceImpl extends ServiceImpl<BlackRiverRelevanceMapper, BlackRiverRelevance> implements BlackRiverRelevanceService {

    @Autowired
    private PackRealTimeDataController packRealTimeDataController;

    @Override
    public void relevance(BlackRiverRelevanceDTO blackRiverRelevanceDTO) {
        if(StringUtils.isEmpty(blackRiverRelevanceDTO)){
            throw new CustomException(ResultCode.PARAM_NULL);
        }
        EntityWrapper<BlackRiverRelevance> wrapper = new EntityWrapper<>();
        wrapper.eq("yu_liang_site_no",blackRiverRelevanceDTO.getYuLiangSiteNo());
        //已配置过的集合
        List<BlackRiverRelevance> riverRelevances = this.selectList(wrapper);

        List<BlackRiverRelevance> list = new ArrayList<>();
        for (String siteNo : blackRiverRelevanceDTO.getSiteNo()) {
            BlackRiverRelevance relevance = new BlackRiverRelevance();
            relevance.setYuLiangSiteNo(blackRiverRelevanceDTO.getYuLiangSiteNo());
            relevance.setCameraNumber(blackRiverRelevanceDTO.getCameraNumber());
            relevance.setSiteNo(siteNo);
            list.add(relevance);
        }
        //如果查询出来已有的集合与新配置的集合长度相等,那就执行更新操作
        if(riverRelevances.size() > 0 && riverRelevances.size() == list.size()){
            for(int i = 0; i < riverRelevances.size(); i++){
                BlackRiverRelevance relevance = riverRelevances.get(i);
                relevance.setCameraNumber(list.get(i).getCameraNumber());
                relevance.setSiteNo(list.get(i).getSiteNo());
            }
            //批量更新
            boolean batchById = this.updateBatchById(riverRelevances);
            if (!batchById) {
                throw new CustomException(500,"批量更新失败!");
            }
        }else{
            //否则执行删除后再新增操作
            boolean delete = this.delete(wrapper);
            if(delete){
                //新增
                this.insertBatch(list);
            }else{
                throw new CustomException(500,"对已有的关联删除失败!");
            }
        }
    }


    @Override
    public List<JSONObject> findCameraNumberByBlackId(String blackId) {
        if(StringUtils.isEmpty(blackId)){
            throw new CustomException(ResultCode.PARAM_NULL);
        }
        EntityWrapper<BlackRiverRelevance> wrapper = new EntityWrapper<>();
        wrapper.eq("yu_liang_site_no",blackId);
        List<BlackRiverRelevance> blackRiverRelevances = this.selectList(wrapper);
        if(StringUtils.isEmpty(blackRiverRelevances)){
            return null;
        }
        List<JSONObject> list = new ArrayList<>();
        for (BlackRiverRelevance riverRelevance : blackRiverRelevances) {
            JSONObject jsonObject = packRealTimeDataController.selectBySiteNo(riverRelevance.getSiteNo());
            //给每个流量站配置相机编号
            jsonObject.put("cameraNo",riverRelevance.getCameraNumber());
            jsonObject.put("waterLevel",riverRelevance.getWarnLevel());
            if(jsonObject  == null){
                throw new CustomException(500,"远程调用失败!");
            }
            list.add(jsonObject);
        }
        return list;
    }

    @Override
    public List<BlackRiverRelevance> waterLevel() {
        EntityWrapper<BlackRiverRelevance> wrapper = new EntityWrapper<>();
        wrapper.isNotNull("site_no");
        wrapper.isNotNull("warn_level");
        return this.selectList(wrapper);
    }
}