package airDrop

import (
	"courseSign/server/global"
	"courseSign/server/model/airDrop"
	"courseSign/server/model/common/request"
    airDropReq "courseSign/server/model/airDrop/request"
    "gorm.io/gorm"
)

type RedPackageTestService struct {
}

// CreateRedPackageTest 创建RedPackageTest记录
// Author [piexlmax](https://github.com/piexlmax)
func (rptestService *RedPackageTestService) CreateRedPackageTest(rptest airDrop.RedPackageTest) (err error) {
	err = global.GVA_DB.Create(&rptest).Error
	return err
}

// DeleteRedPackageTest 删除RedPackageTest记录
// Author [piexlmax](https://github.com/piexlmax)
func (rptestService *RedPackageTestService)DeleteRedPackageTest(rptest airDrop.RedPackageTest) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
	    if err := tx.Model(&airDrop.RedPackageTest{}).Where("id = ?", rptest.ID).Update("deleted_by", rptest.DeletedBy).Error; err != nil {
              return err
        }
        if err = tx.Delete(&rptest).Error; err != nil {
              return err
        }
        return nil
	})
	return err
}

// DeleteRedPackageTestByIds 批量删除RedPackageTest记录
// Author [piexlmax](https://github.com/piexlmax)
func (rptestService *RedPackageTestService)DeleteRedPackageTestByIds(ids request.IdsReq,deleted_by uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
	    if err := tx.Model(&airDrop.RedPackageTest{}).Where("id in ?", ids.Ids).Update("deleted_by", deleted_by).Error; err != nil {
            return err
        }
        if err := tx.Where("id in ?", ids.Ids).Delete(&airDrop.RedPackageTest{}).Error; err != nil {
            return err
        }
        return nil
    })
	return err
}

// UpdateRedPackageTest 更新RedPackageTest记录
// Author [piexlmax](https://github.com/piexlmax)
func (rptestService *RedPackageTestService)UpdateRedPackageTest(rptest airDrop.RedPackageTest) (err error) {
	err = global.GVA_DB.Save(&rptest).Error
	return err
}

// GetRedPackageTest 根据id获取RedPackageTest记录
// Author [piexlmax](https://github.com/piexlmax)
func (rptestService *RedPackageTestService)GetRedPackageTest(id uint) (rptest airDrop.RedPackageTest, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&rptest).Error
	return
}

// GetRedPackageTestInfoList 分页获取RedPackageTest记录
// Author [piexlmax](https://github.com/piexlmax)
func (rptestService *RedPackageTestService)GetRedPackageTestInfoList(info airDropReq.RedPackageTestSearch) (list []airDrop.RedPackageTest, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&airDrop.RedPackageTest{})
    var rptests []airDrop.RedPackageTest
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

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