package entrustPool

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

type PoolRewardListService struct {
}

// CreatePoolRewardList 创建PoolRewardList记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolRewardListService *PoolRewardListService) CreatePoolRewardList(poolRewardList entrustPool.PoolRewardList) (err error) {
	err = global.GVA_DB.Create(&poolRewardList).Error
	return err
}

// DeletePoolRewardList 删除PoolRewardList记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolRewardListService *PoolRewardListService)DeletePoolRewardList(poolRewardList entrustPool.PoolRewardList) (err error) {
	err = global.GVA_DB.Delete(&poolRewardList).Error
	return err
}

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

// UpdatePoolRewardList 更新PoolRewardList记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolRewardListService *PoolRewardListService)UpdatePoolRewardList(poolRewardList entrustPool.PoolRewardList) (err error) {
	err = global.GVA_DB.Save(&poolRewardList).Error
	return err
}

// GetPoolRewardList 根据id获取PoolRewardList记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolRewardListService *PoolRewardListService)GetPoolRewardList(id uint) (poolRewardList entrustPool.PoolRewardList, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&poolRewardList).Error
	return
}

// GetPoolRewardListInfoList 分页获取PoolRewardList记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolRewardListService *PoolRewardListService)GetPoolRewardListInfoList(info entrustPoolReq.PoolRewardListSearch) (list []entrustPool.PoolRewardList, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&entrustPool.PoolRewardList{})
    var poolRewardLists []entrustPool.PoolRewardList
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
    if info.Type != nil {
        db = db.Where("type = ?",info.Type)
    }
    if info.UserId != nil {
        db = db.Where("user_id = ?",info.UserId)
    }
    if info.InvitorId != nil {
        db = db.Where("invitor_id = ?",info.InvitorId)
    }
    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(&poolRewardLists).Error
	return  poolRewardLists, total, err
}
