12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- package module
- import (
- "context"
- "errors"
- "gitee.com/xuyiping_admin/pkg/xerr"
- "github.com/xormplus/xorm"
- "strconv"
- "time"
- "tmr-watch/http/handle/restful"
- "tmr-watch/models"
- operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
- )
- // SearchTrmGearByTmrId 查询撒料电机档位数据
- func SearchTrmGearByTmrId(ctx context.Context, tmrId int64) ([]*models.TmrGear, error) {
- res := make([]*models.TmrGear, 0)
- if err := restful.Engine.NewSession().Table(new(models.TmrGear).TableName()).Where("is_delete = ?", operationPb.IsShow_OK).
- Where("tmr_id = ?", tmrId).Find(&res); err != nil {
- if !errors.Is(err, xorm.ErrNotExist) {
- return nil, err
- }
- }
- return res, nil
- }
- func CreateOrUpdateTmrGear(ctx context.Context, req *operationPb.UpdateOrCreateTmrGearRequest) error {
- tmrId, _ := strconv.ParseInt(req.TmrId, 10, 64)
- tmrGearList, err := SearchTrmGearByTmrId(ctx, tmrId)
- if err != nil {
- return xerr.WithStack(err)
- }
- tx := restful.Engine.NewSession()
- defer tx.Close()
- if err = tx.Begin(); err != nil {
- return xerr.WithStack(err)
- }
- if len(tmrGearList) >= 1 {
- if _, err = tx.Table(new(models.TmrGear).TableName()).Where("tmr_id = ?", req.TmrId).Update(map[string]interface{}{
- "is_delete": operationPb.IsShow_NO,
- "updated_at": time.Now().Unix(),
- }); err != nil {
- tx.Rollback()
- return xerr.WithStack(err)
- }
- }
- newTmrGearList := models.NewTmrGearList(tmrId, req)
- if _, err = tx.Table(new(models.TmrGear).TableName()).Insert(newTmrGearList); err != nil {
- tx.Rollback()
- return xerr.WithStack(err)
- }
- return tx.Commit()
- }
|