package dao

import (
	"youngee_b_api/app/entity"
	"youngee_b_api/app/vo"
)

type LocalLifeTaskInfoDao struct{}

// 获取指定违约类型的本地生活子任务数量
func (d LocalLifeTaskInfoDao) CountByDefaultType(localId string, defaultType int64) int64 {
	var total int64
	Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id = ? AND cur_default_type = ?", localId, defaultType).Count(&total)
	return total
}

// 获取指定任务阶段的本地生活子任务数量
func (d LocalLifeTaskInfoDao) CountByTaskStage(localId string, taskStage int64) int64 {
	var total int64
	Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id = ? AND task_stage = ?", localId, taskStage).Count(&total)
	return total
}

// 获取指定任务阶段的本地生活子任务
func (d LocalLifeTaskInfoDao) GetListByTaskStage(localId string, bookStatus int64, time string, page int, pageSize int) ([]*entity.LocalLifeTaskInfo, int64, error) {
	var taskInfos []*entity.LocalLifeTaskInfo
	var total int64
	query := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id = ? AND book_status = ?", localId, bookStatus)
	// 计算偏移量
	offset := (page - 1) * pageSize
	var err error
	if bookStatus == 1 {
		query.Count(&total)
		err = query.Order("select_date asc").Offset(offset).Limit(pageSize).Find(&taskInfos).Error
	} else if bookStatus == 2 {
		if time != "" {
			query = query.Where("DATE(reserve_time) = ?", time)
		}
		query.Count(&total)
		err = query.Order("reserve_time desc").Offset(offset).Limit(pageSize).Find(&taskInfos).Error
	} else if bookStatus == 3 {
		if time != "" {
			query = query.Where("DATE(explore_time) = ?", time)
		}
		query.Count(&total)
		err = query.Order("explore_time desc").Offset(offset).Limit(pageSize).Find(&taskInfos).Error
	} else if bookStatus == 4 {
		if time != "" {
			query = query.Where("DATE(finish_explore_time) = ?", time)
		}
		query.Count(&total)
		err = query.Order("finish_explore_time desc").Offset(offset).Limit(pageSize).Find(&taskInfos).Error
	}
	if err != nil {
		return nil, 0, err
	}
	return taskInfos, total, nil
}

// 获取未传初稿的本地生活子任务数据
func (d LocalLifeTaskInfoDao) GetListBySketchDefault(param *vo.DefaultSearchParam) ([]entity.LocalLifeTaskInfo, int64, error) {
	localTaskInfos := []entity.LocalLifeTaskInfo{}
	var total int64
	query := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id = ? AND cur_default_type = ?", param.TaskId, 4)
	query.Count(&total)
	query = query.Select("task_id, talent_id, settle_amount, draft_fee, sketch_missing_time")
	offset := (param.Page - 1) * param.PageSize
	if err := query.Order("sketch_missing_time desc").Offset(offset).Limit(param.PageSize).Find(&localTaskInfos).Error; err != nil {
		return nil, 0, err
	}
	return localTaskInfos, total, nil
}

// 获取未发作品的本地生活子任务数据
func (d LocalLifeTaskInfoDao) GetListByLinkDefault(param *vo.DefaultSearchParam) ([]entity.LocalLifeTaskInfo, int64, error) {
	localTaskInfos := []entity.LocalLifeTaskInfo{}
	var total int64
	query := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id = ? AND cur_default_type = ?", param.TaskId, 6)
	query.Count(&total)
	query = query.Select("task_id, talent_id, settle_amount, draft_fee, link_missing_time")
	offset := (param.Page - 1) * param.PageSize
	if err := query.Order("link_missing_time desc").Offset(offset).Limit(param.PageSize).Find(&localTaskInfos).Error; err != nil {
		return nil, 0, err
	}
	return localTaskInfos, total, nil
}

// 获取未传数据的本地生活子任务数据
func (d LocalLifeTaskInfoDao) GetListByDataDefault(param *vo.DefaultSearchParam) ([]entity.LocalLifeTaskInfo, int64, error) {
	localTaskInfos := []entity.LocalLifeTaskInfo{}
	var total int64
	query := Db.Model(&entity.LocalLifeTaskInfo{}).Where("local_id = ? AND cur_default_type = ?", param.TaskId, 8)
	query.Count(&total)
	query = query.Select("task_id, talent_id, settle_amount, draft_fee, data_missing_time")
	offset := (param.Page - 1) * param.PageSize
	if err := query.Order("data_missing_time desc").Offset(offset).Limit(param.PageSize).Find(&localTaskInfos).Error; err != nil {
		return nil, 0, err
	}
	return localTaskInfos, total, nil
}

// 获取终止合作的本地生活子任务数据
func (d LocalLifeTaskInfoDao) GetListByTerminateDefault(param *vo.DefaultSearchParam) ([]entity.LocalLifeTaskInfo, int64, error) {
	localTaskInfos := []entity.LocalLifeTaskInfo{}
	var total int64
	query := Db.Debug().Model(&entity.LocalLifeTaskInfo{}).Where("local_id = ? AND task_stage = ?", param.TaskId, 17)
	query.Count(&total)
	query = query.Select("task_id, talent_id, settle_amount, draft_fee, terminate_time, terminate_reason, terminate_operator_type, terminate_operator")
	offset := (param.Page - 1) * param.PageSize
	if err := query.Order("terminate_time desc").Offset(offset).Limit(param.PageSize).Find(&localTaskInfos).Error; err != nil {
		return nil, 0, err
	}
	return localTaskInfos, total, nil
}

// 获取已解约的本地生活子任务数据
func (d LocalLifeTaskInfoDao) GetListByCancelDefault(param *vo.DefaultSearchParam) ([]entity.LocalLifeTaskInfo, int64, error) {
	localTaskInfos := []entity.LocalLifeTaskInfo{}
	var total int64
	query := Db.Debug().Model(&entity.LocalLifeTaskInfo{}).Where("local_id = ? AND task_stage = ?", param.TaskId, 16)
	query.Count(&total)
	query = query.Select("task_id, talent_id, settle_amount, draft_fee, cancel_time, cancel_reason, cancel_operator_type, cancel_operator")
	offset := (param.Page - 1) * param.PageSize
	if err := query.Order("cancel_time desc").Offset(offset).Limit(param.PageSize).Find(&localTaskInfos).Error; err != nil {
		return nil, 0, err
	}
	return localTaskInfos, total, nil
}

// 更新字段
func (d LocalLifeTaskInfoDao) UpdateField(taskId string, updateData map[string]interface{}) error {
	err := Db.Model(&entity.LocalLifeTaskInfo{}).Where("task_id = ?", taskId).Updates(updateData).Error
	if err != nil {
		return err
	}
	return nil
}

// 批量更新字段
func (d LocalLifeTaskInfoDao) UpdateFieldBatch(taskIds []string, updateData map[string]interface{}) error {
	err := Db.Model(&entity.LocalLifeTaskInfo{}).Where("task_id IN ?", taskIds).Updates(updateData).Error
	if err != nil {
		return err
	}
	return nil
}

// 批量更新字段
func (d LocalLifeTaskInfoDao) UpdateLocalStatus(taskIds []string, localLifeTaskInfo entity.LocalLifeTaskInfo) error {
	err := Db.Debug().Model(&entity.LocalLifeTaskInfo{}).Where("task_id IN ?", taskIds).Updates(localLifeTaskInfo).Error
	if err != nil {
		return err
	}
	return nil
}
