sheep.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package module
  2. import (
  3. "context"
  4. "errors"
  5. "gitee.com/xuyiping_admin/pkg/xerr"
  6. "github.com/xormplus/xorm"
  7. "strconv"
  8. "time"
  9. "tmr-watch/http/handle/restful"
  10. "tmr-watch/models"
  11. operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
  12. )
  13. // SearchTrmGearByTmrId 查询撒料电机档位数据
  14. func SearchTrmGearByTmrId(ctx context.Context, tmrId int64) ([]*models.TmrGear, error) {
  15. res := make([]*models.TmrGear, 0)
  16. if err := restful.Engine.NewSession().Table(new(models.TmrGear).TableName()).Where("is_delete = ?", operationPb.IsShow_OK).
  17. Where("tmr_id = ?", tmrId).Find(&res); err != nil {
  18. if !errors.Is(err, xorm.ErrNotExist) {
  19. return nil, err
  20. }
  21. }
  22. return res, nil
  23. }
  24. func CreateOrUpdateTmrGear(ctx context.Context, req *operationPb.UpdateOrCreateTmrGearRequest) error {
  25. tmrId, _ := strconv.ParseInt(req.TmrId, 10, 64)
  26. tmrGearList, err := SearchTrmGearByTmrId(ctx, tmrId)
  27. if err != nil {
  28. return xerr.WithStack(err)
  29. }
  30. tx := restful.Engine.NewSession()
  31. defer tx.Close()
  32. if err = tx.Begin(); err != nil {
  33. return xerr.WithStack(err)
  34. }
  35. if len(tmrGearList) >= 1 {
  36. if _, err = tx.Table(new(models.TmrGear).TableName()).Where("tmr_id = ?", req.TmrId).Update(map[string]interface{}{
  37. "is_delete": operationPb.IsShow_NO,
  38. "updated_at": time.Now().Unix(),
  39. }); err != nil {
  40. tx.Rollback()
  41. return xerr.WithStack(err)
  42. }
  43. }
  44. newTmrGearList := models.NewTmrGearList(tmrId, req)
  45. if _, err = tx.Table(new(models.TmrGear).TableName()).Insert(newTmrGearList); err != nil {
  46. tx.Rollback()
  47. return xerr.WithStack(err)
  48. }
  49. return tx.Commit()
  50. }