ListPigPurchases
This commit is contained in:
@@ -747,3 +747,49 @@ func (c *Controller) ListPigSickLogs(ctx *gin.Context) {
|
||||
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取病猪日志成功", resp, actionType, "获取病猪日志成功", req)
|
||||
}
|
||||
|
||||
// ListPigPurchases godoc
|
||||
// @Summary 获取猪只采购记录列表
|
||||
// @Description 根据提供的过滤条件,分页获取猪只采购记录
|
||||
// @Tags 数据监控
|
||||
// @Security BearerAuth
|
||||
// @Produce json
|
||||
// @Param query query dto.ListPigPurchaseRequest true "查询参数"
|
||||
// @Success 200 {object} controller.Response{data=dto.ListPigPurchaseResponse}
|
||||
// @Router /api/v1/monitor/pig-purchases [get]
|
||||
func (c *Controller) ListPigPurchases(ctx *gin.Context) {
|
||||
const actionType = "获取猪只采购记录列表"
|
||||
|
||||
var req dto.ListPigPurchaseRequest
|
||||
if err := ctx.ShouldBindQuery(&req); err != nil {
|
||||
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
|
||||
return
|
||||
}
|
||||
|
||||
opts := repository.PigPurchaseListOptions{
|
||||
PigBatchID: req.PigBatchID,
|
||||
Supplier: req.Supplier,
|
||||
OperatorID: req.OperatorID,
|
||||
OrderBy: req.OrderBy,
|
||||
StartTime: req.StartTime,
|
||||
EndTime: req.EndTime,
|
||||
}
|
||||
|
||||
data, total, err := c.monitorService.ListPigPurchases(opts, req.Page, req.PageSize)
|
||||
if err != nil {
|
||||
if errors.Is(err, repository.ErrInvalidPagination) {
|
||||
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
|
||||
return
|
||||
}
|
||||
|
||||
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪只采购记录失败: "+err.Error(), actionType, "服务层查询失败", req)
|
||||
return
|
||||
}
|
||||
|
||||
resp := dto.NewListPigPurchaseResponse(data, total, req.Page, req.PageSize)
|
||||
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪只采购记录成功", resp, actionType, "获取猪只采购记录成功", req)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package dto
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
)
|
||||
@@ -423,3 +424,32 @@ func NewListPigSickLogResponse(data []models.PigSickLog, total int64, page, page
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// NewListPigPurchaseResponse 从模型数据创建列表响应 DTO
|
||||
func NewListPigPurchaseResponse(data []models.PigPurchase, total int64, page, pageSize int) *ListPigPurchaseResponse {
|
||||
dtos := make([]PigPurchaseDTO, len(data))
|
||||
for i, item := range data {
|
||||
dtos[i] = PigPurchaseDTO{
|
||||
ID: item.ID,
|
||||
CreatedAt: item.CreatedAt,
|
||||
UpdatedAt: item.UpdatedAt,
|
||||
PigBatchID: item.PigBatchID,
|
||||
PurchaseDate: item.PurchaseDate,
|
||||
Supplier: item.Supplier,
|
||||
Quantity: item.Quantity,
|
||||
UnitPrice: item.UnitPrice,
|
||||
TotalPrice: item.TotalPrice,
|
||||
Remarks: item.Remarks,
|
||||
OperatorID: item.OperatorID,
|
||||
}
|
||||
}
|
||||
|
||||
return &ListPigPurchaseResponse{
|
||||
List: dtos,
|
||||
Pagination: PaginationDTO{
|
||||
Total: total,
|
||||
Page: page,
|
||||
PageSize: pageSize,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -536,3 +536,38 @@ type ListPigSickLogResponse struct {
|
||||
List []PigSickLogDTO `json:"list"`
|
||||
Pagination PaginationDTO `json:"pagination"`
|
||||
}
|
||||
|
||||
// --- PigPurchase ---
|
||||
|
||||
// ListPigPurchaseRequest 定义了获取猪只采购记录列表的请求参数
|
||||
type ListPigPurchaseRequest struct {
|
||||
Page int `form:"page,default=1"`
|
||||
PageSize int `form:"pageSize,default=10"`
|
||||
PigBatchID *uint `form:"pig_batch_id"`
|
||||
Supplier *string `form:"supplier"`
|
||||
OperatorID *uint `form:"operator_id"`
|
||||
StartTime *time.Time `form:"start_time" time_format:"rfc3339"`
|
||||
EndTime *time.Time `form:"end_time" time_format:"rfc3339"`
|
||||
OrderBy string `form:"order_by"`
|
||||
}
|
||||
|
||||
// PigPurchaseDTO 是用于API响应的猪只采购记录结构
|
||||
type PigPurchaseDTO struct {
|
||||
ID uint `json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
PigBatchID uint `json:"pig_batch_id"`
|
||||
PurchaseDate time.Time `json:"purchase_date"`
|
||||
Supplier string `json:"supplier"`
|
||||
Quantity int `json:"quantity"`
|
||||
UnitPrice float64 `json:"unit_price"`
|
||||
TotalPrice float64 `json:"total_price"`
|
||||
Remarks string `json:"remarks"`
|
||||
OperatorID uint `json:"operator_id"`
|
||||
}
|
||||
|
||||
// ListPigPurchaseResponse 是获取猪只采购记录列表的响应结构
|
||||
type ListPigPurchaseResponse struct {
|
||||
List []PigPurchaseDTO `json:"list"`
|
||||
Pagination PaginationDTO `json:"pagination"`
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ type MonitorService struct {
|
||||
pigBatchLogRepo repository.PigBatchLogRepository
|
||||
pigTransferLogRepo repository.PigTransferLogRepository
|
||||
pigSickLogRepo repository.PigSickLogRepository
|
||||
pigTradeRepo repository.PigTradeRepository
|
||||
}
|
||||
|
||||
// NewMonitorService 创建一个新的 MonitorService 实例
|
||||
@@ -33,6 +34,7 @@ func NewMonitorService(
|
||||
pigBatchLogRepo repository.PigBatchLogRepository,
|
||||
pigTransferLogRepo repository.PigTransferLogRepository,
|
||||
pigSickLogRepo repository.PigSickLogRepository,
|
||||
pigTradeRepo repository.PigTradeRepository,
|
||||
) *MonitorService {
|
||||
return &MonitorService{
|
||||
sensorDataRepo: sensorDataRepo,
|
||||
@@ -46,6 +48,7 @@ func NewMonitorService(
|
||||
pigBatchLogRepo: pigBatchLogRepo,
|
||||
pigTransferLogRepo: pigTransferLogRepo,
|
||||
pigSickLogRepo: pigSickLogRepo,
|
||||
pigTradeRepo: pigTradeRepo,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,3 +126,8 @@ func (s *MonitorService) ListPigTransferLogs(opts repository.PigTransferLogListO
|
||||
func (s *MonitorService) ListPigSickLogs(opts repository.PigSickLogListOptions, page, pageSize int) ([]models.PigSickLog, int64, error) {
|
||||
return s.pigSickLogRepo.ListPigSickLogs(opts, page, pageSize)
|
||||
}
|
||||
|
||||
// ListPigPurchases 负责处理查询猪只采购记录列表的业务逻辑
|
||||
func (s *MonitorService) ListPigPurchases(opts repository.PigPurchaseListOptions, page, pageSize int) ([]models.PigPurchase, int64, error) {
|
||||
return s.pigTradeRepo.ListPigPurchases(opts, page, pageSize)
|
||||
}
|
||||
|
||||
@@ -1,10 +1,22 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// PigPurchaseListOptions 定义了查询猪只采购记录时的可选参数
|
||||
type PigPurchaseListOptions struct {
|
||||
PigBatchID *uint
|
||||
Supplier *string
|
||||
OperatorID *uint
|
||||
StartTime *time.Time // 基于 purchase_date 字段
|
||||
EndTime *time.Time // 基于 purchase_date 字段
|
||||
OrderBy string // 例如 "purchase_date desc"
|
||||
}
|
||||
|
||||
// PigTradeRepository 定义了猪只交易数据持久化的接口。
|
||||
// 领域服务通过此接口与数据层交互,实现解耦。
|
||||
type PigTradeRepository interface {
|
||||
@@ -13,6 +25,9 @@ type PigTradeRepository interface {
|
||||
|
||||
// CreatePigPurchaseTx 在数据库中创建一条猪只采购记录。
|
||||
CreatePigPurchaseTx(tx *gorm.DB, purchase *models.PigPurchase) error
|
||||
|
||||
// ListPigPurchases 支持分页和过滤的猪只采购记录列表查询
|
||||
ListPigPurchases(opts PigPurchaseListOptions, page, pageSize int) ([]models.PigPurchase, int64, error)
|
||||
}
|
||||
|
||||
// gormPigTradeRepository 是 PigTradeRepository 接口的 GORM 实现。
|
||||
@@ -34,3 +49,46 @@ func (r *gormPigTradeRepository) CreatePigSaleTx(tx *gorm.DB, sale *models.PigSa
|
||||
func (r *gormPigTradeRepository) CreatePigPurchaseTx(tx *gorm.DB, purchase *models.PigPurchase) error {
|
||||
return tx.Create(purchase).Error
|
||||
}
|
||||
|
||||
// ListPigPurchases 实现了分页和过滤查询猪只采购记录的功能
|
||||
func (r *gormPigTradeRepository) ListPigPurchases(opts PigPurchaseListOptions, page, pageSize int) ([]models.PigPurchase, int64, error) {
|
||||
if page <= 0 || pageSize <= 0 {
|
||||
return nil, 0, ErrInvalidPagination
|
||||
}
|
||||
|
||||
var results []models.PigPurchase
|
||||
var total int64
|
||||
|
||||
query := r.db.Model(&models.PigPurchase{})
|
||||
|
||||
if opts.PigBatchID != nil {
|
||||
query = query.Where("pig_batch_id = ?", *opts.PigBatchID)
|
||||
}
|
||||
if opts.Supplier != nil {
|
||||
query = query.Where("supplier LIKE ?", "%"+*opts.Supplier+"%")
|
||||
}
|
||||
if opts.OperatorID != nil {
|
||||
query = query.Where("operator_id = ?", *opts.OperatorID)
|
||||
}
|
||||
if opts.StartTime != nil {
|
||||
query = query.Where("purchase_date >= ?", *opts.StartTime)
|
||||
}
|
||||
if opts.EndTime != nil {
|
||||
query = query.Where("purchase_date <= ?", *opts.EndTime)
|
||||
}
|
||||
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
orderBy := "purchase_date DESC"
|
||||
if opts.OrderBy != "" {
|
||||
orderBy = opts.OrderBy
|
||||
}
|
||||
query = query.Order(orderBy)
|
||||
|
||||
offset := (page - 1) * pageSize
|
||||
err := query.Limit(pageSize).Offset(offset).Find(&results).Error
|
||||
|
||||
return results, total, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user