zhangherong
2025-04-01 92bd9ed78ad1da026b81fcd8e88445b6834bba76
lxzn-module-eam-common/src/main/java/org/jeecg/modules/eam/service/impl/EamEquipmentServiceImpl.java
@@ -1,7 +1,17 @@
package org.jeecg.modules.eam.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.DataBaseConstant;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.eam.aspect.annotation.EquipmentHistoryLog;
import org.jeecg.modules.eam.constant.AssetStatusEnum;
import org.jeecg.modules.eam.constant.EquipmentMaintenanceStatus;
@@ -10,18 +20,29 @@
import org.jeecg.modules.eam.entity.EamEquipment;
import org.jeecg.modules.eam.entity.EamEquipmentExtend;
import org.jeecg.modules.eam.mapper.EamEquipmentMapper;
import org.jeecg.modules.eam.request.EamEquipmentQuery;
import org.jeecg.modules.eam.service.IEamEquipmentExtendService;
import org.jeecg.modules.eam.service.IEamEquipmentService;
import org.jeecg.modules.eam.tree.FindsEquipmentProductionUtil;
import org.jeecg.modules.eam.vo.EamEquipmentTree;
import org.jeecg.modules.eam.vo.EquipmentSearchResult;
import org.jeecg.modules.system.entity.MdcProduction;
import org.jeecg.modules.system.service.IMdcProductionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/**
 * @Description: 设备台账
 * @Author: jeecg-boot
 * @Date:   2025-03-19
 * @Date: 2025-03-19
 * @Version: V1.0
 */
@Service
@@ -31,6 +52,8 @@
    private EamEquipmentMapper eamEquipmentMapper;
    @Autowired
    private IEamEquipmentExtendService equipmentExtendService;
    @Autowired
    private IMdcProductionService mdcProductionService;
    @Override
    @Transactional(rollbackFor = Exception.class)
