push_device_info_repository.go 3.6 KB
package repository

import (
	"github.com/astaxie/beego/orm"
	"openapi/pkg/domain"
	"openapi/pkg/infrastructure/bgorm/models"
	"openapi/pkg/infrastructure/utils"
	"strings"
	"time"
)

type PushDeviceRepository struct {
}

func (repository *PushDeviceRepository) Save(device *domain.UpdateDeviceRequest) error {
	o := orm.NewOrm()

	m := &models.PushDeviceInfo{
		Uid:              device.Muid,
		ClientId:         strings.TrimSpace(device.ClientId),
		Phone:            device.Phone,
		DeviceToken:      strings.TrimSpace(device.DeviceToken),
		CreateAt:         time.Now(),
		UpdateAt:         time.Now(),
		ProjectMasterKey: device.ProjectKey,
		IsActive:         1,
	}
	_, err := o.Insert(m)
	return err
}

func (repository *PushDeviceRepository) FindOne(queryOptions map[string]interface{}) (*domain.Device, error) {
	o := orm.NewOrm()
	model := new(models.PushDeviceInfo)
	qs := o.QueryTable(model.TableName()).
		Filter("uid", queryOptions["uid"]).
		Filter("project_master_key", queryOptions["project_master_key"])
	err := qs.One(model)
	if err != nil {
		if err == orm.ErrNoRows {
			err = domain.ERR_DB_NOT_FOUND
		}
		return nil, err
	}
	return repository.transformBgormModelToDomainModel(model)
}

func (repository *PushDeviceRepository) Find(queryOptions map[string]interface{}) (rsp []*domain.Device, err error) {
	o := orm.NewOrm()
	model := new(models.PushDeviceInfo)
	var ms []*models.PushDeviceInfo
	qs := o.QueryTable(model.TableName()).Filter("uid__in", queryOptions["receivers"]).Filter("is_active", 1).Filter("project_master_key", queryOptions["project_master_key"])
	qs.All(&ms)
	if len(ms) == 0 {
		return
	}
	for i := range ms {
		v, _ := repository.transformBgormModelToDomainModel(ms[i])
		rsp = append(rsp, v)
	}
	return
}

func (repository *PushDeviceRepository) UpdateDevice(uid int64, clientId, deviceToken string, projectKey string, phone string) error {
	o := orm.NewOrm()
	o.Begin()
	//更新(这个项目)其他绑定这个client_id的设备 is_active=0
	_, err := o.Raw("UPDATE push_device_info SET update_at=now(),is_active=0 where client_id=? and is_active=1 and project_master_key=?", clientId, projectKey).Exec()
	if err != nil {
		o.Rollback()
		return err
	}

	_, err = o.Raw("UPDATE push_device_info SET client_id=?,device_token = ?,update_at=now(),is_active=1,phone=? where uid=? and project_master_key=?", clientId, deviceToken, phone, uid, projectKey).Exec()
	if err != nil {
		o.Rollback()
		return err
	}
	o.Commit()
	return nil
}

func (repository *PushDeviceRepository) SaveLog(request *domain.PushInfoRequest, extData map[string]interface{}, err error) error {
	var (
		result int
		errmsg string
	)
	if err != nil {
		result = 1
		errmsg = err.Error()
		if v, ok := err.(*domain.ErrWithMessage); ok {
			result = v.Errno
		}
	}

	sql := `INSERT INTO push_log(msg_type,project,receivers,message,result,err_msg,create_at,ext_data)VALUES(?,?,?,?,?,?,now(),?)`
	o := orm.NewOrm()
	_, err = o.Raw(sql,
		request.Type,
		request.ProjectKey,
		utils.JsonAssertString(request.ActualReceivers),
		utils.JsonAssertString(request),
		result,
		errmsg,
		utils.JsonAssertString(extData),
	).Exec()
	if err != nil {
		return err
	}
	return nil
}

func (repository *PushDeviceRepository) transformBgormModelToDomainModel(model *models.PushDeviceInfo) (*domain.Device, error) {
	return &domain.Device{
		Uid:         model.Uid,
		ClientId:    model.ClientId,
		DeviceToken: model.DeviceToken,
		IsActive:    model.IsActive,
	}, nil
}

func NewPushDeviceRepository(transactionContext interface{}) (*PushDeviceRepository, error) {
	if transactionContext == nil {
		return &PushDeviceRepository{}, nil
	} else {
		return &PushDeviceRepository{}, nil
	}
}