123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- package dao
- import (
- "demo/internal/model"
- "github.com/pkg/errors"
- uuid "github.com/satori/go.uuid"
- "github.com/siddontang/go/log"
- )
- func (d *Dao) GetReceiverList(index, pagesize int64, code string, status int64) ([]*model.ListReceiver, int64, error) {
- rows := d.db.Table("t_sb_device_connection").Alias("s")
- rows.Join("left", []string{"sim", "s1"}, "s1.id=s.simid")
- rows.Join("inner", []string{"t_jc_housephy", "h"}, "s.PHYHOUSE_UUID=h.UUID")
- rows.Join("inner", []string{"t_nz_farm", "f"}, "s.FARM_UUID=f.uuid")
- rows.Select("s.ID, s.UUID, s.DEVICE_NAME, s.DEVICE_SECRET, s.`HOST`, s.TOPIC_NAME, s.IMEI, f.FARM_NUM, " +
- "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")
- rows.Where("s.TYPE='meck' AND IFNULL(s.DEL, 0)=0")
- defer rows.Close()
- if index != 0 && pagesize != 0 {
- first := (index - 1) * pagesize
- rows.Limit(int(pagesize), int(first))
- }
- if code != "" {
- rows.Where("s.IMEI = ? ", code)
- }
- if status != -1 {
- rows.Where("s.status = ? ", status)
- }
- dataList := make([]*model.ListReceiver, 0)
- count, err := rows.Desc("s.LAST_UPDATE_DATE").FindAndCount(&dataList)
- if err != nil {
- err = errors.Wrap(err, "GetReceiverList-db-error")
- log.Error(err)
- return dataList, 0, err
- }
- return dataList, count, nil
- }
- func (d *Dao) AddReceiver(arg model.Receiver) error {
- arg.TYPE = "meck"
- arg.UUID = uuid.NewV4().String()
- _, err := d.db.Insert(&arg)
- if err != nil {
- err = errors.Wrap(err, "AddReceiver-db-error")
- log.Error(err)
- return err
- }
- return nil
- }
- func (d *Dao) EditReceiver(arg model.Receiver) error {
- _, err := d.db.Where("id = ?", arg.Id).Cols("IMEI,FARM_UUID,PHYHOUSE_UUID,updatedate,DEL,status,simid").Update(&arg)
- if err != nil {
- err = errors.Wrap(err, "EditReceiver-db-error")
- log.Error(err)
- return err
- }
- return nil
- }
- func (d *Dao) GetReceiverPull() ([]*model.Receiver, error) {
- dataList := make([]*model.Receiver, 0)
- err := d.db.Select("id,IMEI").Where("TYPE='meck' ").And(" IFNULL(DEL, 0)=0").
- And(" ( select count(1) from sim where sim.id = t_sb_device_connection.simid ) = 0 ").Find(&dataList)
- if err != nil {
- err = errors.Wrap(err, "GetReceiverPull-db-error")
- log.Error(err)
- return dataList, err
- }
- return dataList, nil
- }
- func (d *Dao) GetReceiverByPHYHOUSEUUID(PHYHOUSEUUID string) ([]*model.Receiver, error) {
- rows := d.db.NewSession()
- defer rows.Close()
- rows.Where("PHYHOUSE_UUID = ? ", PHYHOUSEUUID)
- rows.Where("TYPE = ? ", "meck")
- rows.Where("DEL = 0 or del is null")
- dataList := make([]*model.Receiver, 0)
- err := rows.Cols("id,status,coordinates,FARM_UUID").OrderBy("CREATION_DATE").Find(&dataList)
- if err != nil {
- err = errors.Wrap(err, "GetReceiverByPHYHOUSEUUID-db-error")
- log.Error(err)
- return dataList, err
- }
- return dataList, nil
- }
- func (d *Dao) UpdateReceiverById(data []*model.Receiver) error {
- rows := d.db.NewSession()
- rows.Begin()
- for _, item := range data {
- _, err := rows.Cols("status,coordinates").Where("ID = ? ", item.Id).Update(item)
- if err != nil {
- err = errors.Wrap(err, "UpdateReceiverById-db-error")
- log.Error(err)
- rows.Rollback()
- return err
- }
- }
- return rows.Commit()
- }
- func (d *Dao) UpdateReceiverSimId(data *model.Receiver) error {
- rows := d.db.NewSession()
- rows.Begin()
- //for _, item := range data {
- if data.Id > 0 {
- rows.Where("ID = ? ", data.Id)
- } else {
- rows.Where("simid = ? ", data.SimID)
- data.SimID = 0
- }
- _, err := rows.Cols("simid").Update(data)
- if err != nil {
- err = errors.Wrap(err, "UpdateReceiverById-db-error")
- log.Error(err)
- rows.Rollback()
- return err
- }
- return rows.Commit()
- }
- func (d *Dao) CheckReceiverCode(id int64, IMEI string) (bool, error) {
- rows := d.db.NewSession().Table(new(model.Receiver).TableName())
- rows.Close()
- if id != 0 {
- rows.Where("id != ? ", id)
- }
- return rows.Where("IMEI = ? ", IMEI).Exist()
- }
|