Browse Source

feat:添加矿石、工艺表与相关实代码实现

tags/freeze
白凤吉 3 months ago
parent
commit
71d4ae131d
  1. 106
      src/main/java/com/iflytop/gd/app/controller/CraftsController.java
  2. 74
      src/main/java/com/iflytop/gd/app/controller/OresController.java
  3. 2
      src/main/java/com/iflytop/gd/app/core/CraftsContext.java
  4. 8
      src/main/java/com/iflytop/gd/app/mapper/CraftsMapper.java
  5. 4
      src/main/java/com/iflytop/gd/app/mapper/OresMapper.java
  6. 11
      src/main/java/com/iflytop/gd/app/model/dto/PauseCraftsDto.java
  7. 11
      src/main/java/com/iflytop/gd/app/model/dto/ResumeCraftsDto.java
  8. 13
      src/main/java/com/iflytop/gd/app/model/dto/StartCraftsDTO.java
  9. 11
      src/main/java/com/iflytop/gd/app/model/dto/StopCraftsDto.java
  10. 2
      src/main/java/com/iflytop/gd/app/model/entity/Crafts.java
  11. 2
      src/main/java/com/iflytop/gd/app/model/entity/Ores.java
  12. 4
      src/main/java/com/iflytop/gd/app/model/vo/OresCraftsListVO.java
  13. 17
      src/main/java/com/iflytop/gd/app/service/CraftsService.java
  14. 145
      src/main/java/com/iflytop/gd/app/service/CraftsStepService.java
  15. 20
      src/main/java/com/iflytop/gd/app/service/OresService.java
  16. 2
      src/main/java/com/iflytop/gd/system/common/result/ResultCode.java
  17. 23
      src/main/resources/sql/init.sql

106
src/main/java/com/iflytop/gd/app/controller/CraftsController.java

@ -0,0 +1,106 @@
package com.iflytop.gd.app.controller;
import com.iflytop.gd.app.model.dto.PauseCraftsDto;
import com.iflytop.gd.app.model.dto.ResumeCraftsDto;
import com.iflytop.gd.app.model.dto.StartCraftsDTO;
import com.iflytop.gd.app.model.dto.StopCraftsDto;
import com.iflytop.gd.app.service.CraftsStepService;
import com.iflytop.gd.system.common.result.Result;
import com.iflytop.gd.system.common.result.ResultCode;
import com.iflytop.gd.app.model.entity.Crafts;
import com.iflytop.gd.app.service.CraftsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Tag(name = "工艺管理")
@RestController
@RequestMapping("/api/crafts")
@RequiredArgsConstructor
@Slf4j
public class CraftsController {
private final CraftsService craftsService;
private final CraftsStepService craftsStepService;
@Operation(summary = "根据矿石id获取工艺列表")
@GetMapping("/list/{oresId}")
public Result<List<Crafts>> getAllCrafts(@Parameter(description = "矿石ID") @PathVariable Long oresId) {
List<Crafts> craftList = craftsService.selectAllByOresId(oresId);
return Result.success(craftList);
}
@Operation(summary = "添加新工艺")
@PostMapping("/")
public Result<String> addCrafts(@RequestBody Crafts crafts) {
Crafts existingCrafts = craftsService.findByName(crafts.getName());
if (existingCrafts == null) {
boolean isSuccess = craftsService.addCrafts(crafts);
if (isSuccess) {
return Result.success();
}
} else {
return Result.failed(ResultCode.DATA_ALREADY_EXISTS);
}
return Result.failed();
}
@Operation(summary = "更新工艺")
@PutMapping("/{id}")
public Result<String> updateCrafts(@PathVariable Long id, @RequestBody Crafts crafts) {
crafts.setId(id);
boolean isSuccess = craftsService.updateCrafts(crafts);
if (isSuccess) {
return Result.success();
}
return Result.failed();
}
@Operation(summary = "删除工艺")
@DeleteMapping("/{ids}")
public Result<String> deleteCrafts(@Parameter(description = "工艺ID,多个以英文逗号(,)分割") @PathVariable String ids) {
boolean isSuccess = craftsService.deleteCrafts(ids);
if (isSuccess) {
return Result.success();
}
return Result.failed();
}
@Operation(summary = "开始执行工艺")
@PostMapping("/start")
public Result<String> startCrafts(@RequestBody StartCraftsDTO startCraftsDTO) {
boolean isSuccess = craftsStepService.startCrafts(startCraftsDTO.getCraftId(), startCraftsDTO.getHeatId());
if (isSuccess) {
return Result.success();
}
return Result.failed();
}
@Operation(summary = "暂停执行工艺")
@PostMapping("/pause")
public Result<String> pauseCrafts(@RequestBody PauseCraftsDto pauseCraftsDto) {
craftsStepService.pauseCrafts(pauseCraftsDto.getHeatId());
return Result.success();
}
@Operation(summary = "恢复执行工艺")
@PostMapping("/resume")
public Result<String> resumeCrafts(@RequestBody ResumeCraftsDto resumeCraftsDto) {
craftsStepService.resumeCrafts(resumeCraftsDto.getHeatId());
return Result.success();
}
@Operation(summary = "停止执行工艺")
@PostMapping("/stop")
public Result<String> stopCrafts(@RequestBody StopCraftsDto stopCraftsDto) {
boolean isSuccess = craftsStepService.stopCrafts(stopCraftsDto.getHeatId());
if (isSuccess) {
return Result.success();
}
return Result.failed();
}
}

