From 4651bd6f9b3d28bf26f5a7142a8ed7bb1263ac91 Mon Sep 17 00:00:00 2001 From: sige Date: Thu, 21 Dec 2023 15:38:36 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=99=BB=E5=BD=95=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E8=B4=A6=E5=8F=B7=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../boditech/controller/AccountController.java | 108 ++++++++++++++++ .../boditech/controller/DeviceController.java | 1 + .../boditech/controller/UserController.java | 98 --------------- .../com/dreamworks/boditech/driver/Device.java | 5 + .../driver/consumable/CsmBufferTubeBox.java | 3 + .../driver/consumable/CsmBufferTubeManager.java | 70 +++++++---- .../dreamworks/boditech/driver/task/Executor.java | 9 ++ .../dreamworks/boditech/driver/task/TaskLoad.java | 35 ++++++ .../com/dreamworks/boditech/entity/Account.java | 12 ++ .../java/com/dreamworks/boditech/entity/User.java | 9 -- .../dreamworks/boditech/mapper/AccountMapper.java | 33 +++++ .../com/dreamworks/boditech/mapper/UserMapper.java | 25 ---- .../boditech/service/AccountService.java | 138 +++++++++++++++++++++ .../dreamworks/boditech/service/DeviceService.java | 11 ++ .../dreamworks/boditech/service/UserService.java | 95 -------------- 15 files changed, 400 insertions(+), 252 deletions(-) create mode 100644 src/main/java/com/dreamworks/boditech/controller/AccountController.java delete mode 100644 src/main/java/com/dreamworks/boditech/controller/UserController.java create mode 100644 src/main/java/com/dreamworks/boditech/entity/Account.java delete mode 100644 src/main/java/com/dreamworks/boditech/entity/User.java create mode 100644 src/main/java/com/dreamworks/boditech/mapper/AccountMapper.java delete mode 100644 src/main/java/com/dreamworks/boditech/mapper/UserMapper.java create mode 100644 src/main/java/com/dreamworks/boditech/service/AccountService.java delete mode 100644 src/main/java/com/dreamworks/boditech/service/UserService.java diff --git a/src/main/java/com/dreamworks/boditech/controller/AccountController.java b/src/main/java/com/dreamworks/boditech/controller/AccountController.java new file mode 100644 index 0000000..a64437e --- /dev/null +++ b/src/main/java/com/dreamworks/boditech/controller/AccountController.java @@ -0,0 +1,108 @@ +package com.dreamworks.boditech.controller; +import com.dreamworks.boditech.controller.entity.ApiResponse; +import com.dreamworks.boditech.entity.ParamUserLogin; +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 AccountController extends BaseController { + @Resource + AccountService accountService; + + @ResponseBody + @PostMapping("/api/account/login-list") + public ApiResponse loginList(@RequestBody Map params) { + Integer limit = (Integer)params.get("limit"); + return this.success(this.accountService.listLoginAccounts(limit)); + } + + @ResponseBody + @PostMapping("/api/account/login") + public ApiResponse login(@RequestBody ParamUserLogin param ) { + Account user = this.accountService.login(param); + return this.success(user); + } + + @ResponseBody + @PostMapping("/api/account/logout") + public ApiResponse logout() { + this.accountService.logout(); + return this.success(); + } + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + // @TODO : 下面这些接口还没测试 + + @ResponseBody + @PostMapping("/api/user/create") + public ApiResponse create( String name, String pin ) { + try { + Account user = this.accountService.create(name, pin); + return this.success(user); + } catch ( RuntimeException e ) { + return this.error(e.getMessage()); + } + } + + @ResponseBody + @PostMapping("/api/user/pin-update") + public ApiResponse pinUpdate( String pin ) { + try { + this.accountService.pinUpdate(pin); + return this.success(); + } catch ( RuntimeException e ) { + return this.error(e.getMessage()); + } + } + + @ResponseBody + @PostMapping("/api/user/delete") + public ApiResponse delete( int id ) { + try { + this.accountService.delete(id); + return this.success(); + } catch ( RuntimeException e ) { + return this.error(e.getMessage()); + } + } +} diff --git a/src/main/java/com/dreamworks/boditech/controller/DeviceController.java b/src/main/java/com/dreamworks/boditech/controller/DeviceController.java index c424ab8..281e8fc 100644 --- a/src/main/java/com/dreamworks/boditech/controller/DeviceController.java +++ b/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); } diff --git a/src/main/java/com/dreamworks/boditech/controller/UserController.java b/src/main/java/com/dreamworks/boditech/controller/UserController.java deleted file mode 100644 index 862db72..0000000 --- a/src/main/java/com/dreamworks/boditech/controller/UserController.java +++ /dev/null @@ -1,98 +0,0 @@ -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 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; -@Controller -public class UserController extends BaseController { - @Resource - UserService userService; - - @ResponseBody - @PostMapping("/api/user/login") - public ApiResponse login(@RequestBody ParamUserLogin param ) { - User user = this.userService.login(param); - return this.success(user); - } - - @ResponseBody - @PostMapping("/api/user/logout") - public ApiResponse logout() { - this.userService.logout(); - return this.success(); - } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // @TODO : 下面这些接口还没测试 - - @ResponseBody - @PostMapping("/api/user/create") - public ApiResponse create( String name, String pin ) { - try { - User user = this.userService.create(name, pin); - return this.success(user); - } catch ( RuntimeException e ) { - return this.error(e.getMessage()); - } - } - - @ResponseBody - @PostMapping("/api/user/pin-update") - public ApiResponse pinUpdate( String pin ) { - try { - this.userService.pinUpdate(pin); - return this.success(); - } catch ( RuntimeException e ) { - return this.error(e.getMessage()); - } - } - - @ResponseBody - @PostMapping("/api/user/delete") - public ApiResponse delete( int id ) { - try { - this.userService.delete(id); - return this.success(); - } catch ( RuntimeException e ) { - return this.error(e.getMessage()); - } - } -} diff --git a/src/main/java/com/dreamworks/boditech/driver/Device.java b/src/main/java/com/dreamworks/boditech/driver/Device.java index a7dc476..4db9a07 100644 --- a/src/main/java/com/dreamworks/boditech/driver/Device.java +++ b/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 diff --git a/src/main/java/com/dreamworks/boditech/driver/consumable/CsmBufferTubeBox.java b/src/main/java/com/dreamworks/boditech/driver/consumable/CsmBufferTubeBox.java index 2e19831..e0ee325 100644 --- a/src/main/java/com/dreamworks/boditech/driver/consumable/CsmBufferTubeBox.java +++ b/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; } diff --git a/src/main/java/com/dreamworks/boditech/driver/consumable/CsmBufferTubeManager.java b/src/main/java/com/dreamworks/boditech/driver/consumable/CsmBufferTubeManager.java index d09ab6e..208c445 100644 --- a/src/main/java/com/dreamworks/boditech/driver/consumable/CsmBufferTubeManager.java +++ b/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 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); - } - } } diff --git a/src/main/java/com/dreamworks/boditech/driver/task/Executor.java b/src/main/java/com/dreamworks/boditech/driver/task/Executor.java index 840addc..db82386 100644 --- a/src/main/java/com/dreamworks/boditech/driver/task/Executor.java +++ b/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 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) { diff --git a/src/main/java/com/dreamworks/boditech/driver/task/TaskLoad.java b/src/main/java/com/dreamworks/boditech/driver/task/TaskLoad.java index a085943..61dfe97 100644 --- a/src/main/java/com/dreamworks/boditech/driver/task/TaskLoad.java +++ b/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(); + } } diff --git a/src/main/java/com/dreamworks/boditech/entity/Account.java b/src/main/java/com/dreamworks/boditech/entity/Account.java new file mode 100644 index 0000000..7c65631 --- /dev/null +++ b/src/main/java/com/dreamworks/boditech/entity/Account.java @@ -0,0 +1,12 @@ +package com.dreamworks.boditech.entity; +public class Account { + public int id; + public String account; + public String pin; + public int isAdmin; + public long createdAt; + public int createdBy; + + // 上次登录时间 + public Integer lastLoginTime; +} diff --git a/src/main/java/com/dreamworks/boditech/entity/User.java b/src/main/java/com/dreamworks/boditech/entity/User.java deleted file mode 100644 index 0a0f1c0..0000000 --- a/src/main/java/com/dreamworks/boditech/entity/User.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.dreamworks.boditech.entity; -public class User { - public int id; - public String account; - public String pin; - public int isAdmin; - public long createdAt; - public int createdBy; -} diff --git a/src/main/java/com/dreamworks/boditech/mapper/AccountMapper.java b/src/main/java/com/dreamworks/boditech/mapper/AccountMapper.java new file mode 100644 index 0000000..8b24720 --- /dev/null +++ b/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 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); +} \ No newline at end of file diff --git a/src/main/java/com/dreamworks/boditech/mapper/UserMapper.java b/src/main/java/com/dreamworks/boditech/mapper/UserMapper.java deleted file mode 100644 index d71de10..0000000 --- a/src/main/java/com/dreamworks/boditech/mapper/UserMapper.java +++ /dev/null @@ -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); -} \ No newline at end of file diff --git a/src/main/java/com/dreamworks/boditech/service/AccountService.java b/src/main/java/com/dreamworks/boditech/service/AccountService.java new file mode 100644 index 0000000..17e2f51 --- /dev/null +++ b/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 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("数据删除异常"); + } + } +} diff --git a/src/main/java/com/dreamworks/boditech/service/DeviceService.java b/src/main/java/com/dreamworks/boditech/service/DeviceService.java index c6ecb5c..04bf386 100644 --- a/src/main/java/com/dreamworks/boditech/service/DeviceService.java +++ b/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() { diff --git a/src/main/java/com/dreamworks/boditech/service/UserService.java b/src/main/java/com/dreamworks/boditech/service/UserService.java deleted file mode 100644 index 72b1eb4..0000000 --- a/src/main/java/com/dreamworks/boditech/service/UserService.java +++ /dev/null @@ -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("数据删除异常"); - } - } -}