package twi

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

type TwiActFinishDetailService struct {
}

// CreateTwiActFinishDetail 创建TwiActFinishDetail记录
// Author [piexlmax](https://github.com/piexlmax)
func (twiActFinishDetailService *TwiActFinishDetailService) CreateTwiActFinishDetail(twiActFinishDetail twi.TwiActFinishDetail) (err error) {
	err = global.GVA_DB.Create(&twiActFinishDetail).Error
	return err
}

// DeleteTwiActFinishDetail 删除TwiActFinishDetail记录
// Author [piexlmax](https://github.com/piexlmax)
func (twiActFinishDetailService *TwiActFinishDetailService)DeleteTwiActFinishDetail(twiActFinishDetail twi.TwiActFinishDetail) (err error) {
	err = global.GVA_DB.Delete(&twiActFinishDetail).Error
	return err
}

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

// UpdateTwiActFinishDetail 更新TwiActFinishDetail记录
// Author [piexlmax](https://github.com/piexlmax)
func (twiActFinishDetailService *TwiActFinishDetailService)UpdateTwiActFinishDetail(twiActFinishDetail twi.TwiActFinishDetail) (err error) {
	err = global.GVA_DB.Save(&twiActFinishDetail).Error
	return err
}

// GetTwiActFinishDetail 根据id获取TwiActFinishDetail记录
// Author [piexlmax](https://github.com/piexlmax)
func (twiActFinishDetailService *TwiActFinishDetailService)GetTwiActFinishDetail(id uint) (twiActFinishDetail twi.TwiActFinishDetail, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&twiActFinishDetail).Error
	return
}

// GetTwiActFinishDetailByOther 根据id其他获取TwiActFinishDetail记录
// Author [piexlmax](https://github.com/piexlmax)
func (twiActFinishDetailService *TwiActFinishDetailService)GetTwiActFinishDetailByOther(actId int,templateId uint,uid uint) (twiActFinishDetail twi.TwiActFinishDetail, err error) {
	err = global.GVA_DB.Where("act_id = ? and template_id = ? and uid = ?", actId,templateId,uid).First(&twiActFinishDetail).Error
	return
}

// GetPersonsByActId 根据id其他获取参与人数
// Author [piexlmax](https://github.com/piexlmax)
func (twiActFinishDetailService *TwiActFinishDetailService)GetPersonsByActId(actId int) (list []twi.UserSimpleInfo,count int64, err error) {
	err = global.GVA_DB.Model(&twi.TwiActFinishDetail{}).Where("act_id = ? ", actId).Group("uid").Count(&count).Error
	if err != nil {
		return
	}
	err = global.GVA_DB.Raw("SELECT d.uid as id,u.twi_photo_url,u.twi_username," +
		"u.tele_photo_url,u.tele_username from twi_act_finish_detail as d " +
		"left join twi_metamask as u on d.uid = u.id " +
		"where d.act_id = ? group by d.uid",actId).Scan(&list).Error
	return
}

// GetTwiActFinishDetailInfoList 分页获取TwiActFinishDetail记录
// Author [piexlmax](https://github.com/piexlmax)
func (twiActFinishDetailService *TwiActFinishDetailService)GetTwiActFinishDetailInfoList(info twiReq.TwiActFinishDetailSearch) (list []twi.TwiActFinishDetail, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&twi.TwiActFinishDetail{})
    var twiActFinishDetails []twi.TwiActFinishDetail
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
    if info.ActId != nil {
        db = db.Where("act_id = ?",info.ActId)
    }
    if info.TemplateId != nil {
        db = db.Where("template_id = ?",info.TemplateId)
    }
    if info.Uid != nil {
        db = db.Where("uid = ?",info.Uid)
    }
        if info.StartFinishTime != nil && info.EndFinishTime != nil {
            db = db.Where("finish_time BETWEEN ? AND ? ",info.StartFinishTime,info.EndFinishTime)
        }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

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