yangys
2024-02-02 b82c71a3e3a97a78bd18ff598d27f3062600d22a
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
package com.qianwen.mdc.service.plant;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qianwen.mdc.domain.Machine;
import com.qianwen.mdc.domain.plant.Plant;
import com.qianwen.mdc.dto.plant.PlantDTO;
import com.qianwen.mdc.mapper.MachineMapper;
import com.qianwen.mdc.mapper.PlantMapper;
 
/**
 * @author y_ys79
 * 厂房服务
 */
@Service
public class PlantService {
    @Autowired
    private PlantMapper plantMapper;
    @Autowired
    private MachineMapper machineMapper;
    
    public static final Logger logger = LoggerFactory.getLogger(PlantService.class);
    /**
     * 新增厂房
     * @param plantDTO
     */
    @Transactional
    public void save(PlantDTO plantDTO) {
 
        Plant p = new Plant();
        p.setName(plantDTO.getName());
        p.setGridSetting(plantDTO.getGridSetting());
       
        
        plantMapper.insert(p);
        
        Long plantId = p.getId();
        
        List<Integer> machineIds = this.parseMachineIdInGrid(plantDTO.getGridSetting());
        
        for(Integer mid:machineIds ) {
            Machine m = machineMapper.selectById(mid);
            if(m != null) {
                m.setPlantId(plantId);
                machineMapper.updateById(m);
            }
        }
        
    }
    
    public List<Integer> parseMachineIdInGrid(String gridSetting){
        List<Integer> macineIds = new ArrayList<>();
        try {
            
            JSONArray arr1 = JSONArray.parseArray(gridSetting);
            JSONObject node;
            for(int i=0;i<arr1.size();i++) {
                JSONArray arr2= arr1.getJSONArray(i);
                
                for(int j=0;j<arr2.size();j++) {
                    node = arr2.getJSONObject(j);
                    if(node.containsKey("id")) {
                        macineIds.add(node.getInteger("id"));
                    }
                }
            }
            
            return macineIds;
        }catch(Exception e) {
            logger.error("解析失败",e);
            return Collections.emptyList();
        }
        
    }
    /**
     * 修改厂房
     * @param plantDTO
     */
    @Transactional
    public void modify(PlantDTO plantDTO) {
 
        Plant p = plantMapper.selectById(plantDTO.getId());
        String oriGridSetting = p.getGridSetting();
       
        //清空原有的machine的plantId
        List<Integer> oriMachineIds = this.parseMachineIdInGrid(oriGridSetting);
        for(Integer mid:oriMachineIds ) {
            Machine m = machineMapper.selectById(mid);
            if(m != null) {
                m.setPlantId(null);
                machineMapper.updateById(m);
            }
        }
        
        //新配置的机床,更新plantId
        List<Integer> machineIds = this.parseMachineIdInGrid(plantDTO.getGridSetting());
        
        Long plantId = plantDTO.getId();
        for(Integer mid:machineIds ) {
            Machine m = machineMapper.selectById(mid);
            if(m != null) {
                m.setPlantId(plantId);
                machineMapper.updateById(m);
            }
        }
        
        //machineMapper.clearPlantIdInPlant(plantId);
        
        p.setName(plantDTO.getName());
        p.setGridSetting(plantDTO.getGridSetting());
        plantMapper.updateById(p);
    }
    
    
    
    /**
     * 删除厂房
     * @param ids 厂房id列表
     */
    @Transactional
    public void delete(List<Long> ids) {
        //TODO 检查厂房的设备数量
        for(Long plantId :ids) {
            plantMapper.deleteById(plantId);
            machineMapper.clearPlantIdInPlant(plantId);
        }
    }
}