sim.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package dao
  2. import (
  3. "demo/internal/model"
  4. "github.com/pkg/errors"
  5. "github.com/siddontang/go/log"
  6. )
  7. func (d *Dao) GetSimList(index, pagesize int64, card, company string, status int64) ([]*model.ListSim, int64, error) {
  8. rows := d.db.Table("sim").Alias("s")
  9. rows.Join("left", []string{"t_sb_device_connection", "r"}, "r.simid = s.id")
  10. rows.Join("left", []string{"t_nz_farm", "p"}, "r.FARM_UUID = p.UUID")
  11. rows.Join("left", []string{"t_jc_housephy", "h"}, "r.PHYHOUSE_UUID=h.UUID")
  12. rows.Select("s.*,r.IMEI, p.FARM_NAME,h.PHYSICALNAME as bname,p.FARM_NUM")
  13. defer rows.Close()
  14. if index != 0 && pagesize != 0 {
  15. first := (index - 1) * pagesize
  16. rows.Limit(int(pagesize), int(first))
  17. }
  18. if card != "" {
  19. rows.Where("s.card = ? ", card)
  20. }
  21. if company != "" {
  22. rows.Where("s.company = ? ", company)
  23. }
  24. if status != -1 {
  25. rows.Where("s.status = ? ", status)
  26. }
  27. dataList := make([]*model.ListSim, 0)
  28. count, err := rows.Desc("s.purchasedate").FindAndCount(&dataList)
  29. if err != nil {
  30. err = errors.Wrap(err, "GetSimList-db-error")
  31. log.Error(err)
  32. return dataList, 0, err
  33. }
  34. return dataList, count, nil
  35. }
  36. func (d *Dao) AddSim(arg model.Sim) (int64, error) {
  37. _, err := d.db.Insert(&arg)
  38. if err != nil {
  39. err = errors.Wrap(err, "AddSim-db-error")
  40. log.Error(err)
  41. return 0, err
  42. }
  43. return arg.Id, nil
  44. }
  45. func (d *Dao) EditSim(arg model.Sim) error {
  46. _, err := d.db.Where("id = ?", arg.Id).
  47. Cols("card,totalflow,used,remaining,company,purchasedate,activatedate,topupdate,enddate,status,receiverid,pastureid,barid,barname ").Update(&arg)
  48. if err != nil {
  49. err = errors.Wrap(err, "EditSim-db-error")
  50. log.Error(err)
  51. return err
  52. }
  53. return nil
  54. }
  55. func (d *Dao) GetSimPull() ([]*model.Sim, error) {
  56. dataList := make([]*model.Sim, 0)
  57. err := d.db.Select("id,card").Where("status = 1 ").
  58. Where(" (select count(1) from t_sb_device_connection where sim.id = t_sb_device_connection.simid ) = 0 ").Find(&dataList)
  59. if err != nil {
  60. err = errors.Wrap(err, "GetSimPull-db-error")
  61. log.Error(err)
  62. return dataList, err
  63. }
  64. return dataList, nil
  65. }
  66. func (d *Dao) EditSimReceiverid(arg model.Sim) error {
  67. _, err := d.db.Where("id = ?", arg.Id).
  68. Cols("receiverid ").Update(&arg)
  69. if err != nil {
  70. err = errors.Wrap(err, "EditSim-db-error")
  71. log.Error(err)
  72. return err
  73. }
  74. return nil
  75. }
  76. func (d *Dao) CheckSimCode(id int64, card string) (bool, error) {
  77. rows := d.db.NewSession().Table(new(model.Sim).TableName())
  78. rows.Close()
  79. if id != 0 {
  80. rows.Where("id != ? ", id)
  81. }
  82. return rows.Where("card = ? ", card).Exist()
  83. }