neckringlisting.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package dao
  2. import (
  3. "demo/internal/model"
  4. "fmt"
  5. "github.com/pkg/errors"
  6. "github.com/siddontang/go/log"
  7. "strings"
  8. )
  9. func (d *Dao) GetNeckRingListing(index, pagesize int64, code int64, barName, purchaseStart, purchaseEnd,
  10. manufactureStart, manufactureEnd string, status int64, cattle string, recallID, factoryID int64) ([]*model.NeckRingListingList, int64, error) {
  11. dataList := make([]*model.NeckRingListingList, 0)
  12. rows := d.db.NewSession().Table("t_nz_jbqid").Alias("j")
  13. rows.Join("inner", []string{"t_nz_farm", "f"}, "j.FARM_UUID=f.UUID")
  14. if recallID == 0 && factoryID == 0 {
  15. rows.Select(`j.id,j.FARM_UUID ,j.REGIST_NUM_2,j.BUY_DATE,j.ENTER_DATE,j.START_DATE,j.ISUSED as status,j.STOP_DATE,j.CREATETIME`)
  16. } else {
  17. rows.Join("left", []string{"factory", "fa"}, " fa.id = j.factoryid ")
  18. rows.Select(`j.id,j.FARM_UUID ,j.REGIST_NUM_2,j.BUY_DATE,j.ENTER_DATE,j.START_DATE,j.ISUSED as status,j.STOP_DATE,j.CREATETIME
  19. ,fa.createtime,fa.batch`)
  20. }
  21. defer rows.Close()
  22. if code != 0 {
  23. rows.Where("j.REGIST_NUM_2 = ? ", code)
  24. }
  25. if barName != "" || cattle != "" {
  26. rows.Join("left", []string{"t_nz_cow", "c"}, " j.FARM_UUID=c.FARM_UUID And j.REGIST_NUM_2=c.REGIST_NUM_2")
  27. rows.Join("left", []string{"t_jc_house", "h"}, " c.HOUSE_UUID=h.UUID ")
  28. }
  29. if barName != "" {
  30. rows.Where("h.HOUSE_NAME like ? ", "%"+barName+"%")
  31. }
  32. if purchaseStart != "" && purchaseEnd != "" {
  33. rows.Where(" j.BUY_DATE between ? and ? ", purchaseStart, purchaseEnd)
  34. }
  35. if manufactureStart != "" && manufactureEnd != "" {
  36. rows.Where(" j.ENTER_DATE between ? and ? ", manufactureStart, manufactureEnd)
  37. }
  38. if status > 0 {
  39. if status == 0 {
  40. rows.Where("j.STOP_DATE is not null ")
  41. } else {
  42. rows.Where("j.STOP_DATE is null ")
  43. }
  44. }
  45. if cattle != "" {
  46. rows.Where("c.COW_NUM = ? ", cattle)
  47. }
  48. if recallID > 0 {
  49. rows.Where("REGIST_NUM_2 in(select REGIST_NUM_2 from deliveryandrecall where factoryid = ? and status = 1)", recallID)
  50. }
  51. if factoryID > 0 {
  52. rows.Where("factoryid =? ", factoryID)
  53. }
  54. if index != 0 && pagesize != 0 {
  55. first := (index - 1) * pagesize
  56. rows.Limit(int(pagesize), int(first))
  57. }
  58. count, err := rows.Desc("j.CREATETIME").FindAndCount(&dataList)
  59. if err != nil {
  60. err = errors.Wrap(err, "GetNeckRingListing-db-error")
  61. log.Error(err)
  62. return dataList, count, err
  63. }
  64. return dataList, count, nil
  65. }
  66. func (d *Dao) GetFactoryidByREGISTNUM2(REGISTNUM2 []string) ([]*model.NeckRingListing, error) {
  67. rows := d.db.NewSession()
  68. defer rows.Close()
  69. rows.In("REGIST_NUM_2", REGISTNUM2)
  70. dataList := make([]*model.NeckRingListing, 0)
  71. err := rows.Cols("factoryid").GroupBy("factoryid").Find(&dataList)
  72. if err != nil {
  73. err = errors.Wrap(err, "GetFactoryidByREGISTNUM2-db-error")
  74. log.Error(err)
  75. return dataList, err
  76. }
  77. return dataList, nil
  78. }
  79. func (d *Dao) GetHOUSENAME(codeList []string) ([]map[string]string, error) {
  80. rows := d.db.NewSession()
  81. dataList, err := rows.SQL(fmt.Sprintf(`select c.COW_NUM,
  82. h.HOUSE_NAME ,c.REGIST_NUM_2, c.FARM_UUID from t_nz_cow c JOIN t_jc_house h ON c.HOUSE_UUID = h.UUID
  83. where c.del = 0 and c.REGIST_NUM_2 in(%s)`, strings.Join(codeList, ","))).QueryString()
  84. if err != nil {
  85. err = errors.Wrap(err, "GetFactoryidByREGISTNUM2-db-error")
  86. log.Error(err)
  87. return dataList, err
  88. }
  89. return dataList, nil
  90. }
  91. func (d *Dao) GetNeckRingListingIDByFactoryId(factoryId []int64) ([]*model.NeckRingListing, error) {
  92. rows := d.db.NewSession()
  93. defer rows.Close()
  94. dataList := make([]*model.NeckRingListing, 0)
  95. rows.In("factoryid", factoryId)
  96. rows.Where("recall = 0")
  97. err := rows.Cols("REGIST_NUM_2").Find(&dataList)
  98. if err != nil {
  99. err = errors.Wrap(err, "GetNeckRingListingIDByFactoryId-db-error")
  100. log.Error(err)
  101. return dataList, err
  102. }
  103. return dataList, err
  104. }