recall.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package dao
  2. import (
  3. "demo/internal/model"
  4. "fmt"
  5. "github.com/pkg/errors"
  6. "github.com/siddontang/go/log"
  7. "time"
  8. )
  9. func (d *Dao) AddRecall(arg model.Factory, factory []int64, REGISTNUM2 []string) error {
  10. row := d.db.NewSession()
  11. defer row.Close()
  12. row.Begin()
  13. c := time.Now().Format("20060102")
  14. count1, err := row.Where(" batch like ? ", "%"+fmt.Sprintf("RB%s", c)+"%").Count(&model.Factory{})
  15. if err != nil {
  16. err = errors.Wrap(err, "AddRecall-db-error")
  17. log.Error(err)
  18. row.Rollback()
  19. return err
  20. }
  21. count1 = count1 + 1
  22. var batch string
  23. //for i := 0; i <= int(count1); i++ {
  24. batch = fmt.Sprintf("RB%s", c)
  25. // //count1 = count1 + 1
  26. num := 3 - len(fmt.Sprintf("%d", count1))
  27. for n := 0; n < num; n++ {
  28. batch = fmt.Sprintf("%s0", batch)
  29. }
  30. neckRingList := make([]*model.NeckRingListing, 0)
  31. err = row.In("REGIST_NUM_2", REGISTNUM2).Find(&neckRingList)
  32. if err != nil {
  33. err = errors.Wrap(err, "AddRecall-db-error")
  34. log.Error(err)
  35. row.Rollback()
  36. return err
  37. }
  38. factorymap := make(map[int64]int64)
  39. for _, n := range neckRingList {
  40. factorymap[n.FactoryId] = 1
  41. }
  42. arg.Batch = batch
  43. arg.Count = int64(len(REGISTNUM2))
  44. arg.Recall = int64(len(factorymap))
  45. _, err = row.Insert(&arg)
  46. if err != nil {
  47. err = errors.Wrap(err, "AddRecall-db-error")
  48. log.Error(err)
  49. row.Rollback()
  50. return err
  51. }
  52. deliveryandrecallList := make([]*model.DeliveryAndRecall, 0)
  53. for _, item := range neckRingList {
  54. var deliveryandrecall model.DeliveryAndRecall
  55. deliveryandrecall.FactoryId = arg.Id
  56. deliveryandrecall.Status = 1
  57. deliveryandrecall.RecallId = item.FactoryId
  58. deliveryandrecall.REGISTNUM2 = item.Code
  59. deliveryandrecallList = append(deliveryandrecallList, &deliveryandrecall)
  60. }
  61. _, err = row.Insert(&deliveryandrecallList)
  62. if err != nil {
  63. err = errors.Wrap(err, "AddRecall-db-error")
  64. log.Error(err)
  65. row.Rollback()
  66. return err
  67. }
  68. row.Cols("recall").In("REGIST_NUM_2", REGISTNUM2).Update(&model.NeckRingListing{Recall: 1})
  69. err = row.Commit()
  70. if err != nil {
  71. err = errors.Wrap(err, "AddRecall-db-error")
  72. log.Error(err)
  73. row.Rollback()
  74. return err
  75. }
  76. return nil
  77. }
  78. func (d *Dao) GetRecall(index, pagesize int64, pastureid, batch string, startCount, endCount int64, createName, startDate, endDate string, factoryID, replacement int64) ([]*model.FactoryList, int64, error) {
  79. rows := d.db.NewSession()
  80. defer rows.Close()
  81. rows.Table("factory").Alias("f")
  82. rows.Join("inner", []string{"t_nz_farm", "fa"}, "f.FARM_UUID=fa.UUID")
  83. if batch != "" {
  84. rows.Where(" f.batch like ? ", "%"+batch+"%")
  85. }
  86. if index != 0 && pagesize != 0 {
  87. first := (index - 1) * pagesize
  88. rows.Limit(int(pagesize), int(first))
  89. }
  90. if startCount > 0 {
  91. rows.Where(" f.count >= ? ", startCount)
  92. }
  93. if endCount > 0 {
  94. rows.Where(" f.count <= ? ", endCount)
  95. }
  96. if createName != "" {
  97. rows.Where(" f.createname like ? ", "%"+createName+"%")
  98. }
  99. if pastureid != "" {
  100. rows.Where(" f.FARM_UUID = ? ", pastureid)
  101. }
  102. if startDate != "" && endDate != "" {
  103. rows.Where(" f.createtime between ? and ? ", startDate, endDate)
  104. }
  105. if factoryID > 0 {
  106. rows.Where("f.id in(select recallid from deliveryandrecall where factoryid = ? and status = 0)", factoryID)
  107. }
  108. if replacement == 1 {
  109. rows.Where("( select count(1) from deliveryandrecall where recallid = f.id) =0 ")
  110. }
  111. rows.Where("f.genre = 2")
  112. dataList := make([]*model.FactoryList, 0)
  113. count, err := rows.Desc("f.createtime").FindAndCount(&dataList)
  114. if err != nil {
  115. err = errors.Wrap(err, "GetRecall-db-error")
  116. log.Error(err)
  117. return dataList, count, err
  118. }
  119. return dataList, count, nil
  120. }