123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- package backend
- import (
- "context"
- "kpt-pasture/model"
- "kpt-pasture/util"
- 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(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
- }
|