新火炬后端单体项目初始化代码
zhangherong
2025-06-26 0a66b4e946ebbe3ac09a193ad5a60cf7a95fe99d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package org.jeecg.modules.base.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.FillRuleConstant;
import org.jeecg.common.util.FillRuleUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.base.entity.Factory;
import org.jeecg.modules.base.entity.UserFactory;
import org.jeecg.modules.base.mapper.FactoryMapper;
import org.jeecg.modules.base.mapper.UserFactoryMapper;
import org.jeecg.modules.base.model.FactoryIdModel;
import org.jeecg.modules.base.model.FactoryTreeModel;
import org.jeecg.modules.base.service.IFactoryService;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.jeecg.modules.system.util.FindsProductionsChildrenUtil;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
 
/**
 * @Description: 工厂/车间/产线
 * @Author: jeecg-boot
 * @Date:   2025-06-24
 * @Version: V1.0
 */
@Service
public class FactoryServiceImpl extends ServiceImpl<FactoryMapper, Factory> implements IFactoryService {
 
    @Resource
    private SysUserMapper sysUserMapper;
 
    @Resource
    private UserFactoryMapper userFactoryMapper;
 
    /**
     * saveFactoryData 对应 add 保存用户在页面添加的新的产线对象数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveFactoryData(Factory factory) {
        if (factory != null) {
            if (factory.getParentId() == null) {
                factory.setParentId("");
            }
            factory.setId(IdWorker.getIdStr(factory));
            // 先判断该对象有无父级ID,有则意味着不是最高级,否则意味着是最高级
            // 获取父级ID
            String parentId = factory.getParentId();
            JSONObject formData = new JSONObject();
            formData.put("parentId",parentId);
            String factoryCategory = "";
            if(StringUtils.isBlank(parentId)){
                factoryCategory = "1";
            }else {
                // 查询出父级产线
                Factory parentFactory = super.getById(parentId);
                // 根据父级产线类型算出当前产线的类型
                factoryCategory = String.valueOf(Integer.valueOf(parentFactory.getFactoryCategory()) + 1);
            }
            factory.setFactoryCategory(factoryCategory);
            factory.setDelFlag(CommonConstant.DEL_FLAG_0);
            this.save(factory);
 
            //处理存在父子关系 mdc标记统一的问题
            //1.mdc标记 为 1 开启 父级节点要统一开启
            //2.mdc标记 为 0 关闭 子级节点要统一关闭  新增操作 不存在此情况
            if(StringUtils.isNotBlank(parentId) && CommonConstant.DEFAULT_1.equals(factory.getMdcFlag())){
                openParentMdcFlag(parentId);
            }
        }
    }
 
    /**
     * queryTreeList 对应 queryTreeList 查询所有的产线数据,以树结构形式响应给前端
     */
    @Override
    //@Cacheable(value = "mdc:cache:production:alldata")
    public List<FactoryTreeModel> queryTreeList() {
        LambdaQueryWrapper<Factory> query = new LambdaQueryWrapper<Factory>();
        query.eq(Factory::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
        query.orderByAsc(Factory::getSorter);
        List<Factory> list = this.list(query);
        //调用wrapTreeDataToTreeList方法生成树状数据
        return FindsProductionsChildrenUtil.wrapTreeDataToTreeList(list);
    }
 
    /**
     * queryTreeList 根据产线id查询,前端回显调用
     */
    @Override
    public List<FactoryTreeModel> queryTreeList(String ids) {
        List<FactoryTreeModel> listResult = new ArrayList<>();
        LambdaQueryWrapper<Factory> query = new LambdaQueryWrapper<Factory>();
        query.eq(Factory::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
        if (oConvertUtils.isNotEmpty(ids)) {
            query.in(true, Factory::getId, ids.split(","));
        }
        query.orderByAsc(Factory::getSorter);
        List<Factory> list = this.list(query);
        for (Factory factory : list) {
            if (factory.getRemark().isEmpty()){
                factory.setRemark("");
            }
            listResult.add(new FactoryTreeModel(factory));
        }
        return listResult;
    }
 
    /**
     * 根据产线id删除并删除其可能存在的子级产线
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(String id) {
        List<String> idList = new ArrayList<>();
        idList.add(id);
        this.checkChildrenExists(id, idList);
        boolean result = this.removeByIds(idList);
        //根据产线id删除用户与产线关系
        userFactoryMapper.delete(new LambdaQueryWrapper<UserFactory>().in(UserFactory::getFactoryId, idList));
        //根据产线id删除产线与设备关系
        //productionEquipmentMapper.delete(new LambdaQueryWrapper<MdcProductionEquipment>().in(MdcProductionEquipment::getProductionId, idList));
        return result;
    }
 
    /**
     * delete 方法调用 递归查找子集id
     */
    private void checkChildrenExists(String id, List<String> idList) {
        LambdaQueryWrapper<Factory> query = new LambdaQueryWrapper<>();
        query.eq(Factory::getParentId, id);
        List<Factory> factoryList = this.list(query);
        if (factoryList != null && !factoryList.isEmpty()) {
            for (Factory factory : factoryList) {
                idList.add(factory.getId());
                this.checkChildrenExists(factory.getId(), idList);
            }
        }
    }
 
    @Override
    //@Cacheable(value = "mdc:cache:production:allids")
    public List<FactoryIdModel> queryFactoryIdTreeList() {
        LambdaQueryWrapper<Factory> query = new LambdaQueryWrapper<>();
        query.eq(Factory::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
        query.orderByAsc(Factory::getSorter);
        List<Factory> list = this.list(query);
        //调用wrapTreeDataToTreeList方法生成树状数据
        return FindsProductionsChildrenUtil.wrapTreeDataToProductionIdTreeList(list);
    }
 
    /**
     * 打开 父节点 及 以上的mdc标记
     * @param parentId
     */
    private void openParentMdcFlag(String parentId) {
        List<String> listParentTree = findParentIdsForFactory(parentId, new ArrayList<>());
        if (!CollectionUtil.isEmpty(listParentTree)) {
            UpdateWrapper<Factory> updateWrapper = new UpdateWrapper<>();
            updateWrapper.in("id", listParentTree);
            updateWrapper.set("mdc_flag", "1");
            super.update(updateWrapper);
        }
    }
 
    public List<String> findParentIdsForFactory(String parentId, List<String> idList) {
        if (StringUtils.isEmpty(parentId)) {
            return null;
        }
        if (idList == null || idList.isEmpty()) {
            idList = new ArrayList<>();
        }
        boolean p = true;
        if (p) {
            Factory en = super.getById(parentId);
            if (en != null) {
                idList.add(0, en.getId());
            }
            if (StringUtils.isNotBlank(en.getParentId())) {
                parentId = en.getParentId();
                findParentIdsForFactory(parentId, idList);
            } else {
                p = false;
                return idList;
            }
        }
        return idList;
    }
}