package user

import (
	"courseSign/server/global"
	"courseSign/server/model/common/request"
	"courseSign/server/model/twi"
	"courseSign/server/model/user"
	userReq "courseSign/server/model/user/request"
)

type UsersMsgService struct {
}

// CreateUsersMsg 创建UsersMsg记录
// Author [piexlmax](https://github.com/piexlmax)
func (usersMsgService *UsersMsgService) CreateUsersMsg(usersMsg user.UsersMsg) (err error) {
	err = global.GVA_DB.Create(&usersMsg).Error
	return err
}

// SendUsersMsg 发送UsersMsg记录
// Author [piexlmax](https://github.com/piexlmax)
func (usersMsgService *UsersMsgService) SendUsersMsg(usersMsg user.UsersMsg) (err error) {
	if *usersMsg.Ty { // 全局发送
		users := make([]twi.TwiMetamask,0)
		// 获取所有已验证绑定twitter和telegram的用户

		err = global.GVA_DB.Model(&twi.TwiMetamask{}).Where("is_tele_auth = ? or is_twi_auth = ?",true,true).Find(&users).Error
		if err != nil {
			return
		}
		tx := global.GVA_DB.Begin()
		for _,v := range users {
			createUserMsg := new(user.UsersMsg)
			createUserMsg.Msg = usersMsg.Msg
			*createUserMsg.Uid = int(v.ID)
			*createUserMsg.Status = false
			err = tx.Create(createUserMsg).Error
			if err != nil {
				tx.Rollback()
				return
			}
		}
		tx.Commit()
	} else { // 单独发送
		var u twi.TwiMetamask
		err = global.GVA_DB.Model(&twi.TwiMetamask{}).Where("address = ?",usersMsg.Address).First(&u).Error
		if err != nil {
			return
		}
		*usersMsg.Uid = int(u.ID)
		err = global.GVA_DB.Create(&usersMsg).Error
	}

	return err
}

// DeleteUsersMsg 删除UsersMsg记录
// Author [piexlmax](https://github.com/piexlmax)
func (usersMsgService *UsersMsgService)DeleteUsersMsg(usersMsg user.UsersMsg) (err error) {
	err = global.GVA_DB.Delete(&usersMsg).Error
	return err
}

// DeleteUsersMsgByIds 批量删除UsersMsg记录
// Author [piexlmax](https://github.com/piexlmax)
func (usersMsgService *UsersMsgService)DeleteUsersMsgByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]user.UsersMsg{},"id in ?",ids.Ids).Error
	return err
}

// UpdateUsersMsg 更新UsersMsg记录
// Author [piexlmax](https://github.com/piexlmax)
func (usersMsgService *UsersMsgService)UpdateUsersMsg(usersMsg user.UsersMsg) (err error) {
	err = global.GVA_DB.Save(&usersMsg).Error
	return err
}

// GetUsersMsg 根据id获取UsersMsg记录
// Author [piexlmax](https://github.com/piexlmax)
func (usersMsgService *UsersMsgService)GetUsersMsg(id uint) (usersMsg user.UsersMsg, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&usersMsg).Error
	return
}

// GetUsersMsgInfoList 分页获取UsersMsg记录
// Author [piexlmax](https://github.com/piexlmax)
func (usersMsgService *UsersMsgService)GetUsersMsgInfoList(info userReq.UsersMsgSearch) (list []user.UsersMsg, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&user.UsersMsg{})
    var usersMsgs []user.UsersMsg
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
    if info.Uid != nil {
        db = db.Where("uid = ?",info.Uid)
    }
    if info.Status != nil {
        db = db.Where("status = ?",info.Status)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	err = db.Limit(limit).Offset(offset).Find(&usersMsgs).Error
	return  usersMsgs, total, err
}
