nili
9 months ago
13 changed files with 193 additions and 12 deletions
@ -0,0 +1,19 @@ |
|||
package awesome.group.game.dao.bean; |
|||
|
|||
import com.baomidou.mybatisplus.annotation.IdType; |
|||
import com.baomidou.mybatisplus.annotation.TableId; |
|||
import lombok.Data; |
|||
|
|||
import java.sql.Timestamp; |
|||
|
|||
@Data |
|||
public class MatrixAdminDevice { |
|||
@TableId(value = "id", type = IdType.AUTO) |
|||
private Integer id; |
|||
private String deviceId; |
|||
private Integer appId; |
|||
private Integer adminId; |
|||
private Integer status;//0默认,-1下线
|
|||
private Timestamp createdAt; |
|||
private Timestamp updatedAt; |
|||
} |
@ -0,0 +1,14 @@ |
|||
package awesome.group.game.dao.mapper; |
|||
|
|||
import awesome.group.game.dao.bean.MatrixAdminDevice; |
|||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
|||
import org.apache.ibatis.annotations.Select; |
|||
import org.apache.ibatis.annotations.Update; |
|||
|
|||
public interface MatrixAdminDeviceMapper extends BaseMapper<MatrixAdminDevice> { |
|||
@Select("select * from matrix_admin_device where device_id = #{deviceId}") |
|||
MatrixAdminDevice selectByDeviceId(String deviceId); |
|||
|
|||
@Update("update matrix_admin_device set status = #{status} where admin_id=#{adminId} and device_id = #{deviceId}") |
|||
int updateStatusByDeviceId(int status, String deviceId, int adminId); |
|||
} |
@ -0,0 +1,92 @@ |
|||
package awesome.group.game.service; |
|||
|
|||
import awesome.group.game.dao.bean.MatrixAdmin; |
|||
import awesome.group.game.dao.bean.MatrixAdminDevice; |
|||
import awesome.group.game.dao.bean.MatrixApp; |
|||
import awesome.group.game.dao.mapper.MatrixAdminDeviceMapper; |
|||
import awesome.group.game.dao.mapper.MatrixAdminMapper; |
|||
import awesome.group.game.dao.mapper.MatrixAppMapper; |
|||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Service; |
|||
import org.springframework.util.Assert; |
|||
import org.springframework.util.StringUtils; |
|||
|
|||
import java.util.Arrays; |
|||
import java.util.List; |
|||
|
|||
@Service |
|||
public class AdminDeviceService { |
|||
|
|||
public static final int STATUS_DEFAULT = 0; |
|||
public static final int STATUS_OFFLINE = -1; |
|||
@Autowired |
|||
private MatrixAdminDeviceMapper mapper; |
|||
|
|||
@Autowired |
|||
private MatrixAdminMapper adminMapper; |
|||
|
|||
@Autowired |
|||
private MatrixAppMapper appMapper; |
|||
|
|||
public Integer getAdminId(String deviceId) { |
|||
MatrixAdminDevice device = mapper.selectByDeviceId(deviceId); |
|||
if (device == null || device.getStatus() == STATUS_OFFLINE) { |
|||
return null; |
|||
} |
|||
return device.getAdminId(); |
|||
} |
|||
|
|||
public List<MatrixAdminDevice> getDeviceList(Integer adminId, String appCode ) { |
|||
MatrixApp app = appMapper.queryByCode(appCode); |
|||
Assert.isTrue(app != null, "非法请求"); |
|||
LambdaQueryWrapper<MatrixAdminDevice> queryWrapper = new LambdaQueryWrapper<>(); |
|||
queryWrapper.eq(MatrixAdminDevice::getAdminId, adminId); |
|||
queryWrapper.eq(MatrixAdminDevice::getAppId, app.getId()); |
|||
return mapper.selectList(queryWrapper); |
|||
} |
|||
|
|||
public void setStatusOffline(Integer adminId, String deviceId) { |
|||
mapper.updateStatusByDeviceId(STATUS_OFFLINE, deviceId, adminId); |
|||
} |
|||
|
|||
public String bind(String deviceId, String appCode, String adminName) { |
|||
MatrixAdminDevice device = mapper.selectByDeviceId(deviceId); |
|||
if(device != null){ |
|||
return "设备已绑定"; |
|||
} |
|||
MatrixAdmin admin = adminMapper.query(adminName); |
|||
if (admin == null || admin.getRole() != AdminService.DEVICE_OWNER) { |
|||
return "设备主不存在"; |
|||
} |
|||
MatrixApp app = appMapper.queryByCode(appCode); |
|||
if (app == null) { |
|||
return "应用不存在"; |
|||
} |
|||
if(!StringUtils.hasText(admin.getAppIds()) || |
|||
!Arrays.stream(admin.getAppIds().split(",")).map(Integer::parseInt).toList().contains(app.getId())){ |
|||
return "无权限绑定该应用"; |
|||
} |
|||
long cnt = queryBindCnt(admin.getId(), app.getId()); |
|||
if (cnt >= admin.getDeviceCnt()) { |
|||
return "绑定设备数已达上限"; |
|||
} |
|||
device = new MatrixAdminDevice(); |
|||
device.setDeviceId(deviceId); |
|||
device.setAppId(app.getId()); |
|||
device.setAdminId(admin.getId()); |
|||
device.setStatus(STATUS_DEFAULT); |
|||
mapper.insert(device); |
|||
return "绑定成功"; |
|||
} |
|||
|
|||
public long queryBindCnt(int adminId, int appId){ |
|||
LambdaQueryWrapper<MatrixAdminDevice> queryWrapper = new LambdaQueryWrapper<>(); |
|||
queryWrapper.eq(MatrixAdminDevice::getAdminId, adminId); |
|||
queryWrapper.eq(MatrixAdminDevice::getAppId, appId); |
|||
queryWrapper.eq(MatrixAdminDevice::getStatus, STATUS_DEFAULT); |
|||
return mapper.selectCount(queryWrapper); |
|||
} |
|||
|
|||
|
|||
} |
@ -0,0 +1,34 @@ |
|||
package awesome.group.game.web.rest.matrix; |
|||
|
|||
import awesome.group.game.dao.bean.MatrixAdminDevice; |
|||
import awesome.group.game.service.AdminDeviceService; |
|||
import awesome.group.game.service.common.response.R; |
|||
import awesome.group.game.web.RequestContext; |
|||
import awesome.group.game.web.aop.RestApi; |
|||
import io.swagger.v3.oas.annotations.tags.Tag; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.web.bind.annotation.*; |
|||
|
|||
import java.util.List; |
|||
|
|||
@RestController |
|||
@RequestMapping("/api/admin/device") |
|||
@Tag(name = "device") |
|||
public class DeviceController { |
|||
|
|||
@Autowired |
|||
private AdminDeviceService adminDeviceService; |
|||
|
|||
@GetMapping("/list") |
|||
@RestApi |
|||
public R<List<MatrixAdminDevice>> deviceList(@RequestParam String appCode) { |
|||
return new R<>(adminDeviceService.getDeviceList(RequestContext.getAdminID(), appCode)); |
|||
} |
|||
|
|||
@PostMapping("/offline") |
|||
@RestApi |
|||
public R<String> offline(@RequestParam String deviceId) { |
|||
adminDeviceService.setStatusOffline(RequestContext.getAdminID(), deviceId); |
|||
return new R<>("ok"); |
|||
} |
|||
} |
Loading…
Reference in new issue