74
src/main/java/com/iflytop/gd/app/controller/OresController.java

@ -0,0 +1,74 @@
package com.iflytop.gd.app.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.iflytop.gd.app.model.entity.Ores;
import com.iflytop.gd.app.model.vo.OresCraftsListVO;
import com.iflytop.gd.system.common.base.BasePageQuery;
import com.iflytop.gd.system.common.result.PageResult;
import com.iflytop.gd.app.service.OresService;
import com.iflytop.gd.system.common.result.Result;
import com.iflytop.gd.system.common.result.ResultCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
@Tag(name = "矿石管理")
@RestController
@RequestMapping("/api/ores")
@RequiredArgsConstructor
@Slf4j
public class OresController {
private final OresService oresService;
@Operation(summary = "矿石工艺列表")
@GetMapping("/list")
public PageResult<OresCraftsListVO> getAllOres(BasePageQuery pageQuery) {
IPage<OresCraftsListVO> result = oresService.getPage(pageQuery);
return PageResult.success(result);
}
@Operation(summary = "添加新矿石")
@PostMapping("/")
public Result<String> addOres(@RequestBody Ores ores) {
Ores existingOres = oresService.findByName(ores.getName());
if (existingOres == null) {
boolean isSuccess = oresService.addOres(ores);
if (isSuccess) {
return Result.success();
}
} else {
return Result.failed(ResultCode.DATA_ALREADY_EXISTS);
}
return Result.failed();
}
@Operation(summary = "更新矿石")
@PutMapping("/{id}")
public Result<String> updateOres(@PathVariable Long id, @RequestBody Ores ores) {
Ores existingOres = oresService.findByName(ores.getName());
if (existingOres == null) {
ores.setId(id);
boolean isSuccess = oresService.updateOres(ores);
if (isSuccess) {
return Result.success();
}
} else {
return Result.failed(ResultCode.DATA_ALREADY_EXISTS);
}
return Result.failed();
}
@Operation(summary = "删除矿石")
@DeleteMapping("/{ids}")
public Result<String> deleteOres(@Parameter(description = "矿石ID,多个以英文逗号(,)分割") @PathVariable String ids) {
boolean isSuccess = oresService.deleteOres(ids);
if (isSuccess) {
return Result.success();
}
return Result.failed();
}
}

2
src/main/java/com/iflytop/gd/app/core/CraftsContext.java

@ -6,7 +6,7 @@ import com.iflytop.gd.app.common.enums.CraftEvents;
import com.iflytop.gd.app.common.enums.CraftStates;
import com.iflytop.gd.app.model.bo.CraftsStep;
import com.iflytop.gd.app.service.CraftsStepService;
import com.iflytop.gd.system.model.entity.Crafts;
import com.iflytop.gd.app.model.entity.Crafts;
import com.iflytop.gd.system.service.WebSocketService;
import lombok.Getter;
import org.springframework.messaging.Message;

