sheep.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. "strconv"
  10. "tmr-watch/http/handle/restful"
  11. "tmr-watch/models"
  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()).
  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).Delete(new(models.TmrGear)); err != nil {
  37. tx.Rollback()
  38. return xerr.WithStack(err)
  39. }
  40. }
  41. newTmrGearList := models.NewTmrGearList(tmrId, req)
  42. if _, err = tx.Table(new(models.TmrGear).TableName()).Insert(newTmrGearList); err != nil {
  43. tx.Rollback()
  44. return xerr.WithStack(err)
  45. }
  46. return tx.Commit()
  47. }
  48. func UseTmrGear(ctx context.Context, req *operationPb.UseGearRequest) ([]*models.TmrGearDetail, error) {
  49. sqlName := fmt.Sprintf(
  50. `SELECT sort,fname,weight,LENGTH,bcode,ccount,speed,gear_rate,useinbar FROM
  51. (SELECT a.sort,a.fname,a.lweight weight,a.fpid, IF(a.useinbartype=0,'转投剩料', IF(a.useinbartype=1,'撒','继续饲喂') ) useinbar,
  52. CONCAT(a.fbarid) fbarid,
  53. CONCAT(a.pid) pid,
  54. b.bcode,
  55. b.length,
  56. c.ccount,
  57. d.speed,
  58. (((a.lweight / b.length) * f.volume) / d.volume_rate) / (60 * 60 / 1000 / d.speed) AS gear_rate
  59. FROM downloadplandtl2 a
  60. JOIN bar b ON a.fbarid = b.id
  61. JOIN feedp c ON c.barid = a.fbarid
  62. JOIN downloadedplan g ON a.pid = g.id
  63. JOIN tmr d ON d.id = g.tmrid
  64. JOIN feedtemplet f ON c.ftid = f.id
  65. WHERE a.pastureid = %s
  66. AND a.pid= %s
  67. AND d.id = %d
  68. ORDER BY a.sort) AS B`, req.PastureId, req.Id, req.TmrId)
  69. tx := restful.Engine.NewSession()
  70. defer tx.Close()
  71. resultList, err := tx.QueryString(sqlName)
  72. if err != nil {
  73. return nil, xerr.WithStack(err)
  74. }
  75. tmrGearDetailList := make([]*models.TmrGearDetail, 0)
  76. for _, v := range resultList {
  77. sort, _ := strconv.ParseInt(v["sort"], 10, 64)
  78. fname := v["fname"]
  79. bcode := v["bcode"]
  80. weight, _ := strconv.ParseFloat(v["weight"], 64)
  81. length, _ := strconv.ParseInt(v["length"], 10, 64)
  82. ccount, _ := strconv.ParseInt(v["ccount"], 10, 64)
  83. speed, _ := strconv.ParseFloat(v["speed"], 64)
  84. gearRate, _ := strconv.ParseFloat(v["gear_rate"], 64)
  85. tmrGearDetailList = append(tmrGearDetailList, &models.TmrGearDetail{
  86. Sort: int32(sort),
  87. FName: fname,
  88. Weight: weight,
  89. Length: int32(length),
  90. BCode: bcode,
  91. CCount: int32(ccount),
  92. Speed: speed,
  93. GearRate: gearRate,
  94. })
  95. }
  96. return tmrGearDetailList, nil
  97. }
  98. func TmrGearListByTmrId(tmrId int64) ([]*models.TmrGear, error) {
  99. tmrGearList := make([]*models.TmrGear, 0)
  100. tx := restful.Engine.NewSession()
  101. defer tx.Close()
  102. err := tx.Table(new(models.TmrGear).TableName()).Where("tmr_id = ?", tmrId).Select("*").Find(&tmrGearList)
  103. if err != nil {
  104. return nil, xerr.WithStack(err)
  105. }
  106. return tmrGearList, nil
  107. }