From 4db67547384ee45992a364eed1bedd122503f120 Mon Sep 17 00:00:00 2001 From: sige Date: Fri, 17 Nov 2023 20:39:42 +0800 Subject: [PATCH] user service --- .../boditech/controller/UserController.java | 53 +++++++------- .../java/com/dreamworks/boditech/entity/User.java | 2 +- .../com/dreamworks/boditech/mapper/UserMapper.java | 4 ++ .../dreamworks/boditech/service/DeviceService.java | 22 +++--- .../dreamworks/boditech/service/UserService.java | 82 ++++++++++++++++++++++ 5 files changed, 127 insertions(+), 36 deletions(-) create mode 100644 src/main/java/com/dreamworks/boditech/service/UserService.java diff --git a/src/main/java/com/dreamworks/boditech/controller/UserController.java b/src/main/java/com/dreamworks/boditech/controller/UserController.java index 5cfc8e3..f8e82c1 100644 --- a/src/main/java/com/dreamworks/boditech/controller/UserController.java +++ b/src/main/java/com/dreamworks/boditech/controller/UserController.java @@ -2,6 +2,7 @@ package com.dreamworks.boditech.controller; import com.dreamworks.boditech.controller.entity.ApiResponse; import com.dreamworks.boditech.entity.User; import com.dreamworks.boditech.mapper.UserMapper; +import com.dreamworks.boditech.service.UserService; import jakarta.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; @@ -10,45 +11,49 @@ import org.springframework.web.bind.annotation.ResponseBody; @Controller public class UserController extends BaseController { @Resource - UserMapper userMapper; + UserService userService; @ResponseBody @PostMapping("/api/user/login") public ApiResponse login( String name, String pin ) { - User user = userMapper.findByName(name); - if ( null == user || !user.pin.equals(pin) ) { - return this.error("无效的用户名或PIN"); + try { + User user = this.userService.login(name, pin); + return this.success(user); + } catch ( RuntimeException e ) { + return this.error(e.getMessage()); } - return this.success(user); } @ResponseBody - @PostMapping("/api/user/pin-update") - public ApiResponse pinUpdate( int id, String pin ) { - User user = userMapper.findById(id); - if ( null == user ) { - return this.error("无效的用户id : " + id); + @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()); } - user.pin = pin; - int changeCount = userMapper.update(user); - if ( 1 != changeCount ) { - return this.error("数据更新异常"); + } + + @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()); } - return this.success(); } @ResponseBody @PostMapping("/api/user/delete") public ApiResponse delete( int id ) { - User user = userMapper.findById(id); - if ( null == user ) { - return this.error("无效的用户id : " + id); - } - - int deleteCount = userMapper.delete(user); - if ( 1 != deleteCount ) { - return this.error("数据删除异常"); + try { + this.userService.delete(id); + return this.success(); + } catch ( RuntimeException e ) { + return this.error(e.getMessage()); } - return this.success(); } } diff --git a/src/main/java/com/dreamworks/boditech/entity/User.java b/src/main/java/com/dreamworks/boditech/entity/User.java index 5849f6d..4c91497 100644 --- a/src/main/java/com/dreamworks/boditech/entity/User.java +++ b/src/main/java/com/dreamworks/boditech/entity/User.java @@ -4,6 +4,6 @@ public class User { public String name; public String pin; public int isAdmin; - public int createdAt; + public long createdAt; public int createdBy; } diff --git a/src/main/java/com/dreamworks/boditech/mapper/UserMapper.java b/src/main/java/com/dreamworks/boditech/mapper/UserMapper.java index 8a9b202..6f8200b 100644 --- a/src/main/java/com/dreamworks/boditech/mapper/UserMapper.java +++ b/src/main/java/com/dreamworks/boditech/mapper/UserMapper.java @@ -4,6 +4,7 @@ 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 { @@ -18,4 +19,7 @@ public interface UserMapper { @Delete("DELETE FROM bdt_users WHERE id=#{id}") int delete(User user); + + @Insert("INSERT INTO bdt_users (name, pin, isAdmin, createdAt, createdBy) VALUES (#{name}, #{pin}, #{isAdmin}, #{createdAt}, #{createdBy})") + int insert(User user); } \ No newline at end of file diff --git a/src/main/java/com/dreamworks/boditech/service/DeviceService.java b/src/main/java/com/dreamworks/boditech/service/DeviceService.java index 2212481..f7a8acb 100644 --- a/src/main/java/com/dreamworks/boditech/service/DeviceService.java +++ b/src/main/java/com/dreamworks/boditech/service/DeviceService.java @@ -346,18 +346,18 @@ public class DeviceService { } // module read adc - public void moduleReadAdc(int mid, int adc_id, int adcindex) { - LOG.info("moduleReadAdc : mid={}, adc_id={}, adcindex={}", mid, adc_id, adcindex); - this.call(mid, BoditechDeviceCmd.MODULE_READ_ADC, adc_id, adcindex); + public void moduleReadAdc(int mid, int adc_id, int adcIndex) { + LOG.info("moduleReadAdc : mid={}, adc_id={}, adcIndex={}", mid, adc_id, adcIndex); + this.call(mid, BoditechDeviceCmd.MODULE_READ_ADC, adc_id, adcIndex); } // module read raw - public void moduleReadRaw(int mid, int startindex) { - LOG.info("moduleReadRaw : mid={}, startindex={}", mid, startindex); - this.call(mid, BoditechDeviceCmd.MODULE_READ_RAW, startindex); + public void moduleReadRaw(int mid, int startIndex) { + LOG.info("moduleReadRaw : mid={}, startIndex={}", mid, startIndex); + this.call(mid, BoditechDeviceCmd.MODULE_READ_RAW, startIndex); } - // module readio + // module read io public void moduleReadIO(int mid) { LOG.info("moduleReadIO : mid={}", mid); this.call(mid, BoditechDeviceCmd.MODULE_READIO); @@ -387,9 +387,9 @@ public class DeviceService { this.call(mid, BoditechDeviceCmd.MODULE_STOP); } - // module writeio - public void moduleWriteIO(int mid, int ioindex, int io) { - LOG.info("moduleWriteIO : mid={}, ioindex={}, io={}", mid, ioindex, io); - this.call(mid, BoditechDeviceCmd.MODULE_WRITEIO, ioindex, io); + // module write io + public void moduleWriteIO(int mid, int ioIndex, int io) { + LOG.info("moduleWriteIO : mid={}, ioIndex={}, io={}", mid, ioIndex, io); + this.call(mid, BoditechDeviceCmd.MODULE_WRITEIO, ioIndex, io); } } diff --git a/src/main/java/com/dreamworks/boditech/service/UserService.java b/src/main/java/com/dreamworks/boditech/service/UserService.java new file mode 100644 index 0000000..4f46e6d --- /dev/null +++ b/src/main/java/com/dreamworks/boditech/service/UserService.java @@ -0,0 +1,82 @@ +package com.dreamworks.boditech.service; +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; +@Service +public class UserService { + private static final Logger LOG = LoggerFactory.getLogger(UserService.class); + + @Resource + private UserMapper userMapper; + + private User curUser = null; + + // login + public User login(String name, String pin) { + User user = userMapper.findByName(name); + if ( null == user || !user.pin.equals(pin) ) { + throw new RuntimeException("无效的用户名或PIN"); + } + + this.curUser = user; + LOG.info("user login success, user id : {}", this.curUser.id); + return user; + } + + // create + public User create(String name, String pin) { + if ( null == this.curUser || 0 == this.curUser.isAdmin) { + throw new RuntimeException("无权限创建用户"); + } + + User user = userMapper.findByName(name); + if ( null != user ) { + throw new RuntimeException("用户名已存在"); + } + + user = new User(); + user.name = name; + user.pin = pin; + user.isAdmin = 0; + user.createdAt = System.currentTimeMillis(); + user.createdBy = this.curUser.id; + userMapper.insert(user); + + user = this.userMapper.findByName(name); + 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("数据删除异常"); + } + } +}