@@ -52,7 +75,201 @@
        equipmentExtendService.save(eamEquipmentExtend);
        //插入设备履历 TODO
        //插入设备履历   @EquipmentHistoryLog
        return eamEquipment;
    }
    @Override
    public List<EamEquipmentTree> loadTreeListByProductionIds(String ids) {
        List<String> productionIds = Arrays.asList(ids.split(","));
        //获取所有产线数据
        List<MdcProduction> productionList = mdcProductionService.list(new LambdaQueryWrapper<MdcProduction>().eq(MdcProduction::getDelFlag, CommonConstant.DEL_FLAG_0.toString()).orderByAsc(MdcProduction::getProductionOrder));
        List<String> allProductionIds = new ArrayList<>();
        //找到所有产线id的上级id
        if (!productionIds.isEmpty()) {
            for (String productionId : productionIds) {
                this.getAllProductionIds(productionList, productionId, allProductionIds);
            }
        }
        //过滤产线数据
        List<MdcProduction> list = productionList.stream().filter((MdcProduction mdcProduction) -> allProductionIds.contains(mdcProduction.getId())).collect(Collectors.toList());
        //组装产线设备树
        List<EamEquipmentTree> treeList = FindsEquipmentProductionUtil.wrapEquipmentProductionTreeList(list);
        //填充设备数据
        fillEquipmentByProduction(treeList);
        return treeList;
    }
    @Override
    public IPage<EamEquipment> queryPageList(IPage<EamEquipment> page, EamEquipmentQuery eamEquipment) {
        QueryWrapper<EamEquipment> queryWrapper = new QueryWrapper<>();
        //用户数据权限
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (sysUser == null) {
            return page;
        }
        if (StringUtils.isNotBlank(sysUser.getEquipmentIds())) {
            //选择了设备,根据设备id过滤设备
            List<String> equipArr = Arrays.asList(sysUser.getEquipmentIds().split(","));
            queryWrapper.in("e.equipment_code", equipArr);
        } else {
            //没有选择设备,根据车间过滤设备
            queryWrapper.exists("select 1 from mdc_user_production t where t.user_id={0} and t.pro_id=e.org_id", sysUser.getId());
        }
        //查询条件过滤
        if (eamEquipment != null) {
            if (StringUtils.isNotBlank(eamEquipment.getEquipmentCode())) {
                queryWrapper.like("e.equipment_code", eamEquipment.getEquipmentCode());
            }
            if (StringUtils.isNotBlank(eamEquipment.getEquipmentName())) {
                queryWrapper.like("e.equipment_name", eamEquipment.getEquipmentName());
            }
            if (StringUtils.isNotBlank(eamEquipment.getEquipmentImportance())) {
                queryWrapper.eq("e.equipment_importance", eamEquipment.getEquipmentImportance());
            }
            if (StringUtils.isNotBlank(eamEquipment.getAssetStatus())) {
                queryWrapper.like("e.asset_status", eamEquipment.getAssetStatus());
            }
            if (StringUtils.isNotBlank(eamEquipment.getTechnologyStatus())) {
                queryWrapper.like("e.technology_status", eamEquipment.getTechnologyStatus());
            }
            if (StringUtils.isNotBlank(eamEquipment.getOperationSystem())) {
                queryWrapper.like("e.operation_system", eamEquipment.getOperationSystem());
            }
            if (StringUtils.isNotBlank(eamEquipment.getOrgId())) {
                queryWrapper.like("e.org_id", eamEquipment.getOrgId());
            }
            if (StringUtils.isNotBlank(eamEquipment.getEquipmentCategory())) {
                queryWrapper.like("e.equipment_category", eamEquipment.getEquipmentCategory());
            }
            //排序
            if (StringUtils.isNotBlank(eamEquipment.getColumn()) && StringUtils.isNotBlank(eamEquipment.getOrder())) {
                //queryWrapper.like("column", eamEquipment.getColumn());
                String column = eamEquipment.getColumn();
                if (column.endsWith(CommonConstant.DICT_TEXT_SUFFIX)) {
                    column = column.substring(0, column.lastIndexOf(CommonConstant.DICT_TEXT_SUFFIX));
                }
                if (DataBaseConstant.SQL_ASC.equalsIgnoreCase(eamEquipment.getOrder())) {
                    queryWrapper.orderByAsc("e." + oConvertUtils.camelToUnderline(column));
                } else {
                    queryWrapper.orderByDesc("e." + oConvertUtils.camelToUnderline(column));
                }
            } else {
                queryWrapper.orderByDesc("e.create_time");
            }
        } else {
            queryWrapper.orderByDesc("e.create_time");
        }
        IPage<EamEquipment> ipage = eamEquipmentMapper.queryPageList(page, queryWrapper);
        return ipage;
    }
    @Override
    public List<EquipmentSearchResult> asyncLoadEquipment(String keyword, Integer pageSize, String id) {
        if (StringUtils.isNotBlank(id)) {
            EamEquipment eamEquipment = eamEquipmentMapper.selectById(id);
            if (eamEquipment != null) {
                List<EquipmentSearchResult> resultList = new ArrayList<>();
                resultList.add(new EquipmentSearchResult(eamEquipment));
                return resultList;
            }
        }
        IPage<EamEquipment> page = new Page<>(1, pageSize);
        QueryWrapper<EamEquipment> queryWrapper = new QueryWrapper<>();
        //用户数据权限
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (sysUser == null) {
            return Collections.emptyList();
        }
        if (StringUtils.isNotBlank(sysUser.getEquipmentIds())) {
            //选择了设备,根据设备id过滤设备
            List<String> equipArr = Arrays.asList(sysUser.getEquipmentIds().split(","));
            queryWrapper.in("equipment_code", equipArr);
        } else {
            //没有选择设备,根据车间过滤设备
            queryWrapper.exists("select 1 from mdc_user_production t where t.user_id={0} and t.pro_id=org_id", sysUser.getId());
        }
        if (StringUtils.isNotBlank(keyword)) {
            queryWrapper.like("equipment_code", keyword);
            queryWrapper.or().like("equipment_name", keyword);
        }
        queryWrapper.eq("del_flag", CommonConstant.DEL_FLAG_0);
        IPage<EamEquipment> pageResult = eamEquipmentMapper.queryPageList(page, queryWrapper);
        if (pageResult != null && CollectionUtil.isNotEmpty(pageResult.getRecords())) {
            List<EquipmentSearchResult> resultList = new ArrayList<>();
            pageResult.getRecords().forEach((record) -> {
                resultList.add(new EquipmentSearchResult(record));
            });
            return resultList;
        }
        return Collections.emptyList();
    }
    @Override
    public EamEquipment selectByEquipmentCode(String equipmentCode) {
        if (StringUtils.isBlank(equipmentCode)) {
            return null;
        }
        QueryWrapper<EamEquipment> queryWrapper = new QueryWrapper<>();
        //用户数据权限
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (sysUser == null) {
            return null;
        }
        if (StringUtils.isNotBlank(sysUser.getEquipmentIds())) {
            //选择了设备,根据设备id过滤设备
            List<String> equipArr = Arrays.asList(sysUser.getEquipmentIds().split(","));
            queryWrapper.in("equipment_code", equipArr);
        } else {
            //没有选择设备,根据车间过滤设备
            queryWrapper.exists("select 1 from mdc_user_production t where t.user_id={0} and t.pro_id=org_id", sysUser.getId());
        }
        queryWrapper.eq("equipment_code", equipmentCode);
        queryWrapper.eq("del_flag", CommonConstant.DEL_FLAG_0);
        return eamEquipmentMapper.selectOne(queryWrapper);
    }
    /**
     * 获取所有的产线id(包含所有上级)
     */
    private void getAllProductionIds(List<MdcProduction> productionList, String productionId, List<String> allProductionIds) {
        if (!allProductionIds.contains(productionId)) {
            allProductionIds.add(productionId);
        }
        for (MdcProduction mdcProduction : productionList) {
            if (StringUtils.isEmpty(mdcProduction.getParentId())) {
                continue;
            }
            if (productionId.equals(mdcProduction.getId())) {
                if (!allProductionIds.contains(mdcProduction.getParentId())) {
                    allProductionIds.add(mdcProduction.getParentId());
                    getAllProductionIds(productionList, mdcProduction.getParentId(), allProductionIds);
                }
            }
        }
    }
    /**
     * 产线设备树填充设备数据
     */
    private void fillEquipmentByProduction(List<EamEquipmentTree> treeList) {
        for (EamEquipmentTree mdcEquipmentTree : treeList) {
            List<EamEquipment> equipmentList = eamEquipmentMapper.queryByProductionId(mdcEquipmentTree.getKey());
            if (CollectionUtil.isNotEmpty(equipmentList)) {
                for (EamEquipment mdcEquipment : equipmentList) {
                    EamEquipmentTree tree = new EamEquipmentTree().convert(mdcEquipment);
                    tree.setParentId(mdcEquipmentTree.getKey());
                    tree.setType(2);
                    mdcEquipmentTree.getChildren().add(tree);
                }
                mdcEquipmentTree.setLeaf(false);
            }
            if (CollectionUtil.isNotEmpty(mdcEquipmentTree.getChildren())) {
                fillEquipmentByProduction(mdcEquipmentTree.getChildren());
            }
        }
    }
}