rts-sim-testing-service/api/trainManage.go
2023-11-17 18:15:27 +08:00

367 lines
11 KiB
Go

package api
// 列车相关的关系管理
import (
"net/http"
"strconv"
jwt "github.com/appleboy/gin-jwt/v2"
"github.com/gin-gonic/gin"
"joylink.club/bj-rtsts-server/dto"
"joylink.club/bj-rtsts-server/middleware"
"joylink.club/bj-rtsts-server/service"
"joylink.club/bj-rtsts-server/sys_error"
)
func InitTrainManageRouter(api *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
authed := api.Group("/v1/trainManage").Use(authMiddleware.MiddlewareFunc(), middleware.PermissMiddleware)
authed.GET("/paging", pageQueryTrainInfo)
authed.GET("/list", queryTrainInfoList)
authed.POST("", createTrainInfo)
authed.GET("/:id", queryTrainInfo)
authed.PUT("/:id", updateTrainInfo)
authed.DELETE("/:id", deleteTrainInfo)
authed.GET("/model/paging", pageQueryTrainModel)
authed.GET("/model/list", queryTrainModelList)
authed.POST("/model", createTrainModel)
authed.GET("/model/:id", queryTrainModel)
authed.PUT("/model/:id", updateTrainModel)
authed.DELETE("/model/:id", deleteTrainModel)
authed.GET("/size/paging", pageQueryTrainSize)
authed.GET("/size/list", queryTrainSizeList)
authed.POST("/size", createTrainSize)
authed.GET("/size/:id", queryTrainSize)
authed.PUT("/size/:id", updateTrainSize)
authed.DELETE("/size/:id", deleteTrainSize)
authed.GET("/wheelDiameter/paging", pageQueryTrainWheelDiameter)
authed.GET("/wheelDiameter/list", queryTrainWheelDiameterList)
authed.POST("/wheelDiameter", createTrainWheelDiameter)
authed.GET("/wheelDiameter/:id", queryTrainWheelDiameter)
authed.PUT("/wheelDiameter/:id", updateTrainWheelDiameter)
authed.DELETE("/wheelDiameter/:id", deleteTrainWheelDiameter)
}
// 分页查询列车列表
//
// @Summary 分页查询列车信息列表
//
// @Security JwtAuth
//
// @Description 可以通过列车名称过滤,分页查询列车信息列表
// @Tags 列车管理Api
// @Accept json
// @Produce json
// @Param pageTrainInfoReqDto query dto.PageTrainInfoReqDto true "列车查询条件带分页信息"
// @Success 200 {object} dto.PageDto
// @Failure 401 {object} dto.ErrorDto
// @Failure 404 {object} dto.ErrorDto
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/trainManage/paging [get]
func pageQueryTrainInfo(c *gin.Context) {
req := dto.PageTrainInfoReqDto{}
if err := c.ShouldBind(&req); err != nil {
panic(sys_error.New("查询失败,参数格式错误", err))
}
c.JSON(http.StatusOK, service.PageTrainInfoQuery(&req))
}
// 查询列车列表
//
// @Summary 查询列车信息列表
//
// @Security JwtAuth
//
// @Description 可以通过列车名称过滤,查询列车信息列表
// @Tags 列车管理Api
// @Accept json
// @Produce json
// @Param trainInfoReqDto query dto.TrainInfoReqDto true "列车查询条件"
// @Success 200 {object} model.TrainInfo
// @Failure 401 {object} dto.ErrorDto
// @Failure 404 {object} dto.ErrorDto
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/trainManage/list [get]
func queryTrainInfoList(c *gin.Context) {
req := dto.TrainInfoReqDto{}
if err := c.ShouldBind(&req); err != nil {
panic(sys_error.New("查询失败,参数格式错误", err))
}
c.JSON(http.StatusOK, service.ListTrainInfoQuery(&req))
}
// 创建列车
//
// @Summary 创建列车
//
// @Security JwtAuth
//
// @Description 创建列车数据
// @Tags 列车管理Api
// @Accept json
// @Produce json
// @Param trainInfoDto query dto.TrainInfoDto true "创建的列车信息"
// @Success 200 {object} nil
// @Failure 401 {object} dto.ErrorDto
// @Failure 404 {object} dto.ErrorDto
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/trainManage [post]
func createTrainInfo(c *gin.Context) {
req := dto.TrainInfoDto{}
if err := c.ShouldBind(&req); err != nil {
panic(sys_error.New("保存失败,参数格式错误", err))
}
c.JSON(http.StatusOK, service.CreateTrainInfo(&req))
}
// 查询列车详情
//
// @Summary 查询列车详情
//
// @Security JwtAuth
//
// @Description 查询列车详情
// @Tags 列车管理Api
// @Accept json
// @Produce json
// @Param id path int true "列车ID"
// @Success 200 {object} model.TrainModel
// @Failure 401 {object} dto.ErrorDto
// @Failure 404 {object} dto.ErrorDto
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/trainManage/{id} [get]
func queryTrainInfo(c *gin.Context) {
id, exist := c.Params.Get("id")
if !exist {
panic(sys_error.New("查询失败,缺少主键"))
}
int64Id, _ := strconv.ParseInt(id, 10, 64)
c.JSON(http.StatusOK, service.QueryTrainInfo(int32(int64Id)))
}
// 修改列车信息
//
// @Summary 修改列车信息
//
// @Security JwtAuth
//
// @Description 修改列车信息
// @Tags 列车管理Api
// @Accept json
// @Produce json
// @Param id path int true "列车ID"
// @Param trainInfoDto query dto.TrainInfoDto true "修改的列车信息"
// @Success 200 {object} nil
// @Failure 401 {object} dto.ErrorDto
// @Failure 404 {object} dto.ErrorDto
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/trainManage/{id} [put]
func updateTrainInfo(c *gin.Context) {
id, exist := c.Params.Get("id")
if !exist {
panic(sys_error.New("更新失败,缺少主键"))
}
req := dto.TrainInfoDto{}
if err := c.ShouldBind(&req); err != nil {
panic(sys_error.New("更新失败,参数格式错误", err))
}
int64Id, _ := strconv.ParseInt(id, 10, 64)
c.JSON(http.StatusOK, service.UpdateTrainInfo(int32(int64Id), &req))
}
// 删除列车数据
//
// @Summary 删除列车数据
//
// @Security JwtAuth
//
// @Description 删除列车数据
// @Tags 列车管理Api
// @Accept json
// @Produce json
// @Param id path int true "列车ID"
// @Success 200 {object} nil
// @Failure 401 {object} dto.ErrorDto
// @Failure 404 {object} dto.ErrorDto
// @Failure 500 {object} dto.ErrorDto
// @Router /api/v1/trainManage/{id} [delete]
func deleteTrainInfo(c *gin.Context) {
idStr := c.Param("id")
id, err := strconv.Atoi(idStr)
if err != nil {
panic(sys_error.New("删除失败,缺少主键"))
}
service.DeleteTrainInfoById(id)
c.JSON(http.StatusOK, true)
}
// TODO: 前端修改完毕后以下代码全部作废
func pageQueryTrainModel(c *gin.Context) {
req := dto.PageTrainManageReqDto{}
if err := c.ShouldBind(&req); err != nil {
panic(sys_error.New("查询失败,参数格式错误", err))
}
c.JSON(http.StatusOK, service.PageTrainModelQuery(&req))
}
func createTrainModel(c *gin.Context) {
req := dto.TrainModelDto{}
if err := c.ShouldBind(&req); err != nil {
panic(sys_error.New("保存失败,参数格式错误", err))
}
c.JSON(http.StatusOK, service.CreateTrainModel(&req))
}
func queryTrainModel(c *gin.Context) {
id, exist := c.Params.Get("id")
if !exist {
panic(sys_error.New("查询失败,缺少主键"))
}
int64Id, _ := strconv.ParseInt(id, 10, 64)
c.JSON(http.StatusOK, service.QueryTrainModel(int32(int64Id)))
}
func updateTrainModel(c *gin.Context) {
id, exist := c.Params.Get("id")
if !exist {
panic(sys_error.New("更新失败,缺少主键"))
}
req := dto.TrainModelDto{}
if err := c.ShouldBind(&req); err != nil {
panic(sys_error.New("更新失败,参数格式错误", err))
}
int64Id, _ := strconv.ParseInt(id, 10, 64)
c.JSON(http.StatusOK, service.UpdateTrainModel(int32(int64Id), &req))
}
func deleteTrainModel(c *gin.Context) {
idStr := c.Param("id")
id, err := strconv.Atoi(idStr)
if err != nil {
panic(sys_error.New("删除失败,缺少主键"))
}
service.DeleteTrainModelById(id)
c.JSON(http.StatusOK, true)
}
func queryTrainModelList(c *gin.Context) {
req := dto.TrainManageReqDto{}
if err := c.ShouldBind(&req); err != nil {
panic(sys_error.New("查询失败,参数格式错误", err))
}
c.JSON(http.StatusOK, service.ListTrainModelQuery(&req))
}
func pageQueryTrainSize(c *gin.Context) {
req := dto.PageTrainManageReqDto{}
if err := c.ShouldBind(&req); err != nil {
panic(sys_error.New("查询失败,参数格式错误", err))
}
c.JSON(http.StatusOK, service.PageTrainSizeQuery(&req))
}
func queryTrainSizeList(c *gin.Context) {
req := dto.TrainManageReqDto{}
if err := c.ShouldBind(&req); err != nil {
panic(sys_error.New("查询失败,参数格式错误", err))
}
c.JSON(http.StatusOK, service.ListTrainSizeQuery(&req))
}
func createTrainSize(c *gin.Context) {
req := dto.TrainSizeDto{}
if err := c.ShouldBind(&req); err != nil {
panic(sys_error.New("创建失败,参数格式错误", err))
}
c.JSON(http.StatusOK, service.CreateTrainSize(&req))
}
func queryTrainSize(c *gin.Context) {
id, exist := c.Params.Get("id")
if !exist {
panic(sys_error.New("查询失败,缺少查询主键"))
}
int64Id, _ := strconv.ParseInt(id, 10, 64)
c.JSON(http.StatusOK, service.QueryTrainSize(int32(int64Id)))
}
func updateTrainSize(c *gin.Context) {
id, exist := c.Params.Get("id")
if !exist {
panic(sys_error.New("更新失败,缺少查询主键"))
}
req := dto.TrainSizeDto{}
if err := c.ShouldBind(&req); err != nil {
panic(sys_error.New("更新失败,参数格式错误", err))
}
int64Id, _ := strconv.ParseInt(id, 10, 64)
c.JSON(http.StatusOK, service.UpdateTrainSize(int32(int64Id), &req))
}
func deleteTrainSize(c *gin.Context) {
idStr := c.Param("id")
id, err := strconv.Atoi(idStr)
if err != nil {
panic(sys_error.New("删除失败,主键格式错误", err))
}
service.DeleteTrainSizeById(id)
c.JSON(http.StatusOK, true)
}
func pageQueryTrainWheelDiameter(c *gin.Context) {
req := dto.PageTrainManageReqDto{}
if err := c.ShouldBind(&req); err != nil {
panic(sys_error.New("查询失败,参数格式错误", err))
}
c.JSON(http.StatusOK, service.PageTrainWheelDiameterQuery(&req))
}
func queryTrainWheelDiameterList(c *gin.Context) {
req := dto.TrainManageReqDto{}
if err := c.ShouldBind(&req); err != nil {
panic(sys_error.New("查询失败,参数格式错误", err))
}
c.JSON(http.StatusOK, service.ListTrainWheelDiameterQuery(&req))
}
func createTrainWheelDiameter(c *gin.Context) {
req := dto.TrainWheelDto{}
if err := c.ShouldBind(&req); err != nil {
panic(sys_error.New("创建失败,参数格式错误", err))
}
c.JSON(http.StatusOK, service.CreateTrainWheelDiameter(&req))
}
func queryTrainWheelDiameter(c *gin.Context) {
id, exist := c.Params.Get("id")
if !exist {
panic(sys_error.New("查询失败,缺少查询主键"))
}
int64Id, _ := strconv.ParseInt(id, 10, 64)
c.JSON(http.StatusOK, service.QueryTrainWheelDiameter(int32(int64Id)))
}
func updateTrainWheelDiameter(c *gin.Context) {
id, exist := c.Params.Get("id")
if !exist {
panic(sys_error.New("更新失败,缺少查询主键"))
}
req := dto.TrainWheelDto{}
if err := c.ShouldBind(&req); err != nil {
panic(sys_error.New("查询失败,参数格式错误", err))
}
int64Id, _ := strconv.ParseInt(id, 10, 64)
c.JSON(http.StatusOK, service.UpdateTrainWheelDiameter(int32(int64Id), &req))
}
func deleteTrainWheelDiameter(c *gin.Context) {
idStr := c.Param("id")
id, err := strconv.Atoi(idStr)
if err != nil {
panic(sys_error.New("删除失败,缺少主键"))
}
service.DeleteTrainWheelDiameterById(id)
c.JSON(http.StatusOK, true)
}