|
@@ -6,17 +6,19 @@ import (
|
|
|
"kpt-pasture/model"
|
|
|
"net/http"
|
|
|
|
|
|
- "gorm.io/gorm"
|
|
|
-
|
|
|
pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
|
|
|
"gitee.com/xuyiping_admin/pkg/xerr"
|
|
|
+ "gorm.io/gorm"
|
|
|
)
|
|
|
|
|
|
-func (s *StoreEntry) EnterList(ctx context.Context, req *pasturePb.SearchEnterEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchEnterEventResponse, error) {
|
|
|
+func (s *StoreEntry) EnterList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchEnterEventResponse, error) {
|
|
|
eventEnterList := make([]*pasturePb.SearchEnterData, 0)
|
|
|
var count int64 = 0
|
|
|
pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventEnter).TableName())).
|
|
|
- Select("a.id,a.birth_at,a.weaning_at,a.dry_milk_at,a.cow_source_id,a.remarks,a.mating_at,a.status,a.lact,a.breed_status_id,a.mother_id,a.cow_kind_id,a.cow_id,b.name as breed_status_name,c.name as cow_source_name,d.name as cow_type_name,e.name as cow_kind_name,f.name as pen_name,g.name as operation_name").
|
|
|
+ Select(`a.id,a.birth_at,a.weaning_at,a.dry_milk_at,a.cow_source_id,a.remarks,a.mating_at,a.status,
|
|
|
+ a.lact,a.breed_status_id,a.mother_id,a.cow_kind_id,a.cow_id,a.ear_number,a.sex,a.created_at,
|
|
|
+ a.weight / 100 as weight,a.price / 100 as price,b.name as breed_status_name,
|
|
|
+ c.name as cow_source_name,d.name as cow_type_name,e.name as cow_kind_name,f.name as pen_name,g.name as operation_name`).
|
|
|
Joins(fmt.Sprintf("JOIN %s AS b ON a.breed_status_id = b.id", new(model.ConfigBreedStatus).TableName())).
|
|
|
Joins(fmt.Sprintf("JOIN %s AS c on a.cow_source_id = c.id", new(model.ConfigCowSource).TableName())).
|
|
|
Joins(fmt.Sprintf("JOIN %s AS d on a.cow_type_id = d.id", new(model.ConfigCowType).TableName())).
|
|
@@ -64,3 +66,102 @@ func (s *StoreEntry) CreateEnter(ctx context.Context, req *pasturePb.SearchEnter
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
+
|
|
|
+func (s *StoreEntry) GroupTransferList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchTransferGroupEventResponse, error) {
|
|
|
+ eventGroupTransferList := make([]*pasturePb.SearchTransferGroupData, 0)
|
|
|
+ var count int64 = 0
|
|
|
+ pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventTransferGroup).TableName())).
|
|
|
+ Select(`a.id,a.cow_id,a.transfer_in_pen_id,a.transfer_out_pen_id,a.lact,a.remarks,a.transfer_reason_id,
|
|
|
+ a.transfer_date,a.created_at,a.staff_member_id,b.name as transfer_in_pen_name,c.name as transfer_out_pen_name,
|
|
|
+ d.name as transfer_reason_name,e.name as staff_member_name,f.lact,f.ear_number`).
|
|
|
+ Joins(fmt.Sprintf("JOIN %s AS b ON a.transfer_in_pen_id = b.id", new(model.Pen).TableName())).
|
|
|
+ Joins(fmt.Sprintf("JOIN %s AS c on a.transfer_out_pen_id = c.id", new(model.Pen).TableName())).
|
|
|
+ Joins(fmt.Sprintf("JOIN %s AS d on a.transfer_reason_id = d.id", new(model.ConfigTransferPenReason).TableName())).
|
|
|
+ Joins(fmt.Sprintf("JOIN %s AS e ON a.staff_member_id = e.id", new(model.SystemUser).TableName())).
|
|
|
+ Joins(fmt.Sprintf("JOIN %s AS f ON a.cow_id = b.id", new(model.Cow).TableName()))
|
|
|
+ if req.CowId > 0 {
|
|
|
+ pref.Where("a.cow_id = ?", req.CowId)
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := pref.Order("a.id desc").Group("a.id").
|
|
|
+ Count(&count).Limit(int(pagination.PageSize)).
|
|
|
+ Offset(int(pagination.PageOffset)).
|
|
|
+ Find(&eventGroupTransferList).Error; err != nil {
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ return &pasturePb.SearchTransferGroupEventResponse{
|
|
|
+ Code: http.StatusOK,
|
|
|
+ Message: "ok",
|
|
|
+ Data: &pasturePb.SearchTransferGroupEventData{
|
|
|
+ List: eventGroupTransferList,
|
|
|
+ Total: int32(count),
|
|
|
+ PageSize: pagination.PageSize,
|
|
|
+ Page: pagination.Page,
|
|
|
+ },
|
|
|
+ }, nil
|
|
|
+}
|
|
|
+
|
|
|
+func (s *StoreEntry) CreateGroupTransfer(ctx context.Context, req *pasturePb.TransferGroupEventRequest) error {
|
|
|
+ system, _ := s.GetCurrentSystemUser(ctx)
|
|
|
+ res := make([]*model.EventTransferGroup, 0)
|
|
|
+ for _, v := range req.Body {
|
|
|
+ cow, err := s.GetCowInfo(ctx, int64(v.CowId))
|
|
|
+ if err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ // 转去栏舍和当前栏舍相同,则不处理
|
|
|
+ if cow.PenId == int64(v.TransferInPenId) {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ pen, err := s.GetPenInfo(ctx, int64(v.TransferInPenId))
|
|
|
+ if err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ transferReasonData, err := s.GetTransferReasonInfo(ctx, int64(v.TransferReasonId))
|
|
|
+ if err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ staffMemberData, err := s.GetSystemUserInfo(ctx, int64(v.StaffMemberId))
|
|
|
+ if err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ res = append(res, &model.EventTransferGroup{
|
|
|
+ CowId: int64(v.CowId),
|
|
|
+ TransferInPenId: int64(v.TransferInPenId),
|
|
|
+ TransferOutPenId: pen.Id,
|
|
|
+ Lact: cow.Lact,
|
|
|
+ TransferDate: v.TransferDate,
|
|
|
+ TransferReasonId: transferReasonData.Id,
|
|
|
+ Remarks: v.Remarks,
|
|
|
+ StaffMemberId: staffMemberData.Id,
|
|
|
+ OperationId: system.Id,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ if len(res) <= 0 {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
+ if err := tx.Create(res).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ for _, v := range res {
|
|
|
+ cow, err := s.GetCowInfo(ctx, v.CowId)
|
|
|
+ if err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ if err = s.DB.Model(cow).Update("pen_id", v.TransferInPenId).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ }); err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|