receiver.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package dao
  2. import (
  3. "demo/internal/model"
  4. "github.com/pkg/errors"
  5. uuid "github.com/satori/go.uuid"
  6. "github.com/siddontang/go/log"
  7. )
  8. func (d *Dao) GetReceiverList(index, pagesize int64, code string, status int64) ([]*model.ListReceiver, int64, error) {
  9. rows := d.db.Table("t_sb_device_connection").Alias("s")
  10. rows.Join("left", []string{"sim", "s1"}, "s1.id=s.simid")
  11. rows.Join("inner", []string{"t_jc_housephy", "h"}, "s.PHYHOUSE_UUID=h.UUID")
  12. rows.Join("inner", []string{"t_nz_farm", "f"}, "s.FARM_UUID=f.uuid")
  13. rows.Select("s.ID, s.UUID, s.DEVICE_NAME, s.DEVICE_SECRET, s.`HOST`, s.TOPIC_NAME, s.IMEI, f.FARM_NUM, " +
  14. "s.FARM_UUID, f.FARM_NAME, s.PHYHOUSE_UUID, h.PHYSICALCODE, h.PHYSICALNAME,s.DEVICE_ADDRESS,s1.card,s1.id,s.status,s.updatedate,s.coordinates,h.image")
  15. rows.Where("s.TYPE='meck' AND IFNULL(s.DEL, 0)=0")
  16. defer rows.Close()
  17. if index != 0 && pagesize != 0 {
  18. first := (index - 1) * pagesize
  19. rows.Limit(int(pagesize), int(first))
  20. }
  21. if code != "" {
  22. rows.Where("s.IMEI = ? ", code)
  23. }
  24. if status != -1 {
  25. rows.Where("s.status = ? ", status)
  26. }
  27. dataList := make([]*model.ListReceiver, 0)
  28. count, err := rows.Desc("s.LAST_UPDATE_DATE").FindAndCount(&dataList)
  29. if err != nil {
  30. err = errors.Wrap(err, "GetReceiverList-db-error")
  31. log.Error(err)
  32. return dataList, 0, err
  33. }
  34. return dataList, count, nil
  35. }
  36. func (d *Dao) AddReceiver(arg model.Receiver) error {
  37. arg.TYPE = "meck"
  38. arg.UUID = uuid.NewV4().String()
  39. _, err := d.db.Insert(&arg)
  40. if err != nil {
  41. err = errors.Wrap(err, "AddReceiver-db-error")
  42. log.Error(err)
  43. return err
  44. }
  45. return nil
  46. }
  47. func (d *Dao) EditReceiver(arg model.Receiver) error {
  48. _, err := d.db.Where("id = ?", arg.Id).Cols("IMEI,FARM_UUID,PHYHOUSE_UUID,updatedate,DEL,status,simid").Update(&arg)
  49. if err != nil {
  50. err = errors.Wrap(err, "EditReceiver-db-error")
  51. log.Error(err)
  52. return err
  53. }
  54. return nil
  55. }
  56. func (d *Dao) GetReceiverPull() ([]*model.Receiver, error) {
  57. dataList := make([]*model.Receiver, 0)
  58. err := d.db.Select("id,IMEI").Where("TYPE='meck' ").And(" IFNULL(DEL, 0)=0").
  59. And(" ( select count(1) from sim where sim.id = t_sb_device_connection.simid ) = 0 ").Find(&dataList)
  60. if err != nil {
  61. err = errors.Wrap(err, "GetReceiverPull-db-error")
  62. log.Error(err)
  63. return dataList, err
  64. }
  65. return dataList, nil
  66. }
  67. func (d *Dao) GetReceiverByPHYHOUSEUUID(PHYHOUSEUUID string) ([]*model.Receiver, error) {
  68. rows := d.db.NewSession()
  69. defer rows.Close()
  70. rows.Where("PHYHOUSE_UUID = ? ", PHYHOUSEUUID)
  71. rows.Where("TYPE = ? ", "meck")
  72. rows.Where("DEL = 0 or del is null")
  73. dataList := make([]*model.Receiver, 0)
  74. err := rows.Cols("id,status,coordinates,FARM_UUID").OrderBy("CREATION_DATE").Find(&dataList)
  75. if err != nil {
  76. err = errors.Wrap(err, "GetReceiverByPHYHOUSEUUID-db-error")
  77. log.Error(err)
  78. return dataList, err
  79. }
  80. return dataList, nil
  81. }
  82. func (d *Dao) UpdateReceiverById(data []*model.Receiver) error {
  83. rows := d.db.NewSession()
  84. rows.Begin()
  85. for _, item := range data {
  86. _, err := rows.Cols("status,coordinates").Where("ID = ? ", item.Id).Update(item)
  87. if err != nil {
  88. err = errors.Wrap(err, "UpdateReceiverById-db-error")
  89. log.Error(err)
  90. rows.Rollback()
  91. return err
  92. }
  93. }
  94. return rows.Commit()
  95. }
  96. func (d *Dao) UpdateReceiverSimId(data *model.Receiver) error {
  97. rows := d.db.NewSession()
  98. rows.Begin()
  99. //for _, item := range data {
  100. if data.Id > 0 {
  101. rows.Where("ID = ? ", data.Id)
  102. } else {
  103. rows.Where("simid = ? ", data.SimID)
  104. data.SimID = 0
  105. }
  106. _, err := rows.Cols("simid").Update(data)
  107. if err != nil {
  108. err = errors.Wrap(err, "UpdateReceiverById-db-error")
  109. log.Error(err)
  110. rows.Rollback()
  111. return err
  112. }
  113. return rows.Commit()
  114. }
  115. func (d *Dao) CheckReceiverCode(id int64, IMEI string) (bool, error) {
  116. rows := d.db.NewSession().Table(new(model.Receiver).TableName())
  117. rows.Close()
  118. if id != 0 {
  119. rows.Where("id != ? ", id)
  120. }
  121. return rows.Where("IMEI = ? ", IMEI).Exist()
  122. }