ListPigSales
This commit is contained in:
@@ -793,3 +793,49 @@ func (c *Controller) ListPigPurchases(ctx *gin.Context) {
|
|||||||
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
|
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
|
||||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪只采购记录成功", resp, actionType, "获取猪只采购记录成功", req)
|
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪只采购记录成功", resp, actionType, "获取猪只采购记录成功", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListPigSales godoc
|
||||||
|
// @Summary 获取猪只售卖记录列表
|
||||||
|
// @Description 根据提供的过滤条件,分页获取猪只售卖记录
|
||||||
|
// @Tags 数据监控
|
||||||
|
// @Security BearerAuth
|
||||||
|
// @Produce json
|
||||||
|
// @Param query query dto.ListPigSaleRequest true "查询参数"
|
||||||
|
// @Success 200 {object} controller.Response{data=dto.ListPigSaleResponse}
|
||||||
|
// @Router /api/v1/monitor/pig-sales [get]
|
||||||
|
func (c *Controller) ListPigSales(ctx *gin.Context) {
|
||||||
|
const actionType = "获取猪只售卖记录列表"
|
||||||
|
|
||||||
|
var req dto.ListPigSaleRequest
|
||||||
|
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.PigSaleListOptions{
|
||||||
|
PigBatchID: req.PigBatchID,
|
||||||
|
Buyer: req.Buyer,
|
||||||
|
OperatorID: req.OperatorID,
|
||||||
|
OrderBy: req.OrderBy,
|
||||||
|
StartTime: req.StartTime,
|
||||||
|
EndTime: req.EndTime,
|
||||||
|
}
|
||||||
|
|
||||||
|
data, total, err := c.monitorService.ListPigSales(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.NewListPigSaleResponse(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,7 +2,6 @@ package dto
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||||
)
|
)
|
||||||
@@ -453,3 +452,32 @@ func NewListPigPurchaseResponse(data []models.PigPurchase, total int64, page, pa
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewListPigSaleResponse 从模型数据创建列表响应 DTO
|
||||||
|
func NewListPigSaleResponse(data []models.PigSale, total int64, page, pageSize int) *ListPigSaleResponse {
|
||||||
|
dtos := make([]PigSaleDTO, len(data))
|
||||||
|
for i, item := range data {
|
||||||
|
dtos[i] = PigSaleDTO{
|
||||||
|
ID: item.ID,
|
||||||
|
CreatedAt: item.CreatedAt,
|
||||||
|
UpdatedAt: item.UpdatedAt,
|
||||||
|
PigBatchID: item.PigBatchID,
|
||||||
|
SaleDate: item.SaleDate,
|
||||||
|
Buyer: item.Buyer,
|
||||||
|
Quantity: item.Quantity,
|
||||||
|
UnitPrice: item.UnitPrice,
|
||||||
|
TotalPrice: item.TotalPrice,
|
||||||
|
Remarks: item.Remarks,
|
||||||
|
OperatorID: item.OperatorID,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &ListPigSaleResponse{
|
||||||
|
List: dtos,
|
||||||
|
Pagination: PaginationDTO{
|
||||||
|
Total: total,
|
||||||
|
Page: page,
|
||||||
|
PageSize: pageSize,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -571,3 +571,38 @@ type ListPigPurchaseResponse struct {
|
|||||||
List []PigPurchaseDTO `json:"list"`
|
List []PigPurchaseDTO `json:"list"`
|
||||||
Pagination PaginationDTO `json:"pagination"`
|
Pagination PaginationDTO `json:"pagination"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- PigSale ---
|
||||||
|
|
||||||
|
// ListPigSaleRequest 定义了获取猪只销售记录列表的请求参数
|
||||||
|
type ListPigSaleRequest struct {
|
||||||
|
Page int `form:"page,default=1"`
|
||||||
|
PageSize int `form:"pageSize,default=10"`
|
||||||
|
PigBatchID *uint `form:"pig_batch_id"`
|
||||||
|
Buyer *string `form:"buyer"`
|
||||||
|
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"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// PigSaleDTO 是用于API响应的猪只销售记录结构
|
||||||
|
type PigSaleDTO struct {
|
||||||
|
ID uint `json:"id"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
PigBatchID uint `json:"pig_batch_id"`
|
||||||
|
SaleDate time.Time `json:"sale_date"`
|
||||||
|
Buyer string `json:"buyer"`
|
||||||
|
Quantity int `json:"quantity"`
|
||||||
|
UnitPrice float64 `json:"unit_price"`
|
||||||
|
TotalPrice float64 `json:"total_price"`
|
||||||
|
Remarks string `json:"remarks"`
|
||||||
|
OperatorID uint `json:"operator_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListPigSaleResponse 是获取猪只销售记录列表的响应结构
|
||||||
|
type ListPigSaleResponse struct {
|
||||||
|
List []PigSaleDTO `json:"list"`
|
||||||
|
Pagination PaginationDTO `json:"pagination"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -131,3 +131,8 @@ func (s *MonitorService) ListPigSickLogs(opts repository.PigSickLogListOptions,
|
|||||||
func (s *MonitorService) ListPigPurchases(opts repository.PigPurchaseListOptions, page, pageSize int) ([]models.PigPurchase, int64, error) {
|
func (s *MonitorService) ListPigPurchases(opts repository.PigPurchaseListOptions, page, pageSize int) ([]models.PigPurchase, int64, error) {
|
||||||
return s.pigTradeRepo.ListPigPurchases(opts, page, pageSize)
|
return s.pigTradeRepo.ListPigPurchases(opts, page, pageSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListPigSales 负责处理查询猪只销售记录列表的业务逻辑
|
||||||
|
func (s *MonitorService) ListPigSales(opts repository.PigSaleListOptions, page, pageSize int) ([]models.PigSale, int64, error) {
|
||||||
|
return s.pigTradeRepo.ListPigSales(opts, page, pageSize)
|
||||||
|
}
|
||||||
|
|||||||
@@ -17,6 +17,16 @@ type PigPurchaseListOptions struct {
|
|||||||
OrderBy string // 例如 "purchase_date desc"
|
OrderBy string // 例如 "purchase_date desc"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PigSaleListOptions 定义了查询猪只销售记录时的可选参数
|
||||||
|
type PigSaleListOptions struct {
|
||||||
|
PigBatchID *uint
|
||||||
|
Buyer *string
|
||||||
|
OperatorID *uint
|
||||||
|
StartTime *time.Time // 基于 sale_date 字段
|
||||||
|
EndTime *time.Time // 基于 sale_date 字段
|
||||||
|
OrderBy string // 例如 "sale_date desc"
|
||||||
|
}
|
||||||
|
|
||||||
// PigTradeRepository 定义了猪只交易数据持久化的接口。
|
// PigTradeRepository 定义了猪只交易数据持久化的接口。
|
||||||
// 领域服务通过此接口与数据层交互,实现解耦。
|
// 领域服务通过此接口与数据层交互,实现解耦。
|
||||||
type PigTradeRepository interface {
|
type PigTradeRepository interface {
|
||||||
@@ -28,6 +38,9 @@ type PigTradeRepository interface {
|
|||||||
|
|
||||||
// ListPigPurchases 支持分页和过滤的猪只采购记录列表查询
|
// ListPigPurchases 支持分页和过滤的猪只采购记录列表查询
|
||||||
ListPigPurchases(opts PigPurchaseListOptions, page, pageSize int) ([]models.PigPurchase, int64, error)
|
ListPigPurchases(opts PigPurchaseListOptions, page, pageSize int) ([]models.PigPurchase, int64, error)
|
||||||
|
|
||||||
|
// ListPigSales 支持分页和过滤的猪只销售记录列表查询
|
||||||
|
ListPigSales(opts PigSaleListOptions, page, pageSize int) ([]models.PigSale, int64, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// gormPigTradeRepository 是 PigTradeRepository 接口的 GORM 实现。
|
// gormPigTradeRepository 是 PigTradeRepository 接口的 GORM 实现。
|
||||||
@@ -92,3 +105,46 @@ func (r *gormPigTradeRepository) ListPigPurchases(opts PigPurchaseListOptions, p
|
|||||||
|
|
||||||
return results, total, err
|
return results, total, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListPigSales 实现了分页和过滤查询猪只销售记录的功能
|
||||||
|
func (r *gormPigTradeRepository) ListPigSales(opts PigSaleListOptions, page, pageSize int) ([]models.PigSale, int64, error) {
|
||||||
|
if page <= 0 || pageSize <= 0 {
|
||||||
|
return nil, 0, ErrInvalidPagination
|
||||||
|
}
|
||||||
|
|
||||||
|
var results []models.PigSale
|
||||||
|
var total int64
|
||||||
|
|
||||||
|
query := r.db.Model(&models.PigSale{})
|
||||||
|
|
||||||
|
if opts.PigBatchID != nil {
|
||||||
|
query = query.Where("pig_batch_id = ?", *opts.PigBatchID)
|
||||||
|
}
|
||||||
|
if opts.Buyer != nil {
|
||||||
|
query = query.Where("buyer LIKE ?", "%"+*opts.Buyer+"%")
|
||||||
|
}
|
||||||
|
if opts.OperatorID != nil {
|
||||||
|
query = query.Where("operator_id = ?", *opts.OperatorID)
|
||||||
|
}
|
||||||
|
if opts.StartTime != nil {
|
||||||
|
query = query.Where("sale_date >= ?", *opts.StartTime)
|
||||||
|
}
|
||||||
|
if opts.EndTime != nil {
|
||||||
|
query = query.Where("sale_date <= ?", *opts.EndTime)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := query.Count(&total).Error; err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
orderBy := "sale_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