package module import ( "context" "errors" "fmt" operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation" "gitee.com/xuyiping_admin/pkg/xerr" "github.com/xormplus/xorm" "go.uber.org/zap" "strconv" "tmr-watch/http/handle/restful" "tmr-watch/models" "tmr-watch/pkg/logger/zaplog" ) // 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("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).Delete(new(models.TmrGear)); 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() } func UseTmrGear(ctx context.Context, req *operationPb.UseGearRequest) ([]*models.TmrGearDetail, error) { sqlName := fmt.Sprintf( `SELECT sort,fname,weight,length,bcode,ccount,speed,gear_rate,useinbar,pid,fbarid FROM (SELECT a.sort,a.fname,a.lweight weight,a.fpid, IF(a.useinbartype=0,'转投剩料', IF(a.useinbartype=1,'撒','继续饲喂') ) useinbar, CONCAT(a.fbarid) fbarid, CONCAT(a.pid) pid, b.bcode, b.length, c.cowcount AS ccount, ROUND( c.ccountradio * c.cowcount / 100, 2 ) ccountratio, d.speed, (((a.lweight / b.length) * f.volume) / d.volume_rate) / (1 / (d.speed * 1000 / 3600)) * 60 AS gear_rate FROM downloadplandtl2 a JOIN bar b ON a.fbarid = b.id JOIN fpdetaildate c ON c.barid = a.fbarid JOIN downloadedplan g ON a.pid = g.id JOIN tmr d ON d.id = g.tmrid JOIN feedtemplet f ON a.feedtempletid = f.id WHERE a.pastureid = %s AND a.pid= %s AND d.id = %d GROUP BY a.sort ORDER BY a.sort ) AS B`, req.PastureId, req.Id, req.TmrId) tx := restful.Engine.NewSession() defer tx.Close() resultList, err := tx.QueryString(sqlName) if err != nil { return nil, xerr.WithStack(err) } zaplog.Info("UseTmrGear", zap.Any("resultList", resultList), zap.Any("sqlName", sqlName)) tmrGearDetailList := make([]*models.TmrGearDetail, 0) for _, v := range resultList { sort, _ := strconv.ParseInt(v["sort"], 10, 64) fname := v["fname"] bcode := v["bcode"] weight, _ := strconv.ParseFloat(v["weight"], 64) length, _ := strconv.ParseInt(v["length"], 10, 64) ccount, _ := strconv.ParseInt(v["ccount"], 10, 64) speed, _ := strconv.ParseFloat(v["speed"], 64) gearRate, _ := strconv.ParseFloat(v["gear_rate"], 64) pid, _ := strconv.ParseInt(v["pid"], 10, 64) fbarid, _ := strconv.ParseInt(v["fbarid"], 10, 64) tmrGearDetailList = append(tmrGearDetailList, &models.TmrGearDetail{ Sort: int32(sort), FName: fname, Weight: weight, Length: int32(length), BCode: bcode, CCount: int32(ccount), Speed: speed, GearRate: gearRate, Pid: pid, FBarid: fbarid, }) } return tmrGearDetailList, nil } func TmrGearListByTmrId(tmrId int64) ([]*models.TmrGear, error) { tmrGearList := make([]*models.TmrGear, 0) tx := restful.Engine.NewSession() defer tx.Close() err := tx.Table(new(models.TmrGear).TableName()).Where("tmr_id = ?", tmrId).Select("*").Find(&tmrGearList) if err != nil { return nil, xerr.WithStack(err) } return tmrGearList, nil }