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<Long> machineIds = this.parseMachineIdInGrid(plantDTO.getGridSetting());
|
for(Long mid:machineIds ) {
|
Machine m = machineMapper.selectById(mid);
|
if(m != null) {
|
m.setPlantId(plantId);
|
machineMapper.updateById(m);
|
}
|
}
|
|
}
|
|
/**
|
* 解析出地图中包括的设备id
|
* @param gridSetting
|
* @return
|
*/
|
public List<Long> parseMachineIdInGrid(String gridSetting){
|
List<Long> 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.getLong("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<Long> oriMachineIds = this.parseMachineIdInGrid(oriGridSetting);
|
for(Long mid:oriMachineIds ) {
|
Machine m = machineMapper.selectById(mid);
|
if(m != null) {
|
m.setPlantId(null);
|
machineMapper.updateById(m);
|
}
|
}
|
|
//新配置的机床,更新plantId
|
List<Long> machineIds = this.parseMachineIdInGrid(plantDTO.getGridSetting());
|
|
Long plantId = plantDTO.getId();
|
for(Long 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);
|
}
|
}
|
}
|