Browse Source

用户登录改为账号登录

master
sige 2 years ago
parent
commit
4651bd6f9b
  1. 32
      src/main/java/com/dreamworks/boditech/controller/AccountController.java
  2. 1
      src/main/java/com/dreamworks/boditech/controller/DeviceController.java
  3. 5
      src/main/java/com/dreamworks/boditech/driver/Device.java
  4. 3
      src/main/java/com/dreamworks/boditech/driver/consumable/CsmBufferTubeBox.java
  5. 70
      src/main/java/com/dreamworks/boditech/driver/consumable/CsmBufferTubeManager.java
  6. 9
      src/main/java/com/dreamworks/boditech/driver/task/Executor.java
  7. 35
      src/main/java/com/dreamworks/boditech/driver/task/TaskLoad.java
  8. 5
      src/main/java/com/dreamworks/boditech/entity/Account.java
  9. 33
      src/main/java/com/dreamworks/boditech/mapper/AccountMapper.java
  10. 25
      src/main/java/com/dreamworks/boditech/mapper/UserMapper.java
  11. 138
      src/main/java/com/dreamworks/boditech/service/AccountService.java
  12. 11
      src/main/java/com/dreamworks/boditech/service/DeviceService.java
  13. 95
      src/main/java/com/dreamworks/boditech/service/UserService.java

32
src/main/java/com/dreamworks/boditech/controller/UserController.java → src/main/java/com/dreamworks/boditech/controller/AccountController.java

