liukangdong
2024-09-24 a1647a5152e3d368ec2fb738ca7cc858e220d06c
server/visits/dmvisit_service/src/main/java/com/doumee/service/business/impl/PlatformServiceImpl.java
@@ -1,5 +1,6 @@
package com.doumee.service.business.impl;
import cn.emay.sdk.util.StringUtil;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
@@ -7,23 +8,28 @@
import com.doumee.core.model.PageWrap;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.DeviceMapper;
import com.doumee.dao.business.PlatformDeviceMapper;
import com.doumee.dao.business.PlatformMapper;
import com.doumee.dao.business.model.Member;
import com.doumee.dao.business.model.Platform;
import com.doumee.dao.business.model.PlatformGroup;
import com.doumee.dao.business.join.PlatformJoinMapper;
import com.doumee.dao.business.model.*;
import com.doumee.dao.openapi.response.PlatformNumByStatusResponse;
import com.doumee.service.business.PlatformDeviceService;
import com.doumee.service.business.PlatformService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.Date;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
/**
 * 月台信息表Service实现
@@ -35,10 +41,64 @@
    @Autowired
    private PlatformMapper platformMapper;
    @Autowired
    private PlatformJoinMapper platformJoinMapper;
    @Autowired
    private PlatformDeviceMapper platformDeviceMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Override
    @Transactional(rollbackFor = {BusinessException.class,Exception.class})
    public Integer create(Platform platform) {
        platform.setCreateDate(new Date());
        platform.setIsdeleted(Constants.ZERO);
        platform.setCreator(platform.getLoginUserInfo().getId());
        platformMapper.insert(platform);
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(platform.getLedIds())){
            List<PlatformDevice> platformDeviceList = new ArrayList<>();
            for (Integer ledId:platform.getLedIds()) {
                Device device = deviceMapper.selectById(ledId);
                if(Objects.isNull(device)){
                    throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"未查询到led设备信息【"+ledId+"】");
                }
                PlatformDevice platformDevice = new PlatformDevice();
                platformDevice.setIsdeleted(Constants.ZERO);
                platformDevice.setPlatformId(platform.getId());
                platformDevice.setCreateDate(new Date());
                platformDevice.setCreator(platform.getLoginUserInfo().getId());
                platformDevice.setType(Constants.ZERO);
                platformDevice.setDeviceId(ledId.toString());
                platformDevice.setHkId(device.getHkId());
                platformDevice.setName(device.getName());
                platformDeviceList.add(platformDevice);
            }
            platformDeviceMapper.insert(platformDeviceList);
        }
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(platform.getBroadcastIds())){
            List<PlatformDevice> platformDeviceList = new ArrayList<>();
            for (Integer broadcastId:platform.getBroadcastIds()) {
                Device device = deviceMapper.selectById(broadcastId);
                if(Objects.isNull(device)){
                    throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"未查询到广播设备信息【"+broadcastId+"】");
                }
                PlatformDevice platformDevice = new PlatformDevice();
                platformDevice.setIsdeleted(Constants.ZERO);
                platformDevice.setPlatformId(platform.getId());
                platformDevice.setCreateDate(new Date());
                platformDevice.setCreator(platform.getLoginUserInfo().getId());
                platformDevice.setType(Constants.TWO);
                platformDevice.setDeviceId(broadcastId.toString());
                platformDevice.setHkId(device.getHkId());
                platformDevice.setName(device.getName());
                platformDeviceList.add(platformDevice);
            }
            platformDeviceMapper.insert(platformDeviceList);
        }
        return platform.getId();
    }
@@ -62,20 +122,75 @@
    }
    @Override
    @Transactional(rollbackFor = {BusinessException.class,Exception.class})
    public void updateById(Platform platform) {
        platform.setEditor(platform.getLoginUserInfo().getId());
        platform.setEditDate(new Date());
        //删除LED/广播数据
        platformDeviceMapper.delete(new QueryWrapper<PlatformDevice>().lambda()
                .eq(PlatformDevice::getPlatformId,platform.getId())
                .in(PlatformDevice::getType,Constants.ZERO,Constants.TWO)
        );
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(platform.getLedIds())){
            List<PlatformDevice> platformDeviceList = new ArrayList<>();
            String ledNames = "";
            for (Integer ledId:platform.getLedIds()) {
                Device device = deviceMapper.selectById(ledId);
                if(Objects.isNull(device)){
                    throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"未查询到led设备信息【"+ledId+"】");
                }
                PlatformDevice platformDevice = new PlatformDevice();
                platformDevice.setIsdeleted(Constants.ZERO);
                platformDevice.setPlatformId(platform.getId());
                platformDevice.setCreateDate(new Date());
                platformDevice.setCreator(platform.getLoginUserInfo().getId());
                platformDevice.setType(Constants.ZERO);
                platformDevice.setDeviceId(ledId.toString());
                platformDevice.setHkId(device.getHkId());
                platformDevice.setName(device.getName());
                platformDeviceList.add(platformDevice);
            }
            platformDeviceMapper.insert(platformDeviceList);
            platform.setLedNames(ledNames);
        }
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(platform.getBroadcastIds())){
            List<PlatformDevice> platformDeviceList = new ArrayList<>();
            String ledNames = "";
            for (Integer broadcastId:platform.getBroadcastIds()) {
                Device device = deviceMapper.selectById(broadcastId);
                if(Objects.isNull(device)){
                    throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"未查询到广播设备信息【"+broadcastId+"】");
                }
                PlatformDevice platformDevice = new PlatformDevice();
                platformDevice.setIsdeleted(Constants.ZERO);
                platformDevice.setPlatformId(platform.getId());
                platformDevice.setCreateDate(new Date());
                platformDevice.setCreator(platform.getLoginUserInfo().getId());
                platformDevice.setType(Constants.TWO);
                platformDevice.setDeviceId(broadcastId.toString());
                platformDevice.setHkId(device.getHkId());
                platformDevice.setName(device.getName());
                platformDeviceList.add(platformDevice);
            }
            platformDeviceMapper.insert(platformDeviceList);
        }
        platformMapper.updateById(platform);
    }
    @Override
    public void updateStatusById(Platform platform) {
        Platform model  =platformMapper.selectById(platform.getId());
        if(model ==null) {
            throw  new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"对不起,人员信息不存在!");
            throw  new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"对不起,该记录信息不存在!");
        }
        LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(loginUserInfo == null){
            loginUserInfo = platform.getLoginUserInfo();
        }
        Platform update = new Platform();
        update.setId(model.getId());
        update.setEditDate(new Date());
        update.setEditor(loginUserInfo.getId());
        update.setStatus(platform.getStatus());
@@ -127,7 +242,7 @@
                .ge(pageWrap.getModel().getEditDate() != null, Platform::getEditDate, Utils.Date.getStart(pageWrap.getModel().getEditDate()))
                .le(pageWrap.getModel().getEditDate() != null, Platform::getEditDate, Utils.Date.getEnd(pageWrap.getModel().getEditDate()))
                .eq(pageWrap.getModel().getIsdeleted() != null, Platform::getIsdeleted, pageWrap.getModel().getIsdeleted())
                .eq(pageWrap.getModel().getName() != null, Platform::getName, pageWrap.getModel().getName())
                .like(pageWrap.getModel().getName() != null, Platform::getName, pageWrap.getModel().getName())
                .eq(pageWrap.getModel().getRemark() != null, Platform::getRemark, pageWrap.getModel().getRemark())
                .eq(pageWrap.getModel().getStatus() != null, Platform::getStatus, pageWrap.getModel().getStatus())
                .eq(pageWrap.getModel().getSortnum() != null, Platform::getSortnum, pageWrap.getModel().getSortnum())
@@ -135,7 +250,6 @@
                .eq(pageWrap.getModel().getStartTime() != null, Platform::getStartTime, pageWrap.getModel().getStartTime())
                .eq(pageWrap.getModel().getEndTime() != null, Platform::getEndTime, pageWrap.getModel().getEndTime())
                .eq(pageWrap.getModel().getWorkingNum() != null, Platform::getWorkingNum, pageWrap.getModel().getWorkingNum())
                .eq(pageWrap.getModel().getAlermTime() != null, Platform::getAlermTime, pageWrap.getModel().getAlermTime())
                .ge(pageWrap.getModel().getLastEventTime() != null, Platform::getLastEventTime, Utils.Date.getStart(pageWrap.getModel().getLastEventTime()))
                .le(pageWrap.getModel().getLastEventTime() != null, Platform::getLastEventTime, Utils.Date.getEnd(pageWrap.getModel().getLastEventTime()))
                .eq(pageWrap.getModel().getDeviceNames() != null, Platform::getDeviceNames, pageWrap.getModel().getDeviceNames())
@@ -146,8 +260,7 @@
                .eq(pageWrap.getModel().getYpos() != null, Platform::getYpos, pageWrap.getModel().getYpos())
                .eq(pageWrap.getModel().getWidth() != null, Platform::getWidth, pageWrap.getModel().getWidth())
                .eq(pageWrap.getModel().getHeight() != null, Platform::getHeight, pageWrap.getModel().getHeight())
                .eq(pageWrap.getModel().getAngle() != null, Platform::getAngle, pageWrap.getModel().getAngle())
        ;
                .eq(pageWrap.getModel().getAngle() != null, Platform::getAngle, pageWrap.getModel().getAngle());
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
@@ -155,7 +268,35 @@
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(platformMapper.selectJoinPage(page,Platform.class, queryWrapper));
        PageData<Platform> platformPageData = PageData.from(platformMapper.selectJoinPage(page,Platform.class, queryWrapper));
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(platformPageData.getRecords())){
            for (Platform platform:platformPageData.getRecords()) {
                List<PlatformDevice> ledDevice =  platformDeviceMapper.selectList(new QueryWrapper<PlatformDevice>().lambda()
                        .eq(PlatformDevice::getPlatformId,platform.getId())
                        .eq(PlatformDevice::getType,Constants.ZERO)
                );
                if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(ledDevice)){
                    platform.setLedNames(String.join(",", ledDevice.stream().map(m->m.getName()).collect(Collectors.toList())));
                    platform.setLedIds(
                            ledDevice.stream().map(m->Integer.valueOf(m.getDeviceId())).collect(Collectors.toList())
                    );
                }
                List<PlatformDevice> broadcastDevice =  platformDeviceMapper.selectList(new QueryWrapper<PlatformDevice>().lambda()
                        .eq(PlatformDevice::getPlatformId,platform.getId())
                        .eq(PlatformDevice::getType,Constants.TWO)
                );
                if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(broadcastDevice)){
                    platform.setBroadcastNames(String.join(",", broadcastDevice.stream().map(m->m.getName()).collect(Collectors.toList())));
                    platform.setBroadcastIds(
                            broadcastDevice.stream().map(m->Integer.valueOf(m.getDeviceId())).collect(Collectors.toList())
                    );
                }
            }
        }
        return platformPageData;
    }
    @Override
@@ -163,4 +304,35 @@
        QueryWrapper<Platform> wrapper = new QueryWrapper<>(platform);
        return platformMapper.selectCount(wrapper);
    }
    @Override
    public PlatformNumByStatusResponse getPlatformNumByStatusResponse(){
        PlatformNumByStatusResponse platformNumByStatusResponse = new PlatformNumByStatusResponse();
        List<Platform> platformList =  platformJoinMapper.selectJoinList(Platform.class,new MPJLambdaWrapper<Platform>()
                .selectAll(Platform.class)
                .select(" ( select count(1) from platform_job pj where t.id = pj.PLATFORM_ID and pj.STATUS = "+Constants.PlatformJobStatus.WORKING.getKey()+" ) as workStatus ")
                .eq(Platform::getIsdeleted,Constants.ZERO)
        );
        if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(platformList)){
            //停用数量
            platformNumByStatusResponse.setForbiddenNum(
                    platformList.stream().filter(i->Constants.equalsInteger(i.getStatus(),Constants.ONE)).collect(Collectors.toList()).size()
            );
            //工作数量
            platformNumByStatusResponse.setUsingNum(
                    platformList.stream().filter(i->i.getWorkStatus()>Constants.ZERO).collect(Collectors.toList()).size()
            );
            //空闲数量
            platformNumByStatusResponse.setIdleNum(
                    platformList.stream().filter(i->i.getWorkStatus()<=Constants.ZERO).collect(Collectors.toList()).size()
            );
        }
        return platformNumByStatusResponse;
    }
}