8
src/main/java/com/iflytop/gd/system/mapper/CraftsMapper.java → src/main/java/com/iflytop/gd/app/mapper/CraftsMapper.java

@ -1,8 +1,9 @@
package com.iflytop.gd.system.mapper;
package com.iflytop.gd.app.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.iflytop.gd.system.model.entity.Crafts;
import com.iflytop.gd.app.model.entity.Crafts;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@ -12,8 +13,7 @@ import java.util.List;
@Mapper
public interface CraftsMapper extends BaseMapper<Crafts> {
@Select("SELECT * FROM crafts WHERE ores_id = #{oresId}")
List<Crafts> selectAllByOresId(Long oresId);
Crafts findByName(String name);
}

4
src/main/java/com/iflytop/gd/system/mapper/OresMapper.java → src/main/java/com/iflytop/gd/app/mapper/OresMapper.java

@ -1,7 +1,7 @@
package com.iflytop.gd.system.mapper;
package com.iflytop.gd.app.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.iflytop.gd.system.model.entity.Ores;
import com.iflytop.gd.app.model.entity.Ores;
import org.apache.ibatis.annotations.Mapper;
/**

11
src/main/java/com/iflytop/gd/app/model/dto/PauseCraftsDto.java

@ -0,0 +1,11 @@
package com.iflytop.gd.app.model.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Schema(description = "暂停执行工艺")
@Data
public class PauseCraftsDto {
@Schema(description = "加热区id")
private String heatId;
}

11
src/main/java/com/iflytop/gd/app/model/dto/ResumeCraftsDto.java

@ -0,0 +1,11 @@
package com.iflytop.gd.app.model.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Schema(description = "恢复执行工艺")
@Data
public class ResumeCraftsDto {
@Schema(description = "加热区id")
private String heatId;
}

13
src/main/java/com/iflytop/gd/app/model/dto/StartCraftsDTO.java

@ -0,0 +1,13 @@
package com.iflytop.gd.app.model.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Schema(description = "开始工艺")
@Data
public class StartCraftsDTO {
@Schema(description = "工艺id")
private Long craftId;
@Schema(description = "加热区id")
private String heatId;
}

11
src/main/java/com/iflytop/gd/app/model/dto/StopCraftsDto.java

@ -0,0 +1,11 @@
package com.iflytop.gd.app.model.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Schema(description = "恢复执行工艺")
@Data
public class StopCraftsDto {
@Schema(description = "加热区id")
private String heatId;
}

2
src/main/java/com/iflytop/gd/system/model/entity/Crafts.java → src/main/java/com/iflytop/gd/app/model/entity/Crafts.java

@ -1,4 +1,4 @@
package com.iflytop.gd.system.model.entity;
package com.iflytop.gd.app.model.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import com.iflytop.gd.system.common.base.BaseEntity;

2
src/main/java/com/iflytop/gd/system/model/entity/Ores.java → src/main/java/com/iflytop/gd/app/model/entity/Ores.java

@ -1,4 +1,4 @@
package com.iflytop.gd.system.model.entity;
package com.iflytop.gd.app.model.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import com.iflytop.gd.system.common.base.BaseEntity;

4
src/main/java/com/iflytop/gd/system/model/vo/OresCraftsListVO.java → src/main/java/com/iflytop/gd/app/model/vo/OresCraftsListVO.java

@ -1,7 +1,7 @@
package com.iflytop.gd.system.model.vo;
package com.iflytop.gd.app.model.vo;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.iflytop.gd.system.model.entity.Crafts;
import com.iflytop.gd.app.model.entity.Crafts;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

17
src/main/java/com/iflytop/gd/system/service/CraftsService.java → src/main/java/com/iflytop/gd/app/service/CraftsService.java

@ -1,8 +1,9 @@
package com.iflytop.gd.system.service;
package com.iflytop.gd.app.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iflytop.gd.system.mapper.CraftsMapper;
import com.iflytop.gd.system.model.entity.Crafts;
import com.iflytop.gd.app.mapper.CraftsMapper;
import com.iflytop.gd.app.model.entity.Crafts;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@ -22,15 +23,15 @@ public class CraftsService extends ServiceImpl<CraftsMapper, Crafts>{
}
public Crafts findByName(String name) {
return this.baseMapper.findByName(name);
return this.getOne(new LambdaQueryWrapper<>(new Crafts()).eq(Crafts::getName, name));
}
public boolean addCrafts(Crafts crafts) {
return this.baseMapper.insert(crafts) > 0;
return this.save(crafts);
}
public boolean updateCrafts(Crafts crafts) {
return this.baseMapper.updateById(crafts) > 0;
return this.updateById(crafts);
}
public boolean deleteCrafts(String idsStr) {
@ -40,8 +41,4 @@ public class CraftsService extends ServiceImpl<CraftsMapper, Crafts>{
return this.removeByIds(ids);
}
public Crafts findCraftsById(Long id) {
return this.baseMapper.selectById(id);
}
}

145
src/main/java/com/iflytop/gd/app/service/CraftsStepService.java

@ -1,11 +1,20 @@
package com.iflytop.gd.app.service;
import com.iflytop.gd.app.model.bo.TubeSol;
import com.iflytop.gd.app.common.enums.CraftEvents;
import com.iflytop.gd.app.common.enums.CraftStates;
import com.iflytop.gd.app.core.CraftsContext;
import com.iflytop.gd.app.model.entity.Crafts;
import com.iflytop.gd.system.service.WebSocketService;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.statemachine.config.StateMachineFactory;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
* 设备步骤操作
@ -14,111 +23,79 @@ import java.util.List;
@Service
@RequiredArgsConstructor
public class CraftsStepService {
private final CraftsService craftsService;
private final StateMachineFactory<CraftStates, CraftEvents> stateMachineFactory;
private final WebSocketService webSocketService;
/**
* 抬起托盘
*
* @param heatId 加热区id
*/
public boolean upTray(String heatId) {
return true;
}
/**
* 降下托盘
*
* @param heatId 加热区id
*/
public boolean downTray(String heatId) {
return true;
}
private ExecutorService executor;
/**
* 添加溶液
*
* @param tubeSolList 需要添加溶液的试管与溶液
*/
public boolean addLiquid(List<TubeSol> tubeSolList) {
return true;
}
private final ConcurrentHashMap<String, CraftsContext> contextMap = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Future<?>> futureMap = new ConcurrentHashMap<>();
/**
* 将指定加热区的托盘移至加液区
*
* @param heatId 加热区id
*/
public boolean moveToSol(String heatId) {
return true;
@PostConstruct
public void init() {
this.executor = Executors.newCachedThreadPool();
}
/**
* 移至加热
*
* @param heatId 加热区id
* 开始执行工艺
*/
public boolean moveToHeat(String heatId) {
return true;
}
/**
* 摇匀
*
* @param second 摇匀时间
*/
public boolean shaking(int second) {
return true;
}
/**
* 开始加热
*
* @param heatId 加热区id
* @param temperature 目标温度
*/
public boolean startHeating(String heatId, double temperature) {
public synchronized boolean startCrafts(Long craftId, String heatId) {
if (futureMap.containsKey(heatId)) {// 已有任务在执行不重复启动
return false;
}
Crafts craft = craftsService.getById(craftId);
if (craft == null) {
return false;
}
CraftsContext ctx = new CraftsContext(
heatId,
craft,
stateMachineFactory,
this,
webSocketService
);
Future<?> future = executor.submit(ctx);
contextMap.put(heatId, ctx);
futureMap.put(heatId, future);
return true;
}
/**
* 停止加热
*
* @param heatId 加热区id
* 暂停执行工艺
*/
public boolean stopHeating(String heatId) {
return true;
public synchronized void pauseCrafts(String heatId) {
CraftsContext ctx = contextMap.get(heatId);
if (ctx != null) {
ctx.pause();
}
}
/**
* 停止加热
* 恢复执行工艺
*/
public boolean takePhoto() {
return true;
}
//移至异常
public boolean moveToExc() {
return true;
}
//移除异常
public boolean moveOutToExc() {
return true;
public synchronized void resumeCrafts(String heatId) {
CraftsContext ctx = contextMap.get(heatId);
if (ctx != null) {
ctx.resume();
}
}
/**
* 等待
*
* @param second
* 停止执行工艺
*/
public boolean delay(int second) {
try {
Thread.sleep(second * 1000L);
public synchronized boolean stopCrafts(String heatId) {
CraftsContext ctx = contextMap.get(heatId);
Future<?> future = futureMap.get(heatId);
if (ctx != null && future != null) {
ctx.stop();
future.cancel(true);
contextMap.remove(heatId);
futureMap.remove(heatId);
return true;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return false;
}
// TODO: 如需提供查询当前执行状态可在此类添加 getStatus(heatId) 方法返回 ctx.getCurrentIndex(), ctx.getSm().getState().getId(), ctx.getRemainingSteps()
}

20
src/main/java/com/iflytop/gd/system/service/OresService.java → src/main/java/com/iflytop/gd/app/service/OresService.java

@ -1,15 +1,17 @@
package com.iflytop.gd.system.service;
package com.iflytop.gd.app.service;
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 com.iflytop.gd.app.model.vo.OresCraftsListVO;
import com.iflytop.gd.system.common.base.BasePageQuery;
import com.iflytop.gd.system.mapper.CraftsMapper;
import com.iflytop.gd.system.mapper.OresMapper;
import com.iflytop.gd.system.model.entity.Crafts;
import com.iflytop.gd.system.model.entity.Ores;
import com.iflytop.gd.system.model.vo.OresCraftsListVO;
import com.iflytop.gd.app.mapper.CraftsMapper;
import com.iflytop.gd.app.mapper.OresMapper;
import com.iflytop.gd.app.model.entity.Crafts;
import com.iflytop.gd.app.model.entity.Ores;
import com.iflytop.gd.system.model.entity.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@ -75,17 +77,17 @@ public class OresService extends ServiceImpl<OresMapper, Ores> {
public Ores findByName(String name) {
return this.baseMapper.findByName(name);
return this.getOne(new LambdaQueryWrapper<Ores>().eq(Ores::getName, name));
}
public boolean addOres(Ores ores) {
return this.baseMapper.insert(ores) > 0;
return this.save(ores);
}
public boolean updateOres(Ores ores) {
return this.baseMapper.updateById(ores) > 0;
return this.updateById(ores);
}

2
src/main/java/com/iflytop/gd/system/common/result/ResultCode.java

@ -35,7 +35,7 @@ public enum ResultCode implements IResultCode, Serializable {
USER_ALREADY_EXISTS("4001", "用户已存在"),
INVALID_CREDENTIALS("4002", "用户名或密码错误"),
OPERATION_NOT_ALLOWED("4003", "业务操作不允许"),
DATA_ALREADY_EXISTS("4004", "数据已存在"),
//============================ 5xxx系统 & 第三方 ============================
SYSTEM_ERROR("5000", "系统内部错误"),
SERVICE_UNAVAILABLE("5001", "服务暂不可用"),

23
src/main/resources/sql/init.sql

@ -13,4 +13,25 @@ CREATE TABLE IF NOT EXISTS user (
INSERT INTO user (username,nickname, password, role, fixed_user, deleted)
SELECT 'admin','Admin', '123456', 'ADMIN', 'ENABLE','DISABLE'
WHERE NOT EXISTS (SELECT 1 FROM user WHERE username = 'admin');
WHERE NOT EXISTS (SELECT 1 FROM user WHERE username = 'admin');
-- 创建 ores 矿石 表
CREATE TABLE IF NOT EXISTS ores
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR NOT NULL,
create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 创建 crafts 工艺 表
CREATE TABLE IF NOT EXISTS crafts
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR NOT NULL,
steps TEXT,
ores_id INTEGER,
create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Loading…
Cancel
Save