package entrustPool

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

type PoolAddrTicketDailyService struct {
}

// CreatePoolAddrTicketDaily 创建PoolAddrTicketDaily记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolAddrTicketDailyService *PoolAddrTicketDailyService) CreatePoolAddrTicketDaily(poolAddrTicketDaily entrustPool.PoolAddrTicketDaily) (err error) {
	err = global.GVA_DB.Create(&poolAddrTicketDaily).Error
	return err
}

// DeletePoolAddrTicketDaily 删除PoolAddrTicketDaily记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolAddrTicketDailyService *PoolAddrTicketDailyService)DeletePoolAddrTicketDaily(poolAddrTicketDaily entrustPool.PoolAddrTicketDaily) (err error) {
	err = global.GVA_DB.Delete(&poolAddrTicketDaily).Error
	return err
}

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

// UpdatePoolAddrTicketDaily 更新PoolAddrTicketDaily记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolAddrTicketDailyService *PoolAddrTicketDailyService)UpdatePoolAddrTicketDaily(poolAddrTicketDaily entrustPool.PoolAddrTicketDaily) (err error) {
	err = global.GVA_DB.Save(&poolAddrTicketDaily).Error
	return err
}

// GetPoolAddrTicketDaily 根据id获取PoolAddrTicketDaily记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolAddrTicketDailyService *PoolAddrTicketDailyService)GetPoolAddrTicketDaily(id uint) (poolAddrTicketDaily entrustPool.PoolAddrTicketDaily, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&poolAddrTicketDaily).Error
	return
}

// GetPoolAddrTicketDailyInfoList 分页获取PoolAddrTicketDaily记录
// Author [piexlmax](https://github.com/piexlmax)
func (poolAddrTicketDailyService *PoolAddrTicketDailyService)GetPoolAddrTicketDailyInfoList(info entrustPoolReq.PoolAddrTicketDailySearch) (list []entrustPool.PoolAddrTicketDaily, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&entrustPool.PoolAddrTicketDaily{})
    var poolAddrTicketDailys []entrustPool.PoolAddrTicketDaily
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
    if info.Addr != "" {
        db = db.Where("addr = ?",info.Addr)
    }
    if info.JobDate != nil {
        db = db.Where("job_date = ?",info.JobDate)
    }
    if info.PoolId != nil {
        db = db.Where("pool_id = ?",info.PoolId)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

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