|
@@ -5,6 +5,9 @@ import (
|
|
|
"fmt"
|
|
|
"kpt-pasture/model"
|
|
|
"net/http"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ "gorm.io/gorm"
|
|
|
|
|
|
"gitee.com/xuyiping_admin/pkg/xerr"
|
|
|
|
|
@@ -93,16 +96,110 @@ func (s *StoreEntry) MedicalEquipmentList(ctx context.Context, req *pasturePb.Se
|
|
|
}
|
|
|
|
|
|
func (s *StoreEntry) MedicalEquipmentCreateOrUpdate(ctx context.Context, req *pasturePb.SearchMedicalEquipmentList) error {
|
|
|
- currentUser, _ := s.GetCurrentSystemUser(ctx)
|
|
|
+ currentUser, err := s.GetCurrentSystemUser(ctx)
|
|
|
+ if err != nil {
|
|
|
+ return xerr.Custom("登录人信息失效")
|
|
|
+ }
|
|
|
newDrugs := model.NewMedicalEquipment(req, currentUser)
|
|
|
if req.Id <= 0 {
|
|
|
- if err := s.DB.Create(newDrugs).Error; err != nil {
|
|
|
+ if err = s.DB.Create(newDrugs).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
} else {
|
|
|
- if err := s.DB.Where("id = ?", req.Id).Updates(newDrugs).Error; err != nil {
|
|
|
+ if err = s.DB.Where("id = ?", req.Id).Updates(newDrugs).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
+
|
|
|
+func (s *StoreEntry) NeckRingLogCreateOrUpdate(ctx context.Context, req *pasturePb.NeckRingCreateRequest) error {
|
|
|
+ currentUser, err := s.GetCurrentSystemUser(ctx)
|
|
|
+ if err != nil {
|
|
|
+ return xerr.Custom("登录人信息失效")
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.Items == nil || len(req.Items) == 0 {
|
|
|
+ return xerr.Custom("请选择要脖环数据")
|
|
|
+ }
|
|
|
+
|
|
|
+ newNeckRingLogList := model.NewNeckRingLogList(req.Items, currentUser)
|
|
|
+ cowIds := make([]int64, 0)
|
|
|
+ for _, v := range newNeckRingLogList {
|
|
|
+ cowIds = append(cowIds, v.CowId)
|
|
|
+ }
|
|
|
+
|
|
|
+ if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
+ // 解绑脖环号
|
|
|
+ if err = tx.Model(new(model.NeckRingLog)).
|
|
|
+ Where("cowId IN ?", cowIds).
|
|
|
+ Updates(map[string]interface{}{
|
|
|
+ "unbind_at": time.Now().Unix(),
|
|
|
+ "status": pasturePb.NeckRingStatus_Unbind,
|
|
|
+ "operation_id": currentUser.Id,
|
|
|
+ "operation_name": currentUser.Name,
|
|
|
+ }).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ // 绑定新脖环
|
|
|
+ if err = tx.Create(newNeckRingLogList).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, v := range req.Items {
|
|
|
+ if v.CowId > 0 {
|
|
|
+ if err = tx.Model(new(model.Cow)).
|
|
|
+ Where("id = ?", v.CowId).
|
|
|
+ Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
|
|
|
+ Updates(map[string]interface{}{
|
|
|
+ "neck_ring_number": v.Number,
|
|
|
+ }).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+ }); err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func (s *StoreEntry) NeckRingLogList(ctx context.Context, req *pasturePb.SearchNeckRingRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchNeckRingResponse, error) {
|
|
|
+ neckRingLogList := make([]*model.NeckRingLog, 0)
|
|
|
+ var count int64 = 0
|
|
|
+
|
|
|
+ pref := s.DB.Model(new(model.NeckRingLog)).Where("status > ?", pasturePb.NeckRingStatus_Unbind)
|
|
|
+ if req.Status > 0 {
|
|
|
+ pref.Where("status = ?", req.Status)
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.CowId > 0 {
|
|
|
+ pref.Where("cow_id = ?", req.CowId)
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.Number != "" {
|
|
|
+ pref.Where("number like ?", fmt.Sprintf("%s%s%s", "%", req.Number, "%"))
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := pref.Order("id desc").
|
|
|
+ Count(&count).
|
|
|
+ Limit(int(pagination.PageSize)).
|
|
|
+ Offset(int(pagination.PageOffset)).
|
|
|
+ Find(&neckRingLogList).Error; err != nil {
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ neckRingStatusMap := s.NeckRingStatusMap()
|
|
|
+ return &pasturePb.SearchNeckRingResponse{
|
|
|
+ Code: http.StatusOK,
|
|
|
+ Message: "ok",
|
|
|
+ Data: &pasturePb.SearchNeckRingData{
|
|
|
+ List: model.NeckRingLogSlice(neckRingLogList).ToPB(neckRingStatusMap),
|
|
|
+ Total: int32(count),
|
|
|
+ PageSize: pagination.PageSize,
|
|
|
+ Page: pagination.Page,
|
|
|
+ },
|
|
|
+ }, nil
|
|
|
+}
|