|
@@ -2,6 +2,7 @@ package backend
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "fmt"
|
|
|
"kpt-pasture/model"
|
|
|
"kpt-pasture/util"
|
|
|
"net/http"
|
|
@@ -135,3 +136,114 @@ func (s *StoreEntry) LongTermInfertility(ctx context.Context, req *pasturePb.Lon
|
|
|
}, nil
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+func (s *StoreEntry) AlreadySale(ctx context.Context, req *pasturePb.AlreadySalesReportRequest, pagination *pasturePb.PaginationModel) (*pasturePb.AlreadySalesReportResponse, error) {
|
|
|
+ userModel, err := s.GetUserModel(ctx)
|
|
|
+ if err != nil {
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ pastureId := userModel.AppPasture.Id
|
|
|
+ eventSaleList := make([]*model.EventSale, 0)
|
|
|
+ pref := s.DB.Table(fmt.Sprintf("%s AS a", new(model.EventSale).TableName())).
|
|
|
+ Select("a.id,a.dealer_name,a.sale_kind,a.sale_price,a.sale_all_weight,a.sale_all_amount,sale_cow_count,a.sale_at,a.remarks").
|
|
|
+ Joins(fmt.Sprintf("JOIN %s AS b on b.sale_id = a.id", new(model.EventSaleCow).TableName())).
|
|
|
+ Where("a.pasture_id = ?", pastureId).
|
|
|
+ Where("a.sale_at BETWEEN ? AND ?", req.StartAt, req.EndAt)
|
|
|
+
|
|
|
+ if req.BatchNumber != "" {
|
|
|
+ pref.Where("b.batch_number = ?", req.BatchNumber)
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(req.PenId) > 0 {
|
|
|
+ pref.Where("b.pen_id IN ?", req.PenId)
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.CowKind > pasturePb.CowKind_Invalid {
|
|
|
+ pref.Where("b.cow_kind = ?", req.CowKind)
|
|
|
+ }
|
|
|
+
|
|
|
+ if err = pref.
|
|
|
+ Limit(int(pagination.PageSize)).
|
|
|
+ Offset(int(pagination.PageOffset)).
|
|
|
+ Group("a.id").
|
|
|
+ Find(&eventSaleList).Error; err != nil {
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ result := &pasturePb.AlreadySalesReportResponse{
|
|
|
+ Code: http.StatusOK,
|
|
|
+ Msg: "ok",
|
|
|
+ Data: &pasturePb.AlreadySalesReportData{
|
|
|
+ List: make([]*pasturePb.AlreadySalesReport, 0),
|
|
|
+ Total: 0,
|
|
|
+ PageSize: pagination.PageSize,
|
|
|
+ Page: pagination.Page,
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(eventSaleList) <= 0 {
|
|
|
+ return result, nil
|
|
|
+ }
|
|
|
+
|
|
|
+ saleIds := make([]int64, 0)
|
|
|
+ eventSaleMap := make(map[int64]*model.EventSale)
|
|
|
+ for _, v := range eventSaleList {
|
|
|
+ saleIds = append(saleIds, v.Id)
|
|
|
+ eventSaleMap[v.Id] = v
|
|
|
+ }
|
|
|
+
|
|
|
+ eventSaleCowList := make([]*model.EventSaleCow, 0)
|
|
|
+ if err = s.DB.Model(new(model.EventSaleCow)).
|
|
|
+ Where("sale_id IN ?", saleIds).
|
|
|
+ Where("pasture_id = ?", pastureId).
|
|
|
+ Find(&eventSaleCowList).Error; err != nil {
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ cowKindMap := s.CowKindMap()
|
|
|
+
|
|
|
+ result.Data.Total = int32(len(eventSaleCowList))
|
|
|
+ result.Data.List = model.EventSaleCowSlice(eventSaleCowList).ToPB(eventSaleMap, cowKindMap)
|
|
|
+ return result, nil
|
|
|
+}
|
|
|
+
|
|
|
+func (s *StoreEntry) CanSale(ctx context.Context, req *pasturePb.CanSalesReportRequest, pagination *pasturePb.PaginationModel) (*pasturePb.CanSalesReportResponse, error) {
|
|
|
+ userModel, err := s.GetUserModel(ctx)
|
|
|
+ if err != nil {
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ pastureId := userModel.AppPasture.Id
|
|
|
+ cowList := make([]*model.Cow, 0)
|
|
|
+ var count int64
|
|
|
+ pref := s.DB.Model(new(model.Cow)).
|
|
|
+ Where("pasture_id = ?", pastureId).
|
|
|
+ Where("current_weight BETWEEN ? AND ?", req.WeightStart, req.WeightEnd)
|
|
|
+
|
|
|
+ if err = pref.Count(&count).
|
|
|
+ Limit(int(pagination.PageSize)).
|
|
|
+ Offset(int(pagination.PageOffset)).
|
|
|
+ Find(&cowList).Error; err != nil {
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ result := &pasturePb.CanSalesReportResponse{
|
|
|
+ Code: http.StatusOK,
|
|
|
+ Msg: "ok",
|
|
|
+ Data: &pasturePb.CanSalesReportData{
|
|
|
+ List: make([]*pasturePb.CanSalesReport, 0),
|
|
|
+ Total: 0,
|
|
|
+ PageSize: pagination.PageSize,
|
|
|
+ Page: pagination.Page,
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(cowList) <= 0 {
|
|
|
+ return result, nil
|
|
|
+ }
|
|
|
+ cowKindMap := s.CowKindMap()
|
|
|
+ result.Data.Total = int32(count)
|
|
|
+ result.Data.List = model.CowSlice(cowList).CanSaleToPB(cowKindMap)
|
|
|
+ return result, nil
|
|
|
+}
|