package entrustPool

import (
	"courseSign/server/global"
	"courseSign/server/model/common/request"
	"courseSign/server/model/entrustPool"
	entrustPoolReq "courseSign/server/model/entrustPool/request"
)

type PoolAddrTxsService struct {
}

// CreatePoolAddrTxs 创建PoolAddrTxs记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolAddrTxsService *PoolAddrTxsService) CreatePoolAddrTxs(poolAddrTxs entrustPool.PoolAddrTxs) (err error) {
	err = global.GVA_DB.Create(&poolAddrTxs).Error
	return err
}

// DeletePoolAddrTxs 删除PoolAddrTxs记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolAddrTxsService *PoolAddrTxsService)DeletePoolAddrTxs(poolAddrTxs entrustPool.PoolAddrTxs) (err error) {
	err = global.GVA_DB.Delete(&poolAddrTxs).Error
	return err
}

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

// UpdatePoolAddrTxs 更新PoolAddrTxs记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolAddrTxsService *PoolAddrTxsService)UpdatePoolAddrTxs(poolAddrTxs entrustPool.PoolAddrTxs) (err error) {
	err = global.GVA_DB.Save(&poolAddrTxs).Error
	return err
}

// GetPoolAddrTxs 根据id获取PoolAddrTxs记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolAddrTxsService *PoolAddrTxsService)GetPoolAddrTxs(id uint) (poolAddrTxs entrustPool.PoolAddrTxs, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&poolAddrTxs).Error
	return
}

// GetPoolAddrTxsInfoList 分页获取PoolAddrTxs记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolAddrTxsService *PoolAddrTxsService)GetPoolAddrTxsInfoList(info entrustPoolReq.PoolAddrTxsSearch) (list []entrustPool.PoolAddrTxs, total int64, err error) {
	if info.ReturnAddr == "" {
		return
	}
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&entrustPool.PoolAddrTxs{})
    var poolAddrTxss []entrustPool.PoolAddrTxs
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
	if info.ReturnAddr != "" {
		db = db.Where("return_addr = ?",info.ReturnAddr)
	}
	if info.MinerAddr != "" {
		db = db.Where("miner_addr = ?",info.MinerAddr)
	}
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }
	if info.Desc {
		db = db.Order("height desc")
	}

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

// GetPoolAddrTxsInfoList 分页获取PoolAddrTxs记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolAddrTxsService *PoolAddrTxsService)GetPoolAddrTxsInfoList2(info entrustPoolReq.PoolAddrTxsSearch) (list []entrustPool.PoolAddrTxs, total int64, err error) {

	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&entrustPool.PoolAddrTxs{})
	var poolAddrTxss []entrustPool.PoolAddrTxs
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.ReturnAddr != "" {
		db = db.Where("return_addr = ?",info.ReturnAddr)
	}
	if info.MinerAddr != "" {
		db = db.Where("miner_addr = ?",info.MinerAddr)
	}
	err = db.Count(&total).Error
	if err!=nil {
		return
	}
	if info.Desc {
		db = db.Order("height desc")
	}

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