Lius
2024-11-07 81eae83295642387de38a97fdc5a35f485307587
lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/EquipmentAlarmServiceImpl.java
@@ -1,12 +1,32 @@
package org.jeecg.modules.mdc.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.mdc.dto.EquipmentAlarmDto;
import org.jeecg.modules.mdc.entity.EquipmentAlarm;
import org.jeecg.modules.mdc.entity.MdcAlarmInfo;
import org.jeecg.modules.mdc.entity.MdcEquipmentOvertime;
import org.jeecg.modules.mdc.mapper.EquipmentAlarmMapper;
import org.jeecg.modules.mdc.service.IEquipmentAlarmService;
import org.jeecg.modules.mdc.service.IMdcAlarmInfoService;
import org.jeecg.modules.mdc.service.IMdcEquipmentService;
import org.jeecg.modules.mdc.vo.EquipmentAlarmVo;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
@@ -16,10 +36,127 @@
 */
@Service
public class EquipmentAlarmServiceImpl extends ServiceImpl<EquipmentAlarmMapper, EquipmentAlarm> implements IEquipmentAlarmService {
    @Resource
    private IMdcEquipmentService mdcEquipmentService;
    @Resource
    private IMdcAlarmInfoService mdcAlarmInfoService;
    @Override
    public List<EquipmentAlarm> findEquipmentAlarmByDate(String equipmentId, Date startTime, Date endTime) {
        return this.list(new LambdaQueryWrapper<EquipmentAlarm>()
                .ge(EquipmentAlarm::getCollecttime, startTime).le(EquipmentAlarm::getCollecttime, endTime)
                .eq(EquipmentAlarm::getEquipmentid, equipmentId).orderByDesc(EquipmentAlarm::getCollecttime));
    }
    /**
     * 分页列表
     *
     * @param userId
     * @param page
     * @param equipmentAlarmVo
     * @param req
     * @return
     */
    @Override
    public IPage<EquipmentAlarmDto> pageList(String userId, Page<EquipmentAlarmDto> page, EquipmentAlarmVo equipmentAlarmVo, HttpServletRequest req) {
        List<String> equipmentIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(equipmentAlarmVo.getParentId()) && StringUtils.isEmpty(equipmentAlarmVo.getEquipmentId())) {
            if ("2".equals(equipmentAlarmVo.getTypeTree())) {
                //部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, equipmentAlarmVo.getParentId());
            } else {
                //产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, equipmentAlarmVo.getParentId());
            }
        } else if (StringUtils.isNotEmpty(equipmentAlarmVo.getEquipmentId())) {
            //单台设备信息
            equipmentAlarmVo.setEquipmentIdList(Collections.singletonList(equipmentAlarmVo.getEquipmentId()));
        } else {
            //查询用户拥有的所有设备信息
            if ("2".equals(equipmentAlarmVo.getTypeTree())) {
                //部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, null);
            } else {
                //产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, null);
            }
        }
        if (equipmentAlarmVo.getEquipmentIdList() == null || equipmentAlarmVo.getEquipmentIdList().isEmpty()) {
            equipmentAlarmVo.setEquipmentIdList(equipmentIds);
        }
        if (equipmentAlarmVo.getEquipmentIdList() == null || equipmentAlarmVo.getEquipmentIdList().isEmpty()) {
            return null;
        }
        IPage<EquipmentAlarmDto> pageList = this.baseMapper.pageList(page, equipmentAlarmVo);
        pageList.getRecords().forEach(item -> {
            if (StringUtils.isBlank(item.getAlarmContent())) {
                List<MdcAlarmInfo> mdcAlarmInfoList = mdcAlarmInfoService.list(new LambdaQueryWrapper<MdcAlarmInfo>().eq(MdcAlarmInfo::getAlarmCode, item.getAlarmNo()).eq(MdcAlarmInfo::getDriveType, item.getDriveType()).eq(MdcAlarmInfo::getIsUse, CommonConstant.STATUS_0));
                if (mdcAlarmInfoList != null && !mdcAlarmInfoList.isEmpty()) {
                    item.setAlarmContent(mdcAlarmInfoList.get(0).getAlarmContent());
                }
            }
        });
        return pageList;
    }
    @Override
    public ModelAndView exportXls(String userId, EquipmentAlarmVo equipmentAlarmVo) {
        List<String> equipmentIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(equipmentAlarmVo.getParentId()) && StringUtils.isEmpty(equipmentAlarmVo.getEquipmentId())) {
            if ("2".equals(equipmentAlarmVo.getTypeTree())) {
                //部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, equipmentAlarmVo.getParentId());
            } else {
                //产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, equipmentAlarmVo.getParentId());
            }
        } else if (StringUtils.isNotEmpty(equipmentAlarmVo.getEquipmentId())) {
            //单台设备信息
            equipmentAlarmVo.setEquipmentIdList(Collections.singletonList(equipmentAlarmVo.getEquipmentId()));
        } else {
            //查询用户拥有的所有设备信息
            if ("2".equals(equipmentAlarmVo.getTypeTree())) {
                //部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, null);
            } else {
                //产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, null);
            }
        }
        if (equipmentAlarmVo.getEquipmentIdList() == null || equipmentAlarmVo.getEquipmentIdList().isEmpty()) {
            equipmentAlarmVo.setEquipmentIdList(equipmentIds);
        }
        if (equipmentAlarmVo.getEquipmentIdList() == null || equipmentAlarmVo.getEquipmentIdList().isEmpty()) {
            return null;
        }
        // Step.2 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        List<EquipmentAlarmDto> equipmentAlarmDtos = this.baseMapper.list(equipmentAlarmVo);
        if (equipmentAlarmDtos != null && !equipmentAlarmDtos.isEmpty()) {
            for (EquipmentAlarmDto equipmentAlarmDto : equipmentAlarmDtos) {
                if (StringUtils.isBlank(equipmentAlarmDto.getAlarmContent())) {
                    List<MdcAlarmInfo> mdcAlarmInfoList = mdcAlarmInfoService.list(new LambdaQueryWrapper<MdcAlarmInfo>().eq(MdcAlarmInfo::getAlarmCode, equipmentAlarmDto.getAlarmNo()).eq(MdcAlarmInfo::getDriveType, equipmentAlarmDto.getDriveType()).eq(MdcAlarmInfo::getIsUse, CommonConstant.STATUS_0));
                    if (mdcAlarmInfoList != null && !mdcAlarmInfoList.isEmpty()) {
                        equipmentAlarmDto.setAlarmContent(mdcAlarmInfoList.get(0).getAlarmContent());
                    }
                }
            }
        }
        // 导出文件名称
        mv.addObject(NormalExcelConstants.FILE_NAME, "设备报警列表");
        mv.addObject(NormalExcelConstants.CLASS, EquipmentAlarmDto.class);
        //获取当前登录用户
        //update-begin---author:wangshuai ---date:20211227  for:[JTC-116]导出人写死了------------
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("设备报警列表数据", "导出人:" + user.getRealname(), "设备报警"));
        //update-end---author:wangshuai ---date:20211227  for:[JTC-116]导出人写死了------------
        mv.addObject(NormalExcelConstants.DATA_LIST, equipmentAlarmDtos);
        return mv;
    }
}