Skip to content

Commit

Permalink
✨ feat: 机器人创建
Browse files Browse the repository at this point in the history
  • Loading branch information
cokie committed Dec 19, 2023
1 parent 2d60dea commit c7073f4
Show file tree
Hide file tree
Showing 19 changed files with 254 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.micro.core.rest
package io.micro.api

/**
*@author Augenstern
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package io.micro.core.rest
package io.micro.api

import kotlinx.serialization.Serializable

/**
*@author Augenstern
*@since 2023/11/24
*/
@Serializable
data class R<T>(
var code: Int = Code.IGNORE,
var message: String = "",
Expand All @@ -12,7 +15,7 @@ data class R<T>(
companion object {

fun <E> success(msg: String, data: E): R<E> {
return R<E>(Code.OK, msg, data)
return R(Code.OK, msg, data)
}

fun success(): R<Unit> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package io.micro.api.register
package io.micro.api.robot

import io.micro.api.R
import io.micro.api.robot.converter.RobotManagerConverter
import io.micro.api.robot.dto.RobotManagerDTO
import io.micro.server.robot.domain.service.RobotManagerService
import io.smallrye.mutiny.Multi
import jakarta.ws.rs.GET
import jakarta.ws.rs.Path
import jakarta.ws.rs.PathParam
import jakarta.ws.rs.Produces
import io.smallrye.mutiny.Uni
import jakarta.ws.rs.*
import jakarta.ws.rs.core.Context
import jakarta.ws.rs.core.MediaType
import jakarta.ws.rs.sse.OutboundSseEvent
Expand All @@ -20,7 +21,8 @@ import org.jboss.resteasy.reactive.RestStreamElementType
*/
@Path("/robot/manager")
class RobotManagerController(
private val robotManagerService: RobotManagerService
private val robotManagerService: RobotManagerService,
private val robotManagerConverter: RobotManagerConverter
) {
@Operation(summary = "QQ机器人扫码登录")
@GET
Expand All @@ -34,7 +36,11 @@ class RobotManagerController(
return robotManagerService.qrQQLogin(qq, sse)
}

fun qqRobotCreate() {

@Operation(summary = "机器人创建")
@POST
@Path("/create")
fun qqRobotCreate(@Parameter(description = "创建信息") robotManagerDTO: RobotManagerDTO): Uni<R<Unit>> {
return robotManagerService.createRobot(robotManagerConverter.robotManagerDTO2RobotManager(robotManagerDTO))
.replaceWith { R.success("创建成功") }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.micro.api.robot.converter

import io.micro.api.robot.dto.RobotManagerDTO
import io.micro.server.robot.domain.model.entity.RobotManager
import org.mapstruct.BeanMapping
import org.mapstruct.Mapper
import org.mapstruct.MappingConstants.ComponentModel
import org.mapstruct.ReportingPolicy

@Mapper(componentModel = ComponentModel.CDI)
interface RobotManagerConverter {

@BeanMapping(unmappedTargetPolicy = ReportingPolicy.IGNORE)
fun robotManagerDTO2RobotManager(dto: RobotManagerDTO): RobotManager

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.micro.api.robot.dto

import kotlinx.serialization.Serializable

@Serializable
data class FeatureFunctionDTO(
var id: Long,
var remark: String? = null,
var enabled: Boolean = false
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.micro.api.robot.dto

import kotlinx.serialization.Serializable

@Serializable
data class RobotManagerDTO(
var userId: Long,

var name: String,

var type: Int,

var state: Int = 0,

var remark: String? = null,

val functions: MutableList<FeatureFunctionDTO> = mutableListOf()
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.micro.api.wx

import io.micro.api.wx.convert.WxMessageConvert
import io.micro.api.wx.dto.AccessInfoDTO
import io.micro.api.wx.dto.ReplyMessage
import io.micro.api.wx.dto.ReplyMessageDTO
import io.micro.api.wx.dto.WxMessageDTO
import io.micro.core.filter.ReqInfo
import io.micro.server.auth.domain.service.WxLoginService
Expand Down Expand Up @@ -44,11 +44,11 @@ class WxOfficialController(
@Path("/callback")
@Consumes(MediaType.TEXT_XML)
@Produces(MediaType.APPLICATION_XML)
fun message(@Parameter(description = "普通文本信息") wxMessageDTO: WxMessageDTO): Uni<ReplyMessage> {
fun message(@Parameter(description = "普通文本信息") wxMessageDTO: WxMessageDTO): Uni<ReplyMessageDTO> {
return wxLoginService.login(wxMessageConvert.wxMessageDTO2WxMessage(wxMessageDTO), sse)
.onItem().ifNotNull()
.transform { user ->
ReplyMessage().apply {
ReplyMessageDTO().apply {
fromUserName = wxMessageDTO.toUserName
toUserName = wxMessageDTO.fromUserName
createTime = System.currentTimeMillis().toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import jakarta.xml.bind.annotation.XmlRootElement
import jakarta.xml.bind.annotation.XmlTransient

@XmlRootElement(name = "xml")
class ReplyMessage {
class ReplyMessageDTO {

/**
* 开发者微信号
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package io.micro.core.exception

class BusinessException(message: String? = null, cause: Throwable? = null) : RuntimeException(message, cause)
34 changes: 34 additions & 0 deletions tokisaki-core/src/main/kotlin/io/micro/core/exception/Throws.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.micro.core.exception

import java.util.*

object Throws {

fun fail(msg: String): Nothing {
throw BusinessException(msg)
}

fun failIfTure(block: () -> Boolean, msg: String) {
if (block()) {
fail(msg)
}
}

fun failIfFalse(block: () -> Boolean, msg: String) {
if (!block()) {
fail(msg)
}
}

fun <T> failIfNull(block: () -> T, msg: String) {
if (Objects.isNull(block())) {
fail(msg)
}
}

fun <T> failIfNonNull(block: () -> T, msg: String) {
if (!Objects.isNull(block())) {
fail(msg)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.micro.server.robot.domain.model.entity

import io.micro.core.entity.BaseDomainEntity
import io.micro.core.exception.Throws
import io.micro.server.robot.domain.model.valobj.FeatureFunction
import kotlin.properties.Delegates

class RobotManager : BaseDomainEntity() {

var userId: Long by Delegates.notNull()

lateinit var name: String

var type: Int by Delegates.notNull()

var state: Int = 0

var remark: String? = null

val functions: MutableList<FeatureFunction> = mutableListOf()

fun isValidType() = validTypeIds.contains(type)

fun isValidState() = validStateIds.contains(state)

fun verify() {
Throws.failIfFalse(::isValidType, "非法的类型")
Throws.failIfFalse(::isValidState, "非法的状态")
}

companion object {
val validTypeIds = listOf(0)
val validStateIds = listOf(0, 1, 2, 3, 4, 5, 6)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.micro.server.robot.domain.model.valobj

data class FeatureFunction(
var id: Long,
var remark: String? = null,
var enabled: Boolean = false
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.micro.server.robot.domain.repository

import io.micro.server.robot.domain.model.entity.RobotManager
import io.smallrye.mutiny.Uni

interface IRobotManagerRepository {
fun saveRobotWithUser(robotManager: RobotManager): Uni<Unit>
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.micro.server.robot.domain.service

import io.micro.server.robot.domain.model.entity.RobotManager
import io.smallrye.mutiny.Multi
import io.smallrye.mutiny.Uni
import jakarta.ws.rs.sse.OutboundSseEvent
import jakarta.ws.rs.sse.Sse

Expand All @@ -16,4 +18,6 @@ interface RobotManagerService {
* @return 服务器推送事件
*/
fun qrQQLogin(qq: String, sse: Sse): Multi<OutboundSseEvent>

fun createRobot(robotManager: RobotManager): Uni<Unit>
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import io.micro.core.robot.Robot
import io.micro.core.robot.qq.QQRobot
import io.micro.core.robot.qq.QQRobotFactory
import io.micro.core.robot.qq.QQRobotManager
import io.micro.server.robot.domain.model.entity.RobotManager
import io.micro.server.robot.domain.repository.IRobotManagerRepository
import io.micro.server.robot.domain.service.RobotManagerService
import io.smallrye.mutiny.Multi
import io.smallrye.mutiny.Uni
import io.smallrye.mutiny.replaceWithUnit
import io.smallrye.mutiny.subscription.MultiEmitter
import jakarta.enterprise.context.ApplicationScoped
import jakarta.ws.rs.sse.OutboundSseEvent
Expand All @@ -20,7 +24,8 @@ import java.util.*
@ApplicationScoped
class RobotManagerServiceImpl(
private val factory: QQRobotFactory,
private val manager: QQRobotManager
private val manager: QQRobotManager,
private val robotManagerRepository: IRobotManagerRepository
) : RobotManagerService {

/**
Expand Down Expand Up @@ -50,6 +55,11 @@ class RobotManagerServiceImpl(
}
}

override fun createRobot(robotManager: RobotManager): Uni<Unit> {
robotManager.verify()
return robotManagerRepository.saveRobotWithUser(robotManager).replaceWithUnit()
}

/**
* 通过半长连接开始QQ扫码登录
* @param qq QQ号
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.micro.server.robot.infra.converter

import io.micro.server.robot.domain.model.entity.RobotManager
import io.micro.server.robot.infra.po.Robot
import org.mapstruct.BeanMapping
import org.mapstruct.Mapper
import org.mapstruct.MappingConstants.ComponentModel
import org.mapstruct.ReportingPolicy

@Mapper(componentModel = ComponentModel.CDI, uses = [RobotMapper::class])
interface RobotConverter {

@BeanMapping(unmappedTargetPolicy = ReportingPolicy.IGNORE)
fun robotManager2RobotPO(robotManager: RobotManager): Robot

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.micro.server.robot.infra.converter

import io.micro.server.function.infra.po.Function
import io.micro.server.robot.domain.model.valobj.FeatureFunction
import io.micro.server.robot.infra.po.Robot
import io.micro.server.robot.infra.po.UseFunction
import jakarta.inject.Singleton

@Singleton
class RobotMapper {

fun number2Type(num: Int): Robot.Type {
val entries = Robot.Type.entries
if (num < entries.size) {
return entries[num]
} else {
throw IllegalArgumentException()
}
}

fun number2State(num: Int): Robot.State {
val entries = Robot.State.entries
if (num < entries.size) {
return entries[num]
} else {
throw IllegalArgumentException()
}
}

fun featureFunction2UseFunction(featureFunction: FeatureFunction): UseFunction {
return UseFunction().apply {
function = Function().apply { id = featureFunction.id }
remark = featureFunction.remark
enabled = featureFunction.enabled
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Robot : BasePO() {
companion object : PanacheCompanion<Robot>

@ManyToOne
@JoinColumn(name = "user_id")
@JoinColumn(name = "user_id", nullable = false)
var user: User? = null

@Column(name = "robot_name", nullable = false)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.micro.server.robot.infra.repository

import io.micro.core.exception.Throws
import io.micro.server.auth.infra.po.User
import io.micro.server.robot.domain.model.entity.RobotManager
import io.micro.server.robot.domain.repository.IRobotManagerRepository
import io.micro.server.robot.infra.converter.RobotConverter
import io.micro.server.robot.infra.po.Robot
import io.quarkus.hibernate.reactive.panache.common.WithSession
import io.smallrye.mutiny.Uni
import io.smallrye.mutiny.replaceWithUnit
import jakarta.enterprise.context.ApplicationScoped

@ApplicationScoped
class RobotManagerRepository(private val robotConverter: RobotConverter) : IRobotManagerRepository {

@WithSession
override fun saveRobotWithUser(robotManager: RobotManager): Uni<Unit> {
return User.findById(robotManager.userId).flatMap { user ->
Throws.failIfNull({ user }, "用户不存在")
robotConverter.robotManager2RobotPO(robotManager)
.apply { this.user = user }
.persistAndFlush<Robot>()
.replaceWithUnit()
}
}

}

0 comments on commit c7073f4

Please sign in to comment.