package backend import ( "context" "kpt-pasture/model" "kpt-pasture/util" "net/http" pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow" "gitee.com/xuyiping_admin/pkg/logger/zaplog" "gitee.com/xuyiping_admin/pkg/xerr" "go.uber.org/zap" "gorm.io/gorm" ) func (s *StoreEntry) DepartureBatch(ctx context.Context, req *pasturePb.EventDepartureBatch) (err error) { if len(req.Item) <= 0 { return xerr.Custom("请选择相关牛只") } userModel, err := s.GetUserModel(ctx) if err != nil { return xerr.WithStack(err) } newEventDepartureModelList := make([]*model.EventDepartureModel, 0) cow := &model.Cow{} for _, item := range req.Item { cow, err = s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, int64(item.CowId)) if err != nil { zaplog.Error("DepartureBatch", zap.Any("item", item), zap.Any("err", err)) return xerr.Customf("获取牛只信息失败: %d", item.CowId) } operationUser, err := s.GetSystemUserById(ctx, int64(item.OperationId)) if err != nil { zaplog.Error("DepartureBatch", zap.Any("item", item), zap.Any("err", err)) return xerr.Customf("获取操作人员信息失败: %d", item.OperationId) } reasonName := "" switch item.DepartureType { case pasturePb.DepartureType_Death: reasonName = s.DeadReasonMap()[pasturePb.DeadReason_Kind(item.DepartureReasonKind)] case pasturePb.DepartureType_Out: reasonName = s.OutReasonMap()[pasturePb.OutReason_Kind(item.DepartureReasonKind)] default: return xerr.Custom("未知的离场类型") } newEventDeparture := model.NewEventDeparture(userModel.AppPasture.Id, cow, item, reasonName, userModel.SystemUser, operationUser) newEventDepartureModelList = append(newEventDepartureModelList, &model.EventDepartureModel{ EventDeparture: newEventDeparture, Cow: cow, DepartureAt: int64(item.DepartureAt), }) } if len(newEventDepartureModelList) <= 0 { return nil } if err = s.DB.Transaction(func(tx *gorm.DB) error { // 记录事件日志 for _, item := range newEventDepartureModelList { eventType := pasturePb.EventType_Death if item.EventDeparture.DepartureType == pasturePb.DepartureType_Out { eventType = pasturePb.EventType_Out } if err = tx.Create(item.EventDeparture).Error; err != nil { return xerr.WithStack(err) } cow.EventDepartureUpdate(item.DepartureAt, item.EventDeparture.DepartureType) if err = tx.Model(cow). Select("admission_status", "health_status", "departure_at"). Where("id = ?", cow.Id). Updates(cow).Error; err != nil { return xerr.WithStack(err) } // 记录事件日志 cowLogs := s.SubmitEventLog(ctx, userModel.AppPasture.Id, item.Cow, eventType, pasturePb.ExposeEstrusType_Invalid, item) if err = tx.Table(cowLogs.TableName()).Create(cowLogs).Error; err != nil { return xerr.WithStack(err) } } return nil }); err != nil { return xerr.WithStack(err) } return nil } func (s *StoreEntry) CowEarNumberUpdate(ctx context.Context, req *pasturePb.EventReplaceEarNumber) (err error) { userModel, err := s.GetUserModel(ctx) if err != nil { return xerr.WithStack(err) } cow, err := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, int64(req.CowId)) if err != nil { return xerr.Custom("未找到该牛只信息") } cow.EventEarNumberUpdate(req.EarNumber) if err = s.DB.Model(cow). Select("ear_number", "ear_old_number"). Where("id = ?", cow.Id). Updates(cow).Error; err != nil { return xerr.WithStack(err) } return nil } func (s *StoreEntry) CowSaleCreate(ctx context.Context, req *pasturePb.EventCowSale) error { userModel, err := s.GetUserModel(ctx) if err != nil { return xerr.WithStack(err) } cowIds := make([]int64, 0) if len(req.CowIds) <= 0 { return xerr.Custom("请选择牛只") } for _, v := range req.CowIds { cowIds = append(cowIds, int64(v)) } cowList, err := s.GetCowInfoByCowIds(ctx, userModel.AppPasture.Id, cowIds) if err != nil { return xerr.WithStack(err) } if len(cowList) != len(req.CowIds) { return xerr.Custom("数据异常") } if len(cowList) <= 0 { return nil } dealerInfo := &model.SaleDealer{} if err = s.DB.Model(new(model.SaleDealer)).Where("id = ?", req.DealerId).First(dealerInfo).Error; err != nil { return xerr.Custom("经销商数据异常") } req.DealerName = dealerInfo.Name operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId)) if err != nil { return xerr.Custom("获取操作人员信息失败") } req.OperationName = operationUser.Name cowIdsStr := util.ArrayInt32ToStrings(req.CowIds, ",") newEventSale := model.NewEventSale(userModel.AppPasture.Id, dealerInfo, cowIdsStr, req, operationUser, userModel.SystemUser) eventCowLogList := make([]*model.EventCowLog, 0) for _, cow := range cowList { newEventCowLog := s.SubmitEventLog(ctx, userModel.AppPasture.Id, cow, pasturePb.EventType_Sale, pasturePb.ExposeEstrusType_Invalid, req) eventCowLogList = append(eventCowLogList, newEventCowLog) } if err = s.DB.Transaction(func(tx *gorm.DB) error { // 创建销售数据 if err = tx.Model(new(model.EventSale)).Create(newEventSale).Error; err != nil { return xerr.WithStack(err) } newEventSaleCarList := model.NewEventSaleCarList(userModel.AppPasture.Id, newEventSale.Id, newEventSale.SaleAt, req.SaleVehicleItems) if err = tx.Model(new(model.EventSaleCar)).Create(&newEventSaleCarList).Error; err != nil { return xerr.WithStack(err) } // 更新牛只状态 if err = tx.Model(new(model.Cow)).Where("id IN (?)", cowIds).Update("admission_status", pasturePb.AdmissionStatus_Sale).Error; err != nil { return xerr.WithStack(err) } // 记录日志 if len(eventCowLogList) > 0 { if err = tx.Model(new(model.EventCowLog)).Create(&eventCowLogList).Error; err != nil { return xerr.WithStack(err) } } return nil }); err != nil { return xerr.WithStack(err) } return nil } func (s *StoreEntry) CowSaleList(ctx context.Context, req *pasturePb.EventCowSaleRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EventCowSaleResponse, error) { userModel, err := s.GetUserModel(ctx) if err != nil { return nil, xerr.WithStack(err) } eventSale := make([]*model.EventSale, 0) var count int64 pref := s.DB.Model(new(model.EventSale)).Where("pasture_id = ?", userModel.AppPasture.Id) if req.StartDayAt > 0 && req.EndDayAt > 0 && req.EndDayAt >= req.StartDayAt { pref.Where("sale_at BETWEEN ? AND ?", req.StartDayAt, req.EndDayAt) } if req.DealerId > 0 { pref.Where("dealer_id = ?", req.DealerId) } if err = pref.Order("a.id desc"). Count(&count).Limit(int(pagination.PageSize)). Offset(int(pagination.PageOffset)). Find(&eventSale).Error; err != nil { return nil, xerr.WithStack(err) } saleIds := make([]int64, 0) for _, v := range eventSale { saleIds = append(saleIds, v.Id) } eventSaleCarList := make([]*model.EventSaleCar, 0) if err = s.DB.Model(new(model.EventSaleCar)). Where("pasture_id = ?", userModel.AppPasture.Id). Where("sale_id IN (?)", saleIds). Find(&eventSaleCarList).Error; err != nil { return nil, xerr.WithStack(err) } eventSaleCarMap := make(map[int64][]*model.EventSaleCar) for _, car := range eventSaleCarList { if eventSaleCarMap[car.SaleId] == nil { eventSaleCarMap[car.SaleId] = make([]*model.EventSaleCar, 0) } eventSaleCarMap[car.SaleId] = append(eventSaleCarMap[car.SaleId], car) } return &pasturePb.EventCowSaleResponse{ Code: http.StatusOK, Msg: "ok", Data: &pasturePb.EventCowSaleData{ List: model.EventSaleSlice(eventSale).ToPB(eventSaleCarMap), Total: int32(count), PageSize: pagination.PageSize, Page: pagination.Page, }, }, err }