package com.example.springboot.service; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.example.springboot.common.Result; import com.example.springboot.component.PLCAutoMes; import com.example.springboot.component.Plchome; import com.example.springboot.component.S7control; import com.example.springboot.entity.FlowCard; import com.example.springboot.entity.GlassInfo; import com.example.springboot.entity.Queue; import com.example.springboot.entity.StorageCage; import com.example.springboot.entity.StorageTask; import com.example.springboot.entity.device.PlcParameterObject; import com.example.springboot.mapper.HomeMapper; import com.example.springboot.mapper.QueueMapper; import com.fasterxml.jackson.databind.ObjectMapper; @Service public class StorageCageService { @Autowired private HomeMapper homeMapper; @Autowired private OutSliceServive outSliceServive; @Autowired private QueueMapper QueueMapper; @Autowired private SpianService spianService; public void EndTask(int types) { String A01glassid1 = spianService.queGlassid("DB103.128", 14).toString(); String A01glassid2 = spianService.queGlassid("DB103.142", 14).toString(); String A02glassid1 = spianService.queGlassid("DB103.156", 14).toString(); String B01glassid = spianService.queGlassid("DB103.184", 14).toString(); String B02glassid = spianService.queGlassid("DB103.212", 14).toString(); List DeviceList = S7control.getinstance().ReadWord("DB103.0", 10); if (types == 0) {// 终止进片任务 List cageList = homeMapper.SelectTaskByState(types); for (StorageTask storageTask : cageList) { // 判断出片任务位置并清除对应玻璃id if ((storageTask.getGlassId().equals(A01glassid1) || storageTask.getGlassId().equals(A01glassid2)) && DeviceList.get(4) == 0) { outSliceServive.StopTask(storageTask.getGlassId(), types); ClearGlassId("DB101.10.0"); } if (storageTask.getGlassId().equals(B01glassid) && DeviceList.get(2) == 0) { outSliceServive.StopTask(storageTask.getGlassId(), types); ClearGlassId("DB101.10.2"); } if (storageTask.getGlassId().equals(B02glassid) && DeviceList.get(3) == 0) { ClearGlassId("DB101.10.3"); outSliceServive.StopTask(storageTask.getGlassId(), types); } } // 发送终止进片任务信号 } else {// 终止出片任务 List cageLists = homeMapper.SelectTaskByState(types); List cageList2 = homeMapper.SelectTaskByState(2); if (cageList2.size() > 0) { cageLists.add(cageList2.get(0)); } for (StorageTask cageList : cageLists) { if (cageList.getTaskType().equals("1")) { System.out.println(DeviceList.get(5)); if (cageList.getGlassId().equals(A02glassid1) && DeviceList.get(5) == 0) { outSliceServive.StopTask(cageList.getGlassId(), types); ClearGlassId("DB101.10.1"); } if (cageList.getGlassId().equals(B01glassid) && DeviceList.get(2) == 0) { outSliceServive.StopTask(cageList.getGlassId(), types); ClearGlassId("DB101.10.2"); } if (cageList.getGlassId().equals(B02glassid) && DeviceList.get(3) == 0) { outSliceServive.StopTask(cageList.getGlassId(), types); ClearGlassId("DB101.10.3"); } } else if (cageList.getTaskType().equals("2")) { if (cageList.getGlassId().equals(A02glassid1) && DeviceList.get(5) == 0) { outSliceServive.StopTask(cageList.getGlassId(), 2); ClearGlassId("DB101.10.1"); } if (cageList.getGlassId().equals(B01glassid) && DeviceList.get(2) == 0) { outSliceServive.StopTask(cageList.getGlassId(), 2); ClearGlassId("DB101.10.2"); } if (cageList.getGlassId().equals(B02glassid) && DeviceList.get(3) == 0) { outSliceServive.StopTask(cageList.getGlassId(), 2); ClearGlassId("DB101.10.3"); } } } // List storageCages = homeMapper.selectinout(3); // for (StorageCage storageCage : storageCages) { // List storageCagess = // homeMapper.SelectStoragesGlassById(storageCage.getGlassId()); // for (StorageCage storageCagez : storageCagess) { // if (storageCagez.getState().equals("2")) {// 调拨进片 // outSliceServive.StopTask(storageCagez.getGlassId(), 0); // } else {// 出片 // outSliceServive.StopTask(storageCagez.getGlassId(), types); // } // // 判断出片任务位置并清除对应玻璃id // if (storageCagez.getGlassId() == A02glassid1&&DeviceList.get(5)==0) { // outSliceServive.StopTask(storageCagez.getGlassId(), types); // ClearGlassId("DB101.10.1"); // } else if (storageCagez.getGlassId() == B01glassid&&DeviceList.get(2)==0) { // outSliceServive.StopTask(storageCagez.getGlassId(), types); // ClearGlassId("DB101.10.2"); // } else if (storageCagez.getGlassId() == B02glassid&&DeviceList.get(3)==0) { // ClearGlassId("DB101.10.3"); // outSliceServive.StopTask(storageCagez.getGlassId(), types); // } // } // } } } // 手动完成任务 public void UpdateTask(Integer types, Integer shelfrack, String glassid) { if (types == 0) {// 完成进片 spianService.overtask(glassid); } else if (types == 1) {// 完成出片 spianService.overtask(glassid); } // else if (types == 2) {// 终止进片任务 // outSliceServive.StopTask(glassid, 0); // // 发送终止进片任务信号 // } else {// 终止出片任务 // outSliceServive.StopTask(glassid, 1); // } } // 判断任务类型后清除对应玻璃id public Result ClearGlassId(String position, String glassid) { Map map = new HashMap<>(); if (S7control.getinstance().CheckConnected() == true) { // 判断进出还是出片任务 Short type = homeMapper.SelectTaskByGlassId(glassid); if (type != null) { if (type == 0) { // 进片 outSliceServive.StopTask(glassid, 0); } else if (type == 1) { // 出片 outSliceServive.StopTask(glassid, 1); } else { // 调拨 outSliceServive.StopTask(glassid, 0); outSliceServive.StopTask(glassid, 1); } } ClearGlassId(position); map.put("message", "200"); } else { map.put("message", "300"); } return Result.success(map); } // 清除玻璃id public Result ClearGlassId(String position) { Map map = new HashMap<>(); if (S7control.getinstance().CheckConnected() == true) { S7control.getinstance().WriteBit(position, true); try { TimeUnit.MILLISECONDS.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); } S7control.getinstance().WriteBit(position, false); map.put("message", "200"); } else { map.put("message", "300"); } return Result.success(map); } public Result InsertQueueGlassId(Short id, Queue queue) { Map map = new HashMap<>(); Short zhi = 200; short result = homeMapper.SelectStorageByGlassId(queue.getglassId()); if (result > 0) { map.put("message", "300"); } else { if (id == 1) { // 调用伍存储过程 zhi = spianService.selectAll(queue.getglassId()); if (zhi == 200) { homeMapper.InsertQueueGlassId(queue, id); } } map.put("message", zhi); } if (zhi != 200) { Plchome.FeedState = true; } return Result.success(map); } public List SelectStorageCageInfo() { return homeMapper.SelectStorageCageInfo(); } // 增加/减少理片笼玻璃数 public Result UpdateStroageCageByCell(Integer cell, Integer num) { if (num == 0) { homeMapper.DeleteStroageCageByCell(cell); } else { homeMapper.updateStroageCageByCell(cell, num); homeMapper.UpdateStroageCageWidthByCell(cell); } Map map = new HashMap<>(); map.put("message", "200"); return Result.success(map); } // 查询进/出片任务 public List SelectStorageTask(int task_type) { List storageTasks = new ArrayList<>(); if (task_type == 0) { storageTasks = homeMapper.SelectStorageTask(task_type); } else { storageTasks = homeMapper.SelectStorageTask(task_type); } for (StorageTask storageTask : storageTasks) { storageTask.setstorageCage(homeMapper.SelectStorageByCell(storageTask.getLoadrack())); } return storageTasks; } // 手动完成任务 public Result FinishTask(Integer id) { StorageTask storageTask = homeMapper.SelectStorageTaskById(id);// 获取任务信息 homeMapper.FinishTask(storageTask.getId());// 完成任务 if (storageTask.getTaskType().equals("0")) { UpdateStroageCageByCell(Integer.parseInt(storageTask.getLoadrack()), 1);// 玻璃数量+1 } else { StorageCage storageCage = homeMapper.SelectStorageByCell(storageTask.getShelfRack()); if (storageCage.getNumber() > 1) { UpdateStroageCageByCell(Integer.parseInt(storageTask.getShelfRack()), -1);// 玻璃数量-1 } else { UpdateStroageCageByCell(Integer.parseInt(storageTask.getShelfRack()), 0);// 清除格内信息 } homeMapper.AddGlassNo(storageTask.getFlowcard(), storageTask.getMateid(), storageTask.geTier());// 添加出片完成数量 } Map map = new HashMap<>(); map.put("message", "200"); return Result.success(map); } // 查询玻璃信息 public Result SelectGlassInfo(String width, String height, String thickness, String films) { List glassInfoList = homeMapper.SelectGlassInfo(width, height, thickness, films); Map map = new HashMap<>(); map.put("StorageCageAddInfo", glassInfoList); return Result.success(map); } // 添加玻璃到格子内 public Result StorageCageAddGlass(String cell, GlassInfo glassInfo) { homeMapper.StorageCageAddGlass(cell, glassInfo); UpdateStroageCageByCell(Integer.parseInt(cell), 1); Map map = new HashMap<>(); map.put("message", "200"); return Result.success(map); } // 查询订单任务 public List SelectOrderTask() { List OrderTask = homeMapper.SelectOrderTask(); // for (FlowCard flowcard : OrderTask) { // flowcard.setglassinfo(homeMapper.SelectOrderView(flowcard.getFlowcard())); // } return OrderTask; } // 开始任务 public Result ClaimTasks(String flowcard, Integer state) { if (state == 1) { homeMapper.ClaimTasks(flowcard, 0); } else { homeMapper.ClaimTasks(flowcard, 1); } Map map = new HashMap<>(); map.put("message", "200"); return Result.success(map); } // 任务模式修改 public Result ModeChange(String flowcard, Integer method) { if (method == 1) { homeMapper.ModeChange(flowcard, 0); } else { homeMapper.ModeChange(flowcard, 1); } Map map = new HashMap<>(); map.put("message", "200"); return Result.success(map); } // 修改测量信息 public Result UpdateQueue(GlassInfo glassInfo) { homeMapper.UpdateQueue(glassInfo.getFlowcard(), glassInfo.getWidth(), glassInfo.getHeight(), glassInfo.getGlasstype()); Map map = new HashMap<>(); map.put("message", "200"); return Result.success(map); } public List SelectStorageCageByCage(int cage) { return homeMapper.SelectStorageCageByCage(cage); } // 人工拿走 public Result ManualTake(String glassInfo) { // 读取DB105区文件 PlcParameterObject plcmes = PLCAutoMes.PlcMesObject; // 移除 S7control.getinstance().WriteWord(plcmes.getPlcParameter("MESToGaStatus").getAddress(),(short) 3); //删除数据 QueueMapper.DeleteErrorQueue(); // plcmes.getPlcParameter("GaToMES").setValue("3"); Map map = new HashMap<>(); map.put("message", "200"); return Result.success(map); } // 重新测量 public Result AnewMeasure(String glassInfo) { // 读取DB105区文件 PlcParameterObject plcmes = PLCAutoMes.PlcMesObject; // 重新测量 S7control.getinstance().WriteWord(plcmes.getPlcParameter("MESToGaStatus").getAddress(),(short) 2); //删除数据 QueueMapper.DeleteErrorQueue(); // plcmes.getPlcParameter("GaToMES").setValue("2"); Map map = new HashMap<>(); map.put("message", "200"); return Result.success(map); } // 添加小片信息 public Result AddGlassinfo(List IportDataStr) { // 筛选处理数据 1.去除无用数据 2.计算配片ID 3.计算ID 4. 计算总层数 List TypeDatas = homeMapper.SelectType(); Map TypeData = new HashMap();// 得到处理完后所有的类型 Map groupby=new HashMap(); // 得到处理完后所有总层数; List RemoveErrorData = IportDataStr.stream() .filter(item -> (item.get("order") != null && item.get("glasstype") != null && item.get("tier") != null && item.get("films") != null && item.get("base") != null && item.get("height") != null && item.get("thickness") != null && item.get("quantity") != null&& item.get("matching") != null)) .collect(Collectors.toList()); TypeDatas.forEach(item -> { TypeData.put(item.get("type").toString(), item.get("glasstype").toString()); }); RemoveErrorData.forEach(item -> { String key=item.get("order").toString()+"_"+item.get("matching").toString(); if (groupby.get(key)!=null) { Integer Tiers=groupby.get(key)+1; groupby.put(key, Tiers); }else{ groupby.put(key, 1); } }); System.out.println(RemoveErrorData.size()); // 处理完成 添加数据库 for (Map map : RemoveErrorData) { String key = map.get("base").toString() + "_" + map.get("height").toString() + "_" + map.get("thickness").toString() + "_" + map.get("films").toString(); String tierkey = map.get("order").toString() + "_" + map.get("matching").toString(); String value = TypeData.get(key); Integer glasstype; if (TypeData.get(key) == null) { glasstype = homeMapper.SelectMaxType(); } else { glasstype = Integer.valueOf(value); } homeMapper.AddGlassinfo(map.get("order").toString(), glasstype, Integer.parseInt(map.get("glasstype").toString()) // 需要计算 配片ID , glasstype+"" // 需要计算 玻璃ID , Integer.parseInt(map.get("tier").toString()), map.get("films").toString(), Double.parseDouble(map.get("base").toString()), Double.parseDouble(map.get("height").toString()), Double.parseDouble(map.get("thickness").toString()), Integer.parseInt(map.get("quantity").toString()),groupby.get(tierkey));// 需要计算总层数 } Map ResultCode = new HashMap<>(); ResultCode.put("message", "200"); return Result.success(ResultCode); } }