@ -1,29 +1,39 @@
package com.dreamworks.boditech.controller;
import com.dreamworks.boditech.controller.entity.ApiResponse;
import com.dreamworks.boditech.entity.ParamUserLogin;
import com.dreamworks.boditech.entity.User;
import com.dreamworks.boditech.service.UserService;
import com.dreamworks.boditech.entity.Account;
import com.dreamworks.boditech.service.AccountService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;
@Controller
public class UserController extends BaseController {
public class AccountController extends BaseController {
@Resource
UserService userService;
AccountService accountService;
@ResponseBody
@PostMapping("/api/account/login-list")
public ApiResponse loginList(@RequestBody Map<String,Object> params) {
Integer limit = (Integer)params.get("limit");
return this.success(this.accountService.listLoginAccounts(limit));
}
@ResponseBody
@PostMapping("/api/user/login")
@PostMapping("/api/account/login")
public ApiResponse login(@RequestBody ParamUserLogin param ) {
User user = this.userService.login(param);
Account user = this.accountService.login(param);
return this.success(user);
}
@ResponseBody
@PostMapping("/api/user/logout")
@PostMapping("/api/account/logout")
public ApiResponse logout() {
this.userService.logout();
this.accountService.logout();
return this.success();
}
@ -67,7 +77,7 @@ public class UserController extends BaseController {
@PostMapping("/api/user/create")
public ApiResponse create( String name, String pin ) {
try {
User user = this.userService.create(name, pin);
Account user = this.accountService.create(name, pin);
return this.success(user);
} catch ( RuntimeException e ) {
return this.error(e.getMessage());
@ -78,7 +88,7 @@ public class UserController extends BaseController {
@PostMapping("/api/user/pin-update")
public ApiResponse pinUpdate( String pin ) {
try {
this.userService.pinUpdate(pin);
this.accountService.pinUpdate(pin);
return this.success();
} catch ( RuntimeException e ) {
return this.error(e.getMessage());
@ -89,7 +99,7 @@ public class UserController extends BaseController {
@PostMapping("/api/user/delete")
public ApiResponse delete( int id ) {
try {
this.userService.delete(id);
this.accountService.delete(id);
return this.success();
} catch ( RuntimeException e ) {
return this.error(e.getMessage());

1
src/main/java/com/dreamworks/boditech/controller/DeviceController.java

@ -301,6 +301,7 @@ public class DeviceController extends BaseController {
tube.put("index", Integer.toString(i));
tube.put("status", "WAITING");
tube.put("sampleUID", "016");
tube.put("sampleType","FB");
tube.put("projects",tasks);
tubes.add(tube);
}

5
src/main/java/com/dreamworks/boditech/driver/Device.java

@ -29,6 +29,9 @@ public class Device {
private String connectionType;
@Value("${app.device.debug}")
private Boolean debug;
@Value("${app.device.enable}")
public Boolean enable;
@Resource
private ComSerialPort serialPort;
@Resource
@ -86,6 +89,8 @@ public class Device {
this.appendActuator(new ActMotor(ActuatorModule.TEST_CARD_BOX_MOTOR, this));
this.appendActuator(new ActCodeScanner(ActuatorModule.ARM_Z_SCANNER, this));
this.appendActuator(new ActModuleTestCardBoxCase(ActuatorModule.TEST_CARD_BOX_CASE, this));
this.bufferTubes.setup();
}
// append actuator

3
src/main/java/com/dreamworks/boditech/driver/consumable/CsmBufferTubeBox.java

@ -1,7 +1,10 @@
package com.dreamworks.boditech.driver.consumable;
public class CsmBufferTubeBox {
public static final String STATUS_NOT_LOADED = "NOT_LOADED";
public Integer index;
public Integer projectId;
public String projectName;
public Integer tubeAmount;
public String status;
}

70
src/main/java/com/dreamworks/boditech/driver/consumable/CsmBufferTubeManager.java

@ -13,17 +13,29 @@ public class CsmBufferTubeManager {
// constructor
public CsmBufferTubeManager(Device device) {
this.device = device;
}
// @TODO : 移除demo数据
// setup manager
public void setup() {
this.bufferTubeBoxes.clear();
for ( int i=0; i<6; i++ ) {
CsmBufferTubeBox box = new CsmBufferTubeBox();
box.tubeAmount = 25;
box.projectName = "demo" + i;
box.index = i;
box.status = CsmBufferTubeBox.STATUS_NOT_LOADED;
this.bufferTubeBoxes.add(box);
}
// @TODO : 移除demo数据
if ( !this.device.enable ) {
for ( int i=0; i<6; i++ ) {
CsmBufferTubeBox box = this.bufferTubeBoxes.get(i);
box.tubeAmount = 25;
box.projectName = "demo" + i;
}
}
}
// get all buffer tube boxes
public List<CsmBufferTubeBox> getAll() {
return this.bufferTubeBoxes;
}
@ -34,6 +46,36 @@ public class CsmBufferTubeManager {
box.tubeAmount = param.tubeAmount;
}
/**
* append buffer tube by box code string
* code example : 1||CAGGB66U||2024.03.26||1279||06
* code format : projectCodeNum||lotCode||expireDate||unknown||unknown
* @param index
* @param code
*/
public void appendByCode( Integer index, String code ) {
// @TODO : 由于缺少 code 格式所以都做 "hsCRP" 项目处理
String projectName = "hsCRP";
Project project = this.device.projectService.findByName(projectName);
if ( null == project ) {
throw new RuntimeException("project not found");
}
// remove all buffer tube A with matched zoneIndex
this.bufferTubeList.removeIf(bufferTubeA -> bufferTubeA.areaIndex.equals(index));
for ( int i=0; i<25; i++ ) {
// append new buffer tube A
CsmBufferTube bufferTube = new CsmBufferTube(this);
bufferTube.areaIndex = index;
bufferTube.position = i;
bufferTube.projectName = project.name;
// @TODO : 这里没法判断是那种盖子所以都做 500μl 处理
bufferTube.coverType = CsmBufferTube.COVER_TYPE_150;
this.bufferTubeList.add(bufferTube);
}
}
@ -64,29 +106,7 @@ public class CsmBufferTubeManager {
// return this.bufferTubeList;
// }
// append buffer tube A
public void appendByCode( Integer areaIndex, String code ) {
// @TODO : 由于缺少 code 格式所以都做 "hsCRP" 项目处理
String projectName = "hsCRP";
Project project = this.device.projectService.findByName(projectName);
if ( null == project ) {
throw new RuntimeException("project not found");
}
// remove all buffer tube A with matched zoneIndex
this.bufferTubeList.removeIf(bufferTubeA -> bufferTubeA.areaIndex.equals(areaIndex));
for ( int i=0; i<25; i++ ) {
// append new buffer tube A
CsmBufferTube bufferTube = new CsmBufferTube(this);
bufferTube.areaIndex = areaIndex;
bufferTube.position = i;
bufferTube.projectName = project.name;
// @TODO : 这里没法判断是那种盖子所以都做 500μl 处理
bufferTube.coverType = CsmBufferTube.COVER_TYPE_150;
this.bufferTubeList.add(bufferTube);
}
}
}

9
src/main/java/com/dreamworks/boditech/driver/task/Executor.java

@ -15,6 +15,7 @@ public class Executor implements Runnable {
private final Device device;
private final List<Task> tasks;
// executor status
private Integer status = Executor.STATUS_STOPPED;
/**
@ -33,6 +34,14 @@ public class Executor implements Runnable {
this.device = device;
}
/**
* get executor status
* @return executor status
*/
public Integer getStatus() {
return this.status;
}
// stop executor
public void stop() {
synchronized (this.tasks) {

35
src/main/java/com/dreamworks/boditech/driver/task/TaskLoad.java

@ -12,6 +12,7 @@ public class TaskLoad extends TaskBase {
try {
this.testCardLoad(executor);
this.bufferTubeLoad(executor);
} catch (RuntimeException e) {
armXY.reset();
throw e;
@ -44,4 +45,38 @@ public class TaskLoad extends TaskBase {
armXY.reset();
}
// load buffer tube
private void bufferTubeLoad(Executor executor) {
Device device = executor.getDevice();
ActArmXY armXY = (ActArmXY)device.getActuator(ActuatorModule.ARM_XY);
ActCodeScanner codeScanner = (ActCodeScanner)device.getActuator(ActuatorModule.ARM_Z_SCANNER);
Integer scanStartX = device.getLocationByName("bufferTubeLineOneScanStart.x");
Integer scanStartY = device.getLocationByName("bufferTubeLineOneScanStart.y");
// buffer tube line one
for ( int i=0; i<3; i++ ) {
armXY.moveTo(scanStartX - i * 1200, scanStartY);
String code = codeScanner.scan(500);
if ( "".equals(code) ) {
continue ;
}
device.bufferTubes.appendByCode(i, code);
}
// buffer tube line two
scanStartX = device.getLocationByName("bufferTubeLineTwoScanStart.x");
scanStartY = device.getLocationByName("bufferTubeLineTwoScanStart.y");
for ( int i=0; i<3; i++ ) {
armXY.moveTo(scanStartX - i * 1200, scanStartY);
String code = codeScanner.scan(500);
if ( "".equals(code) ) {
continue ;
}
device.bufferTubes.appendByCode(i+3, code);
}
armXY.reset();
}
}

5
src/main/java/com/dreamworks/boditech/entity/User.java → src/main/java/com/dreamworks/boditech/entity/Account.java

@ -1,9 +1,12 @@
package com.dreamworks.boditech.entity;
public class User {
public class Account {
public int id;
public String account;
public String pin;
public int isAdmin;
public long createdAt;
public int createdBy;
// 上次登录时间
public Integer lastLoginTime;
}

33
src/main/java/com/dreamworks/boditech/mapper/AccountMapper.java

@ -0,0 +1,33 @@
package com.dreamworks.boditech.mapper;
import com.dreamworks.boditech.entity.Account;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Insert;
import java.util.List;
@Mapper
public interface AccountMapper {
@Select("SELECT account,isAdmin FROM bdt_accounts ORDER BY isAdmin DESC, lastLoginTime DESC LIMIT #{limit}")
List<Account> listLoginAccounts(Integer limit);
@Update("UPDATE bdt_accounts SET lastLoginTime=#{lastLoginTime} WHERE id=#{id}")
void updateLoginTime(Account account);
@Select("SELECT * FROM bdt_accounts WHERE account=#{account} LIMIT 1")
Account findByAccount(String account);
@Select("SELECT * FROM bdt_accounts WHERE id=#{id}")
Account findById(int id);
@Update("UPDATE bdt_accounts SET account=#{account}, pin=#{pin}, isAdmin=#{isAdmin} WHERE id=#{id}")
int update(Account user);
@Delete("DELETE FROM bdt_accounts WHERE id=#{id}")
int delete(Account user);
@Insert("INSERT INTO bdt_accounts (account, pin, isAdmin, createdAt, createdBy) VALUES (#{account}, #{pin}, #{isAdmin}, #{createdAt}, #{createdBy})")
int insert(Account user);
}

25
src/main/java/com/dreamworks/boditech/mapper/UserMapper.java

@ -1,25 +0,0 @@
package com.dreamworks.boditech.mapper;
import com.dreamworks.boditech.entity.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Insert;
@Mapper
public interface UserMapper {
@Select("SELECT * FROM bdt_users WHERE account=#{account} LIMIT 1")
User findByAccount(String account);
@Select("SELECT * FROM bdt_users WHERE id=#{id}")
User findById(int id);
@Update("UPDATE bdt_users SET account=#{account}, pin=#{pin}, isAdmin=#{isAdmin} WHERE id=#{id}")
int update(User user);
@Delete("DELETE FROM bdt_users WHERE id=#{id}")
int delete(User user);
@Insert("INSERT INTO bdt_users (account, pin, isAdmin, createdAt, createdBy) VALUES (#{account}, #{pin}, #{isAdmin}, #{createdAt}, #{createdBy})")
int insert(User user);
}

138
src/main/java/com/dreamworks/boditech/service/AccountService.java

@ -0,0 +1,138 @@
package com.dreamworks.boditech.service;
import com.dreamworks.boditech.entity.ParamUserLogin;
import com.dreamworks.boditech.entity.Account;
import com.dreamworks.boditech.mapper.AccountMapper;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import java.util.List;
@Service
public class AccountService {
private static final Logger LOG = LoggerFactory.getLogger(AccountService.class);
@Resource
private AccountMapper userMapper;
@Resource
private ActionLogService actionLog;
@Resource
private DeviceService deviceService;
// current user
private Account curAccount = null;
// list-login-accounts
public List<Account> listLoginAccounts(Integer limit) {
return this.userMapper.listLoginAccounts(limit);
}
// login
public Account login(ParamUserLogin param) {
Account account = userMapper.findByAccount(param.account);
String hashPin = DigestUtils.md5DigestAsHex(param.pin.getBytes());
if ( null == account || !account.pin.equals(hashPin) ) {
throw new RuntimeException("ACCOUNT_LOGIN_INVALID_ACCOUNT_OR_PIN_CODE");
}
account.lastLoginTime = (int)(System.currentTimeMillis() / 1000);
this.userMapper.updateLoginTime(account);
this.curAccount = account;
this.curAccount.pin = "****";
this.actionLog.setUserId(account.id);
this.actionLog.log("account.login", param.account);
return account;
}
// logout
public void logout() {
if ( this.deviceService.isExecutorRunning() ) {
throw new RuntimeException("ACCOUNT_LOGOUT_EXECUTOR_RUNNING");
}
this.actionLog.log("account.logout");
this.actionLog.setUserId(0);
this.curAccount = null;
}
// create
public Account create(String account, String pin) {
if ( null == this.curAccount || 0 == this.curAccount.isAdmin) {
throw new RuntimeException("无权限创建用户");
}
Account user = userMapper.findByAccount(account);
if ( null != user ) {
throw new RuntimeException("用户名已存在");
}
user = new Account();
user.account = account;
user.pin = pin;
user.isAdmin = 0;
user.createdAt = System.currentTimeMillis();
user.createdBy = this.curAccount.id;
userMapper.insert(user);
user = this.userMapper.findByAccount(account);
LOG.info("user create success, user id : {}", user.id);
return user;
}
// pin-update
public void pinUpdate(String pin) {
if ( null == this.curAccount) {
throw new RuntimeException("请先登录");
}
this.curAccount.pin = pin;
int changeCount = userMapper.update(this.curAccount);
if ( 1 != changeCount ) {
throw new RuntimeException("数据更新异常");
}
}
// delete
public void delete(int id) {
if ( null == this.curAccount || 0 == this.curAccount.isAdmin) {
throw new RuntimeException("无权限删除用户");
}
Account user = this.userMapper.findById(id);
if ( null == user ) {
throw new RuntimeException("无效的用户id : " + id);
}
int deleteCount = this.userMapper.delete(user);
if ( 1 != deleteCount ) {
throw new RuntimeException("数据删除异常");
}
}
}

11
src/main/java/com/dreamworks/boditech/service/DeviceService.java

@ -25,6 +25,17 @@ public class DeviceService {
private Thread taskExecutorThread;
/**
* get is executor running
* @return is executor running
*/
public Boolean isExecutorRunning() {
if ( null == this.taskExecutor ) {
return false;
}
return Executor.STATUS_RUNNING.equals(this.taskExecutor.getStatus());
}
/**
* load consumable resources
*/
public void load() {

95
src/main/java/com/dreamworks/boditech/service/UserService.java

@ -1,95 +0,0 @@
package com.dreamworks.boditech.service;
import com.dreamworks.boditech.entity.ParamUserLogin;
import com.dreamworks.boditech.entity.User;
import com.dreamworks.boditech.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
@Service
public class UserService {
private static final Logger LOG = LoggerFactory.getLogger(UserService.class);
@Resource
private UserMapper userMapper;
@Resource
private ActionLogService actionLog;
// current user
private User curUser = null;
// login
public User login(ParamUserLogin param) {
User user = userMapper.findByAccount(param.account);
String hashPin = DigestUtils.md5DigestAsHex(param.pin.getBytes());
if ( null == user || !user.pin.equals(hashPin) ) {
throw new RuntimeException("USER_LOGIN_INVALID_ACCOUNT_OR_PIN_CODE");
}
this.curUser = user;
this.curUser.pin = "****";
this.actionLog.setUserId(user.id);
this.actionLog.log("user.login", param.account);
return user;
}
// logout
public void logout() {
this.actionLog.log("user.logout");
this.actionLog.setUserId(0);
this.curUser = null;
}
// create
public User create(String account, String pin) {
if ( null == this.curUser || 0 == this.curUser.isAdmin) {
throw new RuntimeException("无权限创建用户");
}
User user = userMapper.findByAccount(account);
if ( null != user ) {
throw new RuntimeException("用户名已存在");
}
user = new User();
user.account = account;
user.pin = pin;
user.isAdmin = 0;
user.createdAt = System.currentTimeMillis();
user.createdBy = this.curUser.id;
userMapper.insert(user);
user = this.userMapper.findByAccount(account);
LOG.info("user create success, user id : {}", user.id);
return user;
}
// pin-update
public void pinUpdate(String pin) {
if ( null == this.curUser ) {
throw new RuntimeException("请先登录");
}
this.curUser.pin = pin;
int changeCount = userMapper.update(this.curUser);
if ( 1 != changeCount ) {
throw new RuntimeException("数据更新异常");
}
}
// delete
public void delete(int id) {
if ( null == this.curUser || 0 == this.curUser.isAdmin) {
throw new RuntimeException("无权限删除用户");
}
User user = this.userMapper.findById(id);
if ( null == user ) {
throw new RuntimeException("无效的用户id : " + id);
}
int deleteCount = this.userMapper.delete(user);
if ( 1 != deleteCount ) {
throw new RuntimeException("数据删除异常");
}
}
}
Loading…
Cancel
Save