112 lines
3.1 KiB
Go
112 lines
3.1 KiB
Go
package controllers
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
"psi/constant"
|
|
"psi/database"
|
|
"psi/models/request"
|
|
"psi/models/response"
|
|
"psi/service"
|
|
"psi/utils"
|
|
)
|
|
|
|
type LogisticsApi struct{}
|
|
|
|
var logisticsService = service.LogisticsService{}
|
|
|
|
func (r *LogisticsApi) GetLogisticsList(c *gin.Context) {
|
|
var req request.QueryLogisticsRequest
|
|
if err := c.ShouldBindQuery(&req); err != nil {
|
|
ValidAndFail(constant.LoggerChannelRequest, "查询物流模板列表请求参数异常", "参数错误: "+err.Error(), c, err)
|
|
return
|
|
}
|
|
|
|
list, total, err := logisticsService.GetLogisticsList(req, database.GetDB(c))
|
|
if err != nil {
|
|
utils.FailWithRequestLog(constant.LoggerChannelWork, "查询物流模板列表异常", err, c, req)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 0,
|
|
"data": gin.H{
|
|
"list": list,
|
|
"total": total,
|
|
"page": req.Page,
|
|
"page_size": req.PageSize,
|
|
},
|
|
"msg": "查询成功",
|
|
})
|
|
}
|
|
|
|
func (r *LogisticsApi) GetLogisticsDetail(c *gin.Context) {
|
|
idStr := c.Param("id")
|
|
|
|
if idStr == "" {
|
|
response.FailWithValidateMessage("参数错误: ID不能为空", c)
|
|
return
|
|
}
|
|
|
|
var id uint64
|
|
if _, err := fmt.Sscanf(idStr, "%d", &id); err != nil || id == 0 {
|
|
response.FailWithValidateMessage("参数错误: ID格式不正确", c)
|
|
return
|
|
}
|
|
|
|
logistics, err := logisticsService.GetLogisticsByID(id, database.GetDB(c))
|
|
if err != nil {
|
|
utils.FailWithRequestLog(constant.LoggerChannelWork, "查询物流模板详情异常", err, c, gin.H{"id": id})
|
|
return
|
|
}
|
|
|
|
response.OkWithDetailed(logistics, "查询成功", c)
|
|
}
|
|
|
|
func (r *LogisticsApi) CreateLogistics(c *gin.Context) {
|
|
var req request.CreateLogisticsRequest
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
ValidAndFail(constant.LoggerChannelRequest, "创建物流模板请求参数异常", "参数错误: "+err.Error(), c, err)
|
|
return
|
|
}
|
|
|
|
id, err := logisticsService.CreateLogistics(req, database.GetDB(c))
|
|
if err != nil {
|
|
utils.FailWithRequestLog(constant.LoggerChannelWork, "创建物流模板异常", err, c, req)
|
|
return
|
|
}
|
|
|
|
response.OkWithDetailed(gin.H{"id": id}, "创建成功", c)
|
|
}
|
|
|
|
func (r *LogisticsApi) UpdateLogistics(c *gin.Context) {
|
|
var req request.UpdateLogisticsRequest
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
ValidAndFail(constant.LoggerChannelRequest, "更新物流模板请求参数异常", "参数错误: "+err.Error(), c, err)
|
|
return
|
|
}
|
|
|
|
if err := logisticsService.UpdateLogistics(req, database.GetDB(c)); err != nil {
|
|
utils.FailWithRequestLog(constant.LoggerChannelWork, "更新物流模板异常", err, c, req)
|
|
return
|
|
}
|
|
|
|
response.OkWithMessage("更新成功", c)
|
|
}
|
|
|
|
func (r *LogisticsApi) DeleteLogistics(c *gin.Context) {
|
|
var req request.DeleteLogisticsRequest
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
ValidAndFail(constant.LoggerChannelRequest, "删除物流模板请求参数异常", "参数错误: "+err.Error(), c, err)
|
|
return
|
|
}
|
|
|
|
if err := logisticsService.DeleteLogistics(req.Id, database.GetDB(c)); err != nil {
|
|
utils.FailWithRequestLog(constant.LoggerChannelWork, "删除物流模板异常", err, c, req)
|
|
return
|
|
}
|
|
|
|
response.OkWithMessage("删除成功", c)
|
|
}
|