sheep.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package module
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
  7. "gitee.com/xuyiping_admin/pkg/xerr"
  8. "github.com/xormplus/xorm"
  9. "go.uber.org/zap"
  10. "strconv"
  11. "tmr-watch/http/handle/restful"
  12. "tmr-watch/models"
  13. "tmr-watch/pkg/logger/zaplog"
  14. )
  15. // SearchTrmGearByTmrId 查询撒料电机档位数据
  16. func SearchTrmGearByTmrId(ctx context.Context, tmrId int64) ([]*models.TmrGear, error) {
  17. res := make([]*models.TmrGear, 0)
  18. if err := restful.Engine.NewSession().Table(new(models.TmrGear).TableName()).
  19. Where("tmr_id = ?", tmrId).Find(&res); err != nil {
  20. if !errors.Is(err, xorm.ErrNotExist) {
  21. return nil, err
  22. }
  23. }
  24. return res, nil
  25. }
  26. func CreateOrUpdateTmrGear(ctx context.Context, req *operationPb.UpdateOrCreateTmrGearRequest) error {
  27. tmrId, _ := strconv.ParseInt(req.TmrId, 10, 64)
  28. tmrGearList, err := SearchTrmGearByTmrId(ctx, tmrId)
  29. if err != nil {
  30. return xerr.WithStack(err)
  31. }
  32. tx := restful.Engine.NewSession()
  33. defer tx.Close()
  34. if err = tx.Begin(); err != nil {
  35. return xerr.WithStack(err)
  36. }
  37. if len(tmrGearList) >= 1 {
  38. if _, err = tx.Table(new(models.TmrGear).TableName()).Where("tmr_id = ?", req.TmrId).Delete(new(models.TmrGear)); err != nil {
  39. tx.Rollback()
  40. return xerr.WithStack(err)
  41. }
  42. }
  43. newTmrGearList := models.NewTmrGearList(tmrId, req)
  44. if _, err = tx.Table(new(models.TmrGear).TableName()).Insert(newTmrGearList); err != nil {
  45. tx.Rollback()
  46. return xerr.WithStack(err)
  47. }
  48. return tx.Commit()
  49. }
  50. func UseTmrGear(ctx context.Context, req *operationPb.UseGearRequest) ([]*models.TmrGearDetail, error) {
  51. sqlName := fmt.Sprintf(
  52. `SELECT sort,fname,weight,length,bcode,ccount,speed,gear_rate,useinbar,pid,fbarid FROM
  53. (SELECT a.sort,a.fname,a.lweight weight,a.fpid, IF(a.useinbartype=0,'转投剩料', IF(a.useinbartype=1,'撒','继续饲喂') ) useinbar,
  54. CONCAT(a.fbarid) fbarid,
  55. CONCAT(a.pid) pid,
  56. b.bcode,
  57. b.length,
  58. c.cowcount AS ccount,
  59. ROUND( c.ccountradio * c.cowcount / 100, 2 ) ccountratio,
  60. d.speed,
  61. (((a.lweight / b.length) * f.volume) / d.volume_rate) / (1 / (d.speed * 1000 / 3600)) * 60 AS gear_rate
  62. FROM downloadplandtl2 a
  63. JOIN bar b ON a.fbarid = b.id
  64. JOIN fpdetaildate c ON c.barid = a.fbarid
  65. JOIN downloadedplan g ON a.pid = g.id
  66. JOIN tmr d ON d.id = g.tmrid
  67. JOIN feedtemplet f ON a.feedtempletid = f.id
  68. WHERE a.pastureid = %s
  69. AND a.pid= %s
  70. AND d.id = %d
  71. GROUP BY a.sort
  72. ORDER BY a.sort
  73. ) AS B`, req.PastureId, req.Id, req.TmrId)
  74. tx := restful.Engine.NewSession()
  75. defer tx.Close()
  76. resultList, err := tx.QueryString(sqlName)
  77. if err != nil {
  78. return nil, xerr.WithStack(err)
  79. }
  80. zaplog.Info("UseTmrGear", zap.Any("resultList", resultList), zap.Any("sqlName", sqlName))
  81. tmrGearDetailList := make([]*models.TmrGearDetail, 0)
  82. for _, v := range resultList {
  83. sort, _ := strconv.ParseInt(v["sort"], 10, 64)
  84. fname := v["fname"]
  85. bcode := v["bcode"]
  86. weight, _ := strconv.ParseFloat(v["weight"], 64)
  87. length, _ := strconv.ParseInt(v["length"], 10, 64)
  88. ccount, _ := strconv.ParseInt(v["ccount"], 10, 64)
  89. speed, _ := strconv.ParseFloat(v["speed"], 64)
  90. gearRate, _ := strconv.ParseFloat(v["gear_rate"], 64)
  91. pid, _ := strconv.ParseInt(v["pid"], 10, 64)
  92. fbarid, _ := strconv.ParseInt(v["fbarid"], 10, 64)
  93. tmrGearDetailList = append(tmrGearDetailList, &models.TmrGearDetail{
  94. Sort: int32(sort),
  95. FName: fname,
  96. Weight: weight,
  97. Length: int32(length),
  98. BCode: bcode,
  99. CCount: int32(ccount),
  100. Speed: speed,
  101. GearRate: gearRate,
  102. Pid: pid,
  103. FBarid: fbarid,
  104. })
  105. }
  106. return tmrGearDetailList, nil
  107. }
  108. func TmrGearListByTmrId(tmrId int64) ([]*models.TmrGear, error) {
  109. tmrGearList := make([]*models.TmrGear, 0)
  110. tx := restful.Engine.NewSession()
  111. defer tx.Close()
  112. err := tx.Table(new(models.TmrGear).TableName()).Where("tmr_id = ?", tmrId).Select("*").Find(&tmrGearList)
  113. if err != nil {
  114. return nil, xerr.WithStack(err)
  115. }
  116. return tmrGearList, nil
  117. }