tmr_gear.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package sheep
  2. import (
  3. "context"
  4. operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
  5. "gitee.com/xuyiping_admin/pkg/xerr"
  6. "net/http"
  7. "tmr-watch/module"
  8. )
  9. func UpdateOrCreateTmrGear(ctx context.Context, req *operationPb.UpdateOrCreateTmrGearRequest) error {
  10. if len(req.Params) <= 0 {
  11. return xerr.Customf("参数错误: params")
  12. }
  13. if len(req.TmrId) <= 0 {
  14. return xerr.Customf("参数错误: tmr_id")
  15. }
  16. revList := make(map[int32]struct{})
  17. for _, v := range req.Params {
  18. if _, ok := revList[v.Rev]; ok {
  19. return xerr.Customf("不能出现同样转速的数据")
  20. }
  21. revList[v.Rev] = struct{}{}
  22. }
  23. if err := module.CreateOrUpdateTmrGear(ctx, req); err != nil {
  24. return xerr.WithStack(err)
  25. }
  26. return nil
  27. }
  28. func UseGear(ctx context.Context, req *operationPb.UseGearRequest) (*operationPb.UseGearResponse, error) {
  29. list, err := module.UseTmrGear(ctx, req)
  30. if err != nil {
  31. return nil, xerr.WithStack(err)
  32. }
  33. tmrGearList, err := module.TmrGearListByTmrId(int64(req.TmrId))
  34. if err != nil {
  35. return nil, xerr.WithStack(err)
  36. }
  37. gear := "0"
  38. useGearDataList := make([]*operationPb.UseGearDataList, 0)
  39. for _, v := range list {
  40. if len(tmrGearList) > 0 {
  41. for i := 0; i < len(tmrGearList)-1; i++ {
  42. if v.GearRate > float64(tmrGearList[i].Rev) && v.GearRate <= float64(tmrGearList[i+1].Rev) {
  43. gear = tmrGearList[i+1].Gear
  44. }
  45. }
  46. if v.GearRate <= float64(tmrGearList[0].Rev) {
  47. gear = tmrGearList[0].Gear
  48. }
  49. if v.GearRate >= float64(tmrGearList[len(tmrGearList)-1].Rev) {
  50. gear = tmrGearList[len(tmrGearList)-1].Gear
  51. }
  52. }
  53. newData := &operationPb.UseGearDataList{
  54. Bcode: v.BCode,
  55. Fname: v.FName,
  56. Length: v.Length,
  57. Sort: v.Sort,
  58. Speed: float32(v.Speed),
  59. Useinbar: v.UseInBar,
  60. Weight: float32(v.Weight),
  61. Gear: gear,
  62. Ccount: v.CCount,
  63. GearRate: float32(v.GearRate),
  64. Pid: v.Pid,
  65. Fbarid: v.FBarid,
  66. }
  67. useGearDataList = append(useGearDataList, newData)
  68. }
  69. return &operationPb.UseGearResponse{
  70. Code: http.StatusOK,
  71. Msg: "ok",
  72. Data: &operationPb.UseGearData{List: useGearDataList},
  73. }, nil
